# 智能合约安全审计工具

随着区块链应用的复杂度不断提高，智能合约安全已经成为整个生态的关键问题。安全审计工具通过静态分析、动态测试、符号执行等多种技术手段，帮助开发者在合约部署前发现潜在漏洞。本节介绍业界主流的智能合约审计工具及其应用场景。

## 1. Slither

**简介**：Slither 是由 Trail of Bits 开发的静态分析框架，用 Python 编写，支持 Solidity 和 Vyper。它能够快速扫描合约代码，识别常见的安全漏洞和代码坏味道，并可接入 CI、Hardhat 和 Foundry 项目。

**核心特性**：

* **快速静态分析**：基于数据流和控制流分析，无需执行代码即可识别潜在问题
* **丰富的检测器库**：内置多类检测器，覆盖重入、整数溢出、权限控制等常见漏洞
* **易于集成**：可作为命令行工具、Python 库使用，支持 CI/CD 集成
* **详细的报告**：按严重级别（高、中、低、信息）分类输出，并提供修复建议

**使用示例**：

```bash

# 安装 Slither
pip install slither-analyzer

# 分析合约
slither contract.sol

# 在 Hardhat/Foundry/Dapp/Brownie 项目根目录分析
slither .

# 输出详细报告
slither contract.sol --json results.json
```

**典型检测场景**：

```solidity
pragma solidity ^0.8.0;

contract VulnerableContract {
    uint public balance;

    // Slither 会警告：未使用的状态变量
    uint unused_var;

    // 重入漏洞检测
    function withdraw(uint amount) public {
        require(balance >= amount);
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success);
        balance -= amount;  // Slither: 状态更新顺序错误
    }

    // 权限控制检测
    function setBalance(uint newBalance) public {
        balance = newBalance;  // Slither: 没有权限检查
    }
}
```

运行 Slither 会输出类似如下的警告：

```
INFO:Slither:Analyzing contract...
Reentrancy in withdraw (VulnerableContract.withdraw):
	External call: (bool success, ) = msg.sender.call{value: amount}("")
	State change after call: balance -= amount
```

## 2. Mythril

**简介**：Mythril 是 ConsenSys Diligence 维护的 EVM 字节码符号执行工具，可分析 Solidity 源码编译产物或链上合约地址，发现特定路径下才触发的漏洞。

**核心特性**：

* **符号执行**：模拟合约执行的所有可能路径，发现在特定条件下才会触发的漏洞
* **字节码分析**：直接分析以太坊虚拟机（EVM）字节码，不仅限于 Solidity 源码
* **安全属性检证**：支持自定义检验属性和约束条件
* **可视化输出**：生成控制流图，直观展示漏洞位置

**使用示例**：

```bash

# 安装 Mythril
pip install mythril

# 分析合约源码
myth analyze contract.sol

# 分析部署在主网上的合约
myth analyze 0x06012c8cf97bead5deae237070f9587f8e7a266d

# 生成适合集成处理的 JSON 报告
myth analyze contract.sol -o jsonv2
```

**适用场景**：

* 检测高级逻辑漏洞（如状态机不一致）
* 分析复杂的资金流向
* 验证访问控制策略

**局限性**：

* 符号执行需要较长的分析时间，不适合超大型合约
* 对于涉及外部调用的复杂场景可能产生误报

## 3. Echidna

**简介**：Echidna 是 Trail of Bits 基于属性的模糊测试（Property-Based Fuzzing）工具，通过随机生成交易序列来探索合约的状态空间。

**核心特性**：

* **属性测试**：开发者定义安全属性（如余额不应为负），Echidna 尝试构造交易序列来违反这些属性
* **高效的随机测试**：相比符号执行，能更快地覆盖复杂的执行路径
* **多合约测试**：支持测试多个相互交互的合约
* **可配置性强**：支持自定义初始化、约束、回调函数等

**使用示例**：

```bash

# 安装 Echidna（需要 Docker）
docker pull trailofbits/echidna

# 创建包含测试属性的合约
cat > contract_with_properties.sol << 'EOF'
pragma solidity ^0.8.0;

contract TestTarget {
    uint public balance = 1000;

    function deposit(uint amount) public {
        balance += amount;
    }

    function withdraw(uint amount) public {
        require(balance >= amount);
        balance -= amount;
    }

    // Echidna 会尝试使此属性返回 false
    function echidna_balance_never_negative() public view returns (bool) {
        return balance >= 0;
    }

    function echidna_balance_not_too_large() public view returns (bool) {
        return balance <= 10**18;  // 防止整数溢出
    }
}
EOF

# 运行 Echidna
docker run --rm -v /path/to/contracts:/contracts trailofbits/echidna echidna /contracts/contract_with_properties.sol
```

**优势与劣势**：

* 优势：能发现符号执行可能遗漏的状态组合问题；快速反馈
* 劣势：不能保证发现所有漏洞；依赖于属性定义的质量

## 4. Hardhat 与 OpenZeppelin Upgrades

**简介**：Hardhat 是开发、测试和部署框架。OpenZeppelin 的 `@openzeppelin/hardhat-upgrades` 插件用于在 Hardhat 脚本中部署和升级代理合约，并做升级安全校验；它不是 Slither/Mythril 的集成审计套件。Slither 和 Mythril 应作为独立 CLI、pre-commit 或 CI 步骤运行。

**核心用途**：

* **开发测试**：使用 Hardhat 编译、运行单元测试和本地主网分叉测试
* **安全升级**：使用 OpenZeppelin Upgrades 部署代理并校验升级兼容性
* **独立扫描**：Slither 支持直接扫描 Hardhat/Foundry 项目；Mythril 通过 `myth analyze` 单独运行
* **安全库**：使用 OpenZeppelin Contracts v5 时按当前路径导入，并处理 v5 构造函数变化

