Back to Blog

#6:Hundred Finance事件:催化脆弱分叉协议的精准漏洞利用浪潮

Code Auditing
February 16, 2024
6 min read

2023年4月16日,作为Compound V2的一个分支,Hundred Finance遭到攻击,损失约740万美元。攻击涉及两个主要问题:

  • 精度损失问题(错误的舍入问题);
  • 空头市场,这使得黑客能够操纵汇率

根据DeFiLlama的数据,Compound V2是分叉最多的借贷协议,拥有超过100个分叉。其合约经OpenZeppelin审计并经过实战检验,被认为是安全的。然而,Hundred Finance的攻击凸显了精度损失,尤其是在低流动性环境下,会严重影响DeFi协议的安全性,并引发了Midas和Radiant等著名分叉的类似漏洞利用浪潮。

为了更深入地了解,鼓励您查阅完整的分析。下面,我们对此次事件进行简要介绍,并将其列为2023年十大安全事件之一。

背景

Hundred Finance概述

Hundred Finance是Compound v2的一个分支,在多个主网上运行,并利用Chainlink预言机。与传统金融借贷实践不同,Compound和Aave等DeFi借贷协议不允许超额抵押借款。 简单来说,如果您存入价值100美元的Token A,您最多只能借入价值低于100美元的资产。根据大多数协议的风险控制系数,这个比例通常在50%到80%之间。

对于借贷协议而言,常见的攻击手段包括价格操纵和重入攻击。有趣的是,Hundred Finance曾在2022年3月遭受过一次重入攻击,但此次事件开辟了新的攻击向量。

hToken

从Compound和Aave分叉出来的借贷协议为每种底层代币(抵押品)创建一个相应的记账代币。对于Hundred Finance:

  • USDC对应hUSDC
  • WBTC对应hWBTC

底层代币与hToken之间的汇率称为exchangeRate

  • 存入资产时,用户应调用hToken的mint()
  • 提取资产时,用户应调用hToken的redeem()

exchangeRate

以下是计算exchangeRate的公式: image

其中:

  • getCash():此hToken合约持有的底层代币余额。这是一个可以被操纵的关键参数。请牢记这一点。
  • totalBorrows():市场上当前已借出的底层代币数量,也是市场提供者的利息累积基础。
  • totalReserves():每个hToken合约中的记账项,代表一部分历史利息,以现金形式预留,可以通过协议治理提取或转移。
  • totalSupply():此hToken市场上当前流通的代币数量。

注意:hToken与底层资产(例如dai与hDai或eth与hEth)之间的exchangeRate初始值为0.020,并以复合市场利率的速率增加。

清算

为防止坏账,借贷协议允许任何用户清算其他用户的债务。以下面的例子来说明:

  1. Alice存入了价值100美元的BTC,并借入了价值70美元的ETH。
  2. 如果ETH价格上涨或BTC价格下跌,Alice的资产可能达到清算阈值。
  3. Bob可以用一定数量的ETH来清算Alice的BTC,将Alice的债务恢复到健康水平,以确保协议资金的安全(协议会奖励发起清算的用户的)。

清算并非本次攻击的重点,但它确实牵涉其中。 这里我们只需要知道,任何用户都可以用一种代币清算另一个用户的债务,这将减少相应的hToken。

漏洞

精度损失问题

黑客通过redeem()提取抵押品时,计算要扣除的hToken数量导致结果为1.99999992(非常接近2但小于2)。truncate()中转换为整数时,使用向下取整导致最终结果为1。

exchangeRate

如前所述,exchangeRate的计算涉及getCash(),它指的是hToken合约持有的底层余额。通过直接将底层代币转入合约(不通过mint,仅传输),黑客可以操纵exchangeRate 然而,需要注意的是,仅凭此exchangeRate问题本身并不会危害协议安全;黑客无法从中单独获利。 在本次攻击中,它主要被用来放大黑客的收益,使其能够迅速耗尽资金池。否则,攻击将从一次决定性的打击变为一系列小规模的“捅刺”,需要多次迭代才能产生显著影响。

简而言之,精度损失是本次攻击的关键问题。

攻击过程

以下是攻击交易,我们将使用Phalcon Explorer来分析这次复杂的交易。

交易:0x6e9ebcdebbabda04fa9f2e3bc21ea8b2e4fb4bf4f4670cb8483e2f0b2604f451

  1. 通过Flashloan从Aave V3借入500 WBTC。

  2. Redeem所有先前获得的hWBTC,将hWBTC的totalSupply重置为0。

