抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Guess the secret number

1. 题目

  • 1.1 Putting the answer in the code makes things a little too easy.

    This time I’ve only stored the hash of the number. Good luck reversing a cryptographic hash!

  • 1.2 题目代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// SPDX-License-Identifier: MIT
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);
}
}
}

2. 分析

  • 2.1 这是我第一次感觉自己能想出来的题目了。。。。
  • 2.2 反正 uint8 的范围是 0-256 嘛,直接 循环一个一个蛮力法,一个一个试出来
  • 2.3 写一个Hack 合约来计算,哈希值为 :0xdb81b4d58595fbbbb592d3661a34cdca14d7ab379441400cbfa1b78bc447c365 的数字
  • Hack 合约代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
contract Hack {
uint8 public number;
bytes32 answer = 0xdb81b4d58595fbbbb592d3661a34cdca14d7ab379441400cbfa1b78bc447c365;

function f() public returns(uint8) {
for (uint8 i = 0; i <= 256; i++) {
if (keccak256(i) == answer) {
number = i;
break;
}
}
return number;
}
}
  • 运行求出来的结果是 :170

3. 解题

  • 3.1 部署合约
  • 3.2 将 170 作为参数,调用guss() 函数
  • 3.3 同时前两步操作都需要将 msg.value 设置为 1 ether
  • ![image-20240412144923642](Guess the secret number/image-20240412144923642.png)

评论



政策 · 统计 | 本站使用 Volantis 主题设计