# Defi Threats Exploit Analysis

> 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.

- Skill: `mizazhaider-ceh/defi-threats-exploit-analysis` (Agent Skill)
- Install (CLI): `npx get-skill add mizazhaider-ceh/defi-threats-exploit-analysis`
- Raw SKILL.md: https://api.skillmd.com/api/skills/mizazhaider-ceh/defi-threats-exploit-analysis/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: mizazhaider-ceh (https://skillmd.com/u/mizazhaider-ceh)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/mizazhaider-ceh/defi-threats-exploit-analysis

---


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

1. 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.
2. Map the money flow: what the attacker borrowed, what they swapped or deposited, what price moved, and where the profit landed.
3. 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.
4. Reproduce the attack on a local mainnet fork at the block before the attack, with the same steps and the same order.
5. Vary one input at a time: loan size, slippage, pool depth, and oracle window, to find the minimum conditions for profit.
6. Check what stopped, or should have stopped, the attack: caps, circuit breakers, timelocks, and monitoring, and whether each one was present and armed.
7. 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.
8. Report the timeline, the profit math, the broken assumption, and the fix, with transaction hashes as evidence.

## 1. Fork reproduction setup

```bash
# 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
```

```bash
# 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

```python
#!/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

```solidity
// 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");
}
```

```bash
# 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

```bash
# 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

- Foundry book, fork testing with Anvil: https://getfoundry.sh/
- OpenZeppelin Contracts, access control and timelock: https://docs.openzeppelin.com/contracts/
- Uniswap v3 oracle documentation, time weighted prices: https://docs.uniswap.org/
- Ethereum documentation, DeFi and MEV concepts: https://ethereum.org/en/defi/
- Trail of Bits, DeFi security guidance: https://github.com/crytic/building-secure-contracts

