joemunene-by/ghostchain

GitHub: joemunene-by/ghostchain

ghostchain 是一个纯 Python 编写的 Solidity 智能合约静态安全审计工具,通过模式检测将漏洞映射到 SWC Registry 并支持 console、JSON 和 SARIF 多种输出格式。

Stars: 0 | Forks: 0

ghostchain logo

ghostchain

License: MIT Python 3.10+ Part of GhostSuite

ghostchain 是一个针对 Solidity 智能合约的静态分析安全审计工具。 它会扫描 `.sol` 源代码以查找众所周知的漏洞模式,将每个发现映射到 [SWC registry](https://swcregistry.io/)(智能合约弱点分类),并报告包含严重程度、精确位置、代码片段以及修复指南的结果。它能输出 console、JSON 和 SARIF 2.1.0 格式,因此可以直接无缝集成到 GitHub 代码扫描中。 ghostchain 是纯 Python 编写的。它不需要 `solc` 或任何外部编译器,这使其保持自包含且对 CI 友好。其分析采用了一个具备注释和字符串感知能力的 lexer 以及逐行精确的 pattern detectors,因此检查结果会指向真实的代码,而不是注释或字符串字面量中的文本。 ## 支持的 detectors | SWC | 规则 | 标题 | 严重程度 | |-----|------|-------|----------| | SWC-107 | `reentrancy-external-call-before-state-write` | Reentrancy: external call before state update | high | | SWC-115 | `tx-origin-authorization` | Authorization via tx.origin | high | | SWC-104 | `unchecked-low-level-call` | Unchecked low-level call return value | medium | | SWC-101 | `integer-overflow-pre-0.8` | Unchecked arithmetic on pragma below 0.8.0 | medium | | SWC-106 | `unprotected-selfdestruct` | Unprotected selfdestruct | critical | | SWC-116 | `block-timestamp-dependence` | Dependence on block.timestamp | medium | | SWC-112 | `arbitrary-delegatecall` | Delegatecall to untrusted target | high | | SWC-103 | `floating-pragma` | Floating (unlocked) pragma | low | | SWC-110 | `assert-for-input-validation` | assert used for input validation | low | | SWC-100 | `missing-function-visibility` | Function missing explicit visibility specifier | low | 运行 `ghostchain rules` 可从工具本身打印此列表。 ## 安装 ``` git clone https://github.com/joemunene-by/ghostchain.git cd ghostchain python -m venv .venv && source .venv/bin/activate pip install -e . ``` 要求 Python 3.11 或更高版本。 ## 快速开始 扫描单个文件: ``` ghostchain scan path/to/Contract.sol ``` 递归扫描目录: ``` ghostchain scan contracts/ ``` console 输出示例: ``` ghostchain findings +----------+---------+-------------------------------------+-----------------------+ | Severity | SWC | Rule | Location | +----------+---------+-------------------------------------+-----------------------+ | high | SWC-107 | Reentrancy: external call before... | Vault.sol:13:9 | +----------+---------+-------------------------------------+-----------------------+ Scanned 1 file(s), 1 finding(s). [high=1] ``` JSON 输出: ``` ghostchain scan contracts/ --format json --output report.json ``` SARIF 输出(用于 GitHub 代码扫描): ``` ghostchain scan contracts/ --format sarif --output ghostchain.sarif ``` ### Flags - `--format {console,json,sarif}` 选择输出格式(默认为 `console`)。 - `--min-severity LEVEL` 仅报告严重程度等于或高于 `LEVEL` 的结果。 - `--fail-on LEVEL` 使得当任何发现的严重程度等于或高于 `LEVEL` 时,进程以非零状态退出(默认为 `high`)。使用此项来控制 CI 流程。 - `--output FILE` 将报告写入文件而不是 stdout。 - `--verbose` 启用 debug 日志记录。 严重程度级别,从低到高依次为:`informational`、`low`、`medium`、`high`、`critical`。 ## GitHub 代码扫描集成 ghostchain 生成 SARIF 2.1.0 格式,GitHub 原生支持接收该格式。一个最小的 job: ``` - name: Run ghostchain run: ghostchain scan contracts/ --format sarif --output ghostchain.sarif --fail-on critical - name: Upload SARIF uses: github/codeql-action/upload-sarif@v3 with: sarif_file: ghostchain.sarif ``` 随后,结果将显示在仓库的“Security”选项卡下的“code scanning alerts”中。 ## 局限性与误报免责声明 ghostchain 对 tokenized source 使用静态启发式方法。它不构建完整的 control-flow 或 data-flow 图,不解析 cross-contract 调用,也不执行 symbolic execution。它是一个快速的初步 linter,不能替代人工审查、fuzzing 或 formal verification。请预期会出现误报(被标记但实际上安全的代码)和漏报(它未建模的真实 bug)。请将每个发现视为调查的提示,并根据您的风险承受能力调整 `--min-severity` 和 `--fail-on`。 ## 路线图 - 进行 data-flow 跟踪,将攻击者控制的输入与 delegatecall 目标关联起来。 - 跨 modifiers 和 inherited contracts 的函数级 reentrancy 分析。 - 更多 detectors:unchecked ERC20 transfers、`gasleft()` 误用、initializers 上薄弱的 access control、signature replay。 - 可配置的规则集,以及通过行内注释进行按规则抑制。 - Baseline 模式,仅跟踪新引入的发现。 ## 许可证 MIT。详见 [LICENSE](LICENSE)。 属于 ghostsuite 的一部分:十一款开源安全工具,一个幽灵。
标签:Python, Solidity, 区块链安全, 图数据库, 无后门, 智能合约审计, 逆向工具, 错误基检测, 静态代码分析