NullAILab/nullai-api-security-scanner

GitHub: NullAILab/nullai-api-security-scanner

一款基于 Python 和 FastAPI 的 API 安全扫描器,围绕 OWASP API Top 10、GraphQL 漏洞和参数模糊测试提供可离线运行的自动化检测能力。

Stars: 0 | Forks: 0

# API 安全扫描器 ![Python](https://img.shields.io/badge/Python-3.10+-3776AB?logo=python&logoColor=white) ![Tests](https://img.shields.io/badge/Tests-78%20passing-brightgreen) ![License](https://img.shields.io/badge/License-MIT-green) 一款针对 OWASP API 安全 Top 10、GraphQL 特定漏洞以及参数模糊测试的 API 漏洞扫描器——所有功能均由可注入的 HTTP 客户端驱动,使每项检查都能在离线状态下完全测试。 ## 功能特性 | 检查项 | OWASP ID | 测试内容 | |-------|----------|---------------| | 身份验证失效 | API2 | 未经身份验证即可访问受保护的端点 | | BOLA / IDOR | API1 | 跨用户对象访问(水平越权) | | 批量赋值 | API3 | 更新响应中包含特权字段 | | 速率限制 | API4 | 在重复请求后缺少 429 状态码 | | 函数级授权 | API5 | 普通用户访问管理端点 | | 安全响应头 | API8 | 缺少 X-Content-Type-Options、HSTS、CSP、X-Frame-Options | | 详尽错误信息 | API8 | 响应中包含堆栈跟踪 / 服务器标识 | | 遗留端点 | API9 | `/v0`、`/swagger`、`/openapi.json`、`/api/debug` 返回 200 状态码 | | GraphQL 内省 | — | 完整的 Schema 泄露 | | GraphQL 深度限制 | — | 接受深度嵌套的查询(DoS 攻击向量) | | GraphQL 别名滥用 | — | 每个查询包含无限别名 | | GraphQL 批量滥用 | — | 批量操作绕过速率限制 | | SQL 注入 | — | 错误字符串和基于时间的延迟 | | 路径遍历 | — | 响应中包含 `/etc/passwd`、`win.ini` | | XSS / SSTI | — | 反射的 Payload,`{{7*7}}` → 49 | | SSRF | — | AWS 元数据 / 本地主机探测返回 200 状态码 | | 整数边界 | — | 溢出值导致 500 错误 | ## 项目结构 ``` 30-api-security-scanner/ ├── src/ │ ├── checks/ │ │ ├── models.py ← MockResponse, CheckResult, ScanReport, Severity │ │ ├── owasp_api.py ← API1–API9 check functions │ │ ├── graphql.py ← GraphQL-specific checks │ │ └── fuzzer.py ← Payload library + response analyser │ ├── scanner/ │ │ └── engine.py ← APIScanner orchestrator │ ├── api/ │ │ └── routes.py ← FastAPI endpoints │ ├── templates/ │ │ └── index.html ← Dark-themed single-page UI │ └── app.py ← FastAPI factory ├── tests/ │ └── test_api_scanner.py ← 78 tests, fully offline └── requirements.txt ``` ## 快速开始 ``` cd src pip install -r ../requirements.txt uvicorn app:app --reload # 打开 http://localhost:8000 ``` ### 通过 API 运行扫描 ``` curl -X POST http://localhost:8000/api/scan \ -H "Content-Type: application/json" \ -d '{ "target": "https://api.example.com", "check_auth": true, "check_headers": true, "check_versions": true, "check_graphql": false, "check_fuzz": false }' ``` ### 演示端点(无需真实目标) ``` # 使用 mock 200 响应测试 auth 检查 curl -X POST http://localhost:8000/api/demo/auth \ -d '{"status_code": 200}' -H "Content-Type: application/json" # 使用自定义 mock headers 测试 headers curl -X POST http://localhost:8000/api/demo/headers \ -d '{"headers": {"X-Content-Type-Options": "nosniff"}}' \ -H "Content-Type: application/json" # 对 mock SQL 错误响应运行 fuzzing curl -X POST http://localhost:8000/api/demo/fuzz \ -d '{"status_code": 200, "body": {"error": "sql syntax error"}}' \ -H "Content-Type: application/json" # 列出完整的 payload 库 curl http://localhost:8000/api/payloads ``` ## 运行测试 ``` python -m pytest tests/ -v # 78 个通过 — 所有检查均通过 mock HTTP 客户端离线测试 ``` ## 架构 所有检查函数都是**可注入的**:它们直接接受 `MockResponse` 对象,因此测试永远不会发起真实的网络调用。`APIScanner` 引擎出于同样的原因接受一个自定义的 `http_client` 可调用对象。 ``` def mock_client(responses): def client(url, method="GET", headers=None, body=None): return responses.get(url, MockResponse(404)) return client scanner = APIScanner("http://example.com", http_client=mock_client({...})) report = scanner.run(check_auth=True, check_headers=True) ``` ## 参考资料 - [OWASP API 安全 Top 10](https://owasp.org/www-project-api-security/) - [PortSwigger API 安全实验室](https://portswigger.net/web-security/api-testing) - [GraphQL 安全备忘单](https://cheatsheetseries.owasp.org/cheatsheets/GraphQL_Cheat_Sheet.html)
标签:API安全扫描器, API接口测试, CISA项目, DevSecOps, GraphQL安全测试, OWASP API Top 10, Python安全工具, Web安全, XXE攻击, 上游代理, 代码生成, 参数模糊测试, 安全响应头, 安全规则引擎, 批量赋值, 整数溢出, 服务端模板注入(SSTI), 服务端请求伪造(SSRF), 渗透测试工具, 自动化安全审计, 蓝队分析, 越权漏洞(BOLA/IDOR), 跨站脚本攻击(XSS), 路径遍历, 身份验证绕过, 逆向工具, 速率限制检测, 遗留端点, 错误信息泄露, 黑盒测试