03.Guess the random number
2023-06-23 20:27:14 # 01.Capturetheether CTF

Guess the random number

topic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pragma solidity ^0.4.21;

contract GuessTheRandomNumberChallenge {
uint8 answer;

function GuessTheRandomNumberChallenge() public payable {
require(msg.value == 1 ether);
answer = uint8(keccak256(block.blockhash(block.number - 1), now));
}

function isComplete() public view returns (bool) {
return address(this).balance == 0;
}

function guess(uint8 n) public payable {
require(msg.value == 1 ether);

if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}

analyses

the correct number is stored in the blockchain state (storage). we can get it off-chain.

solution

call ethers’s function getStorageAt() to get the “answer” and then call guess(uint8)