An exploit analysis is not a retelling of a post-mortem. It is a reconstruction that ends with a test you can run: the attacker did this, in this order, because the protocol assumed that, and the profit came from here. This sub-skill belongs to defi-threats, which owns the DeFi threat model, and it covers the reconstruction method for flash loan attacks, oracle manipulation, price and liquidity abuse, and governance-driven theft. Test only on local forks of public data, never against live protocols.
Quick Workflow
- Collect the evidence: the attack transaction hashes, the block numbers, the contracts involved, and the post-mortem if one exists, and treat the post-mortem as a claim to verify, not as truth.
- Map the money flow: what the attacker borrowed, what they swapped or deposited, what price moved, and where the profit landed.
- Name the broken assumption in one sentence, for example that a spot price equals a fair price, or that liquidity cannot move inside one transaction.
- Reproduce the attack on a local mainnet fork at the block before the attack, with the same steps and the same order.
- Vary one input at a time: loan size, slippage, pool depth, and oracle window, to find the minimum conditions for profit.
- Check what stopped, or should have stopped, the attack: caps, circuit breakers, timelocks, and monitoring, and whether each one was present and armed.
- Write the fix as a test first: the fork test must fail before the fix and pass after it, for both the attack and a legitimate user flow.
- Report the timeline, the profit math, the broken assumption, and the fix, with transaction hashes as evidence.
1. Fork reproduction setup
# Fork at the block before the attack so the state matches the exploit
anvil --fork-url "$RPC_URL" --fork-block-number 21000000 &
sleep 3
cast block-number --rpc-url http://127.0.0.1:8545
# Fund a test account on the fork, or impersonate a holder for capital
cast rpc anvil_impersonateAccount 0x0000000000000000000000000000000000000000 --rpc-url http://127.0.0.1:8545
# Pull the attack transaction trace for the reconstruction
cast tx 0x0000000000000000000000000000000000000000000000000000000000000000 \
--rpc-url "$RPC_URL" --json > /tmp/defi/attack-tx.json
jq '.to, .value, .input' /tmp/defi/attack-tx.json | head -20
cast trace 0x0000000000000000000000000000000000000000000000000000000000000000 \
--rpc-url "$RPC_URL" 2>/dev/null | head -60
2. Flash loan profit math
#!/usr/bin/env python3
"""Flash loan exploit profit model, with every cost made explicit."""
# Fill these from the trace, not from the post-mortem summary.
borrowed = 10_000_000.0
borrow_fee = 0.0009 * borrowed
gas_cost = 500.0
swap_loss = 120_000.0
extracted = 850_000.0
profit = extracted - borrowed * 0 - borrow_fee - gas_cost - swap_loss
print(f"borrow fee: {borrow_fee:,.0f}")
print(f"estimated profit: {profit:,.0f}")
print("profit must stay positive after fees, gas, and slippage,")
print("or the attack only works at a larger loan size, which is itself a finding")
3. Oracle manipulation test pattern
// Fork test shape: move the price source, then call the victim.
// A protocol that reads a spot price from one thin pool will reprice
// instantly, and a lending market will then allow an outsized borrow.
function testOracleManipulationMovesBorrowLimit() public {
uint256 borrowBefore = victim.maxBorrow();
// Swap a large amount into the price pool to move the spot price.
// On the fork this is safe; on mainnet it would be an attack.
swapLargeAmountIntoPricePool();
uint256 borrowAfter = victim.maxBorrow();
assertGt(borrowAfter, borrowBefore, "price move must change the limit");
}
# Compare the spot price against a time weighted price at the attack block
cast call $POOL "slot0()" --rpc-url "$RPC_URL"
cast call $ORACLE "observe(uint32[])" "[3600]" --rpc-url "$RPC_URL"
# If the protocol used slot0 directly and the pool was thin,
# the broken assumption is written in that one call
4. Profit path and fix verification
# After the fix, the same fork test must fail to profit
forge test --match-test testExploitNoLongerProfits -vvv
# And a legitimate flow must still work, or the fix is a denial of service
forge test --match-test testLegitimateDepositBorrowRepay -vvv
Edge cases
- An attack that spans several transactions is not one flash loan; check for preparation transactions (funding, approvals, governance delegation) in earlier blocks.
- A post-mortem that blames one bug may hide a second one; keep tracing after the first profit step, because attackers often chain two flaws.
- Fork state drifts from mainnet quickly; pin the fork block, and re-run the test at the exact attack block if the first reproduction fails.
- Profit math that ignores gas, borrow fees, and slippage overstates the attack and misleads the fix priority.
- A fix that only raises a cap or adds a pause still leaves the broken assumption in place; prefer a time weighted price, a deposit cap per block, or a borrow limit tied to verified liquidity.
- Governance attacks look like DeFi exploits but run on a longer clock; check proposal creation, delegation, and voting delay alongside the money flow.
Key references
1---2name: defi-threats-exploit-analysis3description: Use when reconstructing a DeFi exploit from transaction traces, fork tests, and price data to find the broken assumption and the exact profit path. Use when reviewing a post-mortem, testing a flash loan or oracle attack on a local fork, or writing a fix that closes the exploited path without breaking legitimate flows.4version: "1.0.0"5domain: blockchain6lifecycle: stable7last_verified: "2026-10-27"8parent: defi-threats9requires: [defi-threats]10extends: []11relates: [smart-contract-audit, bridge-attacks, blockchain-forensics]12tools:13 - name: Python14 version: "3.12"15 - name: Foundry16 version: "1.0.0"17 - name: Solidity compiler18 version: "0.8.28"19 - name: jq20 version: "1.7"21metadata:22 source: https://skillmd.com/skills/mizazhaider-ceh/defi-threats-exploit-analysis23---2425An exploit analysis is not a retelling of a post-mortem. It is a reconstruction that ends with a test you can run: the attacker did this, in this order, because the protocol assumed that, and the profit came from here. This sub-skill belongs to defi-threats, which owns the DeFi threat model, and it covers the reconstruction method for flash loan attacks, oracle manipulation, price and liquidity abuse, and governance-driven theft. Test only on local forks of public data, never against live protocols.2627## Quick Workflow28291. Collect the evidence: the attack transaction hashes, the block numbers, the contracts involved, and the post-mortem if one exists, and treat the post-mortem as a claim to verify, not as truth.302. Map the money flow: what the attacker borrowed, what they swapped or deposited, what price moved, and where the profit landed.313. Name the broken assumption in one sentence, for example that a spot price equals a fair price, or that liquidity cannot move inside one transaction.324. Reproduce the attack on a local mainnet fork at the block before the attack, with the same steps and the same order.335. Vary one input at a time: loan size, slippage, pool depth, and oracle window, to find the minimum conditions for profit.346. Check what stopped, or should have stopped, the attack: caps, circuit breakers, timelocks, and monitoring, and whether each one was present and armed.357. Write the fix as a test first: the fork test must fail before the fix and pass after it, for both the attack and a legitimate user flow.368. Report the timeline, the profit math, the broken assumption, and the fix, with transaction hashes as evidence.3738## 1. Fork reproduction setup3940```bash41# Fork at the block before the attack so the state matches the exploit42anvil --fork-url "$RPC_URL" --fork-block-number 21000000 &43sleep 344cast block-number --rpc-url http://127.0.0.1:854545# Fund a test account on the fork, or impersonate a holder for capital46cast rpc anvil_impersonateAccount 0x0000000000000000000000000000000000000000 --rpc-url http://127.0.0.1:854547```4849```bash50# Pull the attack transaction trace for the reconstruction51cast tx 0x0000000000000000000000000000000000000000000000000000000000000000 \52 --rpc-url "$RPC_URL" --json > /tmp/defi/attack-tx.json53jq '.to, .value, .input' /tmp/defi/attack-tx.json | head -2054cast trace 0x0000000000000000000000000000000000000000000000000000000000000000 \55 --rpc-url "$RPC_URL" 2>/dev/null | head -6056```5758## 2. Flash loan profit math5960```python61#!/usr/bin/env python362"""Flash loan exploit profit model, with every cost made explicit."""63# Fill these from the trace, not from the post-mortem summary.64borrowed = 10_000_000.065borrow_fee = 0.0009 * borrowed66gas_cost = 500.067swap_loss = 120_000.068extracted = 850_000.069profit = extracted - borrowed * 0 - borrow_fee - gas_cost - swap_loss70print(f"borrow fee: {borrow_fee:,.0f}")71print(f"estimated profit: {profit:,.0f}")72print("profit must stay positive after fees, gas, and slippage,")73print("or the attack only works at a larger loan size, which is itself a finding")74```7576## 3. Oracle manipulation test pattern7778```solidity79// Fork test shape: move the price source, then call the victim.80// A protocol that reads a spot price from one thin pool will reprice81// instantly, and a lending market will then allow an outsized borrow.82function testOracleManipulationMovesBorrowLimit() public {83 uint256 borrowBefore = victim.maxBorrow();84 // Swap a large amount into the price pool to move the spot price.85 // On the fork this is safe; on mainnet it would be an attack.86 swapLargeAmountIntoPricePool();87 uint256 borrowAfter = victim.maxBorrow();88 assertGt(borrowAfter, borrowBefore, "price move must change the limit");89}90```9192```bash93# Compare the spot price against a time weighted price at the attack block94cast call $POOL "slot0()" --rpc-url "$RPC_URL"95cast call $ORACLE "observe(uint32[])" "[3600]" --rpc-url "$RPC_URL"96# If the protocol used slot0 directly and the pool was thin,97# the broken assumption is written in that one call98```99100## 4. Profit path and fix verification101102```bash103# After the fix, the same fork test must fail to profit104forge test --match-test testExploitNoLongerProfits -vvv105# And a legitimate flow must still work, or the fix is a denial of service106forge test --match-test testLegitimateDepositBorrowRepay -vvv107```108109## Edge cases110111- An attack that spans several transactions is not one flash loan; check for preparation transactions (funding, approvals, governance delegation) in earlier blocks.112- A post-mortem that blames one bug may hide a second one; keep tracing after the first profit step, because attackers often chain two flaws.113- Fork state drifts from mainnet quickly; pin the fork block, and re-run the test at the exact attack block if the first reproduction fails.114- Profit math that ignores gas, borrow fees, and slippage overstates the attack and misleads the fix priority.115- A fix that only raises a cap or adds a pause still leaves the broken assumption in place; prefer a time weighted price, a deposit cap per block, or a borrow limit tied to verified liquidity.116- Governance attacks look like DeFi exploits but run on a longer clock; check proposal creation, delegation, and voting delay alongside the money flow.117118## Key references119120- Foundry book, fork testing with Anvil: https://getfoundry.sh/121- OpenZeppelin Contracts, access control and timelock: https://docs.openzeppelin.com/contracts/122- Uniswap v3 oracle documentation, time weighted prices: https://docs.uniswap.org/123- Ethereum documentation, DeFi and MEV concepts: https://ethereum.org/en/defi/124- Trail of Bits, DeFi security guidance: https://github.com/crytic/building-secure-contracts