Writeups/Ethernaut/MagicNumber
HardEthernaut · Ethernaut2024

MagicNumber

Bytecode Crafting — Maximum 10 Opcodes

The contract must respond to `whatIsTheMeaningOfLife()` with `42`, but its deployed bytecode must be at most 10 bytes. This requires hand-crafting raw EVM opcodes for both the creation (constructor) bytecode and the runtime bytecode.

SolidityEVM BytecodeAssemblyCODECOPYMinimal Contract

00Overview

The contract must respond to `whatIsTheMeaningOfLife()` with `42`, but its deployed bytecode must be at most 10 bytes. This requires hand-crafting raw EVM opcodes for both the creation (constructor) bytecode and the runtime bytecode.

01Write runtime bytecode (returns 42 in 10 opcodes)

javascript
// Runtime: PUSH1 0x2a, PUSH1 0x00, MSTORE, PUSH1 0x20, PUSH1 0x00, RETURN
// Hex: 602a60005260206000f3

02Write creation bytecode and deploy

javascript
// Creation: PUSH10 <runtime>, PUSH1 0x00, MSTORE, PUSH1 0x0a, PUSH1 0x16, RETURN
const bytecode = "0x600a600c600039600a6000f3602a60005260206000f3"
const tx = await web3.eth.sendTransaction({from: player, data: bytecode})
await contract.setSolver(tx.contractAddress)

03Level Completed

MagicNumber — completion screenshot 1