Level 1 Noob Hacker
Ethernaut 10 Re-entrancy
0 / 100 EXP 100 EXP to Junior Hacker
0%

Challenge 10 / Ethernaut

Re-entrancy

Goal

  1. Drain the Reentrance contract's ETH balance.

Your task

Read Reentrance.sol, then complete Exploit.s.sol.

:~/targets/ethernaut-reentrance$
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

import "openzeppelin-contracts-06/math/SafeMath.sol";

contract Reentrance {
    using SafeMath for uint256;
    mapping(address => uint256) public balances;

    function donate(address _to) public payable {
        balances[_to] = balances[_to].add(msg.value);
    }

    function withdraw(uint256 _amount) public {
        if (balances[msg.sender] >= _amount) {
            (bool result,) = msg.sender.call{value: _amount}("");
            if (result) {
                _amount;
            }
            balances[msg.sender] -= _amount;
        }
    }

    receive() external payable {}
}