0xSoftBoi/smart-contract-auditor

GitHub: 0xSoftBoi/smart-contract-auditor

一个Python CLI智能合约安全审计工具,通过Slither或内置静态分析检测Solidity合约常见漏洞并输出安全评分报告。

Stars: 0 | Forks: 0

# 智能合约审计器 一个用于审计 Solidity 智能合约常见安全漏洞的 Python CLI 工具。它在可用时运行 [Slither](https://github.com/crytic/slither),否则回退到内置的静态模式匹配。 ## 功能 - 检测重入漏洞(`call.value`,状态更新前转账) - 标记低级调用的未检查返回值 - 对 `tx.origin` 授权模式发出警告 - 识别潜在的整数溢出/下溢(Solidity 0.8 之前) - 检测 `selfdestruct` 和 `delegatecall` 的使用 - 计算 0-100 的安全评分(100 = 安全,根据发现的问题扣分) - 输出彩色终端文本、JSON 或 HTML 格式的报告 ## 安装 ``` pip install -r requirements.txt ``` 要启用通过 Slither 进行的深度分析(可选但推荐): ``` pip install slither-analyzer # Slither 也需要 solc — 通过 solc-select 安装: pip install solc-select && solc-select install latest && solc-select use latest ``` ## 用法 ``` # 基础审计 — 彩色终端输出 python main.py audit contracts/Token.sol # JSON 输出 python main.py audit contracts/Token.sol --format json # HTML 报告保存到文件 python main.py audit contracts/Token.sol --format html --output report.html # Strict mode: 如果发现任何 critical/high 问题则以代码 1 退出(在 CI 中有用) python main.py audit contracts/Token.sol --strict ``` ### 示例输出 ``` Smart Contract Audit Report Analysis engine: static Security Score: 37/100 Found 5 issue(s): 2 critical, 2 high, 1 medium. Issue Summary ╭──────────────┬───────╮ │ Severity │ Count │ ├──────────────┼───────┤ │ CRITICAL │ 2 │ │ HIGH │ 2 │ │ MEDIUM │ 1 │ ╰──────────────┴───────╯ Findings ╭───┬──────────┬────────────────────────────────────────┬──────┬──────────────────────────────────────╮ │ # │ Severity │ Title │ Line │ Description │ ├───┼──────────┼────────────────────────────────────────┼──────┼──────────────────────────────────────┤ │ 1 │ CRITICAL │ Potential Reentrancy (call.value) │ 42 │ Use of call.value() before state │ │ │ │ │ │ updates can allow reentrancy attacks. │ ├───┼──────────┼────────────────────────────────────────┼──────┼──────────────────────────────────────┤ │ 2 │ CRITICAL │ Use of selfdestruct │ 87 │ selfdestruct() can destroy the │ │ │ │ │ │ contract and send all ETH to an │ │ │ │ │ │ arbitrary address. │ ├───┼──────────┼────────────────────────────────────────┼──────┼──────────────────────────────────────┤ │ 3 │ HIGH │ Use of tx.origin for Authorization │ 15 │ tx.origin should not be used for │ │ │ │ │ │ authorization. │ ├───┼──────────┼────────────────────────────────────────┼──────┼──────────────────────────────────────┤ │ 4 │ HIGH │ Use of delegatecall │ 61 │ delegatecall() executes external │ │ │ │ │ │ code in the context of the calling │ │ │ │ │ │ contract. │ ├───┼──────────┼────────────────────────────────────────┼──────┼──────────────────────────────────────┤ │ 5 │ MEDIUM │ Potential Integer Overflow/Underflow │ 1 │ Arithmetic without SafeMath or │ │ │ │ │ │ compiler checks (pre-Solidity 0.8). │ ╰───┴──────────┴────────────────────────────────────────┴──────┴──────────────────────────────────────╯ ``` ## Slither 集成 当安装了 [Slither](https://github.com/crytic/slither) 时,审计器会自动使用它进行更深入的分析,包括数据流跟踪、跨函数重入和污点分析。所有报告格式中的 `engine` 字段指示使用了哪个后端(`slither` 或 `static`)。 ## 严重程度评分 | 严重程度 | 扣分 | |----------|----------------| | Critical | 10 | | High | 7 | | Medium | 3 | | Low | 1 | 评分 = `max(0, 100 − 扣分总和)` ## 选项 ``` Usage: main.py audit [OPTIONS] CONTRACT Audit a Solidity smart contract for security vulnerabilities. Options: --format [text|json|html] Output format. [default: text] -o, --output FILE Write output to FILE instead of stdout. --strict Exit with code 1 if any critical or high severity issues are found. --help Show this message and exit. ``` ## 流水线 `pipeline/` 目录提供了两种持续审计的自动化模式。 ### Webhook 服务器 (CI/CD) 启动一个 GitHub webhook 监听器,每当 PR 开启或更新时自动审计 Solidity 合约,并将结果作为 PR 评论发布。 ``` export GITHUB_WEBHOOK_SECRET=your_secret export GITHUB_TOKEN=your_token python pipeline/continuous.py ``` 将你的 GitHub 仓库的 webhook 配置为指向 `http://your-host:8080/webhook`,内容类型为 `application/json`。 ### 定时审计器 按可配置的时间间隔审计目录中的所有 `.sol` 文件,并将带有时间戳的 JSON 报告写入 `./audit_reports/`。 ``` # 运行一次 python pipeline/scheduler.py ./contracts # 每小时运行 python pipeline/scheduler.py ./contracts --interval 3600 # 自定义报告输出目录 python pipeline/scheduler.py ./contracts --interval 3600 --report-dir ./reports ```
标签:DeFi安全, DNS 反向解析, Gas优化, HTML报告, JSON报告, Python, Slither, Solidity, tx.origin漏洞, Web3安全, 云安全监控, 以太坊, 区块链安全, 安全专业人员, 安全评分, 对称加密, 整数溢出, 无后门, 智能合约安全, 智能合约审计, 未经检查的返回值, 聊天机器人, 自动化审计, 逆向工具, 重入攻击, 静态分析