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

Greeter(1)

1.question

源码

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

contract Greeter {
string greeting;

constructor(string memory _greeting) public {
greeting = _greeting;
}

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
greeting = _greeting;
}

function isSolved() public view returns (bool) {
string memory expected = "HelloChainFlag";
return keccak256(abi.encodePacked(expected)) == keccak256(abi.encodePacked(greeting));
}
}

📌 成功i调用isSolved()

2. analysis

emmm,这题我找不到源码,在我同学的博客找到的,我也不知道为什么这么简单。

3. solve

攻击合约

1
2
3
4
5
6
7
8
9
10
11
12
13
14
contract Hacker {

Greeter greeter;

constructor(address _greeter) {
greeter = Greeter(_greeter);
}

function attack() public {
string memory _greeting = "HelloChainFlag";
greeter.setGreeting(_greeting);
require(greeter.isSolved(), "you don't solve");
}
}

image-20230823222252535

插曲

好嘛,我就说嘛,原来是我同学题目错了。。。。

Greeter(2)

1. question

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Greeter {
uint256 public x;
uint256 public y;
bytes32 public root;
mapping(bytes32 => bool) public used_leafs;

constructor(bytes32 root_hash) {
root = root_hash;
}

modifier onlyGreeter() {
require(msg.sender == address(this));
_;
}

function g(bool a) internal returns (uint256, uint256) {
if (a) return (0, 1);
assembly {
return(0, 0)
}
}

function a(uint256 i, uint256 n) public onlyGreeter {
x = n;
g((n <= 2));
x = i;
}

function b(
bytes32[] calldata leafs,
bytes32[][] calldata proofs,
uint256[] calldata indexs
) public {
require(leafs.length == proofs.length, "Greeter: length not equal");
require(leafs.length == indexs.length, "Greeter: length not equal");

for (uint256 i = 0; i < leafs.length; i++) {
require(
verify(proofs[i], leafs[i], indexs[i]),
"Greeter: proof invalid"
);
require(used_leafs[leafs[i]] == false, "Greeter: leaf has be used");
used_leafs[leafs[i]] = true;
this.a(i, y);
y++;
}
}

function verify(
bytes32[] memory proof,
bytes32 leaf,
uint256 index
) internal view returns (bool) {
bytes32 hash = leaf;

for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];

if (index % 2 == 0) {
hash = keccak256(abi.encodePacked(hash, proofElement));
} else {
hash = keccak256(abi.encodePacked(proofElement, hash));
}

index = index / 2;
}

return hash == root;
}

function isSolved() public view returns (bool) {
return x == 2 && y == 4;
}
}

📌 成功i调用isSolved()

2. analysis

3. solve

评论



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