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

# Injection Into the Cache Key

Injection Into the Cache Key notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Cache Key Injection

### Injection Into the Cache Key

We need to combine multiple faults (including **cache key injection**) to execute **`alert(1)`** in the victim's browser. The lab requires the use of the header **`Pragma: x-get-cache-key`**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4a1acbb2d438aa7bece67036c8469117944898d1%2F%7BBB8AEC0B-2845-4707-92E0-E0EC29FFBE3A%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Reconnaissance

1/) Login behavior

* We have a connection panel.
* After login, we are redirected to **`/login/?lang=en`**.
* A session cookie is then set.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cc2a931ab62b85905c663ead76ee92cf365cc591%2F%7BCFDA343D-18D9-4A4E-B201-AD2D2F448C34%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

2/) Reflection of the `lang` parameter

* Our entry appears in the answer via `lang`:
* Example: **`/login/?lang=HELLLOOOO`**

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

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3f28cdf50b4733c230a62e09dce3a6395f0ba559%2F%7B17906ACC-07FB-404C-A3F6-42BF0D0407C6%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

If we put HTML tags (e.g. `<h1>`), **it doesn't run**: there is HTML encoding\*\*.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-60fad42f0ec8cc458181020f44e82c2045433331%2F%7B475BC26C-7E37-43FF-99FE-E6EA7448F512%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Operational Point of Entry

Hidden JS file: `localize.js`

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

We notice a script in the background:

* **`/js/localize.js?lang=en&CORS=0`**
* Answer observed:
* `document.cookie = 'lang=en';`

If we change `lang`, the value is reflected:

* **`/js/localize.js?lang=hello&CORS=0`** → `hello` appears in the answer.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7743c4ea0cf45a253f8bd6de63e3cae9592ab5ce%2F%7B7FB44CFC-718B-41C9-8590-00A0B2841462%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Observation of the Cache Key

We add:

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

And we get the key:

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

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8aabb24c2d9bcf343b42fb004c768a9b4572c8e3%2F%7B3354A351-D591-4D44-A0AB-4E95DDDAF7FA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

We find that `CORS` influences the key, and that when playing **Origin** + `CORS`, we can make vary what enters the cache.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8811b02b819b929fa990224dc9f5335437a805ed%2F%7B6FB2F228-2C46-4DA5-81D4-9D3AF46409A0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Injection of Headers via Back Cart

We use CRLF injection:

* `%0d%0a`

```
man ascii
```

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

#### 1) Test: Cookie Injection

We send:

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

Result: the cookie is well interpreted on the answer side.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-163ad6f6a0e182eb18fa7ce90134fdf2259b9d77%2F%7B51717951-1BA3-466A-849A-AE795F6B5783%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### JavaScript Injection by Manipulating `Content-Length`

We then try to inject content into the response with:

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

In the answer, we find **`alert(1)`**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5f4dadb4419f9e798e6edd81836d7db4d6b84892%2F%7B66177E98-484A-472A-B58B-FADF52A67773%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Build Targeted Hidden Response

For the server to store this version (with `alert(1)`), the **cache key** must match a form that includes our injected `Origin`, for example:

{% 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="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b27a41b9949dcdd648331b13506c94ade3d9fb49%2F%7BCE27C2CE-0BCC-4773-80C4-D26191AFA5A1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Offset on the Victim Side via an Ignored Parameter

We look for a parameter ignored by the cache (found with Param Miner):

* **`utm_content`**

We re-use the key cache and place the payload in `utm_content` to serve the poisoned resource while visiting `lang=en`.

Final Request (as we used):

{% 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="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d0bd7ade7e74687ff349fe8ff48548b416b6cb63%2F%7B8B884C36-3E90-4DDF-8249-3CA7DAAFB044%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

When the victim opens the page in **`/login/?lang=en`** (via redirection), the poisoned content is served from the cache, and **`alert(1)`** runs.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-688fd4eebedefee8f767ea116d0a28dad5ed05cf%2F%7BC2D5999F-6555-4179-B883-EC42FECE47B3%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/web/cache-poisoning/cache-key-injection.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.