**使用示例**：

```bash

# 安装 Hardhat、ethers 插件和 OpenZeppelin Upgrades
npm install --save-dev hardhat @nomicfoundation/hardhat-ethers ethers @openzeppelin/hardhat-upgrades

# 创建项目
npx hardhat init
```

```javascript
// hardhat.config.js
require("@nomicfoundation/hardhat-ethers");
require("@openzeppelin/hardhat-upgrades");

module.exports = {
  solidity: "0.8.24",
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  networks: {
    hardhat: {},
    localhost: {
      url: "http://127.0.0.1:8545"
    }
  }
};
```

```bash
# 运行测试
npx hardhat test

# 如已安装 solidity-coverage，可运行覆盖率分析
npx hardhat coverage

# 安全扫描作为独立步骤运行
slither .
myth analyze contracts/VulnerableContract.sol -o jsonv2
```

**测试示例**：

```javascript
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("VulnerableContract", function () {
    let contract;

    beforeEach(async function () {
        const VulnerableContract = await ethers.getContractFactory("VulnerableContract");
        contract = await VulnerableContract.deploy();
    });

    it("Should prevent reentrancy attack", async function () {
        const [owner, attacker] = await ethers.getSigners();

        // 部署攻击合约
        const AttackContract = await ethers.getContractFactory("ReentrancyAttack");
        const attack = await AttackContract.deploy(await contract.getAddress());

        // 尝试发动重入攻击，应该被阻止
        await expect(
            attack.attack({ value: ethers.parseEther("1") })
        ).to.be.revertedWithCustomError(contract, "ReentrancyGuardReentrantCall");
    });
});
```

## 5. Manticore

**简介**：Trail of Bits 的 Manticore 是一个符号执行工具，支持智能合约、二进制文件和 WASM。其官方仓库已标明处于维护模式：不再内部持续开发，仅接受小型修复和轻量改进。因此它更适合复现实验、研究或已有流程中的定制分析，新项目通常应优先使用 Slither、Echidna、Foundry fuzz/invariant tests 等仍活跃的工具链。

**核心特性**：

* **多语言支持**：支持 EVM 字节码、二进制、WASM
* **高级分析能力**：可发现复杂的多步漏洞
* **插件扩展**：支持自定义分析器和检测规则

**基本使用**：

```python
from manticore.ethereum import ManticoreEVM

m = ManticoreEVM()

# 从 Solidity 源码加载合约
source_code = open('contract.sol').read()
user_account = m.create_account(balance=1*10**18)
contract_account = m.solidity_create_contract(source_code, owner=user_account, balance=0)

# 执行符号分析
m.run()
```

## 6. 综合安全审计实践

在实际项目中，通常需要多工具组合使用以获得全面的安全覆盖。推荐的审计流程：

```
1. 自动扫描阶段
   ├─ Slither：快速识别常见漏洞（5分钟）
   └─ Mythril：深度符号执行（30-60分钟）

2. 属性测试阶段
   └─ Echidna：基于属性的模糊测试（持续运行，寻找边界情况）

3. 手动审查阶段
   ├─ 代码复审（逻辑验证、权限检查）
   └─ 设计审查（架构安全性、升级机制）

4. 集成测试阶段
   ├─ Hardhat：单元和集成测试
   ├─ 模拟主网环境测试
   └─ 覆盖率目标：>90%

5. 上线前
   ├─ 多签审计（由独立安全公司）
   ├─ Testnet 长期运行
   └─ Bug Bounty 计划
```

## 7. 工具对比表

| 工具        | 类型         | 学习曲线 | 速度 | 准确性 | 最佳用途         |
| --------- | ---------- | ---- | -- | --- | ------------ |
| Slither   | 静态分析       | 低    | 快  | 中   | 快速初步审查       |
| Mythril   | 符号执行       | 高    | 慢  | 高   | 深度逻辑漏洞       |
| Echidna   | 模糊测试       | 中    | 中  | 中   | 状态空间探索       |
| Hardhat   | 开发测试框架     | 低    | 中  | 中   | 编译、测试、本地主网分叉 |
| Manticore | 符号执行（维护模式） | 高    | 慢  | 高   | 研究、复现、已有定制流程 |

## 8. 安全分类和验收基线

* **SWC Registry**：适合作为历史漏洞编号和旧报告映射参考；官方页面已说明内容不再主动维护，不应作为当前唯一验收基线。
* **OWASP SCSVS / SCSTG / SCWE**：适合开发、测试和安全验收中的需求清单、测试指南和弱点枚举。
* **EEA EthTrust Security Levels v2 / Editor's Draft**：适合审计团队和项目方约定安全等级、形成合约验收基线。
* **Securify**：原 Securify v1 仓库已废弃并指向 Securify v2；除非为了复现历史结果，不建议作为新项目默认扫描器。

## 9. 最佳实践建议

1. **尽早整合自动化审计**：在开发阶段而非部署前，将安全检查纳入 CI/CD 流程
2. **多工具交叉验证**：不要完全依赖单一工具，不同工具各有盲点
3. **定义清晰的属性**：使用 Echidna 时，属性定义直接影响测试有效性
4. **关注误报和漏报**：调整工具配置以平衡误报率和覆盖度
5. **持续监控**：合约部署后仍需要链上监控和告警机制
6. **安全库的使用**：优先使用经过审计的库（如 OpenZeppelin）而非自行实现
7. **文档和注释**：为复杂逻辑添加详细注释，便于审计者理解意图

这些工具的合理组合使用，能够显著提高智能合约的安全性，降低被攻击的风险。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/blockchain_guide/05_crypto/security_tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
