Ethernaut Fallout Solution: Foundry Walkthrough

Inspect Ethernaut level 2's allocation contract, find its ownership bug and verify the solution with Foundry.

In Ethernaut level 2, Fallout, the challenge is to claim ownership through a function that was meant to be the constructor. We'll use Foundry in the SCH CTF Arena to call the misspelled Fal1out() function as the player.

Ethernaut series Level 02 · Fallout

Contract overview

The target stores two pieces of state:

mapping(address => uint256) allocations;
address payable public owner;

allocate() adds the caller's payment to their allocation. collectAllocations() transfers the contract's entire ETH balance to the caller, but its onlyOwner modifier first requires msg.sender == owner.

Neither function changes ownership. Look at the function labeled as the constructor.

The misleading constructor comment

The source labels this function as a constructor:

contract Fallout {
    // Other declarations omitted.
    /* constructor */
    function Fal1out() public payable {
        owner = msg.sender;
        allocations[owner] = msg.value;
    }
}

Fallout and Fal1out differ by one character. The declaration is a public function. The comment does not affect how Solidity compiles it.

Why the typo compiles

Before Solidity 0.5.0, a function named after its contract acted as the constructor. A misspelling such as Fal1out instead compiled as a normal public function.

Since Solidity 0.5.0, constructors must use the constructor keyword. The Solidity 0.5.0 breaking changes describe the change.

The Arena compiles this target with Solidity 0.6.12 and preserves Fal1out() as a public function for the exercise. Its unrestricted owner = msg.sender assignment lets any caller take ownership, including after someone else has already claimed it.

Call the function as the player

Once the checker is unlocked, paste this function into the editor. The Arena supplies falloutInstance and the player identity:

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

    falloutInstance.Fal1out();

    vm.stopBroadcast();
}

The call sends no value, so allocations[owner] receives zero. Run it as the supplied player. If a helper contract calls Fal1out(), that helper becomes the owner.

Check the result

Check answer deploys a fresh target and verifies that owner() is the player address.

State Owner in the SCH test
Immediately after new Fallout() Zero address
After the player's Fal1out() call Player address
After a different address calls it later That other address

Because the function stays public, a later caller can replace the owner again.

Passing local Foundry verification of the Fallout walkthrough solution. The local Foundry verifier confirms the new owner.

Common mistakes

  • Call Fal1out() with the digit 1. Fallout() is not the exposed function.

  • collectAllocations() requires ownership, so it cannot replace the owner call.

Watch Johnny's walkthrough

Ethernaut Fallout solution with JohnnyTime

Watch on YouTube if the embedded player is unavailable.

Initialize ownership safely

For an ordinary, directly deployed contract, a real constructor sets the initial owner during deployment:

constructor() public {
    owner = msg.sender;
}

This constructor syntax works with the Arena's Solidity 0.6 compiler. Remove Fal1out() when replacing it. Leaving the public function in place preserves the bug.

For a proxy, a constructor initializes the implementation's storage, not the proxy's. Use an initializer protected against unauthorized and repeated calls.

Challenge source: OpenZeppelin Ethernaut Fallout. Continue to Coin Flip or try Fallout in the Arena.