opspawn/contract-audit-py
GitHub: opspawn/contract-audit-py
针对 NEAR Protocol Rust 智能合约的静态安全分析器,通过内置规则集自动检测常见漏洞并给出评分报告。
Stars: 0 | Forks: 0
# contract-audit-py
NEAR Protocol Rust 智能合约的静态安全分析器。
## 安装
```
pip install contract-audit-py
```
或从源码安装:
```
git clone https://github.com/opspawn/contract-audit-py.git
cd contract-audit-py
pip install -e .
```
## 使用方法
### CLI
扫描合约并打印人类可读的报告:
```
contract-audit --contract ./contract.rs
```
输出 JSON 格式以供编程使用:
```
contract-audit --contract ./contract.rs --json
```
示例输出:
```
Contract Audit Report
Score: 72/100
Findings: 3 (1 HIGH, 2 MEDIUM, 0 LOW)
[HIGH] Line 12: Contract impl block missing #[near_bindgen] annotation
[MEDIUM] Line 18: Using unwrap() may cause panic on None/Err
> let result = some_call().unwrap();
[MEDIUM] Line 21: Accepting deposits without maximum check
> #[payable]
```
### Python API
```
from contract_audit import audit_contract, ContractScanner
# 快速审计
report = audit_contract('./contract.rs')
print(f"Score: {report['score']}/100")
print(f"Findings: {report['summary']['total']}")
# Fine-grained control
scanner = ContractScanner()
findings = scanner.scan_file('./contract.rs')
report = scanner.report()
```
## 安全规则
| 规则 ID | 严重程度 | 描述 |
|---------|----------|-------------|
| MISSING_NEAR_BINDGEN | HIGH | 合约 impl 块缺少 `#[near_bindgen]` 注解 |
| NO_NEAR_BINDGEN | HIGH | impl 块缺少 `#[near_bindgen]` 宏 |
| UNCHECKED_BALANCE | HIGH | 状态变更方法缺少 `predecessor_account_id` 检查 |
| UNSAFE_STORAGE | HIGH | 直接使用 `env::storage_write` 而无 SDK 抽象 |
| REENTRANCY_RISK | HIGH | 跨合约调用后的状态变更模式 |
| OVERFLOW_RISK | MEDIUM | 算术运算中潜在的整数溢出 |
| PANIC_UNWRAP | MEDIUM | 使用 `unwrap()` 可能因 None/Err 导致 panic |
| MISSING_GAS_ATTACH | MEDIUM | 跨合约调用未显式附加 gas |
| EXCESSIVE_DEPOSIT | MEDIUM | 接收存款未设置最大值检查 |
| HARDCODED_ACCOUNT | LOW | 发现硬编码的账户 ID |
| MISSING_ASSERT | LOW | 公共函数缺少断言/require 检查 |
## 评分
审计分数从 100 分开始,并根据发现的问题扣分:
- HIGH 严重程度:-15 分
- MEDIUM 严重程度:-7 分
- LOW 严重程度:-3 分
最低分数为 0 分。
## 运行测试
```
pip install pytest
pytest tests/
```
## 许可证
MIT
标签:NEAR协议, Rust安全, SAST, Web3安全, 代码扫描器, 加密货币安全, 区块链安全, 去中心化应用安全, 反重入攻击, 可视化界面, 安全规则引擎, 整数溢出, 智能合约审计, 盲注攻击, 逆向工具, 错误基检测, 静态代码分析