Communitygithub.com

审计 Foundry 智能合约安全

对 Foundry 项目进行部署前的深度安全审计,结合静态分析、符号执行、属性测试和密钥检查,确保合约在上链前无高危漏洞。

审计 Foundry 智能合约安全 是什么?

本技能提供了一套针对 Foundry 项目的预部署安全审计流程,旨在通过多种独立且互补的技术手段,主动发现并拦截智能合约中常见的严重缺陷。它集成了 Slither 和 Aderyn 的静态分析、可选的 Mythril 符号执行、基于 Foundry 的 Fuzz 和不变性测试,以及结构化的手动审查和密钥泄漏扫描。审计结果会汇总成一份包含 SWC 标识符、严重等级和修复建议的 JSON 报告,并生成最终的 PASS/FAIL 部署门禁。该技能适用于所有处理实际价值(代币、金库、AMM、桥梁、治理等)的 Solidity 合约,要求项目使用 Foundry 并且能够正常编译。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mukul975/Anthropic-Cybersecurity-Skills/tree/main/skills/auditing-foundry-smart-contract-security

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

审计 Foundry 智能合约安全 是做什么的?

概述

已部署的智能合约是不可变的,并且托管着真实资金,因此一个部署到主网上的 bug 无法被修补——它只能被利用。大多数灾难性的 DeFi 损失都源于一小类反复出现的问题:重入、访问控制被破坏、预言机/价格操纵,以及未经检查的算术运算或外部调用。

本技能对一个 Foundry 项目执行纵深防御的预部署审计,分层应用四种独立的技术,每种技术都能捕捉到其他技术遗漏的问题:

  1. 静态分析 — slither(90+ 检测器)和 aderyn(Cyfrin,Rust)在数秒内扫描 AST/IR 以发现已知反模式。
  2. 符号执行 — mythril(可选,较慢)探索执行路径并使用 SMT 求解器寻找深层的算术/重入 bug。
  3. 属性测试 — 使用模糊测试(testFuzz_*)和不变性测试(invariant_* + 带有幽灵变量的处理器合约)的 forge test,在数百万个随机序列上证明协议级别的属性成立。
  4. 手动审查 + 密钥卫生 — 一个结构化的检查清单(参见 references/vulnerability-checklist.md)以及一次秘密/密钥存储审计,确保没有任何私钥以明文形式存在,并且部署使用加密的 cast 密钥库(参见 references/secure-deployment-and-keys.md)。

该技能面向开发者且用于部署前——由构建合约的工程师执行,而不是安全运营中心在事故发生后执行。发现的问题会阻止部署:任何高/严重级别的静态发现、失败的测试、泄漏的密钥或过低的覆盖率 = 失败。

何时使用

  • 在将任何 Solidity 合约部署到测试网或主网 EVM 链之前。
  • 在编写或审查一个 Foundry 项目(foundry.toml、src/、test/、script/)时。
  • 当合约处理价值时:代币(ERC-20/721/1155)、金库、质押、AMM、桥接、治理。
  • 当添加模糊测试或不变性测试,或当涉及价值转移的函数的覆盖率未知时。
  • 当配置部署脚本时——以验证密钥存储在加密的密钥库中,而不是 .env 明文。
  • 当集成价格预言机、外部调用、delegatecall 或可升级代理时。
  • 当分类 Slither/Aderyn 报告,需要区分真正的 bug 和误报时。

前置条件

  • Foundry 已安装(forge、cast、anvil):curl -L https://foundry.paradigm.xyz | bash && foundryup
  • Slither + solc:pip install slither-analyzer 和 solc-select install <ver> && solc-select use <ver>
  • Aderyn(推荐):cargo install aderyn(或 npm i -g @cyfrin/aderyn)
  • Mythril(可选,符号执行较慢):pip install mythril
  • gitleaks(密钥泄漏扫描):请参阅配套技能 implementing-secret-scanning-with-gitleaks
  • 一个编译通过的 Foundry 项目(forge build 成功)——分析器需要构建产物。
  • 假设使用 Solidity ^0.8.x(内置溢出检查);pre-0.8 的合约需要额外的 SafeMath 审查。

