
There are many hacks happening recently, nearly 1-2 hacks per month, so I was looking into the transaction of those attack calls, and wanna look into the attack contract, to know how exactly attacker wrote the code to exploit the codebase. But I was in a confused state once I saw the contract section of attacker contract in etherscan, it has nothing but plain bytecode. So how exactly do I know what was performed, and then get's the motivation to learn about this stuff, converting bytecode to solidity code. So, upon researching about that I got to find about Ethernaut Magic Number challenge.
Yes, we are going to discuss this challenge here. How exactly solidity code can rewrite in the plain bytecode, and of course there are multiple ways we can solve this challenge, and we will discuss key differences in between them. Without any delay let's get started.
The goal of this challenge is to assign a solver whose bytecode should not exceed 10 bytes, and upon calling the function whatIsTheMeaningOfLife() in this contract it should return a constant unsigned integer
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MagicNum {
address public solver;
constructor() {}
function setSolver(address _solver) public {
solver = _solver;
}
/*
____________/\\\_______/\\\\\\\\\_____
__________/\\\\\_____/\\\///////\\\___
________/\\\/\\\____\///______\//\\\__
______/\\\/\/\\\______________/\\\/___
____/\\\/__\/\\\___________/\\\//_____
__/\\\\\\\\\\\\\\\\_____/\\\//________
_\///////////\\\//____/\\\/___________
___________\/\\\_____/\\\\\\\\\\\\\\\_
___________\///_____\///////////////__
*/
}The main goal of this challenge is to set solver address, in which it should have a function whatIsTheMeaningOfLife() which returns a constant 32 bytes unsigned integer. Till here there is no issue, as we can simply deploy smart contract with the same function name, but the main problem we have here is that the byte code length of the contract should not exceed 10 bytes. How can we achieve it?
Yes, the only option we left with is using EVM raw byte code, which means we need to develop a byte code ourselves in such a way that not exceeding 10 bytes. So how can we do it?
Before constructing bytecode, let's see what exactly is bytecode? —— Bytecode is a large hex value, with and every character have it’s own meaning, unlike SHA256 hashing as we don’t know what exactly is that, and that meaning is how exactly the data is being stored in the EVM
What we need is a function that returns a constant number = 42, so we need to store this number and should return upon calling right.
Why 42? —— I have checked everywhere about it, but the best reason I found is that it's likely a reference to "The Answer to the Ultimate Question of Life, the Universe, and Everything is... 42." by _Douglas Adams, The Hitchhiker’s Guide to the Galaxy_ as this is a well-known pop culture Easter egg in programming circles.
60 2a -> PUSH1 -> Push 1 byte data = 42 into the stack (data to store)
60 00 -> PUSH1 -> Push 1 byte data = 0 into the stack (offset)
52 -> MSTORE -> store the data into the memory with offset and clears the stack
60 20 -> PUSH1 -> Push 1 byte data = 32 into the stack (length of return data)
60 00 -> PUSH1 -> Push 1 byte data = 0 into the stack (offset)
f3 -> RETURN -> return the data that stored in the stackNow our runtime bytecode is finished by combination of all the OPCODES and the respective values, i.e., 602a60005260206000f3. So our first task was completed, and what we need to do is to create a contract, such a way that it returns this byte code upon calling, such that upon calling this byte code it returns the number 42.
69 602a60005260206000f3 -> PUSH10 -> Push 10 byte data = 602a60005260206000f3 into the stack (data to store)
60 00 -> PUSH1 -> Push 1 bytes data = 0 into that stack (offset)
52 -> MSTORE -> store the data with offset and clears the stack
60 0a -> PUSH1 -> Push 1 byte data = 10 into the stack (length of return data)
60 16 -> PUSH1 -> Push 1 byte data = 22 into the stack (offset, because in memory the data stored in the least significant bits)
f3 -> RETURN -> return the data that stored in the stackNow our creation bytecode is 69602a60005260206000f3600052600a6016f3, using this we will create a solver contract using create in assembly.
function attack() external returns (address solver) {
bytes memory byteCode = hex"69602060005260206000f3600052600a6016f3";
assembly {
solver := create(0, add(byteCode, 0x20), mstore(byteCode))
}
}Put the obtained address from the above attack function into the setSolver of MagicNum contract, and that’s it challenge is solved.
So, let's see how exactly we can also create a creation bytecode using different OPCODE, i.e., CODECOPY:
60 0a -> PUSH1 -> Push 1 byte data = 10 into the stack (size)
60 0c -> PUSH1 -> Push 1 byte data = 12 into the stack (offset)
60 00 -> PUSH1 -> Push 1 byte data = 0 into ths stack (dest. offset)
37 -> CODECOPY -> copy code running into memory
60 0a -> PUSH1 -> Push 1 byte data = 10 into the stack
60 00 -> PUSH1 -> Push 1 byte data = 0 into the stack
f3 -> RETURN -> return the data that stored in the stack
60 2a -> PUSH1 -> Push 1 byte data = 42
60 00 -> PUSH1 -> Push 1 byte data = 0 (offset)
52 -> MSTORE -> store the data into the memory
60 20 -> PUSH1 -> Push 1 byte data = 32 (size)
60 00 -> PUSH1 -> Push 1 byte data = 0 (offset)
f3 -> RETURN -> return the data stored in memoryHere, the creation byte code is 600a600c600039600a6000f3602a60005260206000f3, which also perform the same operation that we need, but this is quite confusing, I myself confused while writing it.
Now upon putting the same creation bytecode into the attack function, the solver address get obtained, and upon setting it in the magic number contract, our task completes.
function attack() external returns (address solver) {
bytes memory byteCode = hex"600a600c600039600a6000f3602a60005260206000f3";
assembly {
solver := create(0, add(byteCode, 0x20), mstore(byteCode))
}
}The main difference in between these two ways is that the *data stored in the previous solution was in least significant bytes, but here it is stored in the most significant bytes*
The EVM.codes is wonderful playground, where we can actually see how exactly stack get updated in each step and how exactly the RETURN Opcode retrieve data. Here's how we can point out the difference in between these 2 approaches using this platform,

Whole point of this challenge is to get the 10 bytes of data from the memory irrespective of the method you use.
The reason behind is because, they didn't upload the contract into the etherscan, rather they just deployed their smart contract and executed the attack and left with the stolen amount.
When you deploy smart contract into the blockchain, only bytecode get created under your contract address, and to upload solidity contract, created need to upload manually. But for the black hats, it's a useless job, as their main goal was to steal not to show how the code was rather how code break the codebase
There are various blockchain out there, but have you raised yourself a question that how exactly does they created, and can we also create one such thing? Here is the blog on COSMOS SDK, upon using this toolkit you can create your own custom blockchain, and this blog mainly discuss about the architecture of COSMOS SDK
Author - Sai Nikethan
Intern, Security Researcher @TECHFUND