Guess the secret number
pragma solidity ^0.4.21;
contract GuessTheSecretNumberChallenge {
bytes32 answerHash = 0xdb81b4d58595fbbbb592d3661a34cdca14d7ab379441400cbfa1b78bc447c365;
function GuessTheSecretNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
if (keccak256(n) == answerHash) {
msg.sender.transfer(2 ether);
}
}
}
Goal
Guess the number to win ETH
Exploit
This CTF is a bit tricker than the previus one (Guess the number).
Instead of storing the number in the contract we are using hash of it, to be more precise it is keccak256 hash of the number.
Guessing a hash of a number would be nearly impossible if we are working with numbers that can hold extremly large values (such as uint256).
But in our case we can see inside guess(uint8 n) function, that the n parameter is of type uint8.
What this means is that the minimum value uint8 variable can have is 0, and the maximum value is 2^8 - 1, which is 255.
So we can brute-force, and calculate the keccak256 hashes for every number from 0 - 255 and get the correct number with the help of some external script.
In our case the correct number is 170.