{"author":{"address":null,"user":"https://learnblockchain.cn/people/21890"},"content":{"body":"## 基本信息\r\n2025.1.5日SorStaking合约遭受逻辑漏洞攻击，只损失了43K。虽然金额不多，但是警钟长鸣，我还是写一下分析，给自己提个醒。\r\n首次纰漏的X：\r\nhttps://x.com/TenArmorAlert/status/1875582709512188394\r\n其中一笔攻击交易：\r\nhttps://app.blocksec.com/explorer/tx/eth/0x03ddae63fc15519b09d716b038b2685f4c64078c5ea0aa71c16828a089e907fd?line=2\r\n被害合约代码：\r\nhttps://vscode.blockscan.com/ethereum/0x5d16b8ba2a9a4eca6126635a6ffbf05b52727d50\r\n## 黑客攻击步骤\r\n黑客的攻击步骤很简单，他在14天前存入了一笔资金。然后14天后开始withdraw之前存入的钱。每次withdraw只提取1个token，重复withdraw700次，截图如下：\r\n\r\n![image.png](https://img.learnblockchain.cn/attachments/2025/01/A2sEBbSc677f9a12af5fc.png)\r\n在withdraw可以看到，虽然黑客withdraw amount为1，但是在内部transfer时候，sor合约给黑客转了6143个SOR token。\r\n\r\n![image.png](https://img.learnblockchain.cn/attachments/2025/01/sVPEuzqj677f9a2a6d470.png)\r\n## 代码漏洞\r\n为什么withdraw 1个token，可以得到6143个token，深入withdraw代码初见端倪：\r\n![image.png](https://img.learnblockchain.cn/attachments/2025/01/7MGi4EZV677f9a4427a57.png)\r\n可以看到withdraw的时候，不仅返回用户需要提取的_amount，还要计算用户是否有奖励rewardAmount。如果rewardAmount不为零，则返回用户_amount+rewardAmount。计算rewardAmount的代码在getPendingRewards方法中，如下图：\r\n\r\n![image.png](https://img.learnblockchain.cn/attachments/2025/01/0xHPNHVD677f9a52a17ea.png)\r\n在234行可以看到，rewardAmount最终计算方法是：遍历用户所有的质押，每一次质押的所有资产deposit.amount乘以一个系数（deposit.rewardBps/10000）,这个系数在1%左右，是预先设置的定值。再将所有计算出来的reward相加，得到最终的rewardAmount。\r\n所以漏洞就在于，假设我的仓位只有一次deposit，且deposit了10000个token，即使我只withdraw1个token，我的rewardAmount依然是10000*1%=100. 每次我withdraw1个token，都会获得101个token。\r\n我猜这个地方的逻辑原本是rewardAmount=withdrawAmount*1%，但是不知出于什么原因，把depositAmount错当成了要withdrawAmount。\r\n## 总结\r\n1. 作为合约审计人员一定要对资金计算的逻辑多看，理解透，多看十遍也值得。\r\n2. 如果是仿盘，对已有项目进行改造再发布的情况，往往更可怕。因为逻辑改动可能由于对代码理解不足造成重大逻辑漏洞。","title":"SorStaking合约攻击分析---代码逻辑漏洞"},"history":null,"timestamp":1736415924,"version":1}