Blog

  • BSC Flash Loan Attack: The Three Copycats

    BSC Flash Loan Attack: The Three Copycats

    [ad_1]

    A series of attacks compromised several Binance Smart Chain (BSC) projects in May. Following PancakeBunny, its three forks projects — AutoShark, Merlin Labs, and PancakeHunny — were also attacked using similar techniques. PancakeBunny suffered the most costly attack of the four, which saw nearly $45M in total damages. In this article, Dr. Chiachih Wu, Head of the Amber Group Blockchain Security Team, elaborates on the details behind the attacks on the three copycats.

    Copycats

    AutoShark was attacked five days after PancakeBunny, followed by Merlin Labs and PancakeHunny, respectively. The following is an analysis of the problems and possible attack techniques for these three forked projects.

    In the SharkMinter.mintFor() function, the amount of rewarding SHARK tokens to be minted (i.e., mintShark) is derived from sharkBNBAmount computed by tokenToSharkBNB() in line 1494. However, tokenToSharkBNB() references the current balance of flip, which makes it a vulnerable point. One could assume that the amount of tokens received in line 1492 is equal to the amount of the flip balance. Still, a bad actor could manipulate the flip balance simply by sending in some flip tokens right before the getReward() call and indirectly breaking the logic of tokenToSharkBNB().

    In the underlying implementation of tokenToSharkBNB() , there’s another attack surface. As shown in the above code snippet, _flipToSharkBNBFlip() removes liquidity from ApeSwap (line 1243) or PantherSwap (line 1262) and converts the LP tokens into SHARK+WBNB. Later on, the generateFlipToken() is invoked to convert SHARK+WBNB into SHARK-BNB LP tokens.

    Inside generateFlipToken() , the current SHARK and WBNB balances of SharkMinter (amountADesired, amountBDesired) are used to generated LP tokens and the amount of LP tokens are returned to mintFor() as sharkBNBAmount. Based on that, the bad actor could transfer SHARK+WBNB into SharkMinter to manipulate the amount of SHARK tokens to be minted as well.

    The loophole in PancakeHunny is identical to that found in AutoShark, in that the bad actor can manipulate HUNNY reward minting with HUNNY and WBNB tokens.

    Compared to AutoShark and PancakeHunny, Merlin Labs’ _getReward() has a more obvious vulnerability.

    The code snippet above shows that the performanceFee could be manipulated by the balance of CAKE, which indirectly affects the MERL rewards minting. However, the nonContract modifier gets rid of flash loans.

    Even without an exploit contract, the bad actor could still profit through multiple calls.

    Reproducing AutoShark Attack

    To reproduce the AutoShark hack, we need to first get some SHARK-BNB-LP tokens from PantherSwap. Specifically, we swap 0.5 WBNB into SHARK (line 58) and transfer the rest WBNB with those SHARK tokens into PantherSwap for minting SHARK-BNB-LP tokens (line 64). Later on, we deposit those LP tokens into AutoShark’s StrategyCompoundFLIP contract (line 69) to qualify for rewards. Note that we purposely only deposit half of the LP tokens in line 69.

    The second step is to make getReward() go into the SharkMinter contract. In the above code snippet, we know that the reward can be retrieved by the earned() function (line 1658). Besides, 30% of the reward (i.e., performanceFee) should be greater than 1,000 (i.e., DUST) to trigger the SharkMinter.mintFor() in line 1668.

    Therefore, in our exploit code, we transfer some LP tokens to the StrategyCompoundFLIP contract in line 76 to bypass the performanceFee > DUST check and trigger the mintFor() call. Since we need a lot of WBNB+SHARK to manipulate SharkMinter, we leverage PantherSwap’s 100k WBNB via a flash-swap call in line 81.

    In the flash-swap callback, pancakeCall(), we exchange half of the WBNB into SHARK and send the SHARK with the remaining 50,000 WBNB to the SharkMinter contract to manipulate the reward minting.

    The next step is to trigger getReward() when the SharkMinter receives the WBNB+SHARK tokens to mint a large amount of SHARK to the caller.

    The last step is to convert SHARK to WBNB, pay the flash loan, and walk away with the remaining WBNB tokens.

    In our experiment, the bad actor starts with 1 WBNB. With the help of flash loans, he profits from more than 1,000 WBNB being returned in one transaction.

    Reproducing PancakeHunny Attack

    The theory behind the PancakeHunny attack is similar to the AutoShark attack. In brief, we need to send a lot of HUNNY+WBNB to HunnyMinter before triggering getReward(). However, the HUNNY token contract has a protection mechanism called antiWhale to prevent large amount transfers. Therefore, flash loans do not work here.

    To bypass antiWhale, we create multiple child contracts and initiate multiple CakeFlipVault.deposit() calls via said contracts.

    In the above exploit code snippet, the LP tokens gathered in line 116 are divided into 10 parts and transferred to 10 Lib contracts in line 122 followed by Lib.prepare() calls for each of them.

    Inside Lib.prepare(), we approve() the CakeFlipVault to spend the LP tokens and invoke CakeFlipVault.deposit() to enable the later getReward() calls for minting rewarding HUNNY tokens.

    After preparing 10 Lib contracts, the main contract iterates each of them to: 1) swap WBNB to the maximum allowable amount of HUNNY; 2) transfer WBNB+HUNNY to HunnyMinter; 3) trigger getReward() via lib.trigger(); and 4) swap HUNNY back to WBNB.

    In the end, the bad actor with 10 WBNB earns around 200 WBNB from 10 runs of 10 Lib contracts operations.

    Reproducing Merlin Labs Attack

    As mentioned earlier, Merlin Labs has the noContract modifier to get rid of flash loan attacks. However, we could use a script to trigger the attack with multiple transactions initiated from an EOA (Externally Owned Account) address. The only difference is that someone may front-run the bad actor’s transaction to steal the profits.

    Similar to the AutoShark attack, we need to prepare enough LINK and WBNB (line 23), use them to mint WBNB-LINK-LP tokens (line 34), and deposit LP tokens into VaultFlipCake contract (line 38).

    The remaining actions are:

    1. Swapping WBNB to CAKE (line 42).
    2. Manipulating MERL minting by sending CAKE to VaultFlipToCake contract (line 50).
    3. Triggering getReward() in line 55 (a large amount of MERL tokens are minted).
    4. Swapping MERL back to WBNB and repeating the above steps multiple times.

    As mentioned earlier, if someone front runs step 3 right after step 2, that person could remove a large amount of MERL.

    In our experiment, the bad actor starts with 10 WBNB and walks away with around 165 WBNB by repeating the four steps 10 times.

    About Amber Group

    Amber Group is a leading global crypto finance service provider operating around the world and around the clock with a presence in Hong Kong, Taipei, Seoul, and Vancouver. Founded in 2017, Amber Group services over 500 institutional clients and has cumulatively traded over $500 billion across 100+ electronic exchanges, with over $1.5 billion in assets under management. In 2021, Amber Group raised $100 million in Series B funding and became the latest FinTech unicorn valued at over $1 billion. For more information, please visit www.ambergroup.io.

    [ad_2]

    Source link

  • Just HODL! Bitcoin and Ethereum outperform ‘lower risk’ crypto index funds

    Just HODL! Bitcoin and Ethereum outperform ‘lower risk’ crypto index funds

    [ad_1]

    In the past two decades, index and exchange-traded funds (ETF) have become some of the most popular forms of investing because they offer investors a passive way to gain exposure to a basket of stocks as opposed to investing in individual stocks which increases risk of loss. 

    Since 2018, this trend has extended to the crypto sector and products like the Bitwise 10 Large Cap Crypto Index (BITX) tracks the total return of Bitcoin (BTC), Ether (ETH), Cardano (ADA), Bitcoin Cash (BCH), Litecoin (LTC), Solana (SOL), Chainlink (LINK), Polygon (MATIC), Stellar (XLM) and Uniswap (UNI).

    The ability to access multiple top projects through one weighted average market cap index sounds like a great way to spread out risk and gain exposure to a wider range of assets, but do these products offer investors a better return in terms of profit and protection against volatility when compared to the top-ranking cryptocurrencies?

    Hodling versus crypto baskets

    Delphi Digital took a closer look at the performance of the Bitwise 10 and compared it to the performance of Bitcoin following the December 2018 market bottom. The results show that investing in BTC was a more profitable strategy even though BITX was slightly less volatile.

    Bitcoin price vs. Bitwise 10. Source: Delphi Digital

    According to the report, “indices aren’t meant to outperform individual assets, they’re meant to be lower-risk portfolios compared to holding an individual asset,” so it’s not surprising to see BTC outperform BITX on a purely cost basis.

    The index did offer less downside risk to investors as the market sold-off in May but the difference was “trivial” as “BTC’s max drawdown was 53% and Bitwise’s was 50%.”

    Overall, the benefits of investing in an index versus Bitcoin are not that great because the volatile nature of the crypto market and frequent large drawdowns often have a larger effect on altcoins.

    Delphi Digital said:

    “Crypto indices continue to be a work-in-progress. Choosing assets, allocations, and re-balancing thresholds is a difficult task for an emerging asset class like crypto. But as the industry matures, we expect more efficient indices to pop up and gain traction.”

    Ethereum also outperforms DeFi baskets

    Decentralized finance (DeFi) has been one of the hottest crypto sectors in 2021 led by decentralized exchanges like Uniswap (UNI) and SushiSwap (SUSHI) and lending platforms like AAVE and Compound (COMP).

    The DeFi Pulse Index (DPI) aims to tap into this rapid growth and the DPI token has allocations to 14 of the top DeFi tokens, including UNI, SUSHI, AAVE, COMP, Maker (MKR), Synthetic (SNX) and Yearn.finance (YFI).

    When comparing the performance of DPI to Ether since the inception of the index, Ether significantly outperformed in terms of profitability and volatility, as evidenced by a 57% drawdown on Ether versus 65% for DPI.

    Ether price vs. DeFi Pulse Index price. Source: Delphi Digital

    While this is an “imperfect comparison” according to Delphi Digital due to the fact that “the risk and volatility of DeFi tokens are higher than Ether’s,” it still highlights the point that the traditional benefits seen from indices are not mirrored by crypto-based baskets.

    Delphi Digital said:

    “You could’ve just HODL-ed ETH for a superior risk-return profile.”

    For the time being, Bitcoin and Ether have proven to be two of the lower-risk cryptocurrency plays available when compared to crypto index funds that offer exposure to a larger number of assets.

    The views and opinions expressed here are solely those of the author and do not necessarily reflect the views of Cointelegraph.com. Every investment and trading move involves risk, you should conduct your own research when making a decision.