ameerozigi/defi-security-suite

GitHub: ameerozigi/defi-security-suite

一个面向审计的 Foundry 测试套件,提供漏洞 PoC、重现与模糊测试,用于系统性检验 DeFi 合约的安全性。

Stars: 0 | Forks: 0

# defi-security-suite ![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/39/39faa54be350a1dab8afd3b2fb8c1c83e4d9cff84abfef2374d19a18053687c4.svg) ![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg) ![Solidity](https://img.shields.io/badge/Solidity-0.8.24-363636) ![Foundry](https://img.shields.io/badge/Built%20with-Foundry-FFDB1C) 审计级别的 Foundry 测试套件,涵盖常见的 DeFi 漏洞模式、历史漏洞利用复现、不变性测试(invariant tests)和模糊测试活动(fuzz campaigns)。每个漏洞利用测试都会部署易受攻击的合约,执行攻击并展示其获利。修复后的对应合约也在同一文件中进行测试。 ## 快速开始 ``` # 安装 Foundry curl -L https://foundry.paradigm.xyz | bash && foundryup # 安装依赖 (OpenZeppelin) forge install OpenZeppelin/openzeppelin-contracts # 运行所有测试 forge test -vvv # 仅运行 exploit PoC forge test --match-path "test/exploits/**" -vvv # 使用更多 runs 运行 fuzz 测试 FOUNDRY_FUZZ_RUNS=10000 forge test --match-path "test/fuzz/**" # 运行 invariant suite forge test --match-path "test/invariants/**" -vvv ``` ## 漏洞索引 | # | 漏洞 | 严重性 | 映射事件 | 易受攻击的合约 | 测试文件 | |---|---|---|---|---|---| | 1 | 重入攻击 (Reentrancy) | 严重 | The DAO (2016, 3.6M ETH), Rari Fuse (2022, $80M) | `VulnerableVault` | `test/exploits/Reentrancy.t.sol` | | 2 | ERC-4626 通胀 / 捐赠攻击 | 高 | Wise Lending, Hundred Finance (2023, $7M) | `VulnerableVault` | `test/exploits/DonationAttack.t.sol` | | 3 | 闪电贷预言机操纵 | 严重 | Cream Finance (2021, $130M), Mango (2022) | `VulnerableLending` | `test/exploits/FlashLoanManipulation.t.sol` | | 4 | 缺少访问控制 | 严重 | Poly Network (2021, $611M), Ronin Bridge (2022, $625M) | `VulnerableAdmin` | `test/exploits/AccessControl.t.sol` | | 5 | 精度丢失 / 舍入误差 | 中 | Compound COMP bug, Hundred Finance | `VulnerableAMM` | `test/exploits/RoundingExploit.t.sol` | ## 架构 ``` src/ targets/ Intentionally vulnerable contracts (clearly marked) fixed/ Patched versions — same interface, fixed implementation test/ exploits/ PoC tests: deploy vulnerable → exploit → assert profit invariants/ Stateful fuzzing with handler pattern fuzz/ Property-based fuzz tests on fixed contracts ``` 每个易受攻击的合约都与一个已修复的合约 1:1 配对。修复部分使用 `[FIX-N]` 注释在行内进行了标注,并引用了特定的漏洞类别。 ## 如何运行 Fuzz 测试 Fuzz 测试使用 Foundry 内置的 fuzzer。在 `foundry.toml` 中配置运行参数: ``` [profile.default.fuzz] runs = 2000 [profile.ci] fuzz = { runs = 10000 } ``` 使用额外的种子(seeds)运行以保证可复现性: ``` forge test --match-path "test/fuzz/**" --fuzz-seed 0xdeadbeef -vvv ``` ## 如何添加新的漏洞利用 1. 将易受攻击的合约添加到 `src/targets/YourVulnerable.sol` 2. 将修复后的合约添加到 `src/fixed/YourFixed.sol` 3. 创建 `test/exploits/YourExploit.t.sol`,包含: - 顶部注释块:CVE/事件参考、损失金额、根本原因 - `test_exploit_vulnerable()` — 验证攻击 - `test_exploit_fixed()` — 验证修复有效 4. 在 `test/fuzz/FuzzYour.t.sol` 中添加 fuzz 覆盖率 5. 在上方的漏洞索引中添加一行 ## 不变性测试模式 不变性测试使用 `targetContract` + handler 模式: ``` contract Handler { function action1(uint256 seed, uint256 amount) external { ... } function action2(uint256 seed) external { ... } } contract MyInvariants is StdInvariant, Test { function setUp() public { targetContract(address(handler)); } function invariant_myProperty() public view { ... } } ``` Foundry 会以随机序列调用 handler 函数。在每个序列之后,所有的 `invariant_*` 函数都必须通过。这可以捕获那些仅因特定交互顺序而产生的 bug。 ## 许可证 MIT
标签:DeFi, Foundry, Maven, Solidity, 区块链安全, 智能合约审计, 模糊测试, 漏洞验证