Communitygithub.com

審計 Foundry 智能合約安全性

此技能針對 Foundry 專案進行深度防禦、部署前的安全審計,結合靜態分析、符號執行、基於屬性的測試與金鑰衛生檢查,確保合約上鏈前無重大漏洞。

審計 Foundry 智能合約安全性 是什麼?

此技能專為開發者設計,在部署任何 Solidity 合約至測試網或主網之前,對 Foundry 專案進行全面安全審計。它整合了四種獨立技術:Slither 與 Aderyn 的快速靜態分析,Mythril 的深度符號執行,基於 Fuzz 與 Invariant 測試的屬性驗證,以及關鍵衛生與金鑰管理檢查。這些方法互補,能有效捕捉重入、訪問控制缺陷、預言機操縱、算術溢出等常見 DeFi 漏洞。支援 Foundry、Slither、Aderyn、Mythril 與 Gitleaks 等工具,最終輸出含 SWC 標識符、嚴重性評級與 PASS/FAIL 閘門的統一審計報告,確保高風險或關鍵問題、測試失敗或金鑰洩漏時無法部署。

相容平台~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 智能合約安全性 是做什麼的?

概述

已部署的智能合約是不可變的,並且保管著真實資金,因此發佈到主網的錯誤無法修補——只能被利用。大多數災難性的 DeFi 損失來自一組重複出現的漏洞類別:重入、訪問控制中斷、預言機/價格操縱,以及未經檢查的算術或外部調用。

此技能對 Foundry 專案執行深度防禦、部署前審計,分層應用四種獨立技術,每種技術都能捕捉其他技術遺漏的問題:

  1. 靜態分析——slither(90+ 檢測器)和 aderyn(Cyfrin,Rust)在數秒內掃描 AST/IR,尋找已知的反模式。
  2. 符號執行——mythril(可選,較慢)探索執行路徑並通過 SMT 求解器找出深層的算術/重入錯誤。
  3. 基於屬性的測試——forge test 搭配模糊測試(testFuzz_*)和不變量測試(invariant_* + 包含幽靈變數的 Handler 合約),在數百萬個隨機序列中證明協議級屬性成立。
  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 報告,需要區分真實錯誤與誤報時。

先決條件

  • 已安裝 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(內建溢出檢查);低於 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          # 不變量測試套件(基於 Handler)

每個涉及價值轉移的合約都應有包含 Handler 的不變量測試(有界輸入、幽靈變數、targetContract(handler))——而不僅僅是單元測試。有關 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 "WARN: 祕密被 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。編排器會輸出包含 PASS/FAIL 閘門的 audit-report.json。

預期輸出

一份 JSON 審計報告,列出帶有 SWC 標識符、嚴重性、工具來源、受影響的合約/函數/行號以及修復建議的發現;以及測試/覆蓋率摘要和一個統一的 PASS / FAIL 部署閘門。任何高/嚴重等級的靜態發現、失敗的測試、洩漏的祕密,或涉及價值轉移程式碼的覆蓋率低於設定閾值,則判定為 FAIL。

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,

相關技能