Danush-Aries/sast-scanner

GitHub: Danush-Aries/sast-scanner

一款融合 Semgrep、AST 分析与 Claude AI 的 Python 静态安全扫描工具,能自动检测代码漏洞并生成风险评级与修复建议。

Stars: 0 | Forks: 0

# SentrySAST — Semgrep + AST + Entropy,由 Claude 编写修复方案

SentrySAST — SAST + Claude in the loop

build license python sarif claude

## 为什么开发这个工具 Semgrep 标记了一个 `subprocess.call(user_input, shell=True)` —— 很好,但接手该工单的下一位开发者仍然需要打开浏览器,阅读 OWASP,并弄清楚该行代码在这个代码库中是否可被利用。SentrySAST 弥合了这一鸿沟:在三个并行扫描器(Semgrep 规则 + `ast` 遍历 + Shannon 熵密钥检测)运行后,每个发现的问题都会通过启用了 prompt 缓存的 `claude-haiku-4-5` 进行处理,从而返回严重/高/中/低风险标签、通俗的英文解释以及具体的修复方案。输出的格式为 SARIF 2.1,因此 GitHub Advanced Security、VS Code SARIF Viewer 以及每个 SIEM 仪表板都已经知道如何读取它。 ## 60 秒内尝试 ``` git clone https://github.com/Danush-Aries/sast-scanner.git cd sast-scanner python -m venv .venv && source .venv/bin/activate pip install -r requirements.txt cp .env.example .env # add ANTHROPIC_API_KEY (or set DISABLE_AI_EXPLAINER=true) python sast_scanner.py scan /path/to/project --output results.sarif ``` 没有 API key?执行 `DISABLE_AI_EXPLAINER=true python sast_scanner.py scan .` 即可完全离线运行。 REST API:`uvicorn web.backend.main:app --reload --port 8003`。 仪表板:`cd web/frontend && npm install && npm run dev`(会代理到 :8003)。 ## 工作原理 - **三个并行扫描器** — `scanner/engine.py` 将任务分发给 执行 AST 遍历以检查带有受污染参数的 `execute()` / `os.system` / `subprocess.*`,以及 结合正则表达式(AWS key、GitHub token、JWT)与 Shannon 熵来检测未知形式的密钥。 - **带有 prompt 缓存的 AI 解释器** — `scanner/ai_explainer.py` 将系统 prompt 标记为 `cache_control: ephemeral`,因此对同一个 repo 的重复扫描只需花费几美分,而不是几美元。 - **SARIF 2.1 导出 (`scanner/sarif_exporter.py`)** — 业界标准的机器可读格式,可直接接入 GitHub Code Scanning、VS Code SARIF Viewer 和企业级 SIEM。 - **对抗性绕过测试 (`adversarial_tests.py`)** — 一套不断扩充的混淆尝试套件(base64 编码的 shell、字符串拼接的 SQL、跨行分割的密钥),用作回归测试工具。 - **Web 仪表板** — React + TypeScript + Vite;严重程度图表 使用,按发现的问题深入查看,按 rule ID 过滤。 ## 截图 | CLI 扫描 | GitHub 中的 SARIF 报告 | React 仪表板 | |---|---|---| | ![](https://raw.githubusercontent.com/Danush-Aries/sast-scanner/main/assets/screenshot-1.png) | ![](https://raw.githubusercontent.com/Danush-Aries/sast-scanner/main/assets/screenshot-2.png) | ![](https://raw.githubusercontent.com/Danush-Aries/sast-scanner/main/assets/screenshot-3.png) | ## 检测类别 | 类别 | 检测方法 | |---|---| | SQL 注入 | Semgrep 规则 + 对 `execute()` / `executemany()` 调用的 AST 分析 | | 命令注入 | Semgrep 规则 + 对 `os.system`、`subprocess.*` 调用的 AST 分析 | | 泄露的密钥 | 正则表达式模式匹配(AWS key、GitHub token 等)+ Shannon 熵分析 | ## 用法 ### CLI 扫描 ``` python sast_scanner.py scan /path/to/your/python/project python sast_scanner.py scan /path/to/project --output results.sarif python sast_scanner.py scan /path/to/project --rules ./rules --output results.sarif DISABLE_AI_EXPLAINER=true python sast_scanner.py scan /path/to/project ``` 输出示例: ``` Security Findings ┌─────────────────────┬──────────────────┬──────┬───────────────────────────────────────┬──────────┐ │ ID │ File │ Line │ Message │ Risk │ ├─────────────────────┼──────────────────┼──────┼───────────────────────────────────────┼──────────┤ │ command_injection │ app/tasks.py │ 42 │ Potential Command Injection: dynamic… │ High │ │ sql_injection │ app/models.py │ 87 │ Potential SQL Injection: dynamic … │ Critical │ │ secret_detected │ config/dev.py │ 5 │ Potential Generic API Key detected │ High │ └─────────────────────┴──────────────────┴──────┴───────────────────────────────────────┴──────────┘ ``` ### REST API ``` uvicorn web.backend.main:app --reload --port 8003 curl -X POST http://localhost:8003/scan \ -H "Content-Type: application/json" \ -d '{"target_path": "/absolute/path/to/project", "rules_path": "/absolute/path/to/rules"}' ``` ### 测试 ``` DISABLE_AI_EXPLAINER=true pytest tests/ -v DISABLE_AI_EXPLAINER=true python adversarial_tests.py ``` ## 环境变量 | 变量 | 必需 | 默认值 | 描述 | |---|---|---|---| | `ANTHROPIC_API_KEY` | 是(除非禁用 AI) | — | 你的 Anthropic API key | | `DISABLE_AI_EXPLAINER` | 否 | `false` | 设置为 `true` 可完全跳过 AI 调用 | ## 项目结构 ``` sast-scanner/ ├── sast_scanner.py # CLI entry point ├── scanner/ │ ├── engine.py # Orchestrates all scan phases │ ├── cli.py # Argument parsing + rich output │ ├── ast_analyzer.py # Python AST-based vulnerability detection │ ├── secret_detector.py # Regex + Shannon entropy secret detection │ ├── ai_explainer.py # Claude API integration (with prompt caching) │ └── sarif_exporter.py # SARIF 2.1 report generation ├── rules/ │ ├── cmd_injection.yaml │ ├── sqli.yaml │ └── secrets.yaml ├── tests/ ├── web/ │ ├── backend/main.py # FastAPI REST API │ └── frontend/ # React + TypeScript + Vite dashboard ├── adversarial_tests.py # Bypass-attempt test suite ├── requirements.txt └── .github/workflows/ci.yml ``` ## 技术栈 Semgrep · Python `ast` · Shannon 熵 + 正则表达式 · Anthropic `claude-haiku-4-5`(prompt 已缓存) · `sarif-om` · Rich (CLI) · FastAPI + Uvicorn · React 18 + TypeScript + Vite + Tailwind + Recharts · GitHub Actions CI。 ## 许可证 [MIT](https://opensource.org/licenses/MIT)。 ### 来自 Danush 的更多内容 - [ponytail-for-python](https://github.com/Danush-Aries/ponytail-for-python) — 针对 Python 代码库的代码智能 - [Agentic_Systems](https://github.com/Danush-Aries/Agentic_Systems) — Agent 模式的参考实现 - [autonomous-coding-agent](https://github.com/Danush-Aries/autonomous-coding-agent) — 全自动工程 Agent - [computer-use-agent](https://github.com/Danush-Aries/computer-use-agent) — Claude 通过 VNC 操控你的桌面 - [browser-automation-agent](https://github.com/Danush-Aries/browser-automation-agent) — Claude 操控 Playwright - [blinkchat](https://github.com/Danush-Aries/blinkchat) — 充满活力的实时聊天
标签:Claude API, DOE合作, SARIF, 代码安全, 安全助手, 漏洞枚举, 网络测绘, 逆向工具, 错误基检测, 静态代码分析