DeFi Risk Overview Part 3: Economic Vulnerabilities (Oracle Attacks, Flash Loan Attacks, etc.)
Alice: "Hello, Rabbit-senpai! I'm looking forward to our adventure in Cryptocurrency Wonderland again today!"
Rabbit-senpai: "Hi there, Alice! I'm happy to see you so energetic today. Up until now, we've looked at many of the sparkling, magical mechanisms of DeFi, but every brilliant world has a shadow side that requires a bit of caution. Today, we're going to learn about economic vulnerabilities, especially some rather tricky attack methods. Hehe, leave it to Rabbit-senpai—I can make even the most difficult topics easy to understand!"
Alice: "Economic vulnerabilities... is that right? It sounds a bit difficult, but I'm sure it'll be fine if I'm with you, Rabbit-senpai! But is DeFi really that dangerous?"
Rabbit-senpai: "Rather than dangerous, it's more like because it's a new technology, there are still unpaved roads and unexpected pitfalls to be found. But don't worry. If you acquire the proper knowledge and walk carefully, your adventure in Wonderland will be even more fun. Now, today I, Rabbit-senpai, will clearly explain two attacks that are like magical tricks: "oracle attacks and "flash loan attacks—I'll explain them in a way that's easy to understand!"
Alice: "Attacks that are like magical tricks...! That sounds exciting! Please teach me, Rabbit-senpai!"
Is the world of DeFi sparkling, but also full of pitfalls?
Rabbit-senpai: "Well then, Alice, DeFi—or decentralized finance—is a truly revolutionary system where you can freely lend, borrow, and exchange money without a central administrator like a bank. It's really convenient because magical contracts called smart contracts handle everything automatically."
Alice: "Yes! After you taught me last time, it felt so futuristic! I even thought, 'Will we not need banks anymore?'"
Rabbit-senpai: "Hmm, I understand that feeling well. But, Alice, DeFi is still a young technology that has just been born. Therefore, there are inevitably parts that aren't perfect and weaknesses that evil wizards plotting mischief are likely to target. Especially since money is directly involved, if those weaknesses are exploited, it can sometimes lead to significant damage."
Alice: "Oh no!? Are there evil wizards...?"
Rabbit-senpai: "Unfortunately, yes. Just as there are hackers in the internet world, there are people in the DeFi world who try to find holes in systems and exploit them. There is even data showing that 97% of the crypto assets stolen in the first three months of 2022 came from DeFi protocols."
Alice: "N-97%!? That's terrible...! What kind of weaknesses are there?"
Rabbit-senpai: "Well, there are many, but today let's focus on what is called "economic vulnerability". This is a problem that occurs when the gaps in economic mechanisms or external integration are exploited, even if there isn't a direct bug in the program code itself. Especially famous are the "oracle attacks" and "flash loan attacks" that I'm about to explain."
Alice: "Gaps in economic mechanisms... that sounds difficult, but I want to try my best to understand!"
Rabbit-senpai: "That's the spirit, Alice! Then, let's start by looking at 'oracle attacks'!"
It's a disaster if the magic mirror gets cloudy! What is an 'oracle attack'?
Alice: "Oracle... is it? That sounds like a fortune teller!"
Rabbit-senpai: "Oh, you noticed something good, Alice! That's exactly right. An oracle in DeFi is an information messenger that conveys information from the world outside the blockchain to the smart contracts inside the blockchain. For example, it tells you things that can't be known by the blockchain alone, like the real-world price of Bitcoin or tomorrow's weather; it's like a magic mirror or a reliable carrier pigeon."
Alice: "Wow! I thought the blockchain was a closed world, but it can also take in outside information! So, how does that oracle get attacked?"
Rabbit-senpai: "Good question! What would happen if that 'magic mirror' were cloudy or distorted? Or if the 'carrier pigeon' were caught by a bad guy and forced to carry a fake letter?"
Alice: "What!? Then the smart contract would believe the wrong information, right? For example, if 1 Bitcoin is actually worth 10 million yen, but the magic mirror shows '1 Bitcoin is 1 million yen'...
Rabbit-senpai: "Exactly! Since a smart contract is a program, it executes based on the information provided without questioning it. If a DeFi lending protocol (a place to borrow and lend money) believes that distorted price of '1 million yen per Bitcoin,' you might be able to borrow a large amount of coins with only 1 million yen in collateral, even though it would normally require 10 million yen in collateral. This is the basic concept of an oracle attack. Attackers try to deceive DeFi protocols to gain unfair profits by maliciously manipulating the price information reported by the oracle."
Alice: "Wow... that's clever! But how do you manipulate the oracle's information?"
Rabbit-senpai: "There are several methods, but the most common one is to temporarily cause a large fluctuation in the price on a specific exchange (like a DEX) from which the oracle retrieves price information. For example, an attacker uses a large amount of capital to buy up a massive amount of a certain coin, or conversely, dumps a massive amount, intentionally driving the price of that coin up or causing it to crash. Then, the oracle picks up that manipulated price and reports it to the DeFi protocol, and that's how it happens."
Alice: "So it requires a huge amount of capital..."
Rabbit-senpai: "That's right. That's why this oracle attack is often combined with a mechanism called a 'flash loan,' which I'll explain later. They use a flash loan, which allows you to borrow a huge amount of capital without collateral for just a split second, to procure the funds needed for price manipulation."
A scary case of an oracle attack: The tragedy of Cream Finance
Rabbit-senpai: "Let me introduce a scary case that actually happened. In 2021, a DeFi platform called Cream Finance had a massive amount of money, specifically about 130 million dollars, stolen through this oracle attack (which also used a flash loan)."
Alice: "130 million dollars!? In Japanese yen... let's see, over 10 billion yen!? Such a huge amount of money..."
Rabbit-senpai: "That's right. The attacker maliciously drove up the price of a specific stablecoin (yUSD) on a market that the oracle referenced. Cream Finance believed that manipulated, abnormally high price, and the attacker succeeded in unfairly borrowing a large amount of other valuable crypto assets using that yUSD as collateral."
Alice: "Wow... just hearing about it gives me the chills. How can we prevent such attacks?"
Rabbit-senpai: "That's a good question, Alice. There are several countermeasures being considered for oracle attacks. The first is to use a highly reliable decentralized oracle. For example, a project called Chainlink creates a mechanism that provides more accurate and tamper-resistant information by collecting price data from many independent sources, averaging it, and excluding outliers. Even if one evil wizard tries to distort a mirror, if there are many correct mirrors, the lie is easily exposed."
Alice: "I see! It's like listening to the opinions of many people instead of trusting just one person!"
Rabbit-senpai: "Exactly! Other important measures include combining multiple different oracle services and introducing a system that constantly monitors for anomalies in the data provided by the oracle. For example, safety devices such as temporarily halting transactions in the event of an excessively rapid price fluctuation are also being considered."
Alice: "There are various countermeasures. I feel a little relieved."
Rabbit-senpai: "Yeah. But the reality is that perfect countermeasures are quite difficult. If the oracle is secure but the processing is slow, it becomes vulnerable to arbitrage that exploits price differences, and conversely, if the processing is fast but not secure, it becomes vulnerable to price manipulation. This balance is extremely difficult. That's why it's important for us who use DeFi to be aware of what kind of oracle is being used and whether that oracle is trustworthy."
Rich for just a moment? The terrifying method of a 'flash loan attack'
Alice: "Rabbit-senpai, the term 'flash loan' came up in the story about oracle attacks earlier, but what exactly is that? Is it like a magic spell that lets you become rich for just a moment...?"
Rabbit-senpai: "Whoa, Alice, you're quite sharp! It might indeed be called a 'magic spell that lets you borrow for just a moment.' A flash loan is a very unique mechanism unique to DeFi, and it allows you to borrow a massive amount of crypto assets temporarily, and in some cases without collateral."
Alice: "Whaaaat!? You can borrow a large amount of money without collateral!? Is there such a dream-like story? If so, wouldn't everyone just borrow it and run away?"
Rabbit-senpai: "Hehe, that's the key, Alice-chan. Flash loans have a major characteristic, which is that there is an absolute rule: 'the borrowed funds must be repaid within the same transaction'."
Alice: "Repaid within the same transaction...? What does that mean?"
Rabbit-senpai: "Blockchain transactions record a set of processes together, but with a flash loan, you have to pay back the borrowed money with interest before that entire sequence of processes finishes. If you can't pay it back, the transaction itself is treated as if it never happened; in other words, 'the very fact that you borrowed it disappears'. That's why it's designed so that you absolutely cannot run away with the money."
Alice: "Wow! So that means you can't use it unless you have a way to pay the money back immediately. What is the purpose of such a mechanism?"
Rabbit-senpai: "It's mainly used for things like arbitrage. For example, suppose coin X is selling for 100 yen on exchange A and 101 yen on exchange B. In this case, if you borrow a large amount of funds with a flash loan, buy a large amount of coin X on exchange A, and immediately sell it on exchange B, you make a profit of 1 yen times the quantity, right? Then, you repay the borrowed money and interest from that profit. You complete this within a single, instantaneous transaction."
Alice: "I see! That's a clever way to use it! But how does that lead to an attack?"
Rabbit-senpai: "This is where we get to the main point. Evil wizards abuse this characteristic of flash loans, which is the ability to 'temporarily move massive amounts of funds'. As I mentioned regarding oracle attacks, you need a large amount of funds to manipulate the price of a specific coin, right? But usually, people don't have that kind of money. That's where they use flash loans."
Alice: "Ah! So that's what it was!"
Rabbit-senpai: "The steps for a flash loan attack are roughly like this."
The attacker first borrows a massive amount of funds via a flash loan.
Using those borrowed funds, they intentionally manipulate the price of a specific coin significantly on a DEX (decentralized exchange) or similar platform that a DeFi protocol uses for price reference (for example, by buying up a large amount to drive the price up).
With the price manipulated, they perform a fraudulent transaction on that DeFi protocol (for example, using the abnormally high-priced coin as collateral to borrow an unfairly large amount of other coins, or buying up an abnormally low-priced coin at an unfairly low price).
From the profit gained through the fraudulent transaction, they repay the funds and fees borrowed in the initial flash loan.
The remaining difference becomes the attacker's profit."
Alice: "Wow... that sounds like a bank robbery, but much more sophisticated... Have there actually been victims of this?"
Rabbit-senpai: "Yes, unfortunately, there have been many. For example, a platform called bZx has been the target of attacks using flash loans many times in the past. Since they are often combined with oracle attacks, the Cream Finance case I mentioned earlier can also be considered a type of oracle attack that utilized a flash loan."
How can flash loan attacks be prevented?
Alice: "Flash loans themselves are a convenient mechanism, but it's scary that they can be abused... Are there any countermeasures for this?"
Rabbit-senpai: "Of course, countermeasures are being considered. First, thorough security audits of smart contracts. This is the absolute basic. Attacks using flash loans often exploit small logic gaps in the contract. Next, it is also effective to use highly reliable price oracles. Even if the market price is temporarily manipulated by a flash loan, it is effective to incorporate a mechanism that references an average price over a certain period, such as a Time-Weighted Average Price (TWAP), so that the oracle does not immediately reflect those abnormal price fluctuations."
Alice: "TWAP...? What is that?"
Usagi-senpai: "It stands for Time Weighted Average Price, which is Time Weighted Average Price in Japanese. This is calculated by averaging the price over a certain period, and it has the effect of making it harder to be influenced by momentary price manipulation. For example, if you look at the average price over the last hour, a price manipulation lasting only a few seconds due to a flash loan won't have much of an impact, right?"
Alice: "I see! So it's to prevent being fooled by momentary movements!"
Usagi-senpai: "Exactly. Another indirect countermeasure is to maintain high liquidity within the DeFi protocol. If liquidity is low, even a small amount of funds can move the price significantly. If liquidity is high, no matter how much money is borrowed via a flash loan, it becomes difficult to manipulate the price."
Alice: "So countermeasures from various angles are necessary."
Usagi-senpai: "Yes. Flash loan attacks can be said to be very 'DeFi-like' attacks that take advantage of the transparency and programmability of the blockchain. That is precisely why the defense side also needs to deeply understand the mechanisms of DeFi and implement multi-layered countermeasures."
Are there others? Economic weaknesses of DeFi
Alice: "Oracle attacks and flash loan attacks, both were so clever that I was surprised... Are there other economic weaknesses in DeFi?"
Usagi-senpai: "Yes, let me list a few representative ones.
Smart contract vulnerabilities: While this is more of a technical vulnerability than an economic one, it is important because it leads to economic loss. If there are bugs or design flaws in the program code, they can be exploited to steal funds or cause unintended behavior. This is a risk that always lurks for any DeFi protocol. Countermeasures include thorough security audits by experts and the implementation of bug bounty programs that offer rewards to those who discover bugs."
Price manipulation risk due to low liquidity: As I mentioned earlier, liquidity can be low, especially in new DeFi protocols or pools dealing with minor coins. In such places, it is possible to move the price significantly with relatively small funds, making them easy targets for market manipulation."
Private key leakage and management errors: This is not limited to DeFi, but if a user's own private key management is lax, it could be stolen through phishing scams, etc., and the assets being operated in DeFi could be completely taken away. DeFi is a world of self-responsibility, so you really have to be careful about managing your keys."
Protocol governance risk: Many DeFi protocols take the form of a DAO (Decentralized Autonomous Organization) where operational policies are decided by voting among those who hold governance tokens. However, it is theoretically possible for a malicious group to buy up a large amount of governance tokens and pass proposals that are disadvantageous to the protocol."
Alice: "Wow... there really are various risks. DeFi is free and convenient, but that means there are many things you have to be careful about yourself."
Usagi-senpai: "That's right, Alice. '"Self-responsibility' is the keyword. But there's no point in just being afraid. If you properly understand these risks and learn the countermeasures, you should be able to safely enjoy the wonderful possibilities of DeFi."
Advice from Usagi-senpai! Mindset for avoiding danger
Alice: "Usagi-senpai, what specifically should we users be careful about?"
Usagi-senpai: "Alright, then finally, let me, Usagi-senpai, teach you the mindset for avoiding danger in DeFi!
Thoroughly DYOR (Do Your Own Research)!: This is an abbreviation meaning 'do your own research,' and it is the ironclad rule in the cryptocurrency world. Don't just take someone's word for it when they say 'it's profitable.' Investigate the DeFi project yourself until you are satisfied by reading the white paper, checking the community's reputation, and reviewing audit reports to understand what kind of mechanism it has and what risks are involved."
Check security audit reports: Many decent DeFi projects undergo security audits of their smart contracts by third-party specialized agencies and publish the results. Of course, just because it has been audited doesn't mean it's 100% safe, but it can be said to be more reliable than a project that hasn't."
Try with a small amount at first: It is forbidden to pour in a large amount of money all at once. It is wise to start with a small amount that you wouldn't mind losing, and get a feel for how to use the DeFi protocol and its risks."
Diversify your assets: There is a saying, 'Don't put all your eggs in one basket,' and the same applies to DeFi. Instead of depositing all your wealth into a single protocol, it is important to diversify your assets across multiple protocols or even in safe places outside of DeFi.
Be wary of suspicious high yields: Offers that sound too good to be true, like thousands of percent in annual interest, often have a catch. There is a possibility of a Ponzi scheme, so you need to calmly analyze the mechanism and determine whether it is sustainable.
Always keep up with the latest information: The world of DeFi is evolving day by day. New attack methods emerge and protocol specifications often change. Try to always get the latest information from reliable news sources and communities.
Alice: 'Wow, there are so many! But they are all very important things. DYOR, I will definitely remember that!'
Rabbit-senpai: 'Yes, that's the spirit, Alice! DeFi is certainly an attractive world, but it is also a wonderland where unknown dangers lurk. However, if you light the lamp of knowledge and proceed with caution, you will surely find wonderful treasures!'
Alice: 'Yes! Thanks to Rabbit-senpai, I feel like my understanding of the economic vulnerabilities of DeFi has deepened a little!'
Rabbit-senpai: 'That's good to hear! Every adventure has its dangers, but it is by overcoming them that you become a true adventurer. Hehe, just like me, Rabbit-senpai!'
Rabbit-senpai's Quick Break Column: Are flash loans not the bad guys? Surprising ways to use them!
Hey everyone! It's Rabbit-senpai. Today I talked about economic vulnerabilities in DeFi, especially the somewhat dirty topic of flash loan attacks, but I thought I'd take a moment here to restore the reputation of flash loans.
Actually, flash loans were originally devised as a very clever mechanism to perform arbitrage, collateral swapping, and debt refinancing efficiently in a single transaction. Imagine this: suppose you want to switch your mortgage from Bank A to Bank B, which has a lower interest rate. Normally, you would need the time and funds to pay off the loan at Bank A with your own money before borrowing from Bank B. But wouldn't it be incredibly convenient if you could do the whole process of 'borrowing from Bank B to pay off Bank A' in an instant, and if you couldn't borrow from Bank B, the whole thing would just be canceled?
In the world of DeFi, something similar can be achieved with flash loans. For example, when you want to withdraw collateral (like ETH) deposited in a lending protocol and replace it with other collateral (like WBTC). Normally, it takes the effort of withdrawing ETH and re-depositing WBTC, and there is a risk of being liquidated due to insufficient collateral in the meantime. But by using a flash loan, you can borrow the necessary WBTC, swap it with ETH, and use the remaining ETH to repay the borrowed WBTC, all in one atomic (indivisible) operation.
There are also interesting uses like self-liquidation. When your borrowing position is about to be liquidated, you borrow funds with a flash loan to pay off the entire debt and withdraw the collateral. Then, you sell a portion of the withdrawn collateral to repay the flash loan, keeping the rest of the collateral. It's a tricky technique that might allow you to avoid liquidation penalties while minimizing asset losses.
Of course, these require advanced knowledge and skills and are for professionals who can build smart contracts themselves, but do you understand now that flash loans are by no means 'attack-only tools'? Any powerful magic can be used for good or evil depending on the user. The tools of DeFi are the same.
Therefore, Rabbit-senpai thinks that the key to enjoying this wonderland is not to jump to the conclusion that 'flash loans = evil' when you hear news of a flash loan attack, but to have a perspective on the potential of the technology behind it and how to use it safely!
Well, did you have a nice break? Let's get back to the main story!
Summary
Alice: 'Rabbit-senpai, thank you again for today! Like oracle attacks and flash loan attacks, there are various economic risks in DeFi. I felt once again that in a free world, you need the power to protect yourself.'
Alice: 'But if I understand the mechanisms, I feel like I can see why such attacks happen and how to take countermeasures. I will do my DYOR properly, and without forgetting the mindset Rabbit-senpai taught me, I want to explore the world of DeFi little by little!'
Rabbit-senpai: 'Yes, Alice, you will surely be fine. Today's topic might have been a bit difficult, but it is important to proceed step by step, without fear, but with caution. The future of DeFi is being created by new adventurers like you! Now, let's hold our heads high and open the next door!'
Next Episode Preview
Alice: "I'll make sure to review the economic risks of DeFi! But, these aren't the only risks in DeFi, are they...?"
Rabbit-senpai: "Oh, Alice, you've got a sharp eye! Actually, there is one more very mysterious and sometimes troublesome concept of loss in DeFi called 'Impermanent Loss'. Next time, why don't we delve into the mystery of this 'not-so-permanent loss'!"
Alice: "Impermanent loss...? That sounds kind of philosophical! I'm looking forward to it, Rabbit-senpai!"
☕ Adventure Tips Corner ☕
Alice: "Rabbit-senpai! Today's story was also super interesting! Somehow, after reading this article, I've started wanting to try buying some cryptocurrency!"
Rabbit-senpai: "Whoa there, Alice, I understand that feeling well. But, haste makes waste. This adventure log is just a 'map' for understanding how Wonderland works. It's not a 'treasure map'."
Alice: "Not a treasure map...?"
Rabbit-senpai: "That's right. This is by no means intended to be 'investment advice' like 'buy coins here.' Investing is something you do based on your own judgment and responsibility, after studying thoroughly yourself, and only with money that won't affect your daily life if you lose it. If you get that wrong, you'll end up lost in Wonderland. I really want you to keep this promise."
Alice: "Yes, I understand! With my own responsibility, right! But, I also got a little worried... what if the information in this article is wrong?"
Rabbit-senpai: "You've got a good point there, Alice. This Rabbit-senpai tries to provide the most accurate information possible, but this world changes at the speed of light. Information might become outdated, or I might make mistakes. So, if any of you readers notice, 'Huh? This information might be a bit off,' it would be a huge help if you could kindly let me know. I want to grow this adventure log into something even more wonderful with everyone's help!"
Alice: "So it's an adventure log we create together! That's wonderful!"
Related Pages
Pages on DeFi in General and Economic Risks
-
A Beginner's Guide to Decentralized Finance (DeFi) - (Coinbase)
-
The Risks of DeFi: A Deep Dive into the Darker Side of Decentralized Finance - (Halborn)
-
What are the risks of DeFi? Explaining 6 risks and measures for safe use - (Coincheck)
-
Decentralized Finance (DeFi): What It Is and How It Works - (Investopedia)
-
DeFi Risks and Countermeasures: 6 Points for Safe Trading - (GMO Coin)
Pages regarding Oracle Attacks and Flash Loan Attacks
-
What Is an Oracle Attack? - (Chainlink)
-
What Are Flash Loan Attacks? - (Chainlink)
-
Explaining the Most Common Flash Loan Attack Variants - (101 Blockchains)
-
What is a DeFi Flash Loan Attack? Explaining the Mechanism and Past Cases - (CoinPost)
-
What is an "Oracle Attack" Targeting DeFi? Explaining its Mechanism and Countermeasures - (New Economy)
Page Regarding Smart Contract Vulnerabilities and Security Audits
-
What Is a Smart Contract Audit? - (CertiK)
-
Top 10 Smart Contract Vulnerabilities - (ConsenSys)
-
What Are Smart Contract Vulnerabilities? Explaining Major Cases and Countermeasures - (techmedia-think.com)
-
Security Best Practices for DeFi - (ConsenSys)
Other pages related to technology and risks
-
What Is Impermanent Loss? - (Binance Academy)
-
What Is Impermanent Loss? [For Beginners] - (DeFi Llama)
-
What Is a DAO? - (Ethereum.org)
-
Do Your Own Research (DYOR): A Guide for Crypto Beginners - (CoinMarketCap)
-
What Is Time-Weighted Average Price (TWAP)? - (Binance Academy)
-
What is Arbitrage? Explaining the Mechanism in Crypto Assets (Virtual Currency) - (DMM Bitcoin)
URL: https://bitcoin.dmm.com/column/202202_006.html
