Articles

Topics from basics to advanced to enhance your trading skill

What is a Flash Loan Attack? How to avoid it?

Jun 26, 2021 06:15

|

Decentralized Liquidity Is the Backbone of DeFi and Flash loans are exciting and powerful which can provide instant and sizable liquidity to anyone in the world, at any point in time, they have increasingly been used to fund attacks on DeFi protocols. Decentralized finance (DeFi) ecosystem has recently emerged with new liquidity mechanisms. Cryptocurrency and by extension DeFi is a highly experimental field. When so much money is at stake, it’s only a matter of time before vulnerabilities are discovered.

In this article we briefly explained the attacks occurred in Flash loan and prevention of it.

Flash loan attacks are a type of DeFi attack. These attacks were, in a word, magnificent. In each attack, a penniless attacker instantaneously borrowed hundreds of thousands of dollars of ETH, threaded it through a chain of vulnerable on-chain protocols, extracted hundreds of thousands of dollars in stolen assets, and then paid back their massive ETH loans. All of this happened in an instant. Such attacks can occur in mere seconds that is, in a single ethereum transaction.

Flash loan attacks are the most common types of DeFi attacks since they are the cheapest to pull off and easiest to get away with. They have been consistently making headlines since DeFi’s surge in popularity in 2020 and appear to be growing more rampant in 2021, spanning several hundred million dollars in losses to date.

Flash loans allow a user to borrow as much as they want with zero capital. For instance, if you’d like to borrow $70,000 worth of ETH, a lending protocol instantly gives it to you, but that doesn’t mean it’s yours. You need to do something with the borrowed funds in order to pay back the loan and perhaps pocket the excess amount.

For this to work, the process needs to happen fast and the debt must be repaid to the protocol in time, otherwise the transaction will reverse. A decentralized lender doesn’t require collateral from you since the agreement to pay your debt is enforced by a blockchain. Flash loan attackers thrive on finding ways to manipulate the market while still abiding by a blockchain’s rules.

It is the number one cause of attacks right now, by far. It is important to be noted that decentralized exchanges are not decentralized oracles. Using Uniswap, Sushiswap, or Curve to get pricing information to execute trades is pulling data from potocols whose price depends soley on liquidity. Looking at the infamous ground zero bZx attack that sparked this wave of attacks, we can see exactly what happens. These flash loans are used to crash and manipulate the price of these decentralized exchanges, which most projects deemed safe to use. The fact is that issue relies here with these protocols prices depend entirely on liquidity. The easiest way to solve this is to use decentralized oracles. 

A reentrancy attack can occur when you create a function that makes an external call to another untrusted contract before it resolves any effects. If the attacker can control the untrusted contract, they can make a recursive call back to the original function, repeating interactions that would have otherwise not run after the effects were resolved. Leave external transactions to the last parameter. These are the harder ones to prevent. The DAO attack is an example of the reentrancy attack as well, and is also considered the mother of not just defi, but decentralized attacks in general on the ETH chain.

Since everything on-chain is public information, an attacker can watch transactions on-chain and look for those that would be detrimental to the attacker, and make a transaction with a higher gas price to occur before that transaction goes through. For example, they notice a whale is about to dump a token that the attacker holds, so the attacker pays extra gas to dump theirs first. This is known as “front running” in traditional finance, you could also think of it as a race condition because there can be scenarios where it’s more complicated than this example, but still boiled down to this. Reentrancy technical falls under this category.

Pump and arbitrage attacks are difficult to find, some even saying they are less “attacks” and more “the system working as intended”. Liquidity is an important part of any and all processes, so when a whale spikes or crashes a price, does that really reflect the true value of that crash/spike? It’s hard to say. Prevention at the moment hangs around preventing anyone from being able to cause these spikes. Sometimes, coordinated attacks from social groups can be enough to pump and dump a price of an asset.

These attacks are fatal attraction for hackers. The most recent flash loan attack as of May 2021 occurred at PancakeBunny, a BSC-powered yield farming aggregator, which suffered an exploit that caused its token to plummet by more than 95% of its previous value.

