# 合约攻防比赛--- PARADIGM CTF 2022 《Rescue》 **Published by:** [shaneson.eth](https://paragraph.com/@shaneson-eth/) **Published on:** 2022-09-17 **URL:** https://paragraph.com/@shaneson-eth/paradigm-ctf-2022-rescue ## Content 背景 最近大部分精力都在搞Apots和Move,之前承诺的要过一遍CTF 2022的任务清单就一直没有时间继续跟进,刚解完最简单的一题Rescue。代码: https://github.com/shanxuanchen/ParadigmCTF2022 我们先看一下题目。 题目 小明不小心给mcHelper的合约打了10WETH,我们需要把mcHelper的钱移走即可,使得mcHelper的weth合约余额是0,那么就可以拿flag。 分析 mcHelper有三个重要的函数: 1)swapTokenPoolToken,这是唯一一个external方法,也是我们发起攻击的唯一入口。 授权和转账都是标准的ERC20方法,我们可以看到调用了swapTokenPoolToken之后,mcHelper会从用户里读amountIn数量的tokenIn,然后1/2的tokenIn,和1/2的tokenOut组成交易对添加流动性。 2)addLiquility和swap。这里只是调用sushiwap添加流动功能性和兑换。 解题 这个题目的解法是比较简单的。 我们先把价值10WETH的DAI,打给mcHelper,这个时候mcHelper就价值的10WETH的DAI和WETH。 我们把这些资产组成交易对,调用swapTokenPoolToken来触发添加流动性 因为不能在mcHepler上留有WETH,所以DAI要多给一点,来保证WETH的余额为0。 本地环境求解: 启动本地节点fork npx hardhat node --fork https://rpc.ankr.com/eth 运行攻击脚本 npx hardhat test scripts/solve/solve-rescue.js --network localhost 经过验证WETH的余额为0,题目解~~ ## Publication Information - [shaneson.eth](https://paragraph.com/@shaneson-eth/): Publication homepage - [All Posts](https://paragraph.com/@shaneson-eth/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@shaneson-eth): Subscribe to updates