1.什么是XSS攻击?
正常的页面被渗出了攻击者的js脚本,这些脚本可以非法地获取用户信息,然后将信息发送到attacked的服务端。
XSS是需要充分利用输出环境来构造攻击脚本的
2.危害
非法获取用户cookie、ip等内容
窃取用户输入的内容
劫持浏览器,形成DDOS攻击
3.类型
// 合法页面url: http://aa.com?test=1234<input type="text" value={test} />// result is:<input type="text" value="1234" />// XSS攻击url: http://aa.com?test=" /><script>alert('xss')</script><input type="text" value={test} />// result is:<input type="text" value="" /> <script> alert('xss')</script> />
// input: <textarea> hello </div><script src="..."></script></textarea>// 将hello及后面的内容一起提交到服务上//输出上述内容<div> hello</div><script src="..."></script></div>
<div id="inner"></div><script>var ele = document.querySelector('#inner');var search = window.location.search;var age = getAge(search) // 获取search中age的内容ele.innerHTML = age;// if url: aa.com?age=23<div id="inner">23</div></script>// if url: aa.com?age=</div><script src="..."></script><div id="inner"></div><script src="..."></script>
4.解决方案
总体来讲需要对HTML、Attribute、js context、URL、style context、JSON分别进行编码处理。
对html内容进行过滤,例如html purifier、js-xss
X-XSS-PRotection
IE8+以上的浏览器均支持该http header,目前goole里面已经添加该属性:
x-xss-protection:1; mode=block
【XSS】利用 onload 事件监控跨站资源
XSS 前端防火墙 —— 内联事件拦截
XSS 前端防火墙 —— 可疑模块拦截
XSS 前端防火墙 —— 无懈可击的钩子
XSS 前端防火墙 —— 天衣无缝的防护
XSS 前端防火墙 —— 整装待发
content security policy(CSP): W3C和各大浏览器厂商均推荐和实践防御XSS的标准,限制执行、请求具有风险的外链资源,攻击者无法将目标信息传出,并对relected XSS进行防御。
在HTTP的response中部署CSP,指定资源白名单来限制浏览器访问未经允许的外部资源。
目前Google、facebook、twitter均支持该标准,例如twitter的配置信息:
content-security-policy:default-src https:; connect-src https:; font-src https: data:; frame-src https: twitter:; frame-ancestors 'self'; img-src https: blob: data:; media-src https: blob:; object-src https:; script-src 'unsafe-inline' 'nonce-w6FV5VZOKta+7JaW7PpR3A==' 'unsafe-eval' https:; style-src 'unsafe-inline' https:; report-uri https://twitter.com/i/csp_report?a=NVQWGYLXFVZXO2LGOQ%3D%3D%3D%3D%3D%3D&ro=false;
上述配置的详细参数请参考CSP的相关规范,csp1.0,目前csp1.1草案已经出来。 然而,csp中不允许Eval、inline js、白名单获取远程脚本,这些阻碍着csp的推广。
Template Engine:对处理输出内容进行编码,放置恶意代码执行,对stored Xss进行防御。
例如经常使用handlebars就对输出的内容进行了编码:
Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value,use the "triple-stash", {{{.<div class="entry"> <h1>{{title}}</h1> <div class="body"> {{{body}}} </div></div>{ title: "All about <p> Tags", body: "<p>This is a post about <p> tags</p>"}result is as below:<div class="entry"> <h1>All About <p> Tags</h1> <div class="body"> <p>This is a post about <p> tags</p> </div></div>
首先在服务端端,对输出的内容进行编码,对request中的内容进行检查编码。
在客户端,对js获取的widow.location、document.cookie等信息也需要相应处理。
5.参考
https://developer.mozilla.org/zh-CN/docs/Web/Security/CSP
http://www.80sec.com/browser-hijacking.html
https://cure53.de/fp170.pdf
http://www.freebuf.com/articles/web/40520.html
http://www.freebuf.com/articles/web/42727.html
http://blog.knownsec.com/wp-content/uploads/2014/07/%E7%BB%99%E5%BC%80%E5%8F%91%E8%80%85%E7%9A%84%E7%BB%88%E6%9E%81XSS%E9%98%B2%E6%8A%A4%E5%A4%87%E5%BF%98%E5%BD%95.pdf
新闻热点
疑难解答