在虚拟环境中安装 Python 工具(推荐用于外部管理的发行版)。切勿在存有已解锁的、持有资金的钱包的机器上对不受信任的合约源代码进行分析。

步骤

第 1 步:构建并检查项目

forge build                    # 分析器需要最新的构建产物
forge fmt --check              # 样式门控(可选)
cat foundry.toml               # 注意 solc 版本、优化器、重映射、evm_version

第 2 步:静态分析(快速,每次运行)

# Slither — 整个项目(自动使用 foundry.toml + 重映射)
slither . --json slither-report.json

# Aderyn — Cyfrin Rust 分析器,补充检测器
aderyn . -o aderyn-report.json

或者运行集成的编排器,它同时运行两者、去重并门控结果:

python3 scripts/agent.py --project . --output audit-report.json

第 3 步:对关键合约进行符号执行(可选,较慢)

# 只针对价值最高的合约 — Mythril 路径爆炸
myth analyze src/Vault.sol --solc-json mythril.config.json --execution-timeout 300 -o json
# 或者:python3 scripts/agent.py --project . --mythril src/Vault.sol

第 4 步:属性测试 — 模糊 + 不变性

forge test -vvv                                  # 单元 + 模糊测试
forge coverage --report summary                  # 涉及价值转移代码的覆盖率
forge test --match-test invariant_ -vvv          # 不变性测试套件(基于处理器)

每个涉及价值转移的合约都应该有基于处理器的不变性测试(输入边界、幽灵变量、targetContract(handler))——而不仅仅是单元测试。请参阅 references/api-reference.md 了解处理器模式,并为每个访问控制守卫编写一个 test_RevertWhen_*(配合 vm.expectRevert)。

第 5 步:根据检查清单进行手动审查

对每个合约逐一检查 references/vulnerability-checklist.md:重入(检查-效果-交互 / nonReentrant)、访问控制、预言机操纵、delegatecall/代理存储布局、未检查的返回值、tx.origin、弱随机性、拒绝服务、抢跑/MEV,以及特定 ERC 的陷阱(approve 竞态、收费代币、基数变动)。

第 6 步:密钥卫生与安全部署

gitleaks detect --no-banner            # 确保没有私钥 / 助记词 / .env 被提交
git ls-files | grep -E '\.env$|keystore' && echo "警告:Git 跟踪了密钥文件"

# 将部署密钥一次性导入加密的密钥库 — 永远不要使用明文的 PRIVATE_KEY 环境变量
cast wallet import deployer --interactive

# 通过密钥库账户部署(先在测试网上),在 --broadcast 之前模拟
forge script script/Deploy.s.sol --account deployer --rpc-url <testnet> --broadcast --verify

请参阅 references/secure-deployment-and-keys.md 了解完整的加固规则(MetaMask 卫生、主网使用硬件钱包、RPC 信任、部署后验证)。

第 7 步:分类与报告

合并 Slither + Aderyn + Mythril + 测试结果,按(文件,行)去重,排除已确认的误报,按可利用性 × 财务影响排序,并将每个发现项映射到其 SWC ID。编排器输出带有通过/失败门禁的 audit-report.json。

预期输出

一份 JSON 格式的审计报告,其中包含发现项及其 SWC 标识符、严重等级、工具来源、受影响的合约/函数/行以及修复建议;还包括测试/覆盖率摘要和一个单一的 通过 / 失败 部署门禁。任何高/严重级别的静态发现、失败的测试、泄漏的秘密或涉及价值转移的代码覆盖率低于配置阈值,结果即为失败。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

abusing-dpapi-for-credential-access

Extract and decrypt Windows DPAPI-protected secrets (Credential Manager, browser logins/cookies, Wi-Fi credentials, KeePass keys) online or offline using SharpDPAPI, SharpChrome, Mimikatz, or Impacket

abusing-shadow-credentials-for-privesc

Take over Active Directory accounts by writing attacker-controlled public keys to msDS-KeyCredentialLink (Shadow Credentials) with pyWhisker, Whisker, or Certipy, then authenticate via PKINIT to recover the target

