Jerrytriple8/sol-vuln-scan
GitHub: Jerrytriple8/sol-vuln-scan
基于纯正则的零依赖 Solidity 源代码漏洞扫描器,在无需编译的前提下毫秒级检测常见智能合约安全反模式。
Stars: 0 | Forks: 0
# sol-vuln-scan
用于 Solidity 源代码的静态分析扫描器。无需编译或部署即可检测常见的安全反模式 —— 纯基于 regex,零依赖,毫秒级运行。
## 检测内容
| 严重程度 | 规则 | 模式 |
|----------|------|---------|
| Critical | SWSC-001 | 循环中的 delegatecall |
| Critical | SWSC-002 | 未检查的 delegatecall 返回值 |
| Critical | SWSC-003 | selfdestruct 被公开 |
| High | SWSC-010 | tx.origin 身份验证 |
| High | SWSC-011 | 未检查的 send/transfer |
| High | SWSC-012 | assembly sstore |
| High | SWSC-013 | 缺乏零值检查的 ecrecover |
| Medium | SWSC-020 | 浮动 pragma |
| Medium | SWSC-021 | 弱随机性(区块变量) |
| Medium | SWSC-022 | 时间戳依赖 |
| Medium | SWSC-023 | 未检查的底层调用 |
| Medium | SWSC-024 | constructor 中缺失零值检查 |
| Medium | SWSC-025 | 缺少 reentrancy guard |
| Medium | SWSC-026 | 已弃用的 sha3 用法 |
| Low | SWSC-030 | inline assembly 用法 |
| Info | SWSC-031 | magic numbers |
| Info | SWSC-032 | 未索引的事件参数 |
## 安装
零依赖 —— 仅需 Python 3.8+ 标准库。
```
git clone https://github.com/Jerrytriple8/sol-vuln-scan.git
cd sol-vuln-scan
```
## 用法
### 扫描单个文件
```
python scan.py contracts/Token.sol
```
### 递归扫描目录
```
python scan.py contracts/
```
### 按严重程度过滤
```
python scan.py contracts/ -s high # only critical + high
python scan.py contracts/ -s critical # only critical
```
### JSON 输出(用于 CI 流水线)
```
python scan.py --json contracts/
```
### 从 stdin 读取(支持管道)
```
cat Token.sol | python scan.py --stdin
```
### 列出所有规则
```
python scan.py --list-rules
```
## 示例输出
```
contracts/VulnerableContract.sol
critical: 1, high: 4, medium: 9, low: 1
[CRITICAL] SWSC-003 — selfdestruct-exposed
selfdestruct/suicide callable — contract can be permanently destroyed.
contracts/VulnerableContract.sol:55:5
Fix: Remove selfdestruct or restrict to owner with timelock.
[HIGH] SWSC-010 — tx-origin-auth
tx.origin used for authorization — vulnerable to phishing attacks.
contracts/VulnerableContract.sol:17:14
Fix: Use msg.sender instead of tx.origin for authentication.
============================================================
Scan Summary
Files scanned: 1
Total findings: 15
critical: 1
high: 4
medium: 9
low: 1
```
## CI 集成
退出代码与发现的最严重程度相匹配:
| 退出代码 | 含义 |
|-----------|---------|
| 0 | 无 critical/high 发现 |
| 3 | 发现 high 级别严重程度 |
| 4 | 发现 critical 级别严重程度 |
```
# GitHub Actions 示例
- name: Solidity security scan
run: python scan.py contracts/ -s high --no-snippets
```
## 工作原理
该扫描器对原始 Solidity 源代码应用 regex 模式。每个模式都针对 SWC Registry(智能合约弱点分类)中已知的漏洞类别。无需编译,无需 ABI,无需 bytecode —— 仅进行源代码级别的模式匹配。
这是一个**初步分诊工具**。它能捕获明显的反模式,但不能替代:
- 用于数据流分析的 [Slither](https://github.com/crytic/slither)
- 用于符号执行的 [Mythril](https://github.com/Consensys/mythril)
- [Foundry](https://github.com/foundry-rs/foundry) 模糊测试
- 人工审计
在 CI 中使用 `sol-vuln-scan`,以便在进行深入分析之前捕获容易发现的问题。
## 添加自定义规则
编辑 `scanner/rules.py`。每个规则都是一个 dict:
```
{
"id": "SWSC-099",
"name": "my-custom-rule",
"severity": "high",
"category": "custom",
"description": "Description of what's wrong.",
"pattern": re.compile(r"regex_pattern", re.MULTILINE),
"fix": "How to fix it.",
}
```
## 许可证
MIT
标签:Solidity, 云安全监控, 区块链, 安全扫描, 时序注入, 智能合约, 静态分析