Table of Contents
On the 5th of March 2024, WOOFi’s synthetic proactive market making (sPMM) algorithm that controls the pricing of WOOFi Swaps was exploited on Arbitrum for $8.6M.
The exploit involved a series of flash loans. Flash loans are a type of borrowing where funds are borrowed and repaid atomically (within a single transaction). These loans were used to exploit the low liquidity to artificially control the price of WOO. This manipulation allowed the attacker to repay the flash loans at a reduced cost, profiting from the difference.
Cryptocurrency prices derived from on-chain liquidity reserves are influenced by supply and demand dynamics.
When liquidity is low, it becomes easier for a large transaction to impact the price significantly. By executing a sequence of flash loans, the attacker manipulated the price of WOO downwards, allowing them to repurchase it at a lower price and repay the flash loans, making a profit in the process.
The WOOFi team’s post-mortem outlines the following steps that the attacker took:
1. Borrowed USCD & WOO: The attacker borrowed ~10.6 million USDC using a Uniswap flash loan and then borrowed the total pool reserves of WOO tokens using a Trader Joe flash loan.
2. Deposited USDC & Borrowed WOO: The attacker deposited 7 million USDC into Silo and used this to borrow more WOO tokens to bring the total borrowing to ~7.7 million WOO tokens.
3. Swapped Tokens: The attacker performed a series of 4 consecutive swaps on WOOFI:
a) USDC -> WETH: Increase the USDC reserves.
b) USDC -> WOO: Increase the USDC reserves.
c) WOO -> USDC: Manipulated the WOO price.
This attack was predicated on the attacker being able to swap a large base amount of WOO for USDC tokens to ensure that the price was sufficiently small. Therefore, sufficient USDC reserves were required to prevent an underflow from occurring and the transaction reverting. Since the increase in USDC reserves was sufficient as a result of the first two swaps, the attacker was able to swap a large amount of WOO for USDC. Based on the characteristic price calculation in WooPPV2::_calcQuoteAmountSellBase, the price could be manipulated to an extreme value:
The baseAmount was specified so that the numerator of the above calculation was close to 1e18, meaning that the newPrice returned was almost zero (9). With 8 decimals of precision, this yields a price of $0.00000009.
d) USDC -> WOO: bought back the WOO at a minimal price.
This Manipulated the WOOFi Price Oracle. WOOFi’s sPMM algorithm incorrectly adjusted the price of WOO to $0.00000009. Taking advantage of the extreme price adjustment caused by the sPMM, the attacker swapped ~10 million WOO tokens in the same transaction at a minimal cost.
-- To learn more, refer to this guide on price oracle manipulation.
4. Repay the loans: after repaying the loans, the attacker swapped the excess USDC for WETH and then sent their 559 WETH and the additional 2.3 million WOO tokens to another address, after which WOO was swapped for WETH.
5. Repeating the attack: The attacker repeated this attack three times resulting in ~$8.75 million in profits after returning the borrowed funds.
When WooPPV2::swap is called, _sellQuote() is called for swaps 1, 2 and 4 and _sellBase() is called for swap 3.
_sellBase() calls _calcQuoteAmountSellBase() to get the number of tokens to receive and the new price of the token being swapped. Here, the price calculation adjusted the price to an extreme value:
Using approximations, it can be explained why this occurred. Looking at the following section of the newPrice calculation:
δ as (uint256(2) * state.coeff * state.price * baseAmount) / decs.priceDec / decs.baseDec), this calculation becomes 1 - δδ ~ 0 and so it follows the calculation can be approximated as 1 - δ ≈ 1δ is close to 1, it can be approximated that δ ~ 1, and the calculation can be approximated as 1 - δ ≈ 0 meaning that the price calculation will be very close to zero, but still non-zero to avoid underflow.The following code, written using Foundry, is a proof of concept for this hack and recreates the steps described above:
-- The full code including the interfaces can be viewed on GitHub.
The price of WOO was manipulated to an extreme value, close to zero, by swapping a large amount of tokens obtained via various loans. Since the Chainlink oracle for WOO was not set, the fallback logic was not triggered and the extreme price was accepted as valid. This resulted in the attacker stealing ~$8.5 million from the protocol.
Getting your protocol audited significantly decreases the probability of an attack like this happening.