Appearance
Puppet V3: Making the Oracle Dance to Your Tune
In the ever-evolving world of Decentralized Finance (DeFi), security is paramount. Lending protocols, in particular, rely heavily on robust price oracles to maintain solvency and prevent malicious exploitation. The Puppet V3 challenge from Damn Vulnerable DeFi introduces us to a seemingly fortified lending pool, one that has moved beyond the perils of spot price oracles and embraced the supposed invincibility of Uniswap V3's Time-Weighted Average Price (TWAP) oracle. But as we'll discover, even the most sophisticated mechanisms can be made to dance to a clever attacker's tune.
The Stage is Set: A Lending Pool, a TWAP Oracle, and a Million Tokens
The premise of Puppet V3 is compelling. A lending pool, fresh off its previous iteration (Puppet V2, which likely fell victim to a spot price attack), has been upgraded. It now leverages Uniswap V3 to query the time-weighted average price of DVT against WETH. The challenge description proudly states, "This time the pool queries the time-weighted average price of the asset, with all the recommended libraries." This immediately raises our hacker's antennae – a boast like that usually means a subtle vulnerability is lurking.
Here's the initial setup:
- Uniswap V3 Pool: Holds 100 WETH and 100 DVT. At the start, the price is a stable 1 WETH = 1 DVT.
- Lending Pool: A tempting bounty of 1,000,000 DVT tokens.
- Attacker (You!): Equipped with 1 ETH (which can be wrapped to WETH) and a relatively small 110 DVT.
- The Goal: Extract all 1,000,000 DVT from the lending pool and send them to a designated recovery account.
The lending pool's borrowing mechanism is key: to borrow a certain amount of DVT, you must deposit WETH worth DEPOSIT_FACTOR (which is 3) times the DVT's value. This value is determined by the TWAP oracle over a TWAP_PERIOD of 10 minutes.
The Oracle's Illusion: How TWAP Can Be Misled
TWAP oracles are generally considered more resilient than spot price oracles because they average prices over a period, making them resistant to rapid, temporary price fluctuations caused by flash loans or large trades. However, their strength can also be their weakness, especially when timing comes into play.
The core vulnerability in Puppet V3 lies in understanding this TWAP_PERIOD. The oracle reports an average price, not the immediate, live price. If we can drastically manipulate the spot price of DVT in the Uniswap pool and then query the TWAP before the 10-minute window fully incorporates this new, manipulated price, the oracle will still report a price heavily influenced by the old, pre-manipulation market.
The Attack: A Masterclass in Timing
Let's break down the elegant execution of the Puppet V3 attack:
Preparation:
- First, the attacker wraps their 1 ETH into 1 WETH. This gives them WETH to deposit later.
- Approvals are set for the Uniswap router and the lending pool, allowing the necessary token transfers.
Price Manipulation:
- The attacker, possessing 110 DVT, performs a massive swap on the Uniswap V3 pool. They sell all their 110 DVT for WETH.
- Remember, the Uniswap pool initially holds 100 WETH and 100 DVT. By dumping 110 DVT into it, the DVT supply dramatically increases, and the WETH supply decreases (as the attacker takes WETH out). This action crashes the spot price of DVT relative to WETH. The DVT is now worth significantly less on the open market.
The Critical Waiting Game:
- This is where the magic happens. The attacker doesn't immediately attempt to borrow. Instead, they
skipahead in time by precisely 114 seconds. - Why 114 seconds? Because the
TWAP_PERIODis 10 minutes (600 seconds). By waiting only 114 seconds, the TWAP oracle has begun to register the new, lower spot price but is still heavily weighted by the initial, pre-dump 1:1 price that persisted for the much longer 3-day initial setup period. - Essentially, the oracle overvalues DVT compared to its current market price because the average hasn't fully caught up.
- This is where the magic happens. The attacker doesn't immediately attempt to borrow. Instead, they
Exploiting the Oracle's Lag:
- With the TWAP still reporting an artificially high price for DVT, the
calculateDepositOfWETHRequiredfunction in the lending pool believes DVT is worth more than it actually is on the open market. - Consequently, the amount of WETH required to deposit for borrowing the entire 1,000,000 DVT becomes drastically undervalued. The attacker can now borrow the entire 1,000,000 DVT using a fraction of the WETH they should have to deposit if the oracle were truly reflecting the current market value.
- The attacker initiates the
borrowcall, supplying their WETH. The lending pool, fooled by its own oracle, releases all 1,000,000 DVT tokens.
- With the TWAP still reporting an artificially high price for DVT, the
Mission Accomplished:
- Finally, the attacker transfers the newly acquired 1,000,000 DVT to the designated
recoveryaccount, emptying the lending pool and completing the challenge.
- Finally, the attacker transfers the newly acquired 1,000,000 DVT to the designated
The Takeaway: TWAP is Good, But Not Invincible
The Puppet V3 challenge is a brilliant illustration that while Time-Weighted Average Price oracles significantly enhance security by mitigating instant flash loan attacks, they are not entirely immune to manipulation. Their security relies on the assumption that extreme price deviations will be sufficiently averaged out over the defined period.
The vulnerability here highlights:
- The Importance of the Averaging Period: A too-short period makes it susceptible to rapid changes. A too-long period might make the oracle slow to react to legitimate market shifts.
- The Gap Between Spot Price and TWAP: There will always be a lag. An attacker can strategically exploit this lag by creating a massive price divergence and then querying the TWAP before the average fully normalizes.
- Sufficient Buffers: Lending protocols utilizing TWAPs must consider the potential for "sandwich" attacks on the oracle itself – manipulating the spot price, letting the TWAP update partially, and then borrowing before the TWAP fully corrects. This often means requiring a much larger deposit factor or implementing more dynamic oracle update mechanisms.
Puppet V3 reminds us that in DeFi security, constant vigilance and a deep understanding of underlying mechanisms are essential. Even with "all the recommended libraries," a clever attacker can always find a way to make the oracle dance.
Ready to test your own DeFi security prowess? Dive into more challenges at Damn Vulnerable DeFi!