Ethernaut Fallback Solution: Foundry Walkthrough

A Foundry walkthrough of Ethernaut level 1: inspect the contribution contract, take ownership and withdraw its balance.

In Ethernaut level 1, Fallback, the goal is to take ownership and withdraw the contract's ETH. We'll use Foundry in the SCH CTF Arena to send two 1-wei payments through the right entry points.

Ethernaut series Level 01 · Fallback

Contract overview

The contract stores an owner address and a contributions mapping keyed by contributor address. Its constructor makes the deployer the owner and credits that address with 1,000 ether:

constructor() {
    owner = msg.sender;
    contributions[msg.sender] = 1000 * (1 ether);
}

The 1,000 ether is an entry in contributions, not ETH held by the contract. The SCH verifier funds the target with 0.001 ETH.

contribute() accepts less than 0.001 ETH per call and adds the payment to the caller's recorded total. It assigns ownership if that total exceeds the owner's contribution. getContribution() returns the caller's recorded contribution.

withdraw() sends the entire balance to the owner. Its onlyOwner modifier requires msg.sender == owner, so we need ownership before we can empty the contract.

An empty-calldata ETH payment enters receive(), which contains a second owner assignment.

The vulnerability in receive()

Taking ownership through contribute() would require exceeding the deployer's recorded 1,000 ether, while each call accepts less than 0.001 ETH. But receive() contains another owner assignment:

receive() external payable {
    require(msg.value > 0 && contributions[msg.sender] > 0);
    owner = msg.sender;
}

This handler checks for a positive prior contribution and a positive payment. It never compares the caller's contribution with the owner's. Even a minimal contribution and payment can therefore grant withdrawal rights.

Make the two payments from the same address

First call contribute() with 1 wei. Then send 1 wei with empty calldata from the same address. The contribution mapping uses msg.sender as its key.

With empty calldata, Solidity dispatches to receive(). Calling contribute() again would not reach this code. See Solidity's receive-function rules.

After this call Your contribution Owner Target balance in SCH
Initial state 0 Deployer 0.001 ETH
contribute{value: 1 wei}() 1 wei Deployer 0.001 ETH + 1 wei
Empty-calldata payment of 1 wei 1 wei Player 0.001 ETH + 2 wei
withdraw() 1 wei Player 0

Run the Fallback solution

The Arena supplies fallbackInstance, the script wrapper, and PLAYER_PRIVATE_KEY. Paste this complete run() function into the editor without adding imports or another contract:

function run() external {
    vm.startBroadcast(PLAYER_PRIVATE_KEY);

    fallbackInstance.contribute{value: 1 wei}();
    (bool sent, ) = address(fallbackInstance).call{value: 1 wei}("");
    require(sent, "ownership payment failed");
    fallbackInstance.withdraw();

    vm.stopBroadcast();
}

startBroadcast sends all three calls as the player. The low-level call sends ETH with empty calldata and returns a success flag. Check it before withdrawing. Solidity's .transfer() forwards only 2,300 gas, too little for receive() to write the owner to storage.

Verify ownership and balance

Check answer verifies that the player owns the instance and its balance is zero. Compilation alone does not satisfy the verifier.

Local Foundry verification of the Fallback article solution, with the passing ownership and balance test. Excerpt from the passing local Foundry test.

Common errors

  • The first call must contribute more than zero, and it must come from the same player that sends the empty-calldata payment.

  • contribute() rejects 0.001 ETH or more because its limit uses <. One wei works.

  • Call receive() before withdraw(). Keep all three calls inside the supplied player broadcast so the player becomes owner.

Watch Johnny's walkthrough

Ethernaut Fallback solution with JohnnyTime

Watch on YouTube if the embedded player is unavailable.

Keep ETH receipt separate from ownership

Remove the owner assignment from receive() so a direct ETH payment cannot grant withdrawal rights. If the contract needs ownership transfers, put them in a separate function restricted to the current owner.

Review every write to privileged state, including writes in ETH handlers. Protecting withdraw() does not help if an unguarded function can replace the address it trusts.

Continue to Fallout or try Fallback in the Arena.

Challenge source: OpenZeppelin Ethernaut Fallback.