在此之前的目标是利用闪电贷准备准备金,并将hWBTC重置为新市场。

  1. Create第二个攻击合约(下称“攻击合约2”),并将所有WBTC(500.30063816 WBTC)转入攻击合约2。

  2. 使用4 WBTC Mint() hWBTC,产生200 hWBTC。

  3. Redeem() 199.99999998 hWBTC,剩余hWBTC总量为0.00000002(2 wei hWBTC)。

  4. 将所有WBTC(500.30063816 WBTC)转入hWBTC。请注意,直接转账不会增加hWBTC;这可以视为向资金池捐赠WBTC。此步骤的主要目的是操纵前面提到的exchangeRate。此时,hWBTC的totalSupply仍为2 wei hWBTC,但现在有500.30064194 WBTC,使得exchangeRate是原始汇率的数百倍。

  5. 从hETH市场借入1021 Ether。

  6. Redeem() 50030063815 WBTC,计算后应扣除1.9999992 hBTC,但由于精度损失,仅扣除1 hBTC,造成了巨大的精度损失(近50%)。 此时,黑客拥有500 WBTC + 1021 Ether,成功获利1021 Ether。

  7. 攻击者liquidate()剩余的hWBTC,将其totalSupply重置为0,为继续攻击其他市场做准备。 考虑到hWBTC中的几乎所有WBTC都已提取,黑客仅用0.000002 Ether就完成了此操作。

  8. 继续攻击其他市场,耗尽整个协议。

  9. 向Aave偿还闪电贷。

安全建议

借贷协议的缓解措施

这个问题普遍存在,尤其对于Compound和Aave的分叉。一种主动的方法是在启动新市场时,铸造一些记账代币,以确保totalSupply永远不会为0。

精度损失问题的缓解措施

为了更好地规避因精度损失导致的一系列问题,设置最小值是实践中有效的解决方法。 这种策略有助于避免在处理极小值时因精度损失而产生重大影响。

阅读本系列其他文章:

Sign up for the latest updates
Newsletter - April 2026
Security Insights

Newsletter - April 2026

In April 2026, the DeFi ecosystem experienced three major security incidents. KelpDAO lost ~$290M due to an insecure 1-of-1 DVN bridge configuration exploited via RPC infrastructure compromise, Drift Protocol suffered ~$285M from a multisig governance takeover leveraging Solana's durable nonce mechanism, and Rhea Finance incurred ~$18.4M following a business logic flaw in its margin-trading module that allowed circular swap path manipulatio

~$7.04M Lost: GiddyDefi, Volo Vault & More | BlockSec Weekly
Security Insights

~$7.04M Lost: GiddyDefi, Volo Vault & More | BlockSec Weekly

This BlockSec weekly security report covers eight attack incidents detected between April 20 and April 26, 2026, across Ethereum, Avalanche, Sui, Base, HyperLiquid, and MegaETH, with total estimated losses of approximately $7.04M. The highlighted incident is the $1.3M GiddyDefi exploit, where the attacker did not break any cryptography or use a flash loan but simply replayed an existing on-chain EIP-712 signature with the unsigned `aggregator` and `fromToken` fields swapped out for a malicious contract, demonstrating how partial signature coverage turns any historical signature into a generic permit. Other incidents include a $3.5M Volo Vault operator key compromise on Sui, a $1.5M Purrlend privileged-role takeover, a $413K SingularityFinance oracle misconfiguration, a $142.7K Scallop cross-pool index injection, a $72.35K Kipseli Router decimal mismatch, a $50.7K REVLoans (Juicebox) accounting pollution, and a $64K Custom Rebalancer arbitrary-call exploit.

The Decentralization Dilemma: Cascading Risk and Emergency Power in the KelpDAO Crisis
Security Insights

The Decentralization Dilemma: Cascading Risk and Emergency Power in the KelpDAO Crisis

This BlockSec deep-dive analyzes the KelpDAO $290M rsETH cross-chain bridge exploit (April 18, 2026), attributed to the Lazarus Group, tracing a causal chain across three layers: how a single-point DVN dependency enabled the attack, how DeFi composability cascaded the damage through Aave V3 lending markets to freeze WETH liquidity exceeding $6.7B across Ethereum, Arbitrum, Base, Mantle, and Linea, and how the crisis forced decentralized governance to exercise centralized emergency powers. The article examines three parameters that shaped the cascade's severity (LTV, pool depth, and cross-chain deployment count) and provides an exclusive technical breakdown of Arbitrum Security Council's forced state transition, an atomic contract upgrade that moved 30,766 ETH without the holder's signature.

Best Security Auditor for Web3

Validate design, code, and business logic before launch. Aligned with the highest industry security standards.

BlockSec Audit