05.Predict the future
2023-06-23 20:30:06
# 01.Capturetheether CTF
Predict the future
topic
1 | pragma solidity ^0.4.21; |
analyse
This time the answer needs to be locked in first and can only be checked after a certain number of blocks have settled. However, the answer is only in the range of 0 to 9 because of the modulo 10 instruction:
1 | uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now)) % 10; |
- if someone locked the answer, the other can’t guess again until he checks the answer
- so we can locked the answer, and then check that whether the correct answer is the same as we locked or not. If it does, we call
settle()
and get money, otherwise we try again in the next block. No one can stop us to do this because we are alway the guesser until we callsettle()
- So we should lock an answer to be the guesser, forecast the correct number and finally call settle() with the right number. u can use a loop to do this or forecast one by one.
- we use
require
to prevent the tx from continuing, so if the right number is not the same as we locked it would revert and return the ETH.
PS: because the correct number depends on block.blockhash(block.number - 1)
, it means it will get the right number depends on the previous block hash, so we could only try once in each block.
solution
- assume that we lock the number “1”, of course u can select “0~9”
- call attack() again an again until it calls successfully.
if u dont want to call it one by one, u can use loop logic.
1 | function attack() public { |