The attacker initially borrowed a large amount of BNB through PancakeSwap and used it to manipulate the price of USDT/BNB and BUNNY/BNB in PancakeBunny’s pools. This allowed the hacker to steal a large amount of BUNNY, which they dumped on the market, causing the price to crash. The hacker then paid back the debt via PancakeSwap.

Data suggests that the hacker was able to get away with nearly $3 million in profits, leaving a tarnished protocol in its wake.

The largest flash loan hack in 2021 occurred last February when the Alpha Homora protocol was drained of $37 million using Iron Bank, Cream’s lending platform. The leveraged yield farming protocol was hit with a series of flash loans.

The hacker repeatedly borrowed sUSD from Iron Bank via the Alpha Homora dapp, doubling the amount borrowed each time. This was done in a two-transaction process where the hacker lent the funds back into Iron Bank each time, which allowed them to receive Yearn Synth sUSD (cySUSD) in return.

Then, the perpetrator borrowed 1.8 million USD Coin (USDC) from Aave via a flash loan then swapped them with sUSD using Curve. The sUSD was used to pay back the flash loan and lend to Iron Bank, which enabled them to continuously borrow and lend more of them and receive a proportional amount of cySUSD each time.

Basically, the hackers rinsed and repeated this process many times, which allowed them to steal massive amounts of Creamy cyUSD that they in turn used to borrow other cryptocurrencies from Iron Bank. Hence, they borrowed 13K Wrapped Ethereum (WETH), 3.6 million USDC, 5.6 million USDT, and 4.2 million DAI.

As you can see, the process can be quite complex and requires a series of steps that need to happen very fast, which is a testament to how far these attackers are willing to go.

How to Prevent Flash Loan Attacks

Flash loan hackers take a lot of steps to get around the coded security system. These steps vary during attacks and the breadcrumbs can be difficult to follow. If successful, these attackers are able to steal millions in cryptocurrency valuation.

Considering the growing number of flash loan attacks at present, it’s clear that there is no be-all and end-all solution yet. However, there are notable steps that can be taken to combat this issue. 

Use Decentralized Oracles for Price Data

The most optimal way to reduce the attack vector for flash loan exploits is for DeFi platforms to use decentralized pricing oracles like Chainklink and Band Protocol instead of relying on a singular DEX for their price feed. Alpha Homora had to learn this the hard way before deciding to launch their Alpha Oracle Aggregator last May. 

Force Critical Transactions to Go Through Two Blocks

Dragonfly Research has proposed forcing flash loans to go through two blocks instead of one. However, this isn’t a complete solution either since if it is designed incorrectly, the exploiter could simply flash loan attack both blocks. Furthermore, this can drastically affect the UI of DeFi protocols since transactions will no longer be synchronous.

Avoid Front Running Attacks

The best way to prevent against these is with a commit-reveal scheme.  This is when a project sends a transaction that goes through and is accepted, but is hashed or encrypted. Only after the transaction has concluded that they send a “reveal” phrase that decodes the transaction. This method prevents both miners and users from frontrunning transactions as they cannot determine the contents of the transaction. Transactional value however, cannot be commit-revealed, making this far less effective in the defi world. This is another very difficult type of attack to prevent.

Using Flash Loan Attack Detection Tools

Open Zeppelin has recently launched a program called Open Zeppelin Defender that enables project managers to detect smart contract exploits and other unusual activity, which would allow them to respond swiftly and neutralize attacks. According to their blog post, this tool has already been integrated by the Synthetix, Yearn and Opyn teams.

Conclusion

Ultimately, the result of a flash loan attack is out of your hands. By limiting concentrations of singular protocols or altcoins, you can hedge against these attacks to a degree. However, this is the inherent risk involved in cryptocurrency. Balancing your portfolio with more established coins is also a smart move.

As cryptocurrencies shift protocols away from proof-of-work, additional questions will be raised about the security of alternatives.

Loading spinner
Recommended For You
What is FUD and what it..

January 18, 2022