本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2024/4/15

OpenLeverage $236K Exploit | Hack Analysis

Incident Summary

On April 1, 2024, an attacker was able to exploit a vulnerability in Open Leverage's smart contracts, resulting in a loss of around $260,000 in assets

What is Open Leverage?

Open Leverage is a permissionless lending margin trading protocol that enables traders or applications to long or short on any trading pair on decentralized exchanges (DEXs).

Root Cause:

The root cause of the hack was an inadequate tracking mechanism in the accounting process.

Attacker transaction

Steps of the Exploit:

1. The attacker targeted a specific market (Market ID 15) using the marginTrade function of the OpenLevV1 contract to establish a margin position.

function marginTrade(
uint16 marketId,
bool longToken,
bool depositToken,
uint deposit,
uint borrow,
uint minBuyAmount,
bytes memory dexData

) external payable override nonReentrant onlySupportDex(dexData) returns (uint256) {

return _marginTradeFor(msg.sender, marketId, longToken, depositToken, deposit, borrow, minBuyAmount, dexData);

}

2. The attacker deposited approximately 0.8 ETH and borrowed $18,000 worth of USDC to leverage.

3. Another position was opened on the same market with minimal collateral and no debt through an untrusted external call with the OpBorrowing contract.

4. The exploit took advantage of the interconnectedness of contracts, allowing the attacker to break the health check and extract all collateral from the margin positions using the payoffTrade function.

// @param longToken Token to long. False for token0, true for token1.

function payoffTrade(uint16 marketId, bool longToken) external payable override nonReentrant {

[Types.Trade](http://Types.Trade) storage trade = activeTrades[msg.sender][marketId][longToken];

bool depositToken = trade.depositToken;

uint deposited = trade.deposited;

Types.MarketVars memory marketVars = OpenLevV1Lib.toMarketVar(longToken, false, markets[marketId]);

//verify

require(trade.held != 0 && trade.lastBlockNum != block.number, "HI0");

(ControllerInterface(addressConfig.controller)).closeTradeAllowed(marketId);

uint heldAmount = trade.held;

uint closeAmount = OpenLevV1Lib.shareToAmount(heldAmount, totalHelds[address(marketVars.sellToken)], marketVars.reserveSellToken);

uint borrowed = OpenLevV1Lib.borrowCurrent(marketVars.buyPool, msg.sender);

//first transfer token to OpenLeve, then repay to pool, two transactions with two tax deductions

uint24 taxRate = taxes[marketId][address(marketVars.buyToken)][0];

uint firstAmount = Utils.toAmountBeforeTax(borrowed, taxRate);

uint transferAmount = transferIn(msg.sender, marketVars.buyToken, Utils.toAmountBeforeTax(firstAmount, taxRate), true);

OpenLevV1Lib.repay(marketVars.buyPool, msg.sender, transferAmount);

require(marketVars.buyPool.borrowBalanceStored(msg.sender) == 0, "IRP");

delete activeTrades[msg.sender][marketId][longToken];

totalHelds[address(marketVars.sellToken)] = totalHelds[address(marketVars.sellToken)].sub(heldAmount);

doTransferOut(msg.sender, marketVars.sellToken, closeAmount);

emit TradeClosed(msg.sender, marketId, longToken, depositToken, heldAmount, deposited, heldAmount, 0, 0, 0);

}

5. Since both the OpenLevV1 and OpBorrowing contracts relied on the LToken contract for their operations, the contract inherently suggested that the debt position of the first position was also cleared off along with the liquidation of this small position.

The key to the exploit was that the contracts relied on another contract (LToken) that didn't properly track the debt from the first position. This allowed the attacker to trick the system into thinking the debt had been paid off when it really hadn't.

In short, the attacker borrowed a bunch of money, then tricked the system into thinking they had paid it back by exploiting a weakness in how different parts of the platform interacted.

Result

- The exploit resulted in a loss of approximately $220,000 on BNB Chain and $40,000 on Arbitrum.king the debt had been paid off when it really hadn't.

Read here Prisma Finance $11.6M Exploit 

Author: Piyush shukla

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More