uliyach45/PenTest-Assign2-Blockchain-Security

GitHub: uliyach45/PenTest-Assign2-Blockchain-Security

一份针对 Damn Vulnerable DeFi v4 中 UnstoppableVault 合约的综合渗透测试报告,系统运用五种主流安全工具完成从静态分析到符号执行的全链路审计并产出 48 项漏洞发现。

Stars: 0 | Forks: 0

# 渗透测试-作业2-区块链安全 渗透测试的作业 2 —— 使用 Slither、Foundry、Mythril、Manticore 和 Echidna 对 UnstoppableVault 智能合约(Damn Vulnerable DeFi v4)进行高级区块链安全评估。识别出 48 个漏洞,其中包括 2 个高危严重性发现(任意 transferFrom 和受控的 delegatecall)。 # 渗透测试 —— 作业 2:高级区块链安全 **学生:** Uliya Fatima | **学号:** 232098 **课程:** 渗透测试 | **作业:** 02 **日期:** 2026 年 4 月 30 日 ## 概述 本报告展示了对来自 [Damn Vulnerable DeFi v4](https://github.com/tinchoabbate/damn-vulnerable-defi) 的 **UnstoppableVault** 智能合约的全面渗透测试评估,这是一个业界标准的、被顶级审计公司(Trail of Bits、OpenZeppelin、ConsenSys Diligence)使用的、故意留下漏洞的 DeFi 环境。 ## 环境与工具 | 组件 | 详情 | |-----------|---------| | 操作系统 | Debian Linux | | Python | 3.13.5 | | Solidity | 0.8.25(由 Foundry 自动安装) | | 目标合约 | `UnstoppableVault.sol` — Damn Vulnerable DeFi v4 | | 工具 | 版本 | 用途 | |------|---------|---------| | **Slither** | v0.11.5 | 静态分析 —— 12 个合约中的 48 项发现 | | **Foundry / Forge** | v1.5.1-stable | 模糊测试与 EVM 状态模拟 | | **Mythril** | v0.24.8 (Docker) | 符号执行 —— 确认 SWC-116 | | **Manticore** | v0.3.7 (Ubuntu 22.04) | 符号执行 —— 已部署合约,并生成结果 | | **Echidna** | v0.2.1 (Docker) | 基于属性的模糊测试 —— 50,161 次调用,两个不变量均 **通过** | | **Graphviz** | — | 合约继承可视化 | ## 设置步骤 ``` # 验证 Slither slither --version # Clone 目标 repo git clone https://github.com/tinchoabbate/damn-vulnerable-defi.git cd damn-vulnerable-defi # 安装 Foundry curl -L https://foundry.paradigm.xyz | bash && foundryup # 安装 dependencies forge install transmissions11/solmate --no-git forge install Vectorized/solady --no-git forge install safe-global/safe-smart-account --no-git # 运行 Slither 分析 slither src/unstoppable/ 2>&1 | tee slither_report.txt # 生成 inheritance graph slither src/unstoppable/ --print inheritance-graph sudo apt install graphviz -y dot -Tpng src/unstoppable/.inheritance-graph.dot -o inheritance_graph.png # 运行 Foundry 测试 forge test --match-path test/unstoppable/Unstoppable.t.sol -vvv ``` ## 漏洞摘要 **Slither 结果:分析了 12 个合约 | 101 个检测器 | 48 项发现** ### 高危严重性 | ID | 检测器 | 位置 | 描述 | |----|----------|----------|-------------| | VULN-001 | `arbitrary-send-erc20` | `flashLoan()` 第 78–104 行 | `safeTransferFrom` 使用了受攻击者控制的 `receiver` —— 允许资金被窃取 | | VULN-002 | `controlled-delegatecall` | `execute()` 第 124–127 行 | 用户控制的 `target` 和 `data` 被直接传递给 `delegatecall` —— 允许完全的存储接管 | ### 中危严重性 | 检测器 | 发现 | |----------|---------| | `divide-before-multiply` | FixedPointMathLib 中的 `rpow()` 在乘法时使用了除法的结果 | | `incorrect-equality` (×4) | 在 4 个 ERC4626 函数中使用了 `supply == 0` 严格相等判断 | | `unused-return` (×2) | UnstoppableMonitor 中忽略了 `approve()` 和 `flashLoan()` 的返回值 | ### 低危严重性 | 检测器 | 发现 | |----------|---------| | `missing-zero-check` (×3) | 3 个函数缺少 `address(0)` 验证 | | `reentrancy-events` | `checkFlashLoan()` 在外部调用后发出事件 | | `timestamp` (×2) | `block.timestamp` 用于 2 处比较(SWC-116 —— 也被 Mythril 确认) | ## 工具结果 ### Mythril —— SWC-116 已确认 通过 Docker (Python 3.10) 执行的 Mythril 独立确认了 `block.timestamp` 依赖(SWC-116)—— 交叉验证了 Slither 的低危严重性时间戳发现。 ### Foundry 测试结果 ``` test_assertInitialState() PASS [57,303 gas] — vault deploys correctly test_unstoppable() FAIL [101,773 gas] — expected: challenge active testFuzz_vaultAccountingInvariant PASS [10,000 runs, mean gas: 59,133] testFuzz_amountBound PASS [256 runs, mean gas: 7,225] testFuzz_feeNeverOverflow PASS [256 runs, mean gas: 6,866] ``` ### Echidna (Docker) - 50,161 次总调用 - 两个不变量:**通过** ### Manticore (Ubuntu 22.04) - 成功部署合约 - 完成符号执行 - 结果文件夹:`mcore_qbeltqha/` ## 合约继承图 UnstoppableVault 同时继承自 4 个父合约: ``` UnstoppableVault ├── ERC4626 ├── ReentrancyGuard ← nonReentrant NOT applied to execute() [critical gap] ├── Owned └── IERC3156FlashLender ``` 在渲染的图中高亮显示的函数(flashLoan、flashFee、beforeWithdraw、afterDeposit)被 Slither 直接标记为存在漏洞。 ## 修复摘要 | 漏洞 | 严重性 | 修复方法 | OpenZeppelin 组件 | |--------------|----------|-----|----------------------| | `flashLoan()` 中任意的 `transferFrom` | 高危 | 验证 `receiver == msg.sender` | `SafeERC20.sol` | | `execute()` 中受控的 `delegatecall` | 高危 | 将允许的目标地址加入白名单 | `AccessControl.sol` | | `checkFlashLoan()` 中的重入 | 中危 | 应用 `nonReentrant` 修饰符 | `ReentrancyGuard.sol` | | 严格相等判断 `supply == 0` | 中危 | 使用 `>=` 比较,实现虚拟份额 | `ERC4626.sol` | | 未使用的返回值 | 中危 | 显式处理所有返回值 | `SafeERC20.sol` | | 缺少零地址检查 | 低危 | 在所有设置器中使用 `require(addr != address(0))` | `Address.sol` | | 区块时间戳操纵 | 低危 | 在对时间敏感的比较中使用 `block.number` | 设计模式 | | 混合的 Solidity 版本 | 低危 | 将所有文件统一为 `=0.8.25` | 编译器配置 | ## 工具局限性 | 工具 | 局限性 | |------|-----------| | Slither | 将 11 个内联汇编块标记为发现 —— 均确认为误报(Gas 优化的数学计算) | | Mythril | 与 Python 3.13 不兼容 —— 需要 Docker (Python 3.10) 作为替代方案 | | Foundry | 缺少子模块阻碍了初始设置时的完整测试套件运行 | | Manticore | 在 Debian 上存在 Python 3.13 `wasm/collections.Callable` 冲突 —— 需要 Ubuntu 22.04 | ## 黄金标准方法论 ``` Smart Contract Layer: Slither + Foundry + Mythril + Echidna + Manticore Infrastructure Layer: Nmap (RPC ports 8545/8546) + Metasploit Business Logic Layer: Manual code review ``` ## 其他工具(前沿技术) | 工具 | 类型 | 描述 | |------|------|-------------| | Aderyn | 静态分析 | 由 Cyfrin 开发的基于 Rust 的 Solidity 分析器 —— 在大型代码库上比 Slither 更快 | | Halmos | 形式化验证 | 符号测试,从数学上证明合约的正确性 | | Medusa | 并行化模糊测试 | 由 Trail of Bits 开发的多线程模糊测试器 —— 下一代 Echidna | | Forta | 运行时监控 | 针对已部署合约的实时链上威胁检测 | | Anchor | Solana 框架 | 针对 Solana/Rust 合约的安全框架 | ## 参考资料 | 来源 | 链接 | |--------|------| | Damn Vulnerable DeFi v4 | github.com/tinchoabbate/damn-vulnerable-defi | | Slither | github.com/crytic/slither | | Mythril | github.com/ConsenSys/mythril | | Foundry Book | book.getfoundry.sh | | Echidna | github.com/crytic/echidna | | Manticore | github.com/trailofbits/manticore | | SWC Registry | swcregistry.io | | OpenZeppelin | github.com/OpenZeppelin/openzeppelin-contracts | | ERC-4626 Standard | eips.ethereum.org/EIPS/eip-4626 | ## 工具与技术 `blockchain-security` `smart-contracts` `solidity` `slither` `foundry` `mythril` `manticore` `echidna` `defi` `erc4626` `penetration-testing` `static-analysis` `symbolic-execution` `fuzzing` `ethereum` `web3-security`
标签:CISA项目, CVE安全研究, Damn Vulnerable DeFi, DeFi 安全, delegatecall漏洞, Echidna, Foundry, Manticore, Mythril, Slither, Solidity, Web3 安全, 代币授权漏洞, 以太坊, 区块链安全, 形式化验证, 智能合约审计, 网络安全实验, 请求拦截, 逆向工具, 错误基检测, 静态代码分析