# 合约攻防比赛--- PARADIGM CTF 2022 《Rescue》

By [shaneson.eth](https://paragraph.com/@shaneson-eth) · 2022-09-17

---

背景
--

最近大部分精力都在搞Apots和Move，之前承诺的要过一遍CTF 2022的任务清单就一直没有时间继续跟进，刚解完最简单的一题Rescue。代码：

[https://github.com/shanxuanchen/ParadigmCTF2022](https://github.com/shanxuanchen/ParadigmCTF2022)

我们先看一下题目。

题目
--

小明不小心给mcHelper的合约打了10WETH，我们需要把mcHelper的钱移走即可，使得mcHelper的weth合约余额是0，那么就可以拿flag。

![](https://storage.googleapis.com/papyrus_images/e91bdd8761e8808c3f6a42c13c27c3b5238f9dd9ba89cdfa1075c02bc98c8a7a.png)

分析
--

mcHelper有三个重要的函数：

1）swapTokenPoolToken，这是唯一一个external方法，也是我们发起攻击的唯一入口。

授权和转账都是标准的ERC20方法，我们可以看到调用了swapTokenPoolToken之后，mcHelper会从用户里读amountIn数量的tokenIn，然后1/2的tokenIn，和1/2的tokenOut组成交易对添加流动性。

![](https://storage.googleapis.com/papyrus_images/d715f17a602ce7a3654ed0469aad9160afd3000cdf19af2054f45bdd49db6867.png)

2）addLiquility和swap。这里只是调用sushiwap添加流动功能性和兑换。

![](https://storage.googleapis.com/papyrus_images/833db0bf63589e302802f39dbf54a301bf05a5f319d1a06bc6211479e3ba3ea1.png)

解题
--

这个题目的解法是比较简单的。

1.  我们先把价值10WETH的DAI，打给mcHelper，这个时候mcHelper就价值的10WETH的DAI和WETH。
    
2.  我们把这些资产组成交易对，调用swapTokenPoolToken来触发添加流动性
    
3.  因为不能在mcHepler上留有WETH，所以DAI要多给一点，来保证WETH的余额为0。
    

本地环境求解：

1.  启动本地节点fork
    

    npx hardhat node --fork https://rpc.ankr.com/eth
    

2.  运行攻击脚本
    

    
    npx hardhat test scripts/solve/solve-rescue.js --network localhost
    

经过验证WETH的余额为0，题目解～～

![](https://storage.googleapis.com/papyrus_images/8b5d7acf91c25c4fdf196b8cfbe400606ff0f69a242abfb950506b9aa0ee44b0.png)

---

*Originally published on [shaneson.eth](https://paragraph.com/@shaneson-eth/paradigm-ctf-2022-rescue)*
