DeFi平台Grim Finance攻击事件复盘

事件背景

Grim Finance 是一种基于 Fantom Opera区块链的计算收益优化器。 现在,Grim Finance 是数百万美元漏洞的最新受害者,该漏洞最终损失了价值 3000 万美元的代币。 DeFi 协议证实了这次攻击,根据推文,在金库合约中发现了漏洞利用,因此所有金库和存入的资金都处于危险之中。 Grim 称自己为Compound收益率优化器,这意味着它承诺从流动性提供者代币中榨取更多价值,以便用户在将它们锁在金库中时从去中心化交易所获得收益。 该平台在其协议文档中吹捧,因为它可以帮助用户轻松获得更多奖励。

据 Grim Finance 在北京时间2021年12月19日 Twitter 发布的消息报道称,Grim Finance官方平台被外部攻击者利用,攻击者盗币价值超过3000万美元 Grim Finance Twitter

攻击者信息

此次攻击发生在Fantom链,主要攻击信息如下:

  • 攻击者钱包地址

https://ftmscan.com/address/0xdefc385d7038f391eb0063c2f7c238cfb55b206c

  • 攻击者合约地址

https://ftmscan.com/address/0xb08ccb39741d746dd1818641900f182448eb5e41#code

  • 攻击交易

https://ftmscan.com/tx/0x19315e5b150d0a83e797203bb9c957ec1fa8a6f404f4f761d970cb29a74a5dd6

  • GrimBoostVault合约

https://ftmscan.com/address/0x660184ce8af80e0b1e5a1172a16168b15f4136bf#code

攻击流程

0x19315e5b150d0a83e797203bb9c957ec1fa8a6f404f4f761d970cb29a74a5dd6

第一步:首先通过闪电贷借款30枚BTC和92万枚WFTM,并通过PancakePair将借款添加流动性获取0.0476枚Spirit LP。

1
2
3
4
5
6
From 0x20dd72ed959b6147912c2e529f0a0c651c33c9ceTo 0xb08ccb39741d746dd1818641900f182448eb5e41 For 937,830 ($1,519,284.60) Wrapped Fant... (WFTM)
From 0x20dd72ed959b6147912c2e529f0a0c651c33c9ceTo 0xb08ccb39741d746dd1818641900f182448eb5e41 For 30 ($1,475,460.00) Bitcoin (BTC)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x279b2c897737a50405ed2091694f225d83f2d3ba For 30 ($1,475,460.00) Bitcoin (BTC)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x279b2c897737a50405ed2091694f225d83f2d3ba For 923,575.591715867110610192 ($1,496,192.46) Wrapped Fant... (WFTM)
From Null Address: 0x000...000To 0x5cec66f552b1d3406f65eb1a4a4914f448f98e9c For 0.000000030478124368 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 0.047610132146053993 Spirit LPs (SPIRIT...)

第二步:将获取的Spirit LP质押后获得GB-BTC-FTM。

1
2
3
4
5
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x660184ce8af80e0b1e5a1172a16168b15f4136bf For 0.047610132146053993 Spirit LPs (SPIRIT...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0.047610132146053993 Spirit LPs (SPIRIT...)
From 0x905f8441df2d7e49c52c4bf480fbdd272188811dTo 0x928144cd396ac88c84d60086e3db20555c56322c For 0.047610132146053993 Spirit LPs (SPIRIT...)
From 0x928144cd396ac88c84d60086e3db20555c56322cTo 0xdccafce93e6e57f0464b4639d4afd7b9ad006f61 For 0.047610132146053993 Spirit LPs (SPIRIT...)
From 0xdccafce93e6e57f0464b4639d4afd7b9ad006f61To 0x5cec66f552b1d3406f65eb1a4a4914f448f98e9c For 0 Spirit LPs (SPIRIT...)

第三步:通过多次质押存款获取GB-BTC-FTM(攻击的主要部分,通过未验证的Token不断质押,提高质押奖励的差值)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 0.032376253762624097 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 0.11472715832368394 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 0.406542429322769007 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 1.440606995366844921 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 5.10487556872493898 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 18.089426648611236895 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 64.100946647994200627 GB-BTC-FTM (GB-BTC...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0 Spirit LPs (SPIRIT...)
From Null Address: 0x000...000To 0xb08ccb39741d746dd1818641900f182448eb5e41 For 227.145472379272462836 GB-BTC-FTM (GB-BTC...)

第四步:将获取的GB-BTC-FTM归还,获得0.0663枚Spirit LP(已通过多次质押差值获利)。

1
2
3
4
5
6
From 0xb08ccb39741d746dd1818641900f182448eb5e41To Null Address: 0x000...000 For 316.434974081378761303 GB-BTC-FTM (GB-BTC...)
From 0xdccafce93e6e57f0464b4639d4afd7b9ad006f61To 0x928144cd396ac88c84d60086e3db20555c56322c For 0.06632538511043804 Spirit LPs (SPIRIT...)
From 0x928144cd396ac88c84d60086e3db20555c56322cTo 0x905f8441df2d7e49c52c4bf480fbdd272188811d For 0.06632538511043804 Spirit LPs (SPIRIT...)
From 0x905f8441df2d7e49c52c4bf480fbdd272188811dTo 0x660184ce8af80e0b1e5a1172a16168b15f4136bf For 0.06632538511043804 Spirit LPs (SPIRIT...)
From 0x660184ce8af80e0b1e5a1172a16168b15f4136bfTo 0xb08ccb39741d746dd1818641900f182448eb5e41 For 0.06632538511043804 Spirit LPs (SPIRIT...)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x279b2c897737a50405ed2091694f225d83f2d3ba For 0.06632538511043804 Spirit LPs (SPIRIT...)

第五步:解除流动性获取41枚BTC和128万枚WFTM,并归还闪电贷借款及手续费,随后将获利的11枚BTC和36万枚WFTM转至攻击者钱包地址。

1
2
3
4
5
6
7
8
9
From 0x279b2c897737a50405ed2091694f225d83f2d3baTo Null Address: 0x000...000 For 0.06632538511043804 Spirit LPs (SPIRIT...)
From 0x279b2c897737a50405ed2091694f225d83f2d3baTo 0xb08ccb39741d746dd1818641900f182448eb5e41 For 1,286,627.531535482533090765 ($2,084,336.60) Wrapped Fant... (WFTM)
From 0x279b2c897737a50405ed2091694f225d83f2d3baTo 0xb08ccb39741d746dd1818641900f182448eb5e41 For 41.79281727 ($2,055,454.34) Bitcoin (BTC)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x20dd72ed959b6147912c2e529f0a0c651c33c9ce For 938,111.349 ($1,519,740.39) Wrapped Fant... (WFTM)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To 0x20dd72ed959b6147912c2e529f0a0c651c33c9ce For 30.009 ($1,475,902.64) Bitcoin (BTC)
From 0x20dd72ed959b6147912c2e529f0a0c651c33c9ceTo 0xc6920d3a369e7c8bd1a22dbe385e11d1f7af948f For 281.349 ($455.79) Wrapped Fant... (WFTM)
From 0x20dd72ed959b6147912c2e529f0a0c651c33c9ceTo 0xc6920d3a369e7c8bd1a22dbe385e11d1f7af948f For 0.009 ($442.64) Bitcoin (BTC)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To Grim Finance Exploiter 1 For 11.78381727 ($579,551.70) Bitcoin (BTC)
From 0xb08ccb39741d746dd1818641900f182448eb5e41To Grim Finance Exploiter 1 For 362,770.590819615422480573 ($587,688.36) Wrapped Fant... (WFTM)

通过同样的攻击手法继续进行了8次攻击,9笔攻击交易共获取累计获取26万枚BTC,1865万枚,75万枚DAI,75万枚USDC,13万枚BOO,价值约3000万美元。

漏洞细节

暂时没懂,后面更新


2021年12月20日更新

通过查询交易详情发现 depositFor函数 depositFor 函数是没有对传入的 token 进行检查的,那么这里攻击者就在攻击过程中,在 token 参数的地方传入了自己部署的恶意地址,由于depositFor方法中会用到token合约地址中的safeTransferFrom方法,token合约地址为用户可控后,safeTransferFrom方法随之可控。

现在明确攻击者的攻击路径,首先创建Token合约,并在合约中的safeTransferFrom方法中写入重新调用GrimBoostVault.depositFor方法的逻辑,在进行多次重入铸造大量的GB-TOKEN0-TOKEN1

最后传入一笔正常的Token代币SLP TOKEN质押,之后虽然会进入到正常流程,但是由于之前的多次重入,导致获取合约余额的前后差值过大,最终铸造了大量的证明代币GB-BTC-FTM

例如:攻击者进行了8次重入,最后一次传入资金为100,那么最终得到的差值就是100*8,铸币时的计算值也是800。

最后用先前获取到的GB-TOKEN0-TOKEN1拿到的SLP-TOKEN,通过SLP-TOKEN移除流动性并获取大量的WBTC/WFTM,然后归还闪电贷并通过调用GrimBoostVault.withdrawAll提取资金完成获利。 withdrawAll函数

总结

通过此次攻击事件来看,攻击者主要利用GrimBoostVault合约depositFor方法未进行Token地址检查,通过多次重入造成巨大的合约余额差值,铸造大量资金,随后通过withdrawAll方法取走获利的资金,所以,关注方法传参的可控性是极其重要