web3-audit

commandupstream۷ کیلوبایت۱۵ بخش

‏نسخه‌ی فشرده‌ی command مربوط به audit قرارداد هوشمند web3 — ‏سیگنال‌های kill مربوط به pre-dive، ‏کلاس‌های باگ DeFi و قواعد سریع تصمیم.

توضیح انگلیسی اصلی (مرجع)

Smart contract security audit — runs through 10 bug class checklist (accounting desync, access control, incomplete path, off-by-one, oracle errors, ERC4626, reentrancy, flash loan, signature replay, proxy/upgrade). Applies pre-dive kill signals first. Generates Foundry PoC template for confirmed findings. Usage: /web3-audit <contract.sol>

فلوی اجرای گام‌به‌گام

◆
web3-auditشروع اجرا
۱
TVL < $500K → max payout too low for effort → SKIP
۲
2+ top-tier audits (Halborn, ToB, Cyfrin, OZ) on simple protocol → SKIP
۳
Protocol < 500 lines, single A→B→C flow → minimal attack surface → SKIP
۴
؟ تصمیمmax_payout = min(10% × TVL, program_cap) → if < $10K → SKIP
۵
؟ تصمیمWhich state variables are updated in the normal path?
۶
؟ تصمیمAre ALL of them also updated in the early return path?
۷
؟ تصمیمIf A updated but B isn't → potential desync bug
۸
List all state changes in function A (deposit/place/create)
۹
List all state changes in function B (withdraw/update/cancel)
۱۰
؟ تصمیمFor each state change in A: does B have the corresponding reverse?
۱۱
؟ تصمیمFor each token transfer in A: does B have the corresponding refund?
۱۲
؟ تصمیمIs staleness checked? (`require(block.timestamp - updatedAt <= MAX_AGE)`)
۱۳
؟ تصمیمIs Pyth confidence interval checked? (`require(conf * 10 <= price)`)
۱۴
؟ تصمیمIs TWAP window > 1800 seconds (30 min)?
✓
پایان — اعتبارسنجی و گزارش

ترتیب مرحله‌ای استخراج‌شده از بخش‌های متدولوژی/اعتبارسنجی — ۱۴ گام؛ گام‌های شرطی با «؟ تصمیم» مشخص شده‌اند.

نقشه‌ی ساختار اسکیل

web3-auditکامند دانش · ۱۵ بخش۱نمای کلی و کاربرد۲نمای کلی و کاربرد۳سطح حمله وسیگنال‌ها۴بخش تکمیلی۵بخش تکمیلی۶بخش تکمیلی۷بخش تکمیلی۸بخش تکمیلی۹بخش تکمیلی۱۰بخش تکمیلی۱۱بخش تکمیلی۱۲بخش تکمیلی۱۳بخش تکمیلی۱۴بخش تکمیلی۱۵بخش تکمیلی
نمای کلی و کاربرد · ۱۴سطح حمله و سیگنال‌ها · ۱

روی هر گره کلیک کنید تا به بخش مربوطه بروید؛ دکمه‌ی «سورس MD» در هدر هر بخش، متن اصلی را با شماره‌ی خط می‌آورد.

بخش‌های اسکیل

هر بخش با عنوان فارسی، نوع محتوا و توضیح فارسی نمایش داده شده. روی «سورس MD» کلیک کنید تا متن اصلی انگلیسی با شماره‌ی خطوط فایل در نمایشگر کناری باز شود.

۱

نمای کلی و کاربرد

Introduction
ترجمه‌ی کامل ✓بخش ۱ از ۱۵

توضیح کلی، دامنه‌ی پوشش و زمان به‌کارگیری اسکیل

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

# /web3-audit

Smart contract security audit using the 10-bug-class methodology.

۲

نمای کلی و کاربرد

Usage
ترجمه‌ی کامل ✓بخش ۲ از ۱۵

توضیح کلی، دامنه‌ی پوشش و زمان به‌کارگیری اسکیل

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

/web3-audit VulnerableContract.sol
/web3-audit https://github.com/protocol/contracts
/web3-audit [paste contract code]
۳

سطح حمله و سیگنال‌ها

Step 0: Pre-Dive Kill Signals
ترجمه‌ی کامل ✓بخش ۳ از ۱۵

نشانه‌های URL، header، JS و tech stack که نشان می‌دهد این تکنیک اینجا جواب می‌دهد

‏همیشه این‌ها را قبل از خواندن هر کدی چک کن:

1. TVL < $500K → max payout too low for effort → SKIP
2. 2+ top-tier audits (Halborn, ToB, Cyfrin, OZ) on simple protocol → SKIP
3. Protocol < 500 lines, single A→B→C flow → minimal attack surface → SKIP
4. max_payout = min(10% × TVL, program_cap) → if < $10K → SKIP

Formula: Is [TVL * 10%] > [hours I'll spend * hourly rate]? If not, skip.

‏فقط ادامه بده اگر امتیاز >= ‏۶/۱۰:

  • ‏‏TVL > ‏۱۰ میلیون دلار: ‏+۲
  • ‏‏Immunefi Critical >= ‏۵۰ هزار دلار: ‏+۲
  • ‏بدون audit سطح-بالا روی نسخه‌ی فعلی: ‏+۲
  • ‏‏< ‏۳۰ روز از deploy: ‏+۱
  • ‏پروتکلی که قبلاً hunt کرده‌ای: ‏+۱
  • ‏‏proxyهای قابل-ارتقا موجود: ‏+۱
۴

بخش تکمیلی

Step 1: Accounting State Desynchronization (28% of Criticals)
ترجمه‌ی کامل ✓بخش ۴ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

# Find accounting variables
grep -rn "totalSupply\|totalShares\|totalAssets\|totalDebt\|cumulativeReward" contracts/

# Find ALL early returns in critical functions
grep -rn "\breturn\b" contracts/ -B3 | grep -B3 "if\b"

Check: For each early return in claim/redeem/withdraw functions:

  • Which state variables are updated in the normal path?
  • Are ALL of them also updated in the early return path?
  • If A updated but B isn't → potential desync bug
۵

بخش تکمیلی

Step 2: Access Control (19% of Criticals)
ترجمه‌ی کامل ✓بخش ۵ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

# Sibling function families — do ALL have same modifier set?
grep -rn "function vote\|function poke\|function reset\|function update\|function claim\|function harvest" contracts/ -A2

# Ownership check: existence vs ownership
grep -rn "_requireOwned\|ownerOf\|_isApprovedOrOwner" contracts/ -B5

# Silent modifiers (if without revert)
grep -rn "modifier\b" contracts/ -A8 | grep -B3 "if (" | grep -v "require\|revert"

# Uninitialized proxy
grep -rn "function initialize\b" contracts/ -A3
grep -rn "_disableInitializers()" contracts/

Check: Does EVERY sibling function in a family have the SAME modifiers?

۶

بخش تکمیلی

Step 3: Incomplete Code Path (17% of Criticals)
ترجمه‌ی کامل ✓بخش ۶ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏تست مقایسه‌ی خانواده‌ی تابع:

1. List all state changes in function A (deposit/place/create)
2. List all state changes in function B (withdraw/update/cancel)
3. For each state change in A: does B have the corresponding reverse?
4. For each token transfer in A: does B have the corresponding refund?
grep -rn "safeApprove\b" contracts/    # safeApprove without zero-reset?
grep -rn "delete\b" contracts/ -B5     # delete before operation completes?
grep -rn "function deposit\|function mint\|function withdraw\|function redeem" contracts/ -A10
۷

بخش تکمیلی

Step 4: Off-By-One (22% of Highs)
ترجمه‌ی کامل ✓بخش ۷ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏تست ذهنی: ‏برای هر if (A > B): «وقتی A == ‏B چه اتفاقی می‌افتد؟» ‏آیا درست است؟

# Boundary comparisons
grep -rn "Period\|Epoch\|Deadline\|period\|epoch\|deadline" contracts/ -A3 | grep "[<>][^=]"

# Loop breaks
grep -rn "\bbreak\b" contracts/ -B10

# Array bounds
grep -rn "\.length\s*-\s*1\|i\s*<=\s*.*\.length\b" contracts/
۸

بخش تکمیلی

Step 5: Oracle / Price Manipulation
ترجمه‌ی کامل ✓بخش ۸ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

# Missing staleness check
grep -rn "latestRoundData" contracts/ -A5 | grep -v "updatedAt\|timestamp"

# Pyth confidence interval
grep -rn "getPriceUnsafe\|getPrice\b" contracts/ -A8 | grep -v "conf\|confidence"

# TWAP windows
grep -rn "secondsAgo\|TWAP\|cardinality" contracts/ -A5

Check:

  • Is staleness checked? (require(block.timestamp - updatedAt <= MAX_AGE))
  • Is Pyth confidence interval checked? (require(conf * 10 <= price))
  • Is TWAP window > 1800 seconds (30 min)?
۹

بخش تکمیلی

Step 6: ERC4626 Vaults
ترجمه‌ی کامل ✓بخش ۹ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

grep -rn "function deposit\|function mint\|function withdraw\|function redeem" contracts/ -A10
grep -rn "function transfer\|function transferFrom" contracts/ -A15

Check:

  • Does mint() call the same validation as deposit()?
  • Does transfer() move lock records along with shares?
  • Is there a _decimalsOffset() virtual shares defense against first-depositor attack?
۱۰

بخش تکمیلی

Step 7: Reentrancy
ترجمه‌ی کامل ✓بخش ۱۰ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

# Effects after interactions
grep -rn "\.call{value\|safeTransfer\|transfer(" contracts/ -B10 | grep -v "require\|revert"

# Missing nonReentrant
grep -rn "function withdraw\|function redeem\|function claim" contracts/ -A2 | grep -v "nonReentrant"

‏چک: ‏آیا هر تابعی که ETH یا ERC20 انتقال می‌دهد ترتیب CEI را دنبال می‌کند؟

‏(Checks ← ‏Effects ← ‏Interactions)

۱۱

بخش تکمیلی

Step 8: Flash Loan Oracle Manipulation
ترجمه‌ی کامل ✓بخش ۱۱ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

grep -rn "getReserves\|getAmountsOut\|slot0\b" contracts/ -A5

Check: Any spot price reading from Uniswap reserves/slot0? → flash loan manipulatable.

۱۲

بخش تکمیلی

Step 9: Signature Replay
ترجمه‌ی کامل ✓بخش ۱۲ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

grep -rn "ecrecover\|ECDSA\.recover" contracts/ -B20
grep -rn "nonce\|_nonces\|nonces\[" contracts/

Check: Does signed hash include: nonce + chainId + contract address?

۱۳

بخش تکمیلی

Step 10: Proxy / Upgrade
ترجمه‌ی کامل ✓بخش ۱۳ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

grep -rn "function initialize\b\|_disableInitializers\|initializer" contracts/
grep -rn "delegatecall\b" contracts/ -B3
grep -rn "0x360894\|_IMPLEMENTATION_SLOT\|EIP1967" contracts/

Check:

  • _disableInitializers() in implementation constructor?
  • Implementation can't be initialized directly by attacker?
  • Storage layout compatible between versions?
۱۴

بخش تکمیلی

Confirming a Finding
ترجمه‌ی کامل ✓بخش ۱۴ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

Apply the 7-Question Gate:

1. Can I demonstrate this with a Foundry test?
2. What is the financial impact (quantify in $)?
3. Is this in the Immunefi scope?
4. Is it a known issue or acknowledged behavior?
5. Does my Foundry PoC actually run? (forge test -vvvv)
۱۵

بخش تکمیلی

Foundry PoC Template
ترجمه‌ی کامل ✓بخش ۱۵ از ۱۵

بخش تکمیلی سند که در دسته‌بندی استاندارد نگنجیده است

‏این بخش صرفاً کد / دستور است — بدون تغییر حفظ شده:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../src/VulnerableContract.sol";

contract ExploitTest is Test {
    VulnerableContract target;
    address attacker = makeAddr("attacker");

    function setUp() public {
        vm.createSelectFork("mainnet", BLOCK_NUMBER);
        target = VulnerableContract(TARGET_ADDRESS);
        deal(address(token), attacker, INITIAL_BALANCE);
    }

    function test_exploit() public {
        uint256 before = token.balanceOf(attacker);
        vm.startPrank(attacker);
        // Execute exploit
        vm.stopPrank();
        assertGt(token.balanceOf(attacker), before, "Exploit failed");
    }
}

Run: forge test --match-test test_exploit -vvvv