achieving-cmmc-level-2-compliance

>-

acquiring-disk-image-with-dd-and-dcfldd

Create forensically sound bit-for-bit disk images with dd or dcfldd on a Linux forensic workstation, preserving evidence integrity through hash verification (MD5/SHA) during acquisition. Use when imaging a suspect drive, USB device, or memory card for investigation, preserving volatile disk evidence during incident response, or producing a verified copy for legal or law-enforcement proceedings before any destructive analysis.

analyzing-active-directory-acl-abuse

Detect dangerous ACL misconfigurations in Active Directory using ldap3

analyzing-android-malware-with-apktool

Perform static analysis of Android APK malware using apktool for resource decompilation, jadx for Java source recovery, and androguard for manifest inspection, dangerous permission-combination detection, and identification of obfuscated code, dynamic code loading, and reflection-based API calls. Use to statically triage a suspicious APK without executing it or to build mobile malware detection rules.

analyzing-api-gateway-access-logs

Parses API Gateway access logs (AWS API Gateway, Kong, Nginx) to detect

analyzing-apt-group-with-mitre-navigator

Query ATT&CK data with attackcti, mitreattack-python, and stix2, then build MITRE ATT&CK Navigator layers and multi-layer heatmap overlays mapping one or more APT groups

analyzing-azure-activity-logs-for-threats

Queries Azure Monitor activity logs and sign-in logs via azure-monitor-query

analyzing-bootkit-and-rootkit-samples

Analyzes bootkit and advanced rootkit malware infecting the Master

analyzing-browser-forensics-with-hindsight

Parse Chromium-based browser databases with Hindsight to extract and correlate browsing history, downloads, cookies, cached content, autofill data, saved passwords, and extensions from Chrome, Edge, Brave, Opera, and Vivaldi into a unified timeline (XLSX, JSON, or SQLite output). Use during incident response, insider-threat investigations, or criminal cases when you need to reconstruct a user

analyzing-campaign-attribution-evidence

Systematically evaluate cyber-campaign evidence to attribute an operation to a threat actor, using the Diamond Model and Analysis of Competing Hypotheses (ACH) to weigh infrastructure overlaps, TTP consistency, malware code similarity, and timing/language artifacts into confidence-weighted attribution assessments. Use when an incident investigation needs a defensible attribution confidence level.

analyzing-certificate-transparency-for-phishing

Monitor Certificate Transparency logs using crt.sh and Certstream to

analyzing-cloud-storage-access-patterns

Detect abnormal access in AWS S3, GCS, and Azure Blob Storage by analyzing CloudTrail Data Events, GCS audit logs, and Azure Storage Analytics for after-hours bulk downloads, new-IP access, and API-call spikes (e.g. GetObject) via statistical baselines and time-series anomaly detection. Use when investigating suspected cloud data exfiltration or building related detection rules.

analyzing-cobalt-strike-beacon-configuration

Extract and analyze Cobalt Strike beacon configuration from PE files

analyzing-cobaltstrike-malleable-c2-profiles

Parse and analyze Cobalt Strike Malleable C2 profiles with dissect.cobaltstrike (profiles and beacon-payload configs) and pyMalleableC2 (AST parsing) to extract HTTP/DNS transforms, URIs, headers, sleep/jitter, and injection behavior, then generate network detection signatures. Use when reverse-engineering a captured malleable profile or building detections against Cobalt Strike Beacon traffic.

analyzing-command-and-control-communication

Analyzes malware C2 communication over HTTP, HTTPS, DNS, and custom

analyzing-cyber-kill-chain

Analyzes intrusion activity against the Lockheed Martin Cyber Kill Chain

analyzing-disk-image-with-autopsy

Perform comprehensive forensic analysis of raw (dd), E01, or AFF disk images with Autopsy and The Sleuth Kit, recovering deleted files, examining metadata and embedded artifacts, keyword searching, and building investigation timelines with visual reports. Use for structured analysis of a forensic disk image or when stakeholders need visual reports from evidence.

analyzing-dns-logs-for-exfiltration

Analyzes DNS query logs to detect data exfiltration via DNS tunneling,

相关技能