$60 Million Gone Because of One Ordering Mistake
In June 2016, an attacker drained 3.6 million ETH — worth roughly $60 million at the time — from The DAO in a single exploit. The vulnerability wasn't some obscure cryptographic flaw. It wasn't a zero-day in the EVM. It was a withdrawal function that sent ETH before updating the user's balance. That's it. One ordering mistake. The fallout was so catastrophic that the Ethereum community hard-forked the entire chain to recover the funds, creating Ethereum Classic in the process.
This isn't ancient history. Reentrancy attacks have drained over $1.5 billion across DeFi protocols since 2016. Cream Finance lost $18.8M to reentrancy in 2021. Fei Protocol lost $80M to a cross-function reentrancy attack in 2022. These aren't obscure edge cases — they're the most well-documented vulnerability class in Solidity, and contracts are still shipping with this exact bug today.
Here's What Actually Happened in The DAO Attack
The DAO was a decentralized investment fund. Users deposited ETH, got voting tokens, and could withdraw their share of the pool by calling a splitDAO function. The withdrawal logic worked like this:
- Check how much ETH the caller is owed
- Send that ETH to the caller
- Update the caller's balance to zero
See the problem? Step 3 happens after step 2. The attacker wrote a malicious smart contract that, every time it received ETH in step 2, immediately called the withdrawal function again — before step 3 ever ran. Because the balance hadn't been zeroed out yet, the contract thought the attacker was still owed the full amount. The attacker's contract called back in, got paid again, called back in again, got paid again — looping until the pool was empty.
This is reentrancy. Your contract calls an external address, that external address calls back into your contract, and your contract's state is still stale from before the external call. The attacker gets to act on that stale state.
Here's the thing most people miss: the contract was behaving exactly as written. There was no bug in the EVM. There was no overflow. The function just had its steps in the wrong order.
The Code Reality: Vulnerable vs. Fixed
Let's look at the classic vulnerable withdrawal pattern in modern Solidity (0.8.24). This is a simplified ETH vault — the same architecture that's gotten projects killed.
Vulnerable Pattern
// SPDX-License-Identifier: MIT
// Solidity 0.8.24 — VULNERABLE — DO NOT USE
pragma solidity ^0.8.24;
contract VulnerableVault {
mapping(address => uint256) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw() external {
uint256 amount = balances[msg.sender];
require(amount > 0, "Nothing to withdraw");
// DANGER: External call happens BEFORE state update
// An attacker's fallback/receive function can call withdraw() again
// before balances[msg.sender] is set to 0
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
// This line runs too late — attacker already re-entered
balances[msg.sender] = 0;
}
}
If you paste this into Remix and deploy it, it works perfectly in normal conditions. That's what makes it dangerous. You'd never catch this from a quick read-through. The attacker deploys this contract and calls it:
// SPDX-License-Identifier: MIT
// Attacker contract — demonstrates the exploit
pragma solidity ^0.8.24;
interface IVulnerableVault {
function deposit() external payable;
function withdraw() external;
}
contract Attacker {
IVulnerableVault public target;
uint256 public attackCount;
constructor(address _target) {
target = IVulnerableVault(_target);
}
function attack() external payable {
require(msg.value >= 1 ether, "Send at least 1 ETH");
target.deposit{value: msg.value}();
target.withdraw();
}
// This is called every time the vault sends ETH to this contract
receive() external payable {
if (attackCount < 5 && address(target).balance >= 1 ether) {
attackCount++;
target.withdraw(); // Re-enter before balance is zeroed
}
}
function collectStolenFunds() external {
payable(msg.sender).transfer(address(this).balance);
}
}
The attacker deposits 1 ETH, calls withdraw(), and their receive() function triggers 5 more withdrawals before the vault ever updates the balance. They started with 1 ETH and walked away with 6.
The Fix: Checks-Effects-Interactions Pattern
// SPDX-License-Identifier: MIT
// Solidity 0.8.24 — SAFE VERSION
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; // OpenZeppelin 5.x
contract SecureVault is ReentrancyGuard {
mapping(address => uint256) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
// nonReentrant modifier from OpenZeppelin 5.x prevents re-entry at the EVM level
function withdraw() external nonReentrant {
uint256 amount = balances[msg.sender];
require(amount > 0, "Nothing to withdraw");
// CHECKS: Validate state (done above with require)
// EFFECTS: Update state BEFORE any external call
balances[msg.sender] = 0;
// INTERACTIONS: External call happens last
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
}
}
Two defenses layered here. First, the state update (balances[msg.sender] = 0) happens before the external call — this is the Checks-Effects-Interactions (CEI) pattern. Even if the attacker re-enters, their balance is already zero, so the require(amount > 0) check kills the second call. Second, the nonReentrant modifier from OpenZeppelin 5.x locks the function at the contract level using a storage slot, so no re-entry is possible at all.
Use both. CEI as your primary defense, nonReentrant as your safety net. Never rely on just one.
The Counterintuitive Take: Reentrancy Isn't Your Biggest Problem
Everyone knows to check for reentrancy. Slither flags it. Every audit checklist has it. But here's what I've seen shipping over and over: cross-function reentrancy — and almost nobody talks about it.
Classic reentrancy is when function A calls back into function A. Cross-function reentrancy is when function A calls an external contract, and that external contract calls function B in your contract — which reads the stale state from function A's in-progress execution.
The Fei Protocol hack worked exactly like this. The attacker triggered a borrow function, which called out to update balances, which allowed them to call a different function that used the still-unupdated borrow state to extract more collateral than they should've had access to. Slither didn't catch it. Most audit checklists don't cover it. $80M gone.
The nonReentrant modifier on a single function doesn't protect you here unless you apply it to every function that reads shared state. You need to think about your contract's entire state machine, not just individual functions.
How I'd Catch This Before It Ships
When I'm walking through a contract looking for reentrancy risk, here's the actual process:
1. Map every external call. Search for .call(, .delegatecall(, .transfer(, .send(, ERC-20 transfer/transferFrom, and any interface calls to user-supplied addresses. Every single one is a potential re-entry point.
2. For each external call, check what state hasn't been updated yet. Draw a line in the function at the point of the external call. Everything above that line — what state changes have happened? What state changes are still pending? That pending state is the attack surface.
3. Check whether any other function in the contract reads that pending state. This is how you find cross-function reentrancy. It's tedious. It's why audits take time. But it's the difference between catching it on paper and losing $80M.
4. Verify CEI is followed everywhere, not just in obvious withdrawal functions. Price oracle updates, LP token minting, reward distribution — any function that touches external contracts and then updates state is vulnerable.
5. Run Slither. It'll catch the obvious patterns. Specifically run it with slither . --detect reentrancy-eth,reentrancy-no-eth,reentrancy-benign,reentrancy-events and go through every finding. But treat Slither as a starting point, not a final answer.
Static analysis tools like Slither are excellent at catching the textbook pattern — external call followed by state change, same function. They struggle with the cross-function variant because that requires understanding the relationship between functions and shared state, which is context-dependent. That's where AI-powered analysis starts to earn its keep, because it can reason about your contract's full state machine and trace attack paths that span multiple functions.
If You're About to Deploy or Invest: Your Checklist
Whether you're a developer shipping a contract or a DeFi user trying to figure out if a new protocol is going to get drained, here are five specific things to check:
- Find every
.call{value:pattern in the contract. Etherscan shows verified source. Search for it. If you see a state update after a.call{value:in any withdrawal or transfer function, that's a red flag. Full stop. - Check for
ReentrancyGuardor equivalent. If a protocol is holding any meaningful ETH and doesn't use OpenZeppelin'sReentrancyGuard(or a custom equivalent), ask why. It's two lines of code. There's no good reason not to use it. - For DeFi protocols, check the audit report for cross-function reentrancy findings. Most audit firms will explicitly call this out. If the report doesn't mention it at all, that's a gap.
- If you're a developer: add
nonReentrantto every public/external function that writes state or reads state that could be stale during another function's execution. Yes, it costs a small amount of extra gas. No, that is not a good reason to skip it. - Run the contract through an automated analysis tool before any real money touches it. Not after. The DAO team knew about smart contract risks in theory. They didn't run their specific code through adversarial analysis before launching with $150M in deposits.
Run Your Contract Before You Deploy
The DAO hack was preventable. Cream Finance was preventable. Fei Protocol was preventable. Every single one of these attacks exploited a pattern that can be detected with the right analysis before a single dollar goes in. SmartContractAuditor.ai flags reentrancy vulnerabilities — including cross-function variants — and explains exactly why each finding is dangerous, in plain English you can actually act on. Not just "reentrancy detected at line 47" but a full explanation of how an attacker would exploit it and what the fix looks like.
Paste your contract into SmartContractAuditor.ai before you deploy. It takes 30 seconds and could save you everything.
Related Articles
Continue exploring smart contract security with these related insights
OpenZeppelin vs Custom Smart Contract Implementations: The Security Trade-offs Nobody Talks About
Custom ERC20 implementations have caused hundreds of millions in losses — not because developers are bad, but because rolling your own token logic is a minefield. Here's what actually separates safe contracts from the ones that get drained.
OpenZeppelin's Library Secured $37 Trillion. Are You Actually Using It Right?
OpenZeppelin's library secures an estimated $37 trillion. But the library being secure and your deployment being secure are two different claims — here are the real misuse patterns that slip through.
Gas Optimization Without Sacrificing Security: The Developer's Guide to Efficient Smart Contracts
Gas optimization in Solidity is the process of reducing computation costs in smart contracts — but the shortcuts that save gas are often the same ones that get protocols exploited. One wrong optimization pattern opened the door to the $25M Uni v1 reentrancy-style drain. Here's how to do it right.
Explore more insights on smart contract security andblockchain vulnerabilities