Skip to content

11. Elevator

r1oga edited this page Oct 27, 2022 · 1 revision

Target

Reach the top of the Building.

Weakness

The Elevator never implements the isLastFloor() function from the Building interface. An attacker can create a contract that implements this function as it pleases him.

Solidity Concepts

Interfaces are similar to abstract contracts, but they cannot have any functions implemented. Contracts need to be marked as abstract when at least one of their functions is not implemented.

Contract Interfaces specifies the WHAT but not the HOW. Interfaces allow different contract classes to talk to each other. They force contracts to communicate in the same language/data structure. However interfaces do not prescribe the logic inside the functions, letting the developer to implement it. Interfaces are often used for token contracts. Different contracts can then work with the same language to handle the tokens.

Interfaces are also often used in conjunction with Inheritance.

When a contract inherits from other contracts, only a single contract is created on the blockchain, and the code from all the base contracts is compiled into the created contract. Derived contracts can access all non-private members including internal functions and state variables. These cannot be accessed externally via this, though. They cannot inherit from other contracts but they can inherit from other interfaces.

Hack

  1. Write a malicious attacker contract that will implement the isLastFloor function of the Building interface
  2. Implement isLastFloor Note that isLastFloor is called 2 times in goTo. The first time it has to return False, but the second time it has to return True
  3. Invoke goTo() from the malicious contract so that the malicious version (Building(msg.sender)) of the isLastFloor function is used in the context of our level’s Elevator instance.

Takeaways

Interfaces guarantee a shared language but not contract security. Just because another contract uses the same interface, doesn’t mean it will behave in the same way.

Clone this wiki locally