root3315/contract-sec-scanner

GitHub: root3315/contract-sec-scanner

基于正则表达式模式的 Solidity 智能合约静态安全扫描工具,内置20条规则覆盖常见漏洞类型并支持深度分析模式。

Stars: 0 | Forks: 0

# 合约安全扫描器 一款用于检测 Solidity 智能合约中安全漏洞的综合静态分析工具。 ## 功能特性 - **20 条安全规则**,涵盖常见的漏洞模式 - **基于模式的检测**,使用正则表达式进行快速扫描 - **严重性分类**(Critical, High, Medium, Low, Info) - **深度分析**模式,用于彻底检查重入攻击和访问控制 - **多种输出格式**(文本和 JSON) - **批量扫描**包含多个合约的目录 ## 安装 ``` # 克隆或下载项目 cd contract-sec-scanner # 安装依赖 npm install # 构建项目 npm run build ``` ## 使用方法 ### 基本扫描 扫描单个 Solidity 文件: ``` npx ts-node src/index.ts ./contracts/Token.sol ``` 扫描目录: ``` npx ts-node src/index.ts ./src/contracts/ ``` ### 命令行选项 | 选项 | 描述 | |--------|-------------| | `-h, --help` | 显示帮助信息 | | `-v, --version` | 显示版本号 | | `-o, --output ` | 将结果写入文件 | | `-f, --format ` | 输出格式 (默认: text) | | `-e, --exclude ` | 排除规则 ID (逗号分隔) | | `-m, --min-sev ` | 最低严重性级别 | | `-c, --category ` | 仅扫描特定类别 | | `--deep` | 启用深度分析模式 | | `--no-snippets` | 不包含代码片段 | | `--verbose` | 显示详细扫描信息 | ### 示例 ``` # 扫描并输出 JSON npx ts-node src/index.ts ./contracts -f json -o report.json # 仅显示 Critical 和 High 严重级别的问题 npx ts-node src/index.ts ./contracts --min-sev high # 排除特定规则 npx ts-node src/index.ts ./contracts --exclude INTEGER-001,LOGIC-001 # 深度分析以进行彻底检查 npx ts-node src/index.ts ./defi-protocol --deep --verbose # 仅扫描 reentrancy 和 access control 问题 npx ts-node src/index.ts ./contracts --category "Reentrancy,Access Control" ``` ## 工作原理 ### 架构 ``` ┌─────────────────────────────────────────────────────────────┐ │ CLI Entry Point │ │ (index.ts) │ └─────────────────────────────────────────────────────────────┘ │ ┌───────────────┼───────────────┐ │ │ │ ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────┐ ┌───────────┐ │ Scanner │ │ Rules │ │ Utils │ │ (scanner.ts) │ │ (rules.ts) │ │ (utils.ts)│ └─────────────────┘ └─────────────┘ └───────────┘ ``` ### 检测流程 1. **文件发现**:递归查找目标路径中的所有 `.sol` 文件 2. **源码解析**:读取并验证 Solidity 源代码 3. **规则应用**:应用来自安全规则的正则表达式模式 4. **深度分析**(可选):执行额外的上下文感知检查 5. **结果聚合**:收集并按严重性对发现进行排序 6. **输出**:格式化结果以供控制台或文件输出 ### 安全规则 扫描器包含跨多个类别的 20 条规则: | 类别 | 规则数 | 示例 | |----------|-------|----------| | Reentrancy | 2 | 缺少 nonReentrant, 调用后状态更改 | | Integer Overflow | 2 | 未检查的算术运算, 旧 Solidity 版本 | | Access Control | 3 | 缺少 onlyOwner, 使用了 tx.origin | | External Calls | 3 | 未检查的 call, 不安全的 transfer | | Timestamp | 1 | block.timestamp 操纵 | | Randomness | 1 | 弱随机性来源 | | Denial of Service | 2 | 无界循环 | | Logic Errors | 2 | 乘法前的除法 | | Best Practices | 4 | 已弃用的函数, Public 变量 | ### 严重性级别 - **Critical**:紧迫的安全风险,必须在部署前修复 - **High**:重大漏洞,应在部署前修复 - **Medium**:潜在问题,建议审查 - **Low**:轻微隐患,建议考虑修复 - **Info**:信息提示,无需立即处理 ## 编程用法 ``` import { createScanner, SECURITY_RULES } from './src/index'; // Create scanner with options const scanner = createScanner({ minSeverity: 'high', includeSnippets: true }); // Scan source code const result = scanner.scan(sourceCode, 'MyContract.sol'); // Access findings for (const finding of result.findings) { console.log(`${finding.severity}: ${finding.ruleName}`); console.log(` Line ${finding.line}: ${finding.message}`); } // Deep analysis const reentrancyIssues = scanner.checkReentrancy(sourceCode); const accessIssues = scanner.checkAccessControl(sourceCode); // Get statistics const stats = scanner.getStats(); console.log(`Scanned ${stats.contractsScanned} contracts`); ``` ## 运行测试 ``` # 使用 ts-node 运行测试 npx ts-node tests/scanner.test.ts # 或者先构建并运行编译后的测试 npm run build node tests/scanner.test.js ``` ## 项目结构 ``` contract-sec-scanner/ ├── src/ │ ├── index.ts # CLI entry point and exports │ ├── scanner.ts # Core scanning logic │ ├── rules.ts # Security rule definitions │ └── utils.ts # Helper utilities ├── tests/ │ └── scanner.test.ts ├── package.json ├── tsconfig.json └── README.md ``` ## 限制 - **仅静态分析**:不执行代码或检测运行时问题 - **基于模式**:可能会产生误报或遗漏复杂的漏洞 - **无 AST 解析**:使用正则表达式模式而非完整的 Solidity 解析器 - **单文件作用域**:不分析跨合约交互 ## 贡献 添加新的安全规则: 1. 将规则定义添加到 `src/rules.ts` 2. 包含模式、严重性和建议 3. 针对已知易受攻击的合约进行测试 4. 更新文档 ## 许可证 MIT
标签:CMS安全, DeFi安全, JavaScript, Linux安全, MITM代理, SAST, Solidity, Streamlit, TypeScript, Web3安全, 代码安全, 代码生成, 以太坊, 加密, 加密货币, 区块链, 安全合规, 安全插件, 对称加密, 无服务器架构, 智能合约安全, 暗色界面, 模式匹配, 渗透测试工具, 漏洞扫描器, 漏洞枚举, 盲注攻击, 网络代理, 自动化审计, 自动化攻击, 自动化资产收集, 访问控制, 软件供应链安全, 远程方法调用, 重入攻击检测, 错误基检测, 静态代码分析, 风险分析