7 min read

Exploitation

Reentrancy Attacks

Reentrancy is the most famous smart contract vulnerability, responsible for the $60M DAO hack in 2016. It occurs when external contract calls are made before state updates, allowing recursive draining of funds.

Authorised Targets Only

The attacker contract below is for a bank you deployed on Foundry/Anvil, Hardhat, or a testnet you control. Deploying and running it against a live contract that holds other people's funds is theft — unauthorised exploitation is a criminal offence in most jurisdictions and may also be a securities or commodities violation.

Vulnerable Contract Example

All four samples target solc 0.8.34. The exploit needs one precondition — the victim sends ETH with an external call before it debits the balance — and then it runs as a loop: attack() deposits 1 ETH and withdraws it; the bank sends ETH to the attacker, which triggers receive(), which calls withdraw() again while the recorded balance is still the pre-withdrawal value. It stops when the bank holds less than 1 ETH.

vulnerable-bank.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

// VULNERABLE: Reentrancy attack
contract VulnerableBank {
    mapping(address => uint256) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 _amount) public {
        require(balances[msg.sender] >= _amount, "Insufficient balance");

        // BAD: External call before state update
        (bool success, ) = msg.sender.call{value: _amount}("");
        require(success, "Transfer failed");

        balances[msg.sender] -= _amount;  // State update AFTER external call
    }
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;

// VULNERABLE: Reentrancy attack
contract VulnerableBank {
    mapping(address => uint256) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 _amount) public {
        require(balances[msg.sender] >= _amount, "Insufficient balance");

        // BAD: External call before state update
        (bool success, ) = msg.sender.call{value: _amount}("");
        require(success, "Transfer failed");

        balances[msg.sender] -= _amount;  // State update AFTER external call
    }
}

The two fixes are not equivalent. Reordering to checks-effects-interactions removes the stale state the second call reads, so it works for every reentrancy-shaped bug you write yourself. A reentrancy guard only blocks re-entry into the guarded function — it does nothing for a cross-function or read-only reentrancy that reaches a differently guarded path, so a guard is a backstop, not a substitute for getting the ordering right.

OpenZeppelin's storage-based guard writes an SSTORE pair on every guarded call. It also ships ReentrancyGuardTransient at @openzeppelin/contracts/utils/ReentrancyGuardTransient.sol, which uses the EIP-1153 transient-storage opcodes TSTORE and TLOAD at roughly 100 gas each instead. It requires a Cancun-or-later EVM target, so a contract still compiled for shanghai cannot use it. The trade-off is the one the Solidity team documents: transient storage is cleared at the end of the transaction, not at the end of the call, and a guard that skips its own reset as a gas saving leaves the lock set for every later call in the same transaction.

The DAO Hack (2016)

The DAO lost 3.6M ETH — roughly $60M at the time — to reentrancy: the attacker recursively withdrew funds before the balance was updated, and the recovery hard fork split Ethereum into ETH and ETC.

Prevention Methods

  • Checks-Effects-Interactions: Update state before external calls
  • ReentrancyGuard: Use OpenZeppelin's nonReentrant modifier (v5 path: @openzeppelin/contracts/utils/ReentrancyGuard.sol)
  • Pull over Push: Let users withdraw instead of sending automatically
  • Mutex Lock: Use boolean flag to prevent reentry