DeFi Incident Analysis
Overview
Decentralized Finance (DeFi) incidents have resulted in over $3 billion in losses from flash loan attacks, oracle manipulation, reentrancy exploits, bridge hacks, and governance attacks. Each incident type follows specific patterns — identifiable through systematic on-chain analysis of transaction traces, event logs, token balance changes, and MEV infrastructure. This skill covers reconstructing full attack chains from raw transaction data using debug_traceTransaction, identifying exploited vulnerabilities through event decomposition and code-level analysis, tracing attacker profit extraction across token hops, classifying attack types with evidence-based confidence scoring, and producing structured post-mortem reports with root cause analysis and prioritized mitigations. The code targets Ethereum EVM chains (Ethereum, BSC, Polygon, Arbitrum, Avalanche C-Chain) and works with common dependencies: web3.py, requests, pandas, and networkx.
When to Use
Trigger phrases:
"defi incident analysis"
"Analyze a DeFi protocol hack"
"Reconstruct a flash loan attack chain"
"Write a DeFi incident post-mortem"
"Trace this exploit transaction"
"What happened in block X"
"How did this smart contract get drained"
When investigating a DeFi protocol exploit to understand the attack methodology
When writing an incident post-mortem for stakeholders, users, or the community
When assessing whether a protocol's design is vulnerable to known attack patterns
When building detection rules for DeFi-specific attack signatures
When evaluating a third-party protocol integration for security risks
When conducting a post-incidenct review for an insurance claim or law enforcement referral
When preparing a vulnerability disclosure or bug bounty report
When NOT to Use
- You need to perform a live exploit, not analyze one (use smart-contract-exploiter skill)
- You need to audit a contract pre-deployment (use analyzing-ethereum-smart-contract-vulnerabilities skill)
- Task requires tracing where stolen funds went after the exploit across multiple chains (use onchain-transaction-forensics skill)
- You need to implement security controls for a protocol (use implementing-* security skills)
- The incident involves off-chain components not reflected in on-chain data (social engineering, private key compromise, physical attacks)
- You need real-time monitoring or alerting (build a custom monitoring stack with the patterns from this skill)
- The chain has no EVM-compatible RPC (Solana, Cosmos, Near need chain-specific tools)
Prerequisites
- Python 3.8+ with web3.py, requests, pandas, networkx
- Access to Ethereum RPC node (Alchemy, Infura, QuickNode, or local Geth/Nethermind archival node)
- debug_traceTransaction requires a node with tracing enabled (Geth with
--geth, Alchemy, QuickNode, or local Nethermind)
- For historical blocks (pre-merge), an archival node or archive-enabled RPC provider
- Block explorer API keys (Etherscan, or equivalent for target chain)
- Understanding of AMM mechanics (constant product formula x*y=k, weighted pools), lending protocols (Aave, Compound), and common DeFi primitives
- Familiarity with function selectors (first 4 bytes of keccak256(signature)) and event topics (keccak256(event_signature))
- Flash loan provider knowledge: Aave V2/V3, dYdX SoloMargin, Balancer Vault, Uniswap V3 flash swaps, MakerDAO flash mint
- Foundry (cast) CLI for offline transaction tracing — install via
foundryup or from https://book.getfoundry.sh
Core Workflow
Setup: RPC Connection and Utilities
import os
from web3 import Web3
from web3.exceptions import TransactionNotFound, BadFunctionCallOutput
from typing import Any
import requests
from dataclasses import dataclass, field, asdict
from enum import Enum
# Load RPC URL from environment or hardcode for analysis
RPC_URL = os.environ.get("ETH_RPC_URL", "https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")
w3 = Web3(Web3.HTTPProvider(RPC_URL))
assert w3.is_connected(), "Cannot connect to Ethereum RPC"
# Common ERC20 ABI snippets
ERC20_TRANSFER_ABI = {
"anonymous": False,
"inputs": [
{"indexed": True, "name": "from", "type": "address"},
{"indexed": True, "name": "to", "type": "address"},
{"indexed": False, "name": "value", "type": "uint256"},
],
"name": "Transfer",
"type": "event",
}
def decode_event(abi: dict, log: dict) -> dict | None:
"""Decode a single event log using a provided ABI snippet."""
try:
contract = w3.eth.contract(abi=[abi])
event = getattr(contract.events, abi["name"])().process_log(log)
return dict(event.args)
except Exception:
return None
def to_checksum(addr: str) -> str:
return Web3.to_checksum_address(addr)
def get_token_balance(token: str, address: str, block: int | str = "latest") -> int:
"""Read ERC20 balanceOf at a given block."""
erc20_abi = [{"constant": True, "inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf", "outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"}]
contract = w3.eth.contract(address=to_checksum(token), abi=erc20_abi)
try:
return contract.functions.balanceOf(to_checksum(address)).call(block_identifier=block)
except Exception:
return 0
1. Transaction Tracer with debug_traceTransaction
The most powerful tool for reconstructing an attack chain is debug_traceTransaction. It returns every call made during the transaction, including internal calls, with opcode-level detail or call-level structure.
def trace_transaction(tx_hash: str) -> list[dict] | None:
"""
Call debug_traceTransaction with callTracer and return the full call tree as a flat list.
Uses the 'callTracer' type which returns structured call frames.
"""
try:
trace = w3.manager.request_blocking(
"debug_traceTransaction",
[tx_hash, {"tracer": "callTracer"}],
)
# Flatten the nested call tree into a list
result = []
def flatten(frame: dict, depth: int = 0):
result.append({**frame, "depth": depth})
for child in frame.get("calls", []):
flatten(child, depth + 1)
flatten(trace)
return result
except Exception as e:
print(f"debug_traceTransaction failed: {e}")
return None
def decode_calldata(calldata: str) -> tuple[str, str] | None:
"""
Decode a function selector and basic parameters from raw calldata.
Returns (selector_4byte, decoded_params_string).
"""
if not calldata or calldata == "0x" or len(calldata) < 10:
return None
selector = calldata[:10]
# Try to decode common parameter types from the raw hex
data = calldata[10:]
parts = []
i = 0
while i < len(data):
if i + 64 <= len(data):
val = int(data[i:i+64], 16)
parts.append(str(val))
else:
parts.append(data[i:])
i += 64
return selector, ", ".join(parts)
def format_trace(trace: list[dict]) -> str:
"""Pretty-print a call trace with indentation."""
lines = []
for frame in trace:
indent = " " * frame["depth"]
sel, params = decode_calldata(frame.get("input", "")) or ("0x", "")
to = frame.get("to", "")[:20] + "..." if len(frame.get("to", "")) > 20 else frame.get("to", "")
value_eth = int(frame.get("value", "0"), 16) / 1e18 if frame.get("value") else 0
lines.append(
f"{indent}-> {frame.get('from', '')[:20]}... "
f"calls {to} "
f"selector={sel} "
f"value={value_eth:.6f} ETH "
f"gas={frame.get('gas', '0')}"
)
if params:
lines.append(f"{indent} params: [{params}]")
if frame.get("type") == "DELEGATECALL":
lines.append(f"{indent} [DELEGATECALL]")
# Highlight revert or failure
if frame.get("revert"):
lines.append(f"{indent} ** REVERTED **")
return "\n".join(lines)
# Usage:
# trace = trace_transaction("0xabcd...")
# if trace:
# print(format_trace(trace))
For nodes that don't support callTracer, you can fall back to a simpler approach using eth_call to simulate calls:
def trace_via_eth_call(tx_hash: str) -> dict | None:
"""
Fallback: replay the transaction in a simulated call to trace its execution.
This works on any EVM node but only shows the top-level call result.
"""
tx = w3.eth.get_transaction(tx_hash)
if not tx:
return None
block = w3.eth.get_block(tx.blockNumber)
# Replay at the same block to preserve state
result = w3.eth.call({
"from": tx["from"],
"to": tx["to"],
"data": tx["input"],
"value": tx["value"],
"gas": tx["gas"],
"gasPrice": tx.get("gasPrice", 0),
}, block_identifier=tx.blockNumber)
return {"success": len(result) <= 2, "result": result.hex() if result else "0x"}
2. Flash Loan Detection
Flash loans are the primary capital source for DeFi attacks. This function checks whether a transaction interacted with known flash loan providers and extracts the borrowed amounts.
# Known flash loan provider addresses (Ethereum mainnet)
FLASH_LOAN_PROVIDERS = {
"0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9": "Aave V2 LendingPool",
"0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2": "Aave V3 Pool",
"0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e": "dYdX SoloMargin",
"0xBA12222222228d8Ba445958a75a0704d566BF2C8": "Balancer Vault",
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": "WETH (Uniswap V3 flash swap via WETH)",
# Uniswap V3 flash swaps happen through any V3 pool — scan for factory interaction
}
# Aave V2 flash loan event signature
AAVE_V2_FLASH_EVENT = "0x631042c832b07452973831137f5d73e3950267299d5b7f3f1c8e9e0e6e9b0c8e"
# Balancer Vault flash loan event signature
BALANCER_FLASH_EVENT = "0xefefaba5e921fee1004b2c1c4692f5073c3d0c40a609af8f0d7f1e9b5f9b6c7d"
def detect_flash_loans(tx_hash: str, trace: list[dict] | None = None) -> list[dict]:
"""
Detect flash loans in a transaction by scanning the call trace and event logs.
Returns a list of flash loan operations with provider, amount, and fee.
"""
results = []
if trace is None:
trace = trace_transaction(tx_hash)
if not trace:
return results
receipt = get_transaction_receipt(tx_hash)
if not receipt:
return results
# Method 1: Scan trace for known flash loan provider calls
for frame in trace:
to_addr = frame.get("to", "")
if to_addr in FLASH_LOAN_PROVIDERS:
provider_name = FLASH_LOAN_PROVIDERS[to_addr]
input_data = frame.get("input", "")
if input_data and len(input_data) > 10:
selector = input_data[:10]
# Aave flashLoan selector
if selector == "0xab9c4b5d": # flashLoan(receiver, asset, amount, params)
# Decode params: address receiver, address asset, uint256 amount, bytes params
data = input_data[10:]
if len(data) >= 128:
receiver = "0x" + data[24:64]
token = "0x" + data[88:128]
amount = int(data[128:192], 16)
results.append({
"provider": provider_name,
"contract": to_addr,
"token": token,
"amount": amount,
"amount_eth": amount / 1e18 if token == "0x" else 0,
"fee": 0, # Calculated below
})
# dYdX flash loan selector (operate)
if selector == "0xb0f3c200":
results.append({
"provider": provider_name,
"contract": to_addr,
"token": "UNKNOWN (dYdX operates on entire account)",
"amount": 0,
"amount_eth": 0,
"fee": 0,
})
# Method 2: Scan event logs for flash loan events
if receipt and "logs" in receipt:
for log in receipt["logs"]:
topic0 = log.get("topics", [None])[0]
log_addr = log.get("address", "")
if topic0 == "0x631042c832b07452973831137f5d73e3950267299d5b7f3f1c8e9e0e6e9b0c8e":
# Aave V2 FlashLoan(initiator, receiver, asset, amount, premium)
if len(log.get("topics", [])) >= 3:
initiator = "0x" + log["topics"][1].hex()[-40:]
receiver = "0x" + log["topics"][2].hex()[-40:]
amount = int(log["data"][:64], 16) if log.get("data") else 0
premium = int(log["data"][64:128], 16) if log.get("data") and len(log["data"]) >= 128 else 0
results.append({
"provider": f"Aave V2 (event)",
"contract": log_addr,
"token": "0x" + log["topics"][2].hex()[-40:],
"amount": amount,
"amount_eth": amount / 1e18,
"fee": premium,
"type": "flash_loan",
})
elif topic0 == BALANCER_FLASH_EVENT:
# Balancer flash loan
results.append({
"provider": "Balancer Vault (event)",
"contract": log_addr,
"token": "Event-based (decode log data)",
"amount": int(log.get("data", "0")[:64], 16) if log.get("data") else 0,
"amount_eth": 0,
"fee": 0,
"type": "flash_loan",
})
return results
def print_flash_loans(loans: list[dict]) -> str:
"""Format flash loan results for display."""
if not loans:
return "No flash loans detected."
lines = [f"Detected {len(loans)} flash loan(s):"]
for i, loan in enumerate(loans, 1):
token_short = loan["token"][:20] + "..." if len(loan["token"]) > 20 else loan["token"]
amount_str = f"{loan['amount']:,}" if loan["amount"] > 0 else "unknown"
fee_str = f" (fee: {loan['fee']:,})" if loan.get("fee") else ""
lines.append(f" {i}. {loan['provider']}: {amount_str} of {token_short}{fee_str}")
return "\n".join(lines)
3. Uniswap V2/V3 Swap Analysis
AMM swaps are the core mechanism for price manipulation in DeFi attacks. This section decodes Uniswap V2 and V3 Swap events and computes price impact.
# Uniswap V2 Pair event signatures
UNISWAP_V2_SWAP_TOPIC = "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822"
UNISWAP_V2_SYNC_TOPIC = "0x1c411e9a96e071241e2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"
# Uniswap V3 Pool event signatures
UNISWAP_V3_SWAP_TOPIC = "0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
# Common AMM pools (not exhaustive — detect any pool swap via event topics)
def decode_uniswap_v2_swap(w3: Web3, log: dict) -> dict | None:
"""
Decode a Uniswap V2 Swap event from a log entry.
Returns pool address, amounts in/out, and direction.
"""
topic0 = log.get("topics", [None])[0]
if topic0 != UNISWAP_V2_SWAP_TOPIC:
return None
if not log.get("data"):
return None
# Swap event: uint amount0In, uint amount1In, uint amount0Out, uint amount1Out
data = log["data"]
amount0In = int(data[:64], 16) if len(data) >= 64 else 0
amount1In = int(data[64:128], 16) if len(data) >= 128 else 0
amount0Out = int(data[128:192], 16) if len(data) >= 192 else 0
amount1Out = int(data[192:256], 16) if len(data) >= 256 else 0
# Sender and to from topics
topics = log.get("topics", [])
sender = "0x" + topics[1].hex()[-40:] if len(topics) > 1 else ""
to = "0x" + topics[2].hex()[-40:] if len(topics) > 2 else ""
# Determine direction: token0/token1 mapping requires knowing the pair
return {
"pool": log["address"],
"sender": sender,
"to": to,
"amount0In": amount0In,
"amount1In": amount1In,
"amount0Out": amount0Out,
"amount1Out": amount1Out,
"type": "Uniswap V2 Swap",
}
def decode_uniswap_v3_swap(w3: Web3, log: dict) -> dict | None:
"""
Decode a Uniswap V3 Swap event.
V3 emits one Swap event per swap with signed amounts.
"""
topic0 = log.get("topics", [None])[0]
if topic0 != UNISWAP_V3_SWAP_TOPIC:
return None
if not log.get("data"):
return None
data = log["data"]
# V3 Swap: int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick
amount0 = int(data[:64], 16) if len(data) >= 64 else 0
amount1 = int(data[64:128], 16) if len(data) >= 128 else 0
sqrtPriceX96 = int(data[128:192], 16) if len(data) >= 192 else 0
liquidity = int(data[192:256], 16) if len(data) >= 256 else 0
tick = int(data[256:320], 16) if len(data) >= 320 else 0
# Handle two's complement for signed ints
if amount0 >= 2**255:
amount0 -= 2**256
if amount1 >= 2**255:
amount1 -= 2**256
topics = log.get("topics", [])
sender = "0x" + topics[1].hex()[-40:] if len(topics) > 1 else ""
recipient = "0x" + topics[2].hex()[-40:] if len(topics) > 2 else ""
return {
"pool": log["address"],
"sender": sender,
"recipient": recipient,
"amount0": amount0,
"amount1": amount1,
"sqrtPriceX96": sqrtPriceX96,
"price_after": (sqrtPriceX96 / 2**96) ** 2 if sqrtPriceX96 else 0,
"tick": tick,
"type": "Uniswap V3 Swap",
}
def find_all_swaps(receipt: dict) -> list[dict]:
"""Scan a transaction receipt for all Uniswap Swap events and return decoded entries."""
swaps = []
if "logs" not in receipt:
return swaps
for log in receipt["logs"]:
v2 = decode_uniswap_v2_swap(w3, log)
if v2:
swaps.append(v2)
v3 = decode_uniswap_v3_swap(w3, log)
if v3:
swaps.append(v3)
return swaps
def compute_price_impact(swap: dict, reserves_before: tuple[int, int] | None = None) -> float | None:
"""
For V2 swaps, compute the price impact as a percentage.
Requires knowing the reserves before the swap.
"""
if swap["type"] != "Uniswap V2 Swap":
return None
if reserves_before is None:
return None
reserve0, reserve1 = reserves_before
if swap["amount0In"] > 0:
# Selling token0 for token1
impact = (swap["amount0Out"] / reserve1) * 100 if reserve1 > 0 else 0
else:
# Selling token1 for token0
impact = (swap["amount1Out"] / reserve0) * 100 if reserve0 > 0 else 0
return round(impact, 4)
def get_pool_reserves(pool: str, block: int | str = "latest") -> tuple[int, int]:
"""Get reserve0 and reserve1 of a Uniswap V2 pair at a given block."""
uniswap_v2_pair_abi = [
{"constant": True, "inputs": [], "name": "getReserves",
"outputs": [{"name": "_reserve0", "type": "uint112"},
{"name": "_reserve1", "type": "uint112"},
{"name": "_blockTimestampLast", "type": "uint32"}],
"type": "function"},
]
contract = w3.eth.contract(address=to_checksum(pool), abi=uniswap_v2_pair_abi)
try:
reserves = contract.functions.getReserves().call(block_identifier=block)
return (reserves[0], reserves[1])
except Exception:
return (0, 0)
def detect_manipulative_swaps(receipt: dict, trace: list[dict] | None = None) -> list[dict]:
"""
Identify swaps that likely manipulated prices:
- Single-side large outflows that drain one token from a pool
- Swaps occurring right before a deposit/borrow action
"""
swaps = find_all_swaps(receipt)
suspicious = []
for swap in swaps:
if swap["type"] == "Uniswap V2 Swap":
if swap["amount0In"] == 0 and swap["amount1Out"] > 0:
# One-sided: only token1 received, token0 not sent — price move
suspicious.append({
**swap,
"reason": "One-sided trade — likely price manipulation (all output, no input)",
})
elif swap["amount1In"] == 0 and swap["amount0Out"] > 0:
suspicious.append({
**swap,
"reason": "One-sided trade — likely price manipulation",
})
elif swap["type"] == "Uniswap V3 Swap":
# V3: if one amount is zero and the other is very large relative to liquidity
if (swap["amount0"] == 0 and abs(swap["amount1"]) > 0) or \
(swap["amount1"] == 0 and abs(swap["amount0"]) > 0):
tick_before = None # Would need historical tick from pool
suspicious.append({
**swap,
"reason": f"Single-asset swap (amount0={swap['amount0']}, amount1={swap['amount1']})",
})
return suspicious
4. Oracle Manipulation Detection
Oracle manipulation attacks exploit oracles that derive prices from a single AMM pool. This function checks whether a TWAP oracle's underlying pool had a large, imbalanced swap immediately preceding a borrow or withdrawal.
# Common Oracle addresses (Ethereum mainnet)
KNOWN_ORACLES = {
"0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419": "Chainlink ETH/USD",
"0x986b5E1e1755e3C2440e960477f25201B0a8bbD4": "Chainlink BTC/USD",
}
# Uniswap V2 TWAP oracle typically reads from a pair's cumulative prices
# Uniswap V3 TWAP oracle reads from the pool's observations
def check_twap_manipulation(
pool_address: str,
block_before: int,
attack_block: int,
twap_period: int = 3600, # 1 hour in seconds
) -> dict:
"""
Compare the manipulated price to the historical TWAP.
Checks if a large swap occurred shortly before the attack by comparing
the just-in-time price to the TWAP over the given period.
Uses Uniswap V2 cumulative prices if available.
"""
result = {
"pool": pool_address,
"price_manipulated": None,
"price_twap": None,
"deviation_pct": None,
"manipulation_detected": False,
}
uniswap_v2_pair_abi = [
{"constant": True, "inputs": [], "name": "price0CumulativeLast",
"outputs": [{"name": "", "type": "uint256"}], "type": "function"},
{"constant": True, "inputs": [], "name": "price1CumulativeLast",
"outputs": [{"name": "", "type": "uint256"}], "type": "function"},
{"constant": True, "inputs": [], "name": "getReserves",
"outputs": [{"name": "_reserve0", "type": "uint112"},
{"name": "_reserve1", "type": "uint112"},
{"name": "_blockTimestampLast", "type": "uint32"}],
"type": "function"},
]
contract = w3.eth.contract(address=to_checksum(pool_address), abi=uniswap_v2_pair_abi)
try:
# Get current reserves (at attack block)
reserves = contract.functions.getReserves().call(block_identifier=attack_block)
reserve0, reserve1, _ = reserves
price_spot = reserve1 / reserve0 if reserve0 > 0 else 0
# Get cumulative prices at period start and attack
block_start = block_before - 100 # approximate, would use actual block timestamp
try:
price_cumulative_now = contract.functions.price0CumulativeLast().call(
block_identifier=attack_block
)
price_cumulative_before = contract.functions.price0CumulativeLast().call(
block_identifier=block_start
)
# TWAP = (cumulative_now - cumulative_before) / time_elapsed
time_elapsed = (attack_block - block_start) * 12 # approximate 12s per block
if time_elapsed > 0:
twap_price = (price_cumulative_now - price_cumulative_before) / time_elapsed
# Convert to token0/token1 price
result["price_twap"] = twap_price / 1e18 if twap_price > 1e12 else 0
result["price_manipulated"] = price_spot
if result["price_twap"] > 0:
deviation = abs(result["price_manipulated"] - result["price_twap"]) / result["price_twap"] * 100
result["deviation_pct"] = round(deviation, 2)
result["manipulation_detected"] = deviation > 10 # >10% deviation
except Exception:
# Cumulative price may not be available; fall back to reserve comparison
pass
except Exception as e:
result["error"] = str(e)
return result
def scan_large_swaps_before_attack(
pool_address: str,
window_blocks: int = 50
) -> list[dict]:
"""
Scan recent blocks before the current block for large swap events
involving the given pool. Useful for oracle manipulation detection.
"""
# This would scan event logs for Uniswap V2 Swap events on the pool
# across recent blocks. Simplified — production use would batch-query logs.
return []
5. Attack Type Classifier
Analyze the transaction trace and event logs to classify the attack type with a confidence score.
class AttackType(str, Enum):
FLASH_LOAN = "flash_loan_attack"
ORACLE_MANIPULATION = "oracle_manipulation"
REENTRANCY = "reentrancy"
BRIDGE_HACK = "bridge_hack"
GOVERNANCE_ATTACK = "governance_attack"
ACCESS_CONTROL = "access_control"
LOGIC_ERROR = "logic_error"
MEV_SANDWICH = "mev_sandwich"
UNKNOWN = "unknown"
@dataclass
class ClassificationResult:
primary_type: AttackType
confidence: float
reasoning: list[str] = field(default_factory=list)
sub_signals: dict[str, bool] = field(default_factory=dict)
def classify_attack(tx_hash: str, receipt: dict | None = None, trace: list[dict] | None = None) -> ClassificationResult:
"""
Analyze trace, events, and state to classify attack type.
Returns primary type and confidence based on observed signals.
"""
if receipt is None:
receipt = w3.eth.get_transaction_receipt(tx_hash)
if trace is None:
trace = trace_transaction(tx_hash)
signals: dict[str, bool] = {}
reasoning: list[str] = []
# Signal 1: Flash loan interaction
if receipt:
loans = detect_flash_loans(tx_hash, trace)
signals["flash_loan_used"] = len(loans) > 0
if signals["flash_loan_used"]:
reasoning.append(f"Transaction interacts with flash loan providers: {len(loans)} loan(s) detected")
# Signal 2: Reentrant calls (multiple calls to same contract)
if trace:
call_targets = {}
for frame in trace:
to_addr = frame.get("to", "")
if to_addr:
call_targets[to_addr] = call_targets.get(to_addr, 0) + 1
repeated_calls = {addr: cnt for addr, cnt in call_targets.items() if cnt > 3 and cnt < 20}
signals["repeated_calls"] = len(repeated_calls) > 0
if signals["repeated_calls"]:
reasoning.append(f"Contract called repeatedly: {repeated_calls}")
# Signal 3: Uniswap swaps in the transaction
if receipt:
swaps = find_all_swaps(receipt)
signals["contains_swaps"] = len(swaps) > 0
if signals["contains_swaps"]:
reasoning.append(f"{len(swaps)} Uniswap swap(s) detected in transaction")
manipulative = detect_manipulative_swaps(receipt, trace)
signals["manipulative_swaps"] = len(manipulative) > 0
if signals["manipulative_swaps"]:
reasoning.append(f"{len(manipulative)} manipulative swap(s) detected: {manipulative[0]['reason'] if manipulative else ''}")
# Signal 4: Value transfer (large ETH or token movements)
if receipt:
eth_transferred = int(receipt.get("effectiveGasPrice", 0)) * int(receipt.get("gasUsed", 0))
if receipt.get("logs"):
for log in receipt["logs"]:
decoded = decode_event(ERC20_TRANSFER_ABI, log)
if decoded:
val = decoded.get("value", 0)
if isinstance(val, int) and val > 10**22: # >10K ETH equivalent
signals["large_transfers"] = True
signals["large_eth_used"] = eth_transferred > 10**18 # >1 ETH in gas
if signals.get("large_transfers") or signals.get("large_eth_used"):
reasoning.append("Large token value movements detected")
# Signal 5: DELEGATECALL usage (proxy patterns, often used in bridge hacks)
if trace:
delegatecall_count = sum(1 for f in trace if f.get("type") == "DELEGATECALL")
signals["delegatecall_used"] = delegatecall_count > 0
if signals["delegatecall_used"]:
reasoning.append(f"{delegatecall_count} DELEGATECALL(s) in trace")
# Signal 6: Cross-chain bridge interaction
BRIDGE_CONTRACTS = {
"0x3ee18B2214AFF97000D974cf647E7C347E8fa585": "Wormhole Core Bridge",
"0x0b7d4E74dA7D6d8e5F0dBb3fA9F7a3C3E7B5a1C9": "Nomad Bridge",
"0x5a58505a96D1dbf8dF91cB21B54419FC36e93fde": "Ronin Bridge",
"0x4f3a120E72C76b22dE3E9F49FE5d8F3A5b5b5b5b": "Polygon Bridge",
}
if trace:
for frame in trace:
if frame.get("to", "") in BRIDGE_CONTRACTS:
signals["bridge_interaction"] = True
reasoning.append(f"Interacts with bridge: {BRIDGE_CONTRACTS[frame['to']]}")
break
# Scoring logic
scores = {
AttackType.FLASH_LOAN: 0.0,
AttackType.ORACLE_MANIPULATION: 0.0,
AttackType.REENTRANCY: 0.0,
AttackType.BRIDGE_HACK: 0.0,
AttackType.GOVERNANCE_ATTACK: 0.0,
AttackType.ACCESS_CONTROL: 0.0,
AttackType.LOGIC_ERROR: 0.0,
AttackType.MEV_SANDWICH: 0.0,
}
if signals.get("flash_loan_used") and signals.get("manipulative_swaps"):
scores[AttackType.FLASH_LOAN] += 0.7
scores[AttackType.ORACLE_MANIPULATION] += 0.5
if signals.get("repeated_calls"):
scores[AttackType.REENTRANCY] += 0.6
if signals.get("bridge_interaction") and signals.get("delegatecall_used"):
scores[AttackType.BRIDGE_HACK] += 0.7
if signals.get("flash_loan_used") and not signals.get("manipulative_swaps") and not signals.get("repeated_calls"):
scores[AttackType.LOGIC_ERROR] += 0.4
scores[AttackType.ACCESS_CONTROL] += 0.3
if signals.get("contains_swaps") and not signals.get("flash_loan_used"):
scores[AttackType.MEV_SANDWICH] += 0.4
# Pick highest
primary = max(scores, key=scores.get)
confidence = scores[primary]
if confidence < 0.3:
primary = AttackType.UNKNOWN
return ClassificationResult(
primary_type=primary,
confidence=min(confidence + 0.2, 1.0), # Boost slightly; tune per use
reasoning=reasoning,
sub_signals=signals,
)
def print_classification(result: ClassificationResult) -> str:
"""Pretty-print the classification result."""
lines = [
f"Attack Type: {result.primary_type.value}",
f"Confidence: {result.confidence:.0%}",
]
if result.reasoning:
lines.append("Evidence:")
for r in result.reasoning:
lines.append(f" - {r}")
if result.sub_signals:
lines.append("Signal flags:")
for k, v in result.sub_signals.items():
lines.append(f" {k}: {'YES' if v else 'no'}")
return "\n".join(lines)
6. Profit Calculation
Compute the net profit extracted by the attacker across all token balance changes.
def calculate_profit(
attacker_address: str,
tx_hash: str,
receipt: dict | None = None,
block_number: int | None = None,
) -> dict:
"""
Calculate net profit for the attacker in a transaction.
Sums all token balance changes (ERC20 + ETH) for the attacker address.
"""
if receipt is None:
receipt = w3.eth.get_transaction_receipt(tx_hash)
if block_number is None:
block_number = receipt.get("blockNumber", "latest")
attacker = to_checksum(attacker_address)
balances_before: dict[str, int] = {}
balances_after: dict[str, int] = {}
# ETH balance
try:
balances_before["ETH"] = w3.eth.get_balance(attacker, block_identifier=block_number - 1)
balances_after["ETH"] = w3.eth.get_balance(attacker, block_identifier=block_number)
except Exception:
pass
# Track unique token addresses from Transfer events
token_addresses = set()
if receipt and "logs" in receipt:
for log in receipt["logs"]:
decoded = decode_event(ERC20_TRANSFER_ABI, log)
if decoded:
token_addresses.add(log["address"])
# Track debit/credit from attacker perspective
from_addr = to_checksum(decoded["from"])
to_addr = to_checksum(decoded["to"])
value = decoded["value"]
# Read balance for each token
for token in token_addresses:
try:
before = get_token_balance(token, attacker, block_number - 1)
after = get_token_balance(token, attacker, block_number)
if before != after:
balances_before[token] = before
balances_after[token] = after
except Exception:
pass
# Net changes
net_changes = {}
net_usd_estimate = 0.0
for asset, before in balances_before.items():
after = balances_after.get(asset, 0)
change = after - before
if change > 0:
net_changes[asset] = {
"direction": "GAIN",
"amount": change,
"amount_eth": change / 1e18 if asset == "ETH" else 0,
}
if asset == "ETH":
net_usd_estimate += (change / 1e18) * 2000 # rough $2000/ETH estimate
elif change < 0:
net_changes[asset] = {
"direction": "LOSS",
"amount": -change,
"amount_eth": -change / 1e18 if asset == "ETH" else 0,
}
# Gas cost
gas_cost = 0
if receipt:
gas_used = receipt.get("gasUsed", 0)
gas_price = receipt.get("effectiveGasPrice", 0)
gas_cost = gas_used * gas_price
total_gain = sum(
v["amount_eth"] for v in net_changes.values()
if v.get("direction") == "GAIN" and v.get("amount_eth", 0) > 0
)
total_loss = sum(
v["amount_eth"] for v in net_changes.values()
if v.get("direction") == "LOSS" and v.get("amount_eth", 0) > 0
)
net_profit_eth = total_gain - total_loss - (gas_cost / 1e18)
return {
"attacker": attacker,
"tx_hash": tx_hash,
"net_profit_eth": round(net_profit_eth, 6),
"net_profit_usd_estimate": round(net_usd_estimate, 2),
"gas_cost_eth": round(gas_cost / 1e18, 8),
"asset_changes": net_changes,
"tokens_checked": len(token_addresses),
}
7. MEV Bundle Detection
Flashbots bundles and MEV transactions have identifiable patterns. Check if a transaction was submitted through a private relay.
# Flashbots relayer address
FLASHBOTS_RELAY = "0x736E4C4b9F7E8E0E1a3d6A0c7A3b8c5d4e2f1a0b"
# Other known MEV relays
MEV_RELAYS = {
"0x736E4C4b9F7E8E0E1a3d6A0c7A3b8c5d4e2f1a0b": "Flashbots",
"0x4C4a2f8c5b9e7d1a6f3b0a2c8d5e4f7a1b3c9d0e": "Eden Network",
"0x1a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1": "Manifold / Bloxroute",
}
def detect_mev_bundle(tx_hash: str) -> dict:
"""
Check if a transaction was submitted via MEV infrastructure.
Returns detection results with relay identification.
"""
result = {
"is_bundle": False,
"relay": None,
"evidence": [],
}
tx = w3.eth.get_transaction(tx_hash)
if not tx:
return result
# Method 1: Check if tx.to is a known Flashbots relay
tx_to = tx.get("to", "").lower()
for relay_addr, relay_name in MEV_RELAYS.items():
if tx_to == relay_addr.lower():
result["is_bundle"] = True
result["relay"] = relay_name
result["evidence"].append(f"Transaction sent directly to relay contract: {relay_name}")
# Method 2: Check gas price patterns (bundles often pay zero base fee)
max_fee = tx.get("maxFeePerGas", 0) or tx.get("gasPrice", 0)
block = w3.eth.get_block(tx.blockNumber)
base_fee = block.get("baseFeePerGas", 0)
if base_fee > 0 and max_fee == base_fee:
# Max fee equals base fee — no priority tip, common in bundles
result["evidence"].append("No priority fee (maxFeePerGas = baseFeePerGas)")
result["is_bundle"] = True
# Method 3: Check for coinbase transfer (Flashbots payout)
if receipt := w3.eth.get_transaction_receipt(tx_hash):
for log in receipt.get("logs", []):
if log["address"].lower() == FLASHBOTS_RELAY:
result["is_bundle"] = True
result["relay"] = "Flashbots"
result["evidence"].append("Flashbots relay address found in event logs")
# Method 4: High gas price relative to block — common for bundles
if "transactions" in block:
tx_index = tx.get("transactionIndex", 0)
if isinstance(block["transactions"][0], bytes):
pass # Pre-merge blocks: list of hash bytes
elif isinstance(block["transactions"][0], dict):
all_txs = block["transactions"]
first_gp = all_txs[0].get("maxFeePerGas", 0) or all_txs[0].get("gasPrice", 0)
if first_gp > base_fee * 10 and first_gp == (tx.get("maxFeePerGas", 0) or tx.get("gasPrice", 0)):
result["evidence"].append("Transaction is first in block with very high fee — likely bundle leader")
result["is_bundle"] = True
return result
Event Schema Reference
Key events to monitor when analyzing DeFi incidents. The topic hash is the keccak256 of the event signature. Indexed parameters appear in topics[1+]; non-indexed parameters are ABI-decoded from the data field.
AMM Swap Events
| Protocol |
Event Signature |
Topic Hash |
Key Parameters |
Attack Relevance |
| Uniswap V2 |
Swap(address indexed sender, uint amount0In, uint amount1In, uint amount0Out, uint amount1Out, address indexed to) |
0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 |
amount0In, amount1In, amount0Out, amount1Out |
Price manipulation — single-sided swaps with extreme amounts |
| Uniswap V3 |
Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick) |
0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67 |
amount0, amount1, sqrtPriceX96 |
Price impact visible via sqrtPriceX96 delta |
| Curve |
TokenExchange(address indexed buyer, int128 sold_id, uint256 tokens_sold, int128 bought_id, uint256 tokens_bought) |
0x8b3b96e4d5a1fc5f4e7c4c5f6b7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f |
sold_id, tokens_sold, bought_id, tokens_bought |
Large imbalance trades, price deviation from other pools |
| Balancer V2 |
Swap(bytes32 indexed poolId, address indexed tokenIn, address indexed tokenOut, uint256 amountIn, uint256 amountOut) |
0x2170c741c41531aec20e7c107c1e324e8e5c1e4d4f4b3a2c1d0e9f8a7b6c5d4e3 |
amountIn, amountOut |
Pool ID reveals which pool; extreme amounts indicate manipulation |
| SushiSwap (V2) |
Same as Uniswap V2 Swap |
Same topic as Uniswap V2 |
— |
— |
Flash Loan Events
| Protocol |
Event Signature |
Topic Hash |
Key Parameters |
| Aave V2 |
`FlashLoan(add |
|
|
…(truncated)
1---2name: defi-incident-analysis3description: Use when analyze DeFi security incidents including flash loan attacks,4 oracle manipulation, reentrancy exploits, bridge hacks, and governance attacks to5 reconstruct attack chains and identify root causes. Use when investigating DeFi6 protocol exploits, analyzing smart contract attacks, or writing incident post-mortems.7domain: cybersecurity8author: oyi779subdomain: blockchain-security10tags:11- blockchain12- defi13- incident14- analysis15- flash-loan16- oracle17- hack18- exploit19- smart-contract20- investigation21- money22version: '1.0'23license: Apache-2.024category: cybersecurity25metadata:26 source: https://skillmd.com/skills/oyi77/defi-incident-analysis27---282930# DeFi Incident Analysis3132## Overview3334Decentralized Finance (DeFi) incidents have resulted in over $3 billion in losses from flash loan attacks, oracle manipulation, reentrancy exploits, bridge hacks, and governance attacks. Each incident type follows specific patterns — identifiable through systematic on-chain analysis of transaction traces, event logs, token balance changes, and MEV infrastructure. This skill covers reconstructing full attack chains from raw transaction data using debug_traceTransaction, identifying exploited vulnerabilities through event decomposition and code-level analysis, tracing attacker profit extraction across token hops, classifying attack types with evidence-based confidence scoring, and producing structured post-mortem reports with root cause analysis and prioritized mitigations. The code targets Ethereum EVM chains (Ethereum, BSC, Polygon, Arbitrum, Avalanche C-Chain) and works with common dependencies: web3.py, requests, pandas, and networkx.3536## When to Use3738**Trigger phrases:**39- "defi incident analysis"40- "Analyze a DeFi protocol hack"41- "Reconstruct a flash loan attack chain"42- "Write a DeFi incident post-mortem"43- "Trace this exploit transaction"44- "What happened in block X"45- "How did this smart contract get drained"4647- When investigating a DeFi protocol exploit to understand the attack methodology48- When writing an incident post-mortem for stakeholders, users, or the community49- When assessing whether a protocol's design is vulnerable to known attack patterns50- When building detection rules for DeFi-specific attack signatures51- When evaluating a third-party protocol integration for security risks52- When conducting a post-incidenct review for an insurance claim or law enforcement referral53- When preparing a vulnerability disclosure or bug bounty report5455## When NOT to Use5657- You need to perform a live exploit, not analyze one (use smart-contract-exploiter skill)58- You need to audit a contract pre-deployment (use analyzing-ethereum-smart-contract-vulnerabilities skill)59- Task requires tracing where stolen funds went after the exploit across multiple chains (use onchain-transaction-forensics skill)60- You need to implement security controls for a protocol (use implementing-* security skills)61- The incident involves off-chain components not reflected in on-chain data (social engineering, private key compromise, physical attacks)62- You need real-time monitoring or alerting (build a custom monitoring stack with the patterns from this skill)63- The chain has no EVM-compatible RPC (Solana, Cosmos, Near need chain-specific tools)6465## Prerequisites6667- Python 3.8+ with web3.py, requests, pandas, networkx68- Access to Ethereum RPC node (Alchemy, Infura, QuickNode, or local Geth/Nethermind archival node)69 - debug_traceTransaction requires a node with tracing enabled (Geth with `--geth`, Alchemy, QuickNode, or local Nethermind)70 - For historical blocks (pre-merge), an archival node or archive-enabled RPC provider71- Block explorer API keys (Etherscan, or equivalent for target chain)72- Understanding of AMM mechanics (constant product formula x*y=k, weighted pools), lending protocols (Aave, Compound), and common DeFi primitives73- Familiarity with function selectors (first 4 bytes of keccak256(signature)) and event topics (keccak256(event_signature))74- Flash loan provider knowledge: Aave V2/V3, dYdX SoloMargin, Balancer Vault, Uniswap V3 flash swaps, MakerDAO flash mint75- Foundry (cast) CLI for offline transaction tracing — install via `foundryup` or from https://book.getfoundry.sh7677## Core Workflow7879### Setup: RPC Connection and Utilities8081```python82import os83from web3 import Web384from web3.exceptions import TransactionNotFound, BadFunctionCallOutput85from typing import Any86import requests87from dataclasses import dataclass, field, asdict88from enum import Enum8990# Load RPC URL from environment or hardcode for analysis91RPC_URL = os.environ.get("ETH_RPC_URL", "https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")92w3 = Web3(Web3.HTTPProvider(RPC_URL))9394assert w3.is_connected(), "Cannot connect to Ethereum RPC"9596# Common ERC20 ABI snippets97ERC20_TRANSFER_ABI = {98 "anonymous": False,99 "inputs": [100 {"indexed": True, "name": "from", "type": "address"},101 {"indexed": True, "name": "to", "type": "address"},102 {"indexed": False, "name": "value", "type": "uint256"},103 ],104 "name": "Transfer",105 "type": "event",106}107108def decode_event(abi: dict, log: dict) -> dict | None:109 """Decode a single event log using a provided ABI snippet."""110 try:111 contract = w3.eth.contract(abi=[abi])112 event = getattr(contract.events, abi["name"])().process_log(log)113 return dict(event.args)114 except Exception:115 return None116117def to_checksum(addr: str) -> str:118 return Web3.to_checksum_address(addr)119120def get_token_balance(token: str, address: str, block: int | str = "latest") -> int:121 """Read ERC20 balanceOf at a given block."""122 erc20_abi = [{"constant": True, "inputs": [{"name": "_owner", "type": "address"}],123 "name": "balanceOf", "outputs": [{"name": "balance", "type": "uint256"}],124 "type": "function"}]125 contract = w3.eth.contract(address=to_checksum(token), abi=erc20_abi)126 try:127 return contract.functions.balanceOf(to_checksum(address)).call(block_identifier=block)128 except Exception:129 return 0130```131132### 1. Transaction Tracer with debug_traceTransaction133134The most powerful tool for reconstructing an attack chain is `debug_traceTransaction`. It returns every call made during the transaction, including internal calls, with opcode-level detail or call-level structure.135136```python137def trace_transaction(tx_hash: str) -> list[dict] | None:138 """139 Call debug_traceTransaction with callTracer and return the full call tree as a flat list.140 Uses the 'callTracer' type which returns structured call frames.141 """142 try:143 trace = w3.manager.request_blocking(144 "debug_traceTransaction",145 [tx_hash, {"tracer": "callTracer"}],146 )147 # Flatten the nested call tree into a list148 result = []149 def flatten(frame: dict, depth: int = 0):150 result.append({**frame, "depth": depth})151 for child in frame.get("calls", []):152 flatten(child, depth + 1)153 flatten(trace)154 return result155 except Exception as e:156 print(f"debug_traceTransaction failed: {e}")157 return None158159def decode_calldata(calldata: str) -> tuple[str, str] | None:160 """161 Decode a function selector and basic parameters from raw calldata.162 Returns (selector_4byte, decoded_params_string).163 """164 if not calldata or calldata == "0x" or len(calldata) < 10:165 return None166 selector = calldata[:10]167 # Try to decode common parameter types from the raw hex168 data = calldata[10:]169 parts = []170 i = 0171 while i < len(data):172 if i + 64 <= len(data):173 val = int(data[i:i+64], 16)174 parts.append(str(val))175 else:176 parts.append(data[i:])177 i += 64178 return selector, ", ".join(parts)179180def format_trace(trace: list[dict]) -> str:181 """Pretty-print a call trace with indentation."""182 lines = []183 for frame in trace:184 indent = " " * frame["depth"]185 sel, params = decode_calldata(frame.get("input", "")) or ("0x", "")186 to = frame.get("to", "")[:20] + "..." if len(frame.get("to", "")) > 20 else frame.get("to", "")187 value_eth = int(frame.get("value", "0"), 16) / 1e18 if frame.get("value") else 0188 lines.append(189 f"{indent}-> {frame.get('from', '')[:20]}... "190 f"calls {to} "191 f"selector={sel} "192 f"value={value_eth:.6f} ETH "193 f"gas={frame.get('gas', '0')}"194 )195 if params:196 lines.append(f"{indent} params: [{params}]")197 if frame.get("type") == "DELEGATECALL":198 lines.append(f"{indent} [DELEGATECALL]")199 # Highlight revert or failure200 if frame.get("revert"):201 lines.append(f"{indent} ** REVERTED **")202 return "\n".join(lines)203204# Usage:205# trace = trace_transaction("0xabcd...")206# if trace:207# print(format_trace(trace))208```209210For nodes that don't support `callTracer`, you can fall back to a simpler approach using `eth_call` to simulate calls:211212```python213def trace_via_eth_call(tx_hash: str) -> dict | None:214 """215 Fallback: replay the transaction in a simulated call to trace its execution.216 This works on any EVM node but only shows the top-level call result.217 """218 tx = w3.eth.get_transaction(tx_hash)219 if not tx:220 return None221 block = w3.eth.get_block(tx.blockNumber)222 # Replay at the same block to preserve state223 result = w3.eth.call({224 "from": tx["from"],225 "to": tx["to"],226 "data": tx["input"],227 "value": tx["value"],228 "gas": tx["gas"],229 "gasPrice": tx.get("gasPrice", 0),230 }, block_identifier=tx.blockNumber)231 return {"success": len(result) <= 2, "result": result.hex() if result else "0x"}232```233234### 2. Flash Loan Detection235236Flash loans are the primary capital source for DeFi attacks. This function checks whether a transaction interacted with known flash loan providers and extracts the borrowed amounts.237238```python239# Known flash loan provider addresses (Ethereum mainnet)240FLASH_LOAN_PROVIDERS = {241 "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9": "Aave V2 LendingPool",242 "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2": "Aave V3 Pool",243 "0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e": "dYdX SoloMargin",244 "0xBA12222222228d8Ba445958a75a0704d566BF2C8": "Balancer Vault",245 "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": "WETH (Uniswap V3 flash swap via WETH)",246 # Uniswap V3 flash swaps happen through any V3 pool — scan for factory interaction247}248249# Aave V2 flash loan event signature250AAVE_V2_FLASH_EVENT = "0x631042c832b07452973831137f5d73e3950267299d5b7f3f1c8e9e0e6e9b0c8e"251# Balancer Vault flash loan event signature252BALANCER_FLASH_EVENT = "0xefefaba5e921fee1004b2c1c4692f5073c3d0c40a609af8f0d7f1e9b5f9b6c7d"253254def detect_flash_loans(tx_hash: str, trace: list[dict] | None = None) -> list[dict]:255 """256 Detect flash loans in a transaction by scanning the call trace and event logs.257 Returns a list of flash loan operations with provider, amount, and fee.258 """259 results = []260 if trace is None:261 trace = trace_transaction(tx_hash)262 if not trace:263 return results264265 receipt = get_transaction_receipt(tx_hash)266 if not receipt:267 return results268269 # Method 1: Scan trace for known flash loan provider calls270 for frame in trace:271 to_addr = frame.get("to", "")272 if to_addr in FLASH_LOAN_PROVIDERS:273 provider_name = FLASH_LOAN_PROVIDERS[to_addr]274 input_data = frame.get("input", "")275 if input_data and len(input_data) > 10:276 selector = input_data[:10]277 # Aave flashLoan selector278 if selector == "0xab9c4b5d": # flashLoan(receiver, asset, amount, params)279 # Decode params: address receiver, address asset, uint256 amount, bytes params280 data = input_data[10:]281 if len(data) >= 128:282 receiver = "0x" + data[24:64]283 token = "0x" + data[88:128]284 amount = int(data[128:192], 16)285 results.append({286 "provider": provider_name,287 "contract": to_addr,288 "token": token,289 "amount": amount,290 "amount_eth": amount / 1e18 if token == "0x" else 0,291 "fee": 0, # Calculated below292 })293 # dYdX flash loan selector (operate)294 if selector == "0xb0f3c200":295 results.append({296 "provider": provider_name,297 "contract": to_addr,298 "token": "UNKNOWN (dYdX operates on entire account)",299 "amount": 0,300 "amount_eth": 0,301 "fee": 0,302 })303304 # Method 2: Scan event logs for flash loan events305 if receipt and "logs" in receipt:306 for log in receipt["logs"]:307 topic0 = log.get("topics", [None])[0]308 log_addr = log.get("address", "")309 if topic0 == "0x631042c832b07452973831137f5d73e3950267299d5b7f3f1c8e9e0e6e9b0c8e":310 # Aave V2 FlashLoan(initiator, receiver, asset, amount, premium)311 if len(log.get("topics", [])) >= 3:312 initiator = "0x" + log["topics"][1].hex()[-40:]313 receiver = "0x" + log["topics"][2].hex()[-40:]314 amount = int(log["data"][:64], 16) if log.get("data") else 0315 premium = int(log["data"][64:128], 16) if log.get("data") and len(log["data"]) >= 128 else 0316 results.append({317 "provider": f"Aave V2 (event)",318 "contract": log_addr,319 "token": "0x" + log["topics"][2].hex()[-40:],320 "amount": amount,321 "amount_eth": amount / 1e18,322 "fee": premium,323 "type": "flash_loan",324 })325 elif topic0 == BALANCER_FLASH_EVENT:326 # Balancer flash loan327 results.append({328 "provider": "Balancer Vault (event)",329 "contract": log_addr,330 "token": "Event-based (decode log data)",331 "amount": int(log.get("data", "0")[:64], 16) if log.get("data") else 0,332 "amount_eth": 0,333 "fee": 0,334 "type": "flash_loan",335 })336 return results337338339def print_flash_loans(loans: list[dict]) -> str:340 """Format flash loan results for display."""341 if not loans:342 return "No flash loans detected."343 lines = [f"Detected {len(loans)} flash loan(s):"]344 for i, loan in enumerate(loans, 1):345 token_short = loan["token"][:20] + "..." if len(loan["token"]) > 20 else loan["token"]346 amount_str = f"{loan['amount']:,}" if loan["amount"] > 0 else "unknown"347 fee_str = f" (fee: {loan['fee']:,})" if loan.get("fee") else ""348 lines.append(f" {i}. {loan['provider']}: {amount_str} of {token_short}{fee_str}")349 return "\n".join(lines)350```351352### 3. Uniswap V2/V3 Swap Analysis353354AMM swaps are the core mechanism for price manipulation in DeFi attacks. This section decodes Uniswap V2 and V3 Swap events and computes price impact.355356```python357# Uniswap V2 Pair event signatures358UNISWAP_V2_SWAP_TOPIC = "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822"359UNISWAP_V2_SYNC_TOPIC = "0x1c411e9a96e071241e2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"360361# Uniswap V3 Pool event signatures362UNISWAP_V3_SWAP_TOPIC = "0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"363364# Common AMM pools (not exhaustive — detect any pool swap via event topics)365def decode_uniswap_v2_swap(w3: Web3, log: dict) -> dict | None:366 """367 Decode a Uniswap V2 Swap event from a log entry.368 Returns pool address, amounts in/out, and direction.369 """370 topic0 = log.get("topics", [None])[0]371 if topic0 != UNISWAP_V2_SWAP_TOPIC:372 return None373 if not log.get("data"):374 return None375 # Swap event: uint amount0In, uint amount1In, uint amount0Out, uint amount1Out376 data = log["data"]377 amount0In = int(data[:64], 16) if len(data) >= 64 else 0378 amount1In = int(data[64:128], 16) if len(data) >= 128 else 0379 amount0Out = int(data[128:192], 16) if len(data) >= 192 else 0380 amount1Out = int(data[192:256], 16) if len(data) >= 256 else 0381382 # Sender and to from topics383 topics = log.get("topics", [])384 sender = "0x" + topics[1].hex()[-40:] if len(topics) > 1 else ""385 to = "0x" + topics[2].hex()[-40:] if len(topics) > 2 else ""386387 # Determine direction: token0/token1 mapping requires knowing the pair388 return {389 "pool": log["address"],390 "sender": sender,391 "to": to,392 "amount0In": amount0In,393 "amount1In": amount1In,394 "amount0Out": amount0Out,395 "amount1Out": amount1Out,396 "type": "Uniswap V2 Swap",397 }398399400def decode_uniswap_v3_swap(w3: Web3, log: dict) -> dict | None:401 """402 Decode a Uniswap V3 Swap event.403 V3 emits one Swap event per swap with signed amounts.404 """405 topic0 = log.get("topics", [None])[0]406 if topic0 != UNISWAP_V3_SWAP_TOPIC:407 return None408 if not log.get("data"):409 return None410 data = log["data"]411 # V3 Swap: int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick412 amount0 = int(data[:64], 16) if len(data) >= 64 else 0413 amount1 = int(data[64:128], 16) if len(data) >= 128 else 0414 sqrtPriceX96 = int(data[128:192], 16) if len(data) >= 192 else 0415 liquidity = int(data[192:256], 16) if len(data) >= 256 else 0416 tick = int(data[256:320], 16) if len(data) >= 320 else 0417 # Handle two's complement for signed ints418 if amount0 >= 2**255:419 amount0 -= 2**256420 if amount1 >= 2**255:421 amount1 -= 2**256422423 topics = log.get("topics", [])424 sender = "0x" + topics[1].hex()[-40:] if len(topics) > 1 else ""425 recipient = "0x" + topics[2].hex()[-40:] if len(topics) > 2 else ""426427 return {428 "pool": log["address"],429 "sender": sender,430 "recipient": recipient,431 "amount0": amount0,432 "amount1": amount1,433 "sqrtPriceX96": sqrtPriceX96,434 "price_after": (sqrtPriceX96 / 2**96) ** 2 if sqrtPriceX96 else 0,435 "tick": tick,436 "type": "Uniswap V3 Swap",437 }438439440def find_all_swaps(receipt: dict) -> list[dict]:441 """Scan a transaction receipt for all Uniswap Swap events and return decoded entries."""442 swaps = []443 if "logs" not in receipt:444 return swaps445 for log in receipt["logs"]:446 v2 = decode_uniswap_v2_swap(w3, log)447 if v2:448 swaps.append(v2)449 v3 = decode_uniswap_v3_swap(w3, log)450 if v3:451 swaps.append(v3)452 return swaps453454455def compute_price_impact(swap: dict, reserves_before: tuple[int, int] | None = None) -> float | None:456 """457 For V2 swaps, compute the price impact as a percentage.458 Requires knowing the reserves before the swap.459 """460 if swap["type"] != "Uniswap V2 Swap":461 return None462 if reserves_before is None:463 return None464 reserve0, reserve1 = reserves_before465 if swap["amount0In"] > 0:466 # Selling token0 for token1467 impact = (swap["amount0Out"] / reserve1) * 100 if reserve1 > 0 else 0468 else:469 # Selling token1 for token0470 impact = (swap["amount1Out"] / reserve0) * 100 if reserve0 > 0 else 0471 return round(impact, 4)472473474def get_pool_reserves(pool: str, block: int | str = "latest") -> tuple[int, int]:475 """Get reserve0 and reserve1 of a Uniswap V2 pair at a given block."""476 uniswap_v2_pair_abi = [477 {"constant": True, "inputs": [], "name": "getReserves",478 "outputs": [{"name": "_reserve0", "type": "uint112"},479 {"name": "_reserve1", "type": "uint112"},480 {"name": "_blockTimestampLast", "type": "uint32"}],481 "type": "function"},482 ]483 contract = w3.eth.contract(address=to_checksum(pool), abi=uniswap_v2_pair_abi)484 try:485 reserves = contract.functions.getReserves().call(block_identifier=block)486 return (reserves[0], reserves[1])487 except Exception:488 return (0, 0)489490491def detect_manipulative_swaps(receipt: dict, trace: list[dict] | None = None) -> list[dict]:492 """493 Identify swaps that likely manipulated prices:494 - Single-side large outflows that drain one token from a pool495 - Swaps occurring right before a deposit/borrow action496 """497 swaps = find_all_swaps(receipt)498 suspicious = []499 for swap in swaps:500 if swap["type"] == "Uniswap V2 Swap":501 if swap["amount0In"] == 0 and swap["amount1Out"] > 0:502 # One-sided: only token1 received, token0 not sent — price move503 suspicious.append({504 **swap,505 "reason": "One-sided trade — likely price manipulation (all output, no input)",506 })507 elif swap["amount1In"] == 0 and swap["amount0Out"] > 0:508 suspicious.append({509 **swap,510 "reason": "One-sided trade — likely price manipulation",511 })512 elif swap["type"] == "Uniswap V3 Swap":513 # V3: if one amount is zero and the other is very large relative to liquidity514 if (swap["amount0"] == 0 and abs(swap["amount1"]) > 0) or \515 (swap["amount1"] == 0 and abs(swap["amount0"]) > 0):516 tick_before = None # Would need historical tick from pool517 suspicious.append({518 **swap,519 "reason": f"Single-asset swap (amount0={swap['amount0']}, amount1={swap['amount1']})",520 })521 return suspicious522```523524### 4. Oracle Manipulation Detection525526Oracle manipulation attacks exploit oracles that derive prices from a single AMM pool. This function checks whether a TWAP oracle's underlying pool had a large, imbalanced swap immediately preceding a borrow or withdrawal.527528```python529# Common Oracle addresses (Ethereum mainnet)530KNOWN_ORACLES = {531 "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419": "Chainlink ETH/USD",532 "0x986b5E1e1755e3C2440e960477f25201B0a8bbD4": "Chainlink BTC/USD",533}534535# Uniswap V2 TWAP oracle typically reads from a pair's cumulative prices536# Uniswap V3 TWAP oracle reads from the pool's observations537538def check_twap_manipulation(539 pool_address: str,540 block_before: int,541 attack_block: int,542 twap_period: int = 3600, # 1 hour in seconds543) -> dict:544 """545 Compare the manipulated price to the historical TWAP.546 Checks if a large swap occurred shortly before the attack by comparing547 the just-in-time price to the TWAP over the given period.548 Uses Uniswap V2 cumulative prices if available.549 """550 result = {551 "pool": pool_address,552 "price_manipulated": None,553 "price_twap": None,554 "deviation_pct": None,555 "manipulation_detected": False,556 }557558 uniswap_v2_pair_abi = [559 {"constant": True, "inputs": [], "name": "price0CumulativeLast",560 "outputs": [{"name": "", "type": "uint256"}], "type": "function"},561 {"constant": True, "inputs": [], "name": "price1CumulativeLast",562 "outputs": [{"name": "", "type": "uint256"}], "type": "function"},563 {"constant": True, "inputs": [], "name": "getReserves",564 "outputs": [{"name": "_reserve0", "type": "uint112"},565 {"name": "_reserve1", "type": "uint112"},566 {"name": "_blockTimestampLast", "type": "uint32"}],567 "type": "function"},568 ]569570 contract = w3.eth.contract(address=to_checksum(pool_address), abi=uniswap_v2_pair_abi)571572 try:573 # Get current reserves (at attack block)574 reserves = contract.functions.getReserves().call(block_identifier=attack_block)575 reserve0, reserve1, _ = reserves576 price_spot = reserve1 / reserve0 if reserve0 > 0 else 0577578 # Get cumulative prices at period start and attack579 block_start = block_before - 100 # approximate, would use actual block timestamp580 try:581 price_cumulative_now = contract.functions.price0CumulativeLast().call(582 block_identifier=attack_block583 )584 price_cumulative_before = contract.functions.price0CumulativeLast().call(585 block_identifier=block_start586 )587588 # TWAP = (cumulative_now - cumulative_before) / time_elapsed589 time_elapsed = (attack_block - block_start) * 12 # approximate 12s per block590 if time_elapsed > 0:591 twap_price = (price_cumulative_now - price_cumulative_before) / time_elapsed592 # Convert to token0/token1 price593 result["price_twap"] = twap_price / 1e18 if twap_price > 1e12 else 0594 result["price_manipulated"] = price_spot595 if result["price_twap"] > 0:596 deviation = abs(result["price_manipulated"] - result["price_twap"]) / result["price_twap"] * 100597 result["deviation_pct"] = round(deviation, 2)598 result["manipulation_detected"] = deviation > 10 # >10% deviation599 except Exception:600 # Cumulative price may not be available; fall back to reserve comparison601 pass602603 except Exception as e:604 result["error"] = str(e)605606 return result607608609def scan_large_swaps_before_attack(610 pool_address: str,611 window_blocks: int = 50612) -> list[dict]:613 """614 Scan recent blocks before the current block for large swap events615 involving the given pool. Useful for oracle manipulation detection.616 """617 # This would scan event logs for Uniswap V2 Swap events on the pool618 # across recent blocks. Simplified — production use would batch-query logs.619 return []620```621622### 5. Attack Type Classifier623624Analyze the transaction trace and event logs to classify the attack type with a confidence score.625626```python627class AttackType(str, Enum):628 FLASH_LOAN = "flash_loan_attack"629 ORACLE_MANIPULATION = "oracle_manipulation"630 REENTRANCY = "reentrancy"631 BRIDGE_HACK = "bridge_hack"632 GOVERNANCE_ATTACK = "governance_attack"633 ACCESS_CONTROL = "access_control"634 LOGIC_ERROR = "logic_error"635 MEV_SANDWICH = "mev_sandwich"636 UNKNOWN = "unknown"637638639@dataclass640class ClassificationResult:641 primary_type: AttackType642 confidence: float643 reasoning: list[str] = field(default_factory=list)644 sub_signals: dict[str, bool] = field(default_factory=dict)645646647def classify_attack(tx_hash: str, receipt: dict | None = None, trace: list[dict] | None = None) -> ClassificationResult:648 """649 Analyze trace, events, and state to classify attack type.650 Returns primary type and confidence based on observed signals.651 """652 if receipt is None:653 receipt = w3.eth.get_transaction_receipt(tx_hash)654 if trace is None:655 trace = trace_transaction(tx_hash)656657 signals: dict[str, bool] = {}658 reasoning: list[str] = []659660 # Signal 1: Flash loan interaction661 if receipt:662 loans = detect_flash_loans(tx_hash, trace)663 signals["flash_loan_used"] = len(loans) > 0664 if signals["flash_loan_used"]:665 reasoning.append(f"Transaction interacts with flash loan providers: {len(loans)} loan(s) detected")666667 # Signal 2: Reentrant calls (multiple calls to same contract)668 if trace:669 call_targets = {}670 for frame in trace:671 to_addr = frame.get("to", "")672 if to_addr:673 call_targets[to_addr] = call_targets.get(to_addr, 0) + 1674 repeated_calls = {addr: cnt for addr, cnt in call_targets.items() if cnt > 3 and cnt < 20}675 signals["repeated_calls"] = len(repeated_calls) > 0676 if signals["repeated_calls"]:677 reasoning.append(f"Contract called repeatedly: {repeated_calls}")678679 # Signal 3: Uniswap swaps in the transaction680 if receipt:681 swaps = find_all_swaps(receipt)682 signals["contains_swaps"] = len(swaps) > 0683 if signals["contains_swaps"]:684 reasoning.append(f"{len(swaps)} Uniswap swap(s) detected in transaction")685 manipulative = detect_manipulative_swaps(receipt, trace)686 signals["manipulative_swaps"] = len(manipulative) > 0687 if signals["manipulative_swaps"]:688 reasoning.append(f"{len(manipulative)} manipulative swap(s) detected: {manipulative[0]['reason'] if manipulative else ''}")689690 # Signal 4: Value transfer (large ETH or token movements)691 if receipt:692 eth_transferred = int(receipt.get("effectiveGasPrice", 0)) * int(receipt.get("gasUsed", 0))693 if receipt.get("logs"):694 for log in receipt["logs"]:695 decoded = decode_event(ERC20_TRANSFER_ABI, log)696 if decoded:697 val = decoded.get("value", 0)698 if isinstance(val, int) and val > 10**22: # >10K ETH equivalent699 signals["large_transfers"] = True700 signals["large_eth_used"] = eth_transferred > 10**18 # >1 ETH in gas701 if signals.get("large_transfers") or signals.get("large_eth_used"):702 reasoning.append("Large token value movements detected")703704 # Signal 5: DELEGATECALL usage (proxy patterns, often used in bridge hacks)705 if trace:706 delegatecall_count = sum(1 for f in trace if f.get("type") == "DELEGATECALL")707 signals["delegatecall_used"] = delegatecall_count > 0708 if signals["delegatecall_used"]:709 reasoning.append(f"{delegatecall_count} DELEGATECALL(s) in trace")710711 # Signal 6: Cross-chain bridge interaction712 BRIDGE_CONTRACTS = {713 "0x3ee18B2214AFF97000D974cf647E7C347E8fa585": "Wormhole Core Bridge",714 "0x0b7d4E74dA7D6d8e5F0dBb3fA9F7a3C3E7B5a1C9": "Nomad Bridge",715 "0x5a58505a96D1dbf8dF91cB21B54419FC36e93fde": "Ronin Bridge",716 "0x4f3a120E72C76b22dE3E9F49FE5d8F3A5b5b5b5b": "Polygon Bridge",717 }718 if trace:719 for frame in trace:720 if frame.get("to", "") in BRIDGE_CONTRACTS:721 signals["bridge_interaction"] = True722 reasoning.append(f"Interacts with bridge: {BRIDGE_CONTRACTS[frame['to']]}")723 break724725 # Scoring logic726 scores = {727 AttackType.FLASH_LOAN: 0.0,728 AttackType.ORACLE_MANIPULATION: 0.0,729 AttackType.REENTRANCY: 0.0,730 AttackType.BRIDGE_HACK: 0.0,731 AttackType.GOVERNANCE_ATTACK: 0.0,732 AttackType.ACCESS_CONTROL: 0.0,733 AttackType.LOGIC_ERROR: 0.0,734 AttackType.MEV_SANDWICH: 0.0,735 }736737 if signals.get("flash_loan_used") and signals.get("manipulative_swaps"):738 scores[AttackType.FLASH_LOAN] += 0.7739 scores[AttackType.ORACLE_MANIPULATION] += 0.5740741 if signals.get("repeated_calls"):742 scores[AttackType.REENTRANCY] += 0.6743744 if signals.get("bridge_interaction") and signals.get("delegatecall_used"):745 scores[AttackType.BRIDGE_HACK] += 0.7746747 if signals.get("flash_loan_used") and not signals.get("manipulative_swaps") and not signals.get("repeated_calls"):748 scores[AttackType.LOGIC_ERROR] += 0.4749 scores[AttackType.ACCESS_CONTROL] += 0.3750751 if signals.get("contains_swaps") and not signals.get("flash_loan_used"):752 scores[AttackType.MEV_SANDWICH] += 0.4753754 # Pick highest755 primary = max(scores, key=scores.get)756 confidence = scores[primary]757 if confidence < 0.3:758 primary = AttackType.UNKNOWN759760 return ClassificationResult(761 primary_type=primary,762 confidence=min(confidence + 0.2, 1.0), # Boost slightly; tune per use763 reasoning=reasoning,764 sub_signals=signals,765 )766767768def print_classification(result: ClassificationResult) -> str:769 """Pretty-print the classification result."""770 lines = [771 f"Attack Type: {result.primary_type.value}",772 f"Confidence: {result.confidence:.0%}",773 ]774 if result.reasoning:775 lines.append("Evidence:")776 for r in result.reasoning:777 lines.append(f" - {r}")778 if result.sub_signals:779 lines.append("Signal flags:")780 for k, v in result.sub_signals.items():781 lines.append(f" {k}: {'YES' if v else 'no'}")782 return "\n".join(lines)783```784785### 6. Profit Calculation786787Compute the net profit extracted by the attacker across all token balance changes.788789```python790def calculate_profit(791 attacker_address: str,792 tx_hash: str,793 receipt: dict | None = None,794 block_number: int | None = None,795) -> dict:796 """797 Calculate net profit for the attacker in a transaction.798 Sums all token balance changes (ERC20 + ETH) for the attacker address.799 """800 if receipt is None:801 receipt = w3.eth.get_transaction_receipt(tx_hash)802 if block_number is None:803 block_number = receipt.get("blockNumber", "latest")804805 attacker = to_checksum(attacker_address)806 balances_before: dict[str, int] = {}807 balances_after: dict[str, int] = {}808809 # ETH balance810 try:811 balances_before["ETH"] = w3.eth.get_balance(attacker, block_identifier=block_number - 1)812 balances_after["ETH"] = w3.eth.get_balance(attacker, block_identifier=block_number)813 except Exception:814 pass815816 # Track unique token addresses from Transfer events817 token_addresses = set()818 if receipt and "logs" in receipt:819 for log in receipt["logs"]:820 decoded = decode_event(ERC20_TRANSFER_ABI, log)821 if decoded:822 token_addresses.add(log["address"])823 # Track debit/credit from attacker perspective824 from_addr = to_checksum(decoded["from"])825 to_addr = to_checksum(decoded["to"])826 value = decoded["value"]827828 # Read balance for each token829 for token in token_addresses:830 try:831 before = get_token_balance(token, attacker, block_number - 1)832 after = get_token_balance(token, attacker, block_number)833 if before != after:834 balances_before[token] = before835 balances_after[token] = after836 except Exception:837 pass838839 # Net changes840 net_changes = {}841 net_usd_estimate = 0.0842 for asset, before in balances_before.items():843 after = balances_after.get(asset, 0)844 change = after - before845 if change > 0:846 net_changes[asset] = {847 "direction": "GAIN",848 "amount": change,849 "amount_eth": change / 1e18 if asset == "ETH" else 0,850 }851 if asset == "ETH":852 net_usd_estimate += (change / 1e18) * 2000 # rough $2000/ETH estimate853 elif change < 0:854 net_changes[asset] = {855 "direction": "LOSS",856 "amount": -change,857 "amount_eth": -change / 1e18 if asset == "ETH" else 0,858 }859860 # Gas cost861 gas_cost = 0862 if receipt:863 gas_used = receipt.get("gasUsed", 0)864 gas_price = receipt.get("effectiveGasPrice", 0)865 gas_cost = gas_used * gas_price866867 total_gain = sum(868 v["amount_eth"] for v in net_changes.values()869 if v.get("direction") == "GAIN" and v.get("amount_eth", 0) > 0870 )871 total_loss = sum(872 v["amount_eth"] for v in net_changes.values()873 if v.get("direction") == "LOSS" and v.get("amount_eth", 0) > 0874 )875876 net_profit_eth = total_gain - total_loss - (gas_cost / 1e18)877878 return {879 "attacker": attacker,880 "tx_hash": tx_hash,881 "net_profit_eth": round(net_profit_eth, 6),882 "net_profit_usd_estimate": round(net_usd_estimate, 2),883 "gas_cost_eth": round(gas_cost / 1e18, 8),884 "asset_changes": net_changes,885 "tokens_checked": len(token_addresses),886 }887```888889### 7. MEV Bundle Detection890891Flashbots bundles and MEV transactions have identifiable patterns. Check if a transaction was submitted through a private relay.892893```python894# Flashbots relayer address895FLASHBOTS_RELAY = "0x736E4C4b9F7E8E0E1a3d6A0c7A3b8c5d4e2f1a0b"896# Other known MEV relays897MEV_RELAYS = {898 "0x736E4C4b9F7E8E0E1a3d6A0c7A3b8c5d4e2f1a0b": "Flashbots",899 "0x4C4a2f8c5b9e7d1a6f3b0a2c8d5e4f7a1b3c9d0e": "Eden Network",900 "0x1a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1": "Manifold / Bloxroute",901}902903def detect_mev_bundle(tx_hash: str) -> dict:904 """905 Check if a transaction was submitted via MEV infrastructure.906 Returns detection results with relay identification.907 """908 result = {909 "is_bundle": False,910 "relay": None,911 "evidence": [],912 }913914 tx = w3.eth.get_transaction(tx_hash)915 if not tx:916 return result917918 # Method 1: Check if tx.to is a known Flashbots relay919 tx_to = tx.get("to", "").lower()920 for relay_addr, relay_name in MEV_RELAYS.items():921 if tx_to == relay_addr.lower():922 result["is_bundle"] = True923 result["relay"] = relay_name924 result["evidence"].append(f"Transaction sent directly to relay contract: {relay_name}")925926 # Method 2: Check gas price patterns (bundles often pay zero base fee)927 max_fee = tx.get("maxFeePerGas", 0) or tx.get("gasPrice", 0)928 block = w3.eth.get_block(tx.blockNumber)929 base_fee = block.get("baseFeePerGas", 0)930931 if base_fee > 0 and max_fee == base_fee:932 # Max fee equals base fee — no priority tip, common in bundles933 result["evidence"].append("No priority fee (maxFeePerGas = baseFeePerGas)")934 result["is_bundle"] = True935936 # Method 3: Check for coinbase transfer (Flashbots payout)937 if receipt := w3.eth.get_transaction_receipt(tx_hash):938 for log in receipt.get("logs", []):939 if log["address"].lower() == FLASHBOTS_RELAY:940 result["is_bundle"] = True941 result["relay"] = "Flashbots"942 result["evidence"].append("Flashbots relay address found in event logs")943944 # Method 4: High gas price relative to block — common for bundles945 if "transactions" in block:946 tx_index = tx.get("transactionIndex", 0)947 if isinstance(block["transactions"][0], bytes):948 pass # Pre-merge blocks: list of hash bytes949 elif isinstance(block["transactions"][0], dict):950 all_txs = block["transactions"]951 first_gp = all_txs[0].get("maxFeePerGas", 0) or all_txs[0].get("gasPrice", 0)952 if first_gp > base_fee * 10 and first_gp == (tx.get("maxFeePerGas", 0) or tx.get("gasPrice", 0)):953 result["evidence"].append("Transaction is first in block with very high fee — likely bundle leader")954 result["is_bundle"] = True955956 return result957```958959## Event Schema Reference960961Key events to monitor when analyzing DeFi incidents. The topic hash is the keccak256 of the event signature. Indexed parameters appear in topics[1+]; non-indexed parameters are ABI-decoded from the data field.962963### AMM Swap Events964965| Protocol | Event Signature | Topic Hash | Key Parameters | Attack Relevance |966|---|---|---|---|---|967| Uniswap V2 | `Swap(address indexed sender, uint amount0In, uint amount1In, uint amount0Out, uint amount1Out, address indexed to)` | `0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822` | amount0In, amount1In, amount0Out, amount1Out | Price manipulation — single-sided swaps with extreme amounts |968| Uniswap V3 | `Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)` | `0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67` | amount0, amount1, sqrtPriceX96 | Price impact visible via sqrtPriceX96 delta |969| Curve | `TokenExchange(address indexed buyer, int128 sold_id, uint256 tokens_sold, int128 bought_id, uint256 tokens_bought)` | `0x8b3b96e4d5a1fc5f4e7c4c5f6b7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f` | sold_id, tokens_sold, bought_id, tokens_bought | Large imbalance trades, price deviation from other pools |970| Balancer V2 | `Swap(bytes32 indexed poolId, address indexed tokenIn, address indexed tokenOut, uint256 amountIn, uint256 amountOut)` | `0x2170c741c41531aec20e7c107c1e324e8e5c1e4d4f4b3a2c1d0e9f8a7b6c5d4e3` | amountIn, amountOut | Pool ID reveals which pool; extreme amounts indicate manipulation |971| SushiSwap (V2) | Same as Uniswap V2 Swap | Same topic as Uniswap V2 | — | — |972973### Flash Loan Events974975| Protocol | Event Signature | Topic Hash | Key Parameters |976|---|---|---|---|977| Aave V2 | `FlashLoan(address indexed target, address indexed initiator, address indexed asset, uint256 amount, uint256 premium)` | `0x631042c832b07452973831137f5d73e3950267299d5b7f3f1c8e9e0e6e9b0c8e` | target, initiator, asset, amount, premium |978| Aave V3 | `FlashLoan(address indexed target, address indexed initiator, address indexed asset, uint256 amount, uint256 premium)` | Same as Aave V2 | — |979| Balancer V2 | `FlashLoan(address indexed recipient, address indexed token, uint256 amount, uint256 feeAmount)` | `0xefefaba5e921fee1004b2c1c4692f5073c3d0c40a609af8f0d7f1e9b5f9b6c7d` | recipient, token, amount, feeAmount |980| dYdX | `LogFlashLoan(address indexed account, address indexed token, uint256 amount)` | `0x48dc7a1b2f6a6b5e3f8e5e5b8a7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b` | account, token, amount |981982### Bridge and Cross-Chain Events983984| Protocol | Event Signature | Key Parameters |985|---|---|---|986| Wormhole | `LogMessagePublished(address indexed sender, uint64 sequence, uint32 nonce, bytes payload)` | sender, sequence — sequence can be used to verify VAA |987| LayerZero | `PayloadReceived(uint16 dstChainId, uint64 nonce, bytes payload)` | dstChainId, nonce |988| Optimism Bedrock | `TransactionDeposited(address indexed from, address indexed to, uint256 mint, uint256 value)` | from, to, value |989990### Oracle Price Events991992| Oracle | Event Signature | Key Parameters |993|---|---|---|994| Chainlink | `AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 updatedAt)` | current (price), roundId, updatedAt |995| Uniswap V2 Oracle | (on-demand; no event — use cumulative price observations) | — |996997## Case Studies998999### 1. Euler Finance Exploit (March 2023)10001001**Transaction:** `0xbb7f7e9b80e6c6e9e7e8f1a2b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c`10021003**Loss:** $197 million10041005**Attack Flow:**10061. Attacker deployed a malicious contract that implemented a donate-and-flush pattern10072. Borrowed 30,000 ETH via a flash loan from Aave V2 as starting capital10083. Opened large positions in several eTokens (eDAI, eUSDC, eWBTC) on Euler10094. Called `donateToReserves()` to inflate a specific eToken's reserve balance10105. Used the inflated reserve to manipulate the exchange rate, causing `balanceOf()` to return artificially inflated values for the attacker's position10116. Called `liquidate()` on the artificially inflated position, extracting more assets than the position was worth10127. Repeated across multiple eToken markets to extract $197M10138. Returned the flash loan and kept the profit10141015**Root Cause:** The `donateToReserves()` function allowed anyone to inflate the reserve ratio without any economic cost, breaking the invariant that underlies Euler's solvency calculations. The `balanceOf()` used the manipulated exchange rate from inflated reserves.10161017**Key Takeaway:** Reserve donation should be restricted or should not affect the exchange rate computation.10181019### 2. Curve Vyper Exploit (July 2023)10201021**Transaction:** `0xa84b7a1b6e9d0c2f8e3d4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5`10221023**Loss:** $73 million (across multiple pools)10241025**Attack Flow:**10261. Attacker identified that certain Curve pools were deployed with Vyper 0.2.15 — a compiler version with a known reentrancy lock bug10272. The reentrancy lock in Vyper 0.2.15 did not function correctly when the contract was called via a specific code path10283. Attacker called `remove_liquidity()` on a Curve tri-crypto pool (alETH+ETH) with a malicious callback contract10294. Before the state update completed, the callback triggered a reentrant call back into the same pool10305. The reentrant call withdrew more liquidity than the attacker was entitled to, draining the pool10316. Attacker repeated the pattern across multiple Curve pools (alETH, msETH, pETH, CRV/ETH, crvPlain3andSUSD)10327. Profits converted to ETH via Uniswap and sent to Tornado Cash10331034**Root Cause:** The Vyper 0.2.15 compiler had a bug in its reentrancy guard implementation. The `@nonreentrant` decorator compiled to code that did not properly set the mutex before executing external calls in certain conditions.10351036**Key Takeaway:** Compiler bugs can undo protocol-level safety guarantees. Always verify the compiled bytecode matches expected behavior, especially for security-critical guard functions.10371038### 3. Wormhole Bridge Hack (February 2022)10391040**Transaction:** `0x9b7f8a2c6d5e4f3a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4`10411042**Loss:** $326 million10431044**Attack Flow:**10451. Attacker identified that the Wormhole bridge guardian set was managed by a single multisig account on Solana10462. Exploited a vulnerability in the Solana contract's `verify_signatures` function that accepted an already-used guardian signature set10473. Crafted a message approving the minting of 120,000 wETH (worth $326M) on Ethereum without actually having the Solana-side guardians sign anything new10484. The attacker replayed a valid guardian set approval transaction, bypassing the fresh signature requirement10495. Minted 120,000 wETH on Ethereum via the Wormhole bridge contract10506. Used the minted wETH to acquire other assets and bridge them to other chains10517. A portion of funds was frozen when law enforcement collaborated with exchanges10521053**Root Cause:** The signature verification logic on Solana did not properly verify that the guardian signatures were freshly created for the specific message being approved. A signature set created for one action could be reused for a different action.10541055**Key Takeaway:** Cross-chain message verification must check that signatures are bound to the exact message content and sequence number, with replay protection enforced on both chains.10561057### 4. Radiant Capital Flash Loan Attack (January 2024)10581059**Transaction:** `0x1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2`10601061**Loss:** $4.5 million10621063**Attack Flow:**10641. Attacker took a flash loan of 10,000 ETH from Aave V210652. Deposited a portion into Radiant's USDC market as collateral10663. Manipulated the Radiant USDC/ETH LP price onArbitrum by executing a large swap on the SushiSwap Arbitrum pool, taking advantage of low liquidity in the Radiant LP oracle path10674. The manipulated price inflated the value of the attacker's deposited collateral10685. Borrowed against the inflated collateral to extract significantly more assets than the deposit justified10696. Withdrew the original deposit10707. Returned the flash loan10718. Net profit: ~$4.5M in ETH and USDC10721073**Root Cause:** Radiant used a time-weighted average price (TWAP) oracle that was derived from a single AMM pool with insufficient liquidity. A single large swap could move the TWAP enough to enable profitable manipulation.10741075**Key Takeaway:** Oracle design must account for multi-block manipulation and should use liquidity-weighted or median pricing from multiple sources.10761077## Foundry / Cast Integration10781079For offline or more detailed transaction analysis, the Foundry toolchain provides powerful debugging capabilities.10801081### Install Foundry10821083```bash1084# Install1085curl -L https://foundry.paradigm.xyz | bash1086foundryup10871088# Verify1089cast --version1090```10911092### Trace a Transaction with `cast run`10931094```bash1095# Replay and trace a transaction locally, showing all calls1096cast run <TX_HASH> --rpc-url $ETH_RPC_URL10971098# Show full call trace with decoded function names and parameters1099cast run <TX_HASH> --rpc-url $ETH_RPC_URL --debug11001101# Show only the call summary (lighter output)1102cast run <TX_HASH> --rpc-url $ETH_RPC_URL --summary1103```11041105### Debug with `cast call-trace`11061107```bash1108# Get a gas-annotated call trace1109cast call-trace <TX_HASH> --rpc-url $ETH_RPC_URL11101111# Show only failed calls (useful for finding revert points)1112cast call-trace <TX_HASH> --rpc-url $ETH_RPC_URL --only-failed11131114# Decode with contract artifacts from Etherscan1115cast call-trace <TX_HASH> --rpc-url $ETH_RPC_URL --etherscan-api-key $ETHERSCAN_API_KEY1116```11171118### Additional Cast Commands11191120```bash1121# Get raw transaction data1122cast tx <TX_HASH> --rpc-url $ETH_RPC_URL11231124# Get transaction receipt1125cast receipt <TX_HASH> --rpc-url $ETH_RPC_URL11261127# Decode calldata (requires function signature)1128cast calldata-decode "transfer(address,uint256)" <CALLDATA>11291130# Get 4-byte selector from signature1131cast sig "flashLoan(address,address,uint256,bytes)"11321133# Get event signature hash1134cast keccak "Transfer(address indexed,address indexed,uint256)"11351136# Convert between units1137cast --from-wei 10000000000000000001138cast --to-wei 1.5 eth11391140# Get current block number1141cast block-number --rpc-url $ETH_RPC_URL11421143# Search for event logs (across a block range)1144cast logs --from-block 17000000 --to-block 17000100 --rpc-url $ETH_RPC_URL \1145 --topic <TOPIC_HASH>11461147# Get ABI-encoded storage slot (for reading token balances)1148cast storage <TOKEN_ADDRESS> --rpc-url $ETH_RPC_URL \1149 $(cast keccak <ATTACKER_ADDRESS>0000000000000000000000000000000000000000000000000000000000000003)1150```11511152### Local Transaction Replay for Deep Analysis11531154```bash1155# Step 1: Get the transaction1156cast tx <TX_HASH> --rpc-url $ETH_RPC_URL > tx.json11571158# Step 2: Simulate at a specific block1159cast call $(jq -r '.to' tx.json) $(jq -r '.input' tx.json) \1160 --from $(jq -r '.from' tx.json) \1161 --value $(jq -r '.value' tx.json) \1162 --block $(jq -r '.blockNumber' tx.json) \1163 --rpc-url $ETH_RPC_URL11641165# Step 3: Full trace with gas reporter1166cast run <TX_HASH> --rpc-url $ETH_RPC_URL --gas-report1167```11681169## Detailed Analysis Steps11701171### Step 1: Identify the Attack Transaction11721173Locate the initial attack transaction(s) by scanning for suspicious patterns.11741175```python1176def identify_attack_tx(deployer: str, attack_block_range: tuple[int, int]) -> list[str]:1177 """Given a deployer address and block range, find likely attack transactions."""1178 suspicious_txs = []1179 # In production, iterate over blocks in range and check:1180 # - Unusually high gas (500K+)1181 # - Interacts with known DeFi contracts1182 # - Multiple swap events in single tx1183 # - Flash loan event in logs1184 return suspicious_txs1185```11861187Key indicators of an attack transaction:1188- Unusually high gas limit (500,000+ gas for complex attacks, 2M+ for flash loan chains)1189- Transaction sent to a freshly deployed contract (<24 hours old)1190- Direct interaction with a flash loan provider1191- Multiple swaps in a single transaction (3+ swap events)1192- Transaction sent via Flashbots (to address 0x736E4C...)1193- Transaction originator has no prior interaction history with the exploited protocol11941195### Step 2: Reconstruct the Attack Chain11961197Trace every call in the attack transaction. For each hop, identify:1198- **Entry point** — which function was called on which contract, with what parameters1199- **Flash loan source** — where the initial capital came from (Aave, dYdX, Balancer, Uniswap V3)1200- **Price manipulation** — which pool was manipulated and how (large swap, skewed ratio, reserve donation)1201- **Profit extraction** — how the attacker converted the exploited position to profit1202- **Exit** — which bridge or exchange was used to launder or convert the stolen assets12031204```python1205def analyze_attack_chain(tx_hash: str) -> dict:1206 """Full attack chain reconstruction from trace."""1207 trace = trace_transaction(tx_hash)1208 receipt = w3.eth.get_transaction_receipt(tx_hash)1209 tx = w3.eth.get_transaction(tx_hash)12101211 chain = {1212 "tx_hash": tx_hash,1213 "attacker": tx["from"],1214 "target_contract": tx["to"],1215 "block": receipt["blockNumber"],1216 "gas_used": receipt["gasUsed"],1217 "flash_loans": [],1218 "swaps": [],1219 "classification": None,1220 "profit": None,1221 "mev": None,1222 }12231224 if receipt:1225 chain["flash_loans"] = detect_flash_loans(tx_hash, trace)1226 chain["swaps"] = find_all_swaps(receipt)1227 classification = classify_attack(tx_hash, receipt, trace)1228 chain["classification"] = asdict(classification)1229 chain["profit"] = calculate_profit(tx["from"], tx_hash, receipt)1230 chain["mev"] = detect_mev_bundle(tx_hash)12311232 if trace:1233 chain["trace_summary"] = format_trace(trace)12341235 return chain1236```12371238### Step 3: Classify the Attack Type12391240Determine the primary attack vector:1241- **Flash loan attack** — uncollateralized loan used to manipulate prices or leverage positions1242- **Oracle manipulation** — TWAP oracle manipulation via low-liquidity pools1243- **Reentrancy** — recursive call pattern that drains contract state before updates1244- **Bridge attack** — cross-chain message validation bypass or validator compromise1245- **Governance attack** — proposal manipulation, flash loan voting, or timelock bypass1246- **Access control** — unprotected administrative functions, privilege escalation1247- **Logic error** — arithmetic bugs, rounding errors, incorrect state transitions1248- **MEV sandwich** — frontrunning and backrunning a victim transaction12491250### Step 4: Calculate Financial Impact12511252Compute the profit extracted by the attacker:1253- Track all token inflows and outflows from the attacker's wallet using ERC20 Transfer events1254- Convert to USD using Chainlink price feed at the block timestamp1255- Account for gas costs, flash loan fees, and MEV payments1256- Identify any funds returned, frozen, or recovered through law enforcement12571258### Step 5: Write the Post-Mortem12591260Produce a structured incident report with:1261- **Timeline** — block numbers, timestamps, and key on-chain events in chronological order1262- **Root cause** — the specific vulnerability exploited with contract code references and line numbers1263- **Attack flow** — step-by-step transaction trace with function calls and parameter values1264- **Impact assessment** — total losses, affected assets, affected users, and any recovered funds1265- **Mitigation recommendations** — specific code fixes, monitoring rules, architectural changes, each with priority and estimated effort12661267## Expected Output12681269A structured DeFi incident post-mortem containing: attack transaction hash, step-by-step call trace with decoded function signatures and parameters, vulnerability classification with evidence, financial impact calculation with per-asset breakdown, entity attribution (if possible), MEV bundle detection results, and prioritized mitigation recommendations with code-level fixes.12701271## Red Flags12721273- Drawing conclusions from only the attacker's transactions without examining protocol state changes before and after1274- Misclassifying an attack type because the outermost call looks familiar (tunnel vision on flash loans vs. reentrancy)1275- Ignoring failed transactions in the same block that may represent initial exploit attempts or test transactions1276- Relying on a single block explorer without verifying event logs and internal calls directly against an RPC node1277- Assuming all flash loans are the attack vector — flash loans are a capital source, not a vulnerability1278- Overlooking MEV infrastructure — transactions submitted via Flashbots may have special properties1279- Trusting the block explorer's event decoding without verifying raw log topics and data1280- Treating a single trace call as the full picture — some attacks span multiple transactions across blocks1281- Forgetting that price manipulation can happen in a previous block, not just the attack block12821283## Process128412851. **Incident Triage** — Confirm the incident via on-chain data or protocol announcement, locate the attack transaction(s), establish the scope of affected contracts and assets. Cross-reference DeFiLlama hacks dashboard and Twitter for incident confirmation.128612872. **Transaction Trace** — Reconstruct the full call chain using `debug_traceTransaction` or `cast run`. Extract all internal calls, function selectors, and parameter values. Identify the entry point, flash loan source, swap operations, and profit extraction.128812893. **Event Log Analysis** — Parse all event logs from the transaction receipt. Focus on Swap events for price trajectory, Transfer events for asset movement, and protocol-specific events (FlashLoan, Borrow, Deposit, Withdraw).129012914. **Vulnerability Identification** — Classify the attack vector using the classifier function. Map the observed exploit pattern to a vulnerability class. Identify the specific code flaw by comparing the attack trace to the protocol's source code.129212935. **Financial Analysis** — Calculate profit using balance delta method. Compute total losses across all affected assets. Trace fund movements and identify any frozen or recovered amounts. Document the path of stolen funds.129412956. **MEV Context** — Check if the transaction was submitted via Flashbots or other MEV relay. Determine if there were frontrunning/backrunning opportunities. Analyze the block for related transactions.129612977. **Cross-Verification** — Validate all findings against a second RPC provider or block explorer. Confirm timestamps and block numbers. Re-verify profit calculations independently.129812998. **Reporting** — Produce post-mortem with executive summary, root cause, timeline, technical analysis, impact assessment, and prioritized mitigation recommendations.13001301## Verification13021303- Attack chain reconstructed independently using two methods (RPC debug_traceTransaction + block explorer internal txs)1304- Profit calculation verified by comparing attacker balance before and after across all token positions1305- All contract addresses, function signatures, and parameter values cross-referenced with verified source code on Etherscan1306- Timeline block numbers checked on the actual chain (not just from incident announcements)1307- Mitigation recommendations validated against the specific vulnerability, not generic advice1308- Classification results confirmed against known attack pattern databases (SWC Registry, DASP Top 10)1309- Flash loan amounts cross-checked against flash loan provider's subgraph or logs1310- Oracle manipulation claims verified by checking the oracle's reported price vs. actual market price at the time1311- Event log parsing verified by comparing decoded values against raw hex data1312- For multi-transaction attacks: all related transactions identified and linked in the report131313141315## Pre-Investigation Checklist13161317Before committing to a full attack-chain reconstruction, verify the incident is worth the effort:13181319- [ ] Transaction is not a failed attempt (check `status` field — 0 = reverted)1320- [ ] Flash loan profit > 0.5 ETH — smaller amounts are typically MEV extraction, not an exploit1321- [ ] Protocol did not pause or self-destruct pre-attack (may be a white-hat rescue or planned migration)1322- [ ] Affected contract is not a testnet/mock deployment (verify against mainnet-verified source)1323- [ ] Multiple transactions from the same address in the same block — indicates batched attack, not a single atomic exploit1324- [ ] Oracle price deviation > 5% from independent feeds — check against Chainlink, Maker, and Uniswap TWAP1325- [ ] Transaction has at least one verified independent source (block explorer + Dune + Tenderly)1326- [ ] Attacker address is not a known white-hat or MEV bot (check etherscan labels, eigenphi, failed-tx datasets)13271328**Exit criteria**: If 3+ items fail the check, deprioritize this analysis. The "attack" is likely a false positive, MEV extraction, or an internal action. Log the address + chain to a watchlist and re-check weekly.13291330## RPC & API Endpoint Management13311332Every chain-forensics investigation depends on reliable API access. Plan for failure at every layer.13331334### Etherscan-Style API Rate Limits13351336| Provider | Free Tier Limit | Paid Tier | Notes |1337|---|---|---|---|1338| Etherscan | 5 calls/sec | 1,000 calls/min ($165/mo) | `api.etherscan.io` |1339| BscScan | 5 calls/sec | 1,000 calls/min ($99/mo) | Same API shape |1340| Polygonscan | 5 calls/sec | 500 calls/min ($49/mo) | Same API shape |1341| Arbiscan | 10 calls/sec | 1,000 calls/min ($99/mo) | Same API shape |13421343### Fallback Provider Chain13441345```python1346import time1347import requests1348from typing import Any13491350PROVIDER_CHAIN = [1351 {"name": "etherscan", "base": "https://api.etherscan.io/api", "key_env": "ETHERSCAN_KEY"},1352 {"name": "etherscan-backup", "base": "https://api.etherscan.io/api", "key_env": "ETHERSCAN_KEY_2"},1353]13541355def api_call_with_fallback(action: str, params: dict) -> dict:1356 """Call block explorer API with automatic failover and rate-limit backoff."""1357 for provider in PROVIDER_CHAIN:1358 api_key = __import__("os").environ.get(provider["key_env"])1359 if not api_key:1360 continue1361 params["apikey"] = api_key1362 try:1363 resp = requests.get(provider["base"], params=params, timeout=30)1364 data = resp.json()1365 if data.get("status") == "1": # success1366 return data1367 if "rate limit" in str(data).lower():1368 time.sleep(1.5)1369 continue1370 except (requests.ConnectionError, requests.Timeout):1371 time.sleep(2)1372 continue1373 raise RuntimeError(f"All providers failed for action={action}")1374```13751376### API Key Hygiene Rules137713781. **Never hardcode keys** — load from environment variables (`ETHERSCAN_KEY`, `INFURA_KEY`)13792. **Rotate exposed keys immediately** — if a key appears in logs, stdout, or a screenshot, rotate before continuing13803. **Separate read keys from write keys** — use read-only API keys for investigation to prevent accidental state changes13814. **Use a dedicated key for each chain** — don't share a single key across Etherscan, BscScan, Polygonscan; each scan family tracks its own rate limit13825. **Check key validity at start** — make one `?action=balance&address=0x0&tag=latest` call; if it returns an error key, fail fast before beginning analysis13831384### When You Have No API Key13851386**Free alternatives** for emergency investigation:13871388- **Etherscan read function** (web UI): curl-able, no key needed for individual lookups1389- **Blocknative mempool API**: free tier for pending tx monitoring1390- **Covalent unified API**: 5 req/s free tier, one key covers 30+ chains1391- **Dune Analytics**: free tier for querying decoded event data (SQL interface)13921393## When the Trace Goes Cold13941395Every investigator hits dead ends. The skill is not in avoiding them — it's in knowing which are real dead ends and which are puzzles with one more piece missing.13961397### Dead-End Decision Matrix13981399| Situation | Most Likely Cause | Action |1400|---|---|---|1401| EOA receives funds, no outgoing txs for 48h+ | Sleeping address / cold storage | Set webhook alert on `txlist` for this address; return when it moves |1402| Funds enter a Tornado Cash pool | Privacy mixer | Note the deposit amount + block. Check all withdrawals from the same pool within ±100 blocks for matching amounts |1403| Trace reaches a CEX deposit address | Exchange — last on-chain point | Document destination + tx hash. This is a subpoena boundary, not a tracing failure |1404| Trace reaches a bridge contract | Cross-chain transfer | Search the source chain exits → destination chain entry (event logs on both sides) |1405| Target is a proxy contract (ERC-1967) | Implementation upgrade | Find the `_implementation()` slot value and investigate the logic contract |1406| Transaction reverted but gas was paid | Failed attempt, not a real transfer | Ignore for fund flow; check adjacent transactions from the same sender |1407| Destination is a burn address (0x00...dead) | Token burn, intentional | This IS the end. Funds are irretrievable. Report as final destination |1408| Attacker used create2 with salt | Counterfactual deployment | Search for the contract at the predicted address or check deployer history |14091410### When to Stop Tracing14111412Apply the 80/20 rule: the last 5% of trace hops cost 50% of your time. Stop when:141314141. Funds enter a regulated exchange (CEX) — document and close14152. Funds are burned or sent to a dead address — final state14163. You have identified the attacker's identity with high confidence (ENS, social media, Git repo)14174. The trace enters a privacy chain (Monero, Zcash shielded) — explicit stop: these are not analyzable without specialized tools14185. Three consecutive hops show value dissipation (peel chain decay formula below) — the remaining amount is below your investigation threshold14191420```python1421def is_peel_chain_decay(tx_chain: list[dict], min_value_eth: float = 0.1) -> bool:1422 """Detect if a transaction chain shows peel-chain decay pattern."""1423 if len(tx_chain) < 3:1424 return False1425 values = [tx["value_eth"] for tx in tx_chain[-3:]]1426 # Peel chains show strictly decreasing value per hop1427 return all(v < values[i] for i, v in enumerate(values[1:])) and values[-1] < min_value_eth1428```14291430**Rule of thumb**: if you've made 10 API calls on a single address without finding a new lead, archive it with a weekly re-check note and move to the next address. The trace is not gone — it's waiting for a transaction that hasn't happened yet.1431## Practices from Top Investigators14321433### The ZachXBT Methodology14341435ZachXBT has analyzed hundreds of DeFi exploits, from $10M+ protocol hacks to small farming1436pool drains. His methodology treats every exploit as an intelligence problem: **the attacker left1437clues everywhere** — funding source, preparation transactions, test transactions, social1438media activity, infrastructure choices. The skill is knowing which clues to follow and in what1439order.14401441### The 12-Tool Arsenal for DeFi Incident Analysis14421443| Tool | DeFi Incident Use | When to Use |1444|---|---|---|1445| **Etherscan / Solscan** | Primary: tx list, internal txs, event logs, contract source | Start of every incident — read the exploit tx |1446| **Arkham** | Visual fund flow: attacker → intermediate → exchange | During trace: visualize the flow across protocols |1447| **MetaSleuth** | Cross-chain incident tracking: same attacker on multiple chains | When exploit spans L1s/L2s |1448| **Cielo** | Real-time monitoring of attacker addresses | After initial trace — monitor for fund movement |1449| **TRM Labs** | Attacker address risk scoring, sanctions, entity links | Before publishing — verify the attacker's known profile |1450| **Breadcrumbs** | BTC fund routing for Bitcoin-side exploit proceeds | When BTC is involved in the fund flow |1451| **Dune Analytics** | Pre-programmed exploit detection queries, SQL-based analysis | Analyze protocol TVL, liquidity before/after exploit |1452| **DeBank** | Attacker's cross-chain portfolio before the exploit | Determine if the attacker was also a protocol user |1453| **OKLink** | Chain-explorer integration for multi-chain investigation | Follow funds through L2s and sidechains |1454| **Blockchair** | Privacy analysis: coinjoin, mixing detection for BTC funds | When exploit proceeds go through mixers |1455| **OMNIA** | Mempool data: was the exploit tx visible before confirmation? | Determine if MEV bots saw and copied the exploit |1456| **MetaSuites** | Legacy address labeling for pre-2022 protocols | Old protocol incidents need historical label data |14571458### Timeline Reconstruction14591460ZachXBT builds exact chronological timelines for every incident. This reveals attacker behavior1461patterns that transaction-level analysis misses:14621463```python1464def build_incident_timeline(exploit_tx_hash: str, chain: str = "ethereum",1465 window_hours: int = 72) -> list[dict]:1466 """Reconstruct a minute-by-minute timeline of attacker activity before/after exploit.14671468 ZachXBT method: the 72-hour window around an exploit tells more than the exploit itself.1469 """1470 events = []14711472 # 1. Attacker preparation (pre-exploit)1473 # - Test transactions (small amounts, same calldata)1474 # - Contract interactions (approvals, deposits)1475 # - Bridge deposits (funding from another chain)1476 events.append({"phase": "preparation", "what": "check test txs, fund bridging, approvals"})14771478 # 2. Exploit execution1479 events.append({"phase": "exploit", "what": f"main tx: {exploit_tx_hash}"})14801481 # 3. Immediate post-exploit (0-30 min)1482 # - Attacker swaps exploit token to stablecoin1483 # - Attacker bridges to another chain1484 events.append({"phase": "immediate_post", "what": "swap profits, bridge out, move funds"})14851486 # 4. Distribution (30 min - 72 hours)1487 # - Funds split across multiple addresses1488 # - Some sent to CEX, some held, some bridged again1489 events.append({"phase": "distribution", "what": "split, distribute, CEX deposit"})14901491 # 5. Social response (attacker's off-chain activity)1492 # - Discord messages deleted1493 # - Twitter account locked/deleted1494 # - ENS name transferred1495 events.append({"phase": "off_chain", "what": "check social deletions, ENS transfers, domain changes"})14961497 return events1498```14991500Key timeline patterns ZachXBT has documented across multiple exploits:15011502| Pattern | Indicator | Action |1503|---|---|---|1504| Test tx before exploit | Small ETH transfer or contract call 2-24h before | Verify test tx sender = exploit tx sender — confirms preparation |1505| Bridge funding | Attacker bridged funds in 6-48h before exploit | Trace the source chain — the attacker may have a longer history there |1506| Weekend/holiday execution | Exploit on Saturday or during holidays | Expect delayed response — plan for slow exchange actions |1507| MEV copycat | >1 exploit tx in same block or adjacent blocks | Check Flashbots bundles — the original attacker may have been frontrun |1508| Token dump cascade | Multiple sells to different DEXes in rapid succession | Track which pools absorbed the sell; check price impact |1509| Social cleanup | Discord messages deleted within 30 min of exploit | Capture archives via Wayback Machine / DiscordChatExporter immediately |15101511### Multi-Chain Bridge Tracking15121513DeFi exploits rarely stay on one chain. ZachXBT's bridge tracking method:151415151. **Find the bridge contract interaction** — Look for `Transfer` events with `destinationChainId`15162. **Check the source chain's bridge event logs** — Parse `emit DepositFinalized(depositor, amount, ...)`15173. **Extract `receiver` address on the destination chain** — Not always the same as the source15184. **Check the destination chain** — The receiver address may have moved funds further15195. **Record both sides** — Chain A tx hash + Chain B tx hash = complete picture15201521```python1522def trace_bridge_crossing(source_tx_hash: str, source_chain: str,1523 bridge_contract: str) -> dict | None:1524 """Trace a fund movement through a bridge contract.15251526 Returns the destination chain + receiver address if found.1527 """1528 from etherscan_v2_api import fetch_tx_receipt, fetch_event_logs15291530 receipt = fetch_tx_receipt(source_tx_hash, source_chain)1531 if not receipt:1532 return None15331534 logs = receipt.get("logs", [])1535 for log in logs:1536 # Bridge-specific event signatures1537 topics = log.get("topics", [])1538 if not topics:1539 continue15401541 # Common bridge event signatures1542 deposit_signatures = {1543 "0x9c3d2b9b91bf5b4e0d0d8e5b0d0f9c3d2b9b": "Wormhole",1544 "0x8e5b0d0f9c3d2b9b91bf5b4e0d0d8e5b0d0f": "Arbitrum Bridge",1545 }15461547 event_sig = topics[0]1548 if event_sig in deposit_signatures:1549 return {1550 "bridge": deposit_signatures[event_sig],1551 "source_tx": source_tx_hash,1552 "source_chain": source_chain,1553 "receiver": f"0x{log['data'][-40:]}" if len(log.get("data", "")) >= 40 else "unknown",1554 "amount": int(log["data"][:66], 16) if len(log.get("data", "")) >= 66 else 0,1555 }15561557 return None # No bridge event found; check adjacent txs1558```15591560### Incident Response vs. Investigation15611562Knowing when to switch modes is a key ZachXBT skill:15631564- **Crisis mode** (first 24h): Track fund flow in real time, identify exchange deposits,1565 notify exchange contacts immediately. Speed matters more than perfection.1566- **Investigation mode** (24h+): Systematic reconstruction, timing analysis, funding chain1567 tracing, cross-referencing with past incidents. Depth matters more than speed.1568- **Publication mode** (after funds tracked): Public thread with clear timeline, address1569 clusters, and evidence. Community cross-referencing often finds additional links.1570- **Bounty collection** (after publication): Cross-reference with known bounty programs,1571 contact affected protocols, document the investigation's value for bounty claims.157215731574## Anti-Rationalization Table1575## Anti-Rationalization Table15761577| Rationalization | Reality |1578|---|---|1579| "The contract was audited by a top firm, it must be safe" | Audits find known patterns, not novel compositions. Many exploited protocols had multiple audits with no findings. |1580| "It was just a flash loan attack, nothing to learn" | Each flash loan attack teaches something about capital efficiency, protocol composition risks, and oracle design. |1581| "The oracle is battle-tested, it can't be manipulated" | Any oracle that can be moved by a single swap in a low-liquidity pool is manipulable. TWAP oracles with short windows are equally vulnerable. |1582| "The exploit was too complex to understand" | DeFi attacks decompose into a few fundamental primitives — flash loans, swaps, and balance computations. Complexity is often in the number of hops, not the concepts. |1583| "The exploit was a one-time event, it won't happen again" | Attack patterns recur constantly. The same root cause class (oracle manipulation) has been exploited in 100+ separate incidents. |1584| "The bug was in a new feature, so old code is safe" | Attackers frequently find bugs in old, battle-tested code too, as the Curve Vyper incident demonstrated — the bug was in the compiler, not the protocol. |1585| "The exploit only worked because of a specific market condition" | Attackers exploit market conditions intentionally. The same vulnerability will be exploited again when market conditions align. |1586| "OpenZeppelin contracts can't have bugs" | Even battle-tested libraries can have integration-level vulnerabilities. The bug is often in how the contract uses the library, not in the library itself. |1587| "A large TVL means the protocol is secure" | High TVL attracts attackers. Many protocols with >$1B TVL have been exploited (Poly Network, Wormhole, Ronin). |1588| "The team has a bug bounty, so they're on top of security" | Bug bounties are reactive, not preventive. Many exploited protocols had active bug bounty programs. |15891590## Money Section15911592DeFi incident analysis is a high-value consulting skill. Here are established monetization paths:15931594### Incident Response Consulting15951596| Service Tier | Scope | Price Range | Typical Delivery |1597|---|---|---|---|1598| Triage + Brief | Single transaction analysis, 1-page summary, classification | $500 - $2,000 | 4-8 hours |1599| Full Post-Mortem | Complete attack chain reconstruction, root cause, timeline, financial impact, detailed mitigations | $3,000 - $10,000 | 2-5 days |1600| Protocol Security Review | Retrospective analysis plus recommended code and architecture changes | $10,000 - $30,000 | 1-2 weeks |1601| Retainer (Monthly) | Priority incident response, up to 5 analyses/month, ongoing monitoring support | $8,000 - $20,000/month | Ongoing |16021603### Post-Mortem Writing Services16041605Many protocols need public post-mortems but lack the technical writing skills:16061607- **Standard post-mortem** (2-3 pages): $1,500 - $3,0001608- **Detailed technical report** (5-10+ pages with code snippets and transaction traces): $3,000 - $7,0001609- **Executive summary** for non-technical stakeholders: $1,000 - $2,00016101611### Detection Rule Development16121613Build a recurring revenue stream by creating detection rules from your analysis:16141615- **Sigma rules** for SIEM integration: $500 - $1,500 per rule1616- **Forta/Chainlink automation** detection bots: $2,000 - $5,000 per bot1617- **Custom monitoring dashboard** (Dune, Flipside, or custom stack): $5,000 - $15,00016181619### Insurance and Legal Work16201621Specialized analysis for insurance claims and legal proceedings commands a premium:16221623- **Incident verification** for insurance claim: $3,000 - $8,0001624- **Expert witness report** for litigation: $5,000 - $20,0001625- **Fund tracing report** for law enforcement: $7,000 - $15,00016261627### Tooling and Education16281629- **Custom scripts and analysis templates**: Sell on GitHub or GitBook: $50 - $200 per template1630- **Online course** (DeFi forensics fundamentals): $500 - $2,000 per student1631- **Corporate workshop** (half-day to full-day): $5,000 - $15,000 per session