-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dos.sol
59 lines (47 loc) · 1.4 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
/*
Denial of Service
Denial of service by rejecting to accept Ether
Code and demo
Preventative technique (Push vs Pull)
*/
/* contract A {
function foo() public {
(bool sent, ) = msg.sender.call{value: 1 ether}("");
require(sent, "Failed to send Ether");
// do something else
}
}
contract B {
function callFoo(A a) public {
a.foo();
}
} */
contract KingOfEther {
address public king;
uint public balance;
mapping(address => uint) public balances;
// Alice sends 1 Ether (king = alice, balance = 1 ether)
// Bob sends 2 Ether
function claimTherone() 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");
balances[king] += balance;
balance = msg.value;
king = msg.sender;
}
function withdraw() public {
require(msg.sender != king, "Current king cannot withdraw");
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send Ether");
}
}
contract Attack {
function attack(KingOfEther kingOfEther) public payable {
kingOfEther.claimTherone{value: msg.value}();
}
}