harunidev/SmartContractAuditTool

GitHub: harunidev/SmartContractAuditTool

一个结合静态分析和AI的智能合约安全审计工具,用于自动化检测Solidity代码中的常见漏洞。

Stars: 0 | Forks: 0

# SmartContractAuditTool [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/bd35a1d9cb165031.svg)](https://github.com/harunidev/SmartContractAuditTool/actions) [![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-blue)](https://www.python.org/) [![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE) ## 功能特性 | 层 | 功能 | |-------|-------------| | **静态分析器** | 映射到 SWC 注册表的 18 种模式规则——零依赖,即时运行 | | **AI 分析器** | 将合约发送给 Claude (claude-opus-4-7) 进行深度语义审查 | | **CLI** | `audit-contract --file token.sol`——支持单文件、批量目录、严重性过滤 | | **Web UI** | 深色模式 SPA:粘贴或上传代码,查看可展开的发现,下载 HTML/JSON 报告 | | **报告器** | 独立的 HTML 报告 + 机器可读的 JSON 报告 | | **Docker** | 单命令容器部署 | ## 检测到的漏洞类别 | ID | 类别 | 严重性 | |----|----------|----------| | SWC-107 | 重入 | 严重 | | SWC-112 | 不受控的委托调用 | 严重 | | SWC-118 | 未受保护的初始化函数 | 严重 | | SWC-115 | tx.origin 认证 | 高 | | SWC-104 | 未检查的返回值 | 高 | | SWC-101 | 整数溢出 (< 0.8) | 高 | | SWC-120 | 弱随机性 (blockhash) | 高 | | SWC-106 | selfdestruct 使用 | 高 | | SWC-116 | 区块时间戳依赖 | 中 | | SWC-127 | 内联汇编 | 中 | | SWC-132 | .transfer() Gas 限制 | 中 | | SWC-128 | 无界循环 (DoS) | 中 | | SWC-114 | ERC20 approve() 竞争条件 | 中 | | SWC-103 | 浮动 Pragma | 低 | | SWC-020 | 缺少零地址检查 | 低 | | SWC-134 | 硬编码地址 | 低 | | SWC-100 | 隐式函数可见性 | 低 | | — | 缺少事件触发 | 信息 | ## 快速开始 ``` # 1. 克隆 git clone https://github.com/harunidev/SmartContractAuditTool.git cd SmartContractAuditTool # 2. 安装 pip install -r requirements.txt pip install -e . # 3. 设置 API 密钥(可选 — 仅 AI 分析需要) export ANTHROPIC_API_KEY=sk-ant-... # 4. 审计合约 audit-contract --file contracts/vulnerable.sol ``` ## CLI 用法 ``` usage: audit-contract [-h] [--file FILE] [--dir DIR] [--format {html,json,console}] [--output PATH] [--no-ai] [--min-severity LEVEL] [--model MODEL] [--api-key KEY] [--open] {demo,web} ... ``` ### 示例 ``` # 审计单个文件 — 生成 vulnerable_audit.html audit-contract --file token.sol # 仅静态分析,在终端显示结果 audit-contract --file token.sol --no-ai --format console # 导出 JSON 报告 audit-contract --file token.sol --format json --output report.json # 筛选 — 仅显示 HIGH 和 CRITICAL 发现 audit-contract --file token.sol --min-severity HIGH # 批量审计整个合约目录 audit-contract --dir ./contracts --no-ai # 生成后在浏览器中打开 HTML 报告 audit-contract --file token.sol --no-ai --open # 从内置的漏洞合约生成演示报告 audit-contract demo # 启动 Web 界面 audit-contract web --port 8080 ``` ## Web 界面 ``` export ANTHROPIC_API_KEY=sk-ant-... audit-contract web # → http://localhost:5000 # 或 python main.py web ``` **功能:** - 粘贴代码或拖放 `.sol` 文件 - 切换 AI 分析的开启/关闭 - 严重性过滤(仅显示从信息到严重) - 点击任何发现卡片可展开描述、建议和 SWC 链接 - 下载完整的 HTML 报告或 JSON ## Docker ``` # 构建 docker build -t smart-contract-audit-tool . # 使用您的 API 密钥运行 docker run -p 5000:5000 -e ANTHROPIC_API_KEY=sk-ant-... smart-contract-audit-tool # 或使用 docker-compose(自动读取 .env) cp .env.example .env # fill in your API key docker-compose up ``` ## 项目结构 ``` SmartContractAuditTool/ ├── audit_tool/ │ ├── analyzer/ │ │ ├── static.py ← 18-rule pattern scanner │ │ └── ai_auditor.py ← Claude API integration │ ├── reporter/ │ │ ├── html_report.py ← Self-contained HTML report │ │ └── json_report.py ← Machine-readable JSON │ ├── web/ │ │ ├── app.py ← Flask API + health endpoint │ │ └── templates/ │ │ └── index.html ← Dark-mode SPA │ ├── cli.py ← argparse CLI (batch, demo, web sub-commands) │ └── config.py ← Env-var configuration ├── contracts/ │ ├── vulnerable.sol ← Intentionally vulnerable (testing) │ ├── safe_example.sol ← Best-practice reference │ └── defi_example.sol ← DeFi contract with subtle bugs ├── tests/ │ ├── test_static.py ← 17 unit tests for pattern scanner │ ├── test_reporters.py ← 12 tests for HTML/JSON reporters │ └── test_cli.py ← 9 tests for CLI behaviour ├── .github/workflows/ │ └── ci.yml ← CI: test matrix + Docker build ├── Dockerfile ├── docker-compose.yml └── requirements.txt ``` ## 架构 ``` ┌─────────────────────────────────────────────────────┐ │ SmartContractAuditTool │ │ │ │ Input layer │ │ ┌───────────────┐ ┌──────────────────────┐ │ │ │ CLI (argparse)│ │ Web UI (Flask + SPA) │ │ │ └──────┬────────┘ └──────────┬───────────┘ │ │ │ │ │ │ Analysis layer │ │ │ ┌──────▼──────────────────────▼──────────────┐ │ │ │ StaticAnalyzer │ AIAuditor │ │ │ │ 18 regex patterns │ Claude API (opus-4-7) │ │ │ │ SWC-mapped │ JSON structured output│ │ │ └──────┬─────────────┴──────────┬────────────┘ │ │ │ │ │ │ Report layer │ │ │ ┌──────▼──────────┐ ┌──────────▼──────┐ │ │ │ HTMLReporter │ │ JSONReporter │ │ │ │ Standalone │ │ Machine- │ │ │ │ HTML file │ │ readable JSON │ │ │ └─────────────────┘ └─────────────────┘ │ └─────────────────────────────────────────────────────┘ ``` ## 开发 ``` # 运行所有测试(无需 API 密钥) python tests/test_static.py python tests/test_reporters.py python tests/test_cli.py # 以可编辑模式安装 pip install -e . # 运行快速冒烟测试 audit-contract demo ``` ## 环境变量 | 变量 | 默认值 | 描述 | |----------|---------|-------------| | `ANTHROPIC_API_KEY` | — | 你的 Anthropic API 密钥(AI 分析必需) | | `AUDIT_MODEL` | `claude-opus-4-7` | 使用的 Claude 模型 | | `AUDIT_WEB_HOST` | `0.0.0.0` | Web 服务器绑定主机 | | `AUDIT_WEB_PORT` | `5000` | Web 服务器端口 | | `AUDIT_WEB_DEBUG` | `false` | Flask 调试模式 | | `AUDIT_MAX_UPLOAD_KB` | `512` | Web UI 最大上传大小 | 参见 `.env.example` 获取模板。 ## 免责声明 本工具仅用于**教育和授权的安全研究**目的。 静态模式匹配和 AI 分析可能存在误报和漏报—— **请勿将审计结果作为生产部署决策的唯一依据**。 在风险较高时,务必结合人工审查和形式化验证。 ## 许可证 MIT © 2026 harunidev
标签:AI分析, AI审计, Docker, NIDS, Python, SWC漏洞, TCP SYN 扫描, Web UI, 云安全态势管理, 云安全监控, 前端应用, 区块链安全, 区块链开发, 区块链漏洞, 后端开发, 安全测试, 安全防御评估, 容器化, 容器化部署, 攻击性安全, 无后门, 智能合约安全, 智能合约审计, 智能合约漏洞, 漏洞分类, 请求拦截, 逆向工具, 静态分析