Flash Loan Attacks – The Plague of DeFi?

Flash Loan Attacks – The Plague of DeFi?

Disclaimer: This article is written by BSC community creator Logan DeFi. The opinions expressed in this publication are those of the author. They do not purport to reflect the opinions or views of the BSC ecosystem or its members.

This year we saw a series of what seemed like endless flash loan attacks on DeFi, which led to significant losses for thousands of users. While some feel intimidated and speculate who is to blame for such exploits, others maintain that flash loan is undoubtedly an innovative financial instrument and it will change the financial market. So at the end of the day, what the hell is a flash loan, and what role does it play in the crypto world?

Only Exists in DeFi

Flash loan is a specialty of DeFi. First introduced in 2020 by AAVE on Ethereum, a flash loan is hailed as having “no real-world analogy” and has since brought about a whole range of new possibilities of what you can do with money.

In layman’s terms, a flash loan is a loan that you can take out from a DeFi protocol without having to put up any collateral or proving your reliability with credit scores. It eliminates the roles of financial intermediaries, thus enabling investors to be more proactive and in control of funds and financial instruments. For the first time ever, you can freely invest while using none of your own money.

Hooray to financial freedom! Goodbye to the complicated loan approval process! I will go from rags to riches with flash loans! Yay me!

Right?

Not quite. As fantastic and revolutionary as this may sound, how it works is quite out of the norm and the associated risks might be hard to accept for many.

In a “normal” scenario, for example, you borrow money from a bank after proving your ability to pay them back, invest with that borrowed money, then return the principal back to the bank. Essentially, the process consists of three transactions, and failure to return the loan in time leads to some kind of punishment such as liquidation of collaterals.

In flash loans, all of those steps magically happen in ONE transaction thanks to the power of blockchain. When you make a flash loan request, the protocol first temporarily lends you the money. Then you can do whatever you want with the loan, as long as you are able to pay it back before the transaction completes on the blockchain. If you are unable to return the loan in time, the transaction is reversed. Therefore, in any case, smart contracts ensure that the lender always gets their money back.

The thing is explaining it like this takes a lot of words, but in reality, a flash loan transaction happens in a matter of seconds. So ideally, if you want to reap any profits from a flash loan, in the second part of the transaction where you have the freedom to play with the borrowed money, the fund needs to be processed by some code or algorithm. That’s what makes flash loans not very user-friendly for average crypto investors.

However, to more tech-savvy investors, a flash loan is the perfect way to make money with zero principal. Some even take advantage of flash loans and other loopholes in DeFi to initiate what’s called “flash loan attacks”.

The First Attack Strikes on Valentine’s Day

Roughly a month after making its debut, on Valentine’s Day 2020, flash loan shakes the DeFi world with the first attack on Ethereum blockchain. An anonymous attacker executes 1 flash loan and 74 other transactions, taking more than 350k USD.

In the beginning, the borrower flash loans 10k ETH from dYdX. The loan is divided into two parts:

  • 1,300 ETH from that loan is shorted in favor of wBTC on bZx. The short order is relayed to and filled on Uniswap. Due to low liquidity on Uniswap, the short has a price slippage of 200.38%, causing the price of wBTC to soar.
  • 5,500 ETH from the same flash loan is used as collateral to borrow 112 wBTC from Compound. The attacker then takes advantage of the increased price of wBTC on Uniswap to convert 112 wBTC to 6,871.41 ETH.

After this, he pays back 10k ETH to dYdX and the flash loan is complete.

Following the exploit, the attacker returns 112 wBTC to Compound to get 5,500 ETH collateral back.

The total amount of profit he takes from this attack is more than 350k USD.

And It’s Definitely Not The Last Incident

As DeFi hasn’t recovered from the first attack, the second blow hits just a few days later. This time, still on bZx, within one single flash loan, the borrower pockets 634.9k USD.

From then on, flash loan attacks get more sophisticated and brutal with each blow. Amidst those cruel strikes, some contain signs of empathy or traces of humor that render us perplexed with the adversaries’ intentions. Are they only after the money? Or are they here to prove a point?

For instance, the attacker of MakerDAO uses the flash loan to influence the outcome of a poll rather than for immediate financial gain.

Regarding Value DeFi’s case in 2020, after a few users leave notes through the attacker’s wallet address begging him for mercy, that anonymous evil(?) man for some reason returns 2 million USD to users.

Funny PancakeBunny perpetrator, this person causes buzz among crypto investors with a message in the flash loan transaction that reads “Aren’t Flash loans Earitating”. Then in an unpredictable move, the attacker sends 100,001 DAI to REKT News, the website that reports, well, rekts of the decentralized world. The REKT team refused to receive stolen money and transferred it to PancakeBunny.

Many 2021 attacks caused tens of millions USD in loss each. From xToken and Alpha on Ethereum to PancakeBunny and Spartan on Binance Smart Chain, the unfortunately frequent flash loan attacks leave crypto investors wondering why they happen to some protocols and not others. Is it because some are more secure? Or is it because of pure luck that some are spared?

Why Are We in This Situation?

First, let’s be clear. Flash loan on itself does not enable the attack. It provides attackers with funds that are big enough to help them exploit already-existing vulnerabilities in the protocols.

Criminals often abuse the decentralization and privacy of crypto to get away with their loot because it is extremely complicated to track them down and recover the funds. The same goes with flash loan attackers.

Also, as aforementioned, flash loan allows people to invest with almost no capital. While other types of manipulation in DeFi require you to either (1) hold an enormous amount of token or (2) be a team member of the project or have internal access. Still, from the flash loan attack diary, it can be seen that there are particular times when the attacks are more prevalent.

The two worst-hit months were November of 2020 and May of 2021. Both months registered record-breaking numbers of COVID-19 cases in different parts of the world. Especially in May 2021, the crypto market saw a great slump, and what ensued was a string of appalling occurrences for the community, including rugpulls and flash loan attacks.

Timing and external factors may propose a possible explanation. However, hungry ghosts always lurk around in the financial market, be it the regulated space or not. In the decentralized sphere, everything is conducted through smart contracts, and smart contracts are basically lines of codes. Hence, while they remove trust issues involving a third party out of a transaction, problems may inadvertently arise when a code does not function as it is designed to, and that is where the hacker aims.

In the first incident of bZx, the pump attack when the short order was filled on Uniswap could have been avoided if the bZx system had recognized the liquidity problem. In fact, bZx had such logic in their contracts, but it was not activated properly during the attack.

In some other cases like the second attack of bZx or the PancakeBunny incident, an exchange uses only one or two on-chain oracles that do not provide enough market information including token price, making it more susceptible to price manipulation for arbitrage.

How Do We Move On From This?

Flash loan is not the sinner. In fact, it sets a new standard for lending and lowers the entry barriers for investors. Yet, considering the alarming rate of flash loan attacks in recent months, there needs to be some proposed solution.

First and foremost, there are quite a few flash loan attacks that have on-chain oracles as the failure point. As stated before, on-chain oracles provide limited market coverage, making protocols more prone to price manipulation. In that case, decentralized oracle networks with large market coverage should be integrated into the system to make sure the lending protocols are more tamper-resistant and secure, especially when projects are very interconnected nowadays. With robust decentralized oracle networks in place, it is harder for adversaries to manipulate token prices on an exchange during an attack.

Following the recent PancakeBunny flash loan attack, the team has chosen to integrate Chainlink’s price feed to improve upon its cross-chain operation. The price feed works in a decentralized manner, provides quality data, and uses a reference contract model, allowing PancakeBunny’s system to receive a more accurate price of a digital asset anytime. As a flash loan only happens in a single block and can only affect on-chain exchanges, it is less likely to influence Chainlink’s price feed that runs on multiple blocks at the same time.

Nonetheless, using quality off-chain oracles may prevent pump attack and arbitrage, but this solution is not a silver bullet as the hacker may deliberately target the oracles.

The ETH sell-off on March 12th, 2020 caused a huge dip in ETH price on MakerDAO. Coupled with Ethereum’s network congestion and ridiculous gas fee, whale’s price manipulation paralyzed the oracles of such giants as Chainlink and MakerDAO. The price feeds had no way to update fast enough, leading to mass liquidation of assets on MakerDAO. Any safeguards that users have applied to their assets do not work in this situation because the safeguards depend on functioning oracles.

This story is not particularly about a flash loan attack, but it is a cautionary tale because a flash loan empowers one to be a whale for a transaction. On Binance Smart Chain, lower liquidity tokens are often indexed by Chainlink, and they are the most susceptible to this kind of manipulation.

That leads us to the second – Oracles need to step up their security game for they are very important to the well-being of the whole market. For example, Deri Protocol realizes a potential exploit on their V1 Oracle, so they immediately initiate the emergency response procedure, safely migrate users’ funds, review a new contract for three days, and finally transfer all funds including that of the malicious address to the new pool. Subsequently, the original pool is removed from the protocol. The timely response of Deri Protocol in the face of an oracle attack has protected their users’ funds and is highly commendable.

Third, projects should have their codes audited by various parties before launching. Most protocols that suffer from flash loan attacks do not have their smart contracts audited, only to find out silly bugs in the codes after being exploited. Some affected protocols have one auditor. Only Alpha and Harvest on Ethereum are the two curious cases where they have two and three auditors (respectively) yet each lost more than 30 million of USD to flash loan attacks. Still, it can be said that the more auditors go through your contracts, the less chance of a flash loan attack.

Fourth, protocols can disable deposits and withdrawals within the same transaction. Since this raises the costs to initiate flash loan attacks, some potential criminals may be discouraged while normal investors can still utilize flash loans as a valuable financial instrument.

Last but not least, flash loan exploits last for seconds, so it is almost impossible for a team to intervene in time. A great solution for DeFi protocols would be to strengthen their risk control capabilities by installing a system that can alert and take actions against flash loan attacks in real-time. DeFi protocols may learn and adopt from the stock market in this case. In the stock market, an increase in volatility would automatically trigger an increase in the premium of options. Similarly, a protocol may implement a system that can dynamically adjust flash loan-related parameters such as interest rate, borrow percentage, etc. when there is a sudden and drastic change in token price. This way, there is no need for protocols to stop the flash loan transaction in question completely, but rather they can be more proactive and flexible with how they deal with potential attacks.

What Is Waiting For Us In The Future?

Flash loan is a relatively new technology that introduces an absolutely unique concept to the financial market. It opens up a world of possibilities for investors and paves the way for a new financial system.

At the same time, flash loan attacks are incidents that remind us that we are still very early in the development of DeFi. Even though we have come up with a few possible solutions, it is highly likely that flash loan attacks will get more complicated and reveal more weaknesses of DeFi in the future.

On the bright side, these “hard lessons” teach projects about their vulnerabilities. The adoption of DeFi is inevitable and knowing more about its weaknesses will only make us stronger in the long run. It is very interesting to see how flash loans and the whole DeFi space, in general, will evolve from here. However, one thing is certain, the projects must put security first and spare no expenses when protecting their users and their funds.

Written by Logan DeFi


Disclaimer: This article is written by BSC community creator Logan DeFi. The opinions expressed in this publication are those of the author. They do not purport to reflect the opinions or views of the BSC ecosystem or its members.