-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreentrancy.sol
33 lines (25 loc) · 1.09 KB
/
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
pragma solidity ^0.6.0;
import 'https://github.com/ConsenSysMesh/openzeppelin-solidity/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); // okay, to make the balance[msg.sender] >= amount, we need to donate to this contract first, then create the fallback function to attack.
}
function balanceOf(address _who) public view returns (uint balance) {
return balances[_who];
}
function withdraw(uint _amount) public {
if(balances[msg.sender] >= _amount) { // but here it has the if statement, we need to bypass this first.
(bool result, bytes memory data) = msg.sender.call.value(_amount)(""); // it uses the low-level function call -> this triggers fallback fucntion
if(result) {
_amount;
}
balances[msg.sender] -= _amount;
}
}
function balanceOfContract() public view returns(uint){
return address(this).balance;
}
fallback() external payable {}
}