# 智能合约黑客攻击 Ethernaut: 18. MagicNumber **Published by:** [Leek DEV](https://paragraph.com/@leekdev/) **Published on:** 2023-09-28 **URL:** https://paragraph.com/@leekdev/ethernaut-18-magicnumber ## Content Ethernaut 是一个由 OpenZeppelin 基于 Solidity 编程语言开发的对抗游戏,每个关卡都有需要被 Hack 的智能合约。教程GitHub - 攻击代码Bilibili - 视频教程YouTube - 视频教程TikTok - ….题目设计一个合约,有一下要求地址存储在 solver 里提供一个 whatIsTheMeaningOfLife() 方法返回 42合约 opcode 小于10Hack思路合约 opcode 小于10只能用汇编了,可以参考代码。 interface IMagicNumHack { function setSolver(address _solver) external; } contract MagicNumHack { IMagicNumHack public exploitInst; constructor(address _target) { exploitInst = IMagicNumHack(_target); bytes memory code = hex"600a600c600039600a6000f3602a60505260206050f3"; address addr; assembly { addr := create(0, add(code, 0x20), mload(code)) if iszero(extcodesize(addr)) { revert(0, 0) } } exploitInst.setSolver(addr); } function attack() external { } } Hack案例…防范思路…参考资料Simple Bytecode Contract ## Publication Information - [Leek DEV](https://paragraph.com/@leekdev/): Publication homepage - [All Posts](https://paragraph.com/@leekdev/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@leekdev): Subscribe to updates - [Twitter](https://twitter.com/LeekDEV): Follow on Twitter