DeFi 102: Common Attacks To Be Aware Of

DeFi 102: Common Attacks To Be Aware Of

By Michael @ CryptoEQ | CryptoEQ | 26 Mar 2024


You are reading an excerpt from our free but shortened abridged report! While still packed with incredible research and data, for just $40/month you can upgrade to our FULL library of 60+ reports (including this one) and complete industry-leading analysis on the top crypto assets. 

67cbbf4723857b85c151585aa280e6d940346c501cef75bafd7dea02b44b24c9.png

Becoming a Premium member means enjoying all the perks of a Basic membership PLUS:

  • Full-length CORE Reports: More technical, in-depth research, actionable insights, and potential market alpha for serious crypto users
  • Early access to future CORE ratings: Being early is sometimes just as important as being right!
  • Premium Member CORE+ Reports: Coverage on the top issues pertaining to crypto users like bridge security, layer two solutions, DeFi plays, and more
  • CORE report Audio playback: Don’t want to read? No problem! Listen on the go.

 

Oracle Attacks

In an oracle manipulation attack, the goal is to trick a system (in this case, a lending protocol) into thinking that more money has been deposited than actually has been. This allows the attacker to borrow more money than they should be able to. For example, if an attacker deposits $1,000, but manipulates the protocol to think they deposited $5,000, they might be able to borrow $3,000. This is a problem because the protocol has been tricked into giving out more money than it should.

One way that attackers can do this is by messing with the prices of cryptocurrencies on decentralized exchanges like Uniswap. Uniswap is a platform where people can trade different cryptocurrencies. Some older or less reputable projects have made the mistake of using Uniswap to get price information. This is risky because it's easy for an attacker to manipulate the prices on Uniswap by taking a short-term loan (called a flashloan) and temporarily changing the price.

Another way that attackers can manipulate prices is by targeting less-liquid assets like LP tokens or shares in vaults. LP tokens are tokens that represent a share of a liquidity pool on a decentralized exchange. An example of this type of attack is the Warp Finance hack. Warp Finance allowed people to deposit LP tokens as collateral for loans. To determine the value of these tokens, Warp Finance used a formula that involved the total value locked (TVL) in a liquidity pool.

However, this formula was flawed because it didn't account for the fact that the TVL can change dramatically when large trades are made. An attacker took advantage of this by taking a flashloan and making a large trade, which affected the TVL calculation and, in turn, the value of the LP tokens. This allowed the attacker to trick the protocol into thinking they had deposited more money than they actually had.

Chainlink is a popular solution for obtaining secure random numbers. It works in two steps:

  1. The smart contract sends a randomness request to the oracle.
  2. Some blocks later, the oracle responds with a random number.

Since an attacker cannot predict the future, they cannot predict the random number. However, if the smart contract uses the oracle incorrectly, an attacker can exploit the following vulnerabilities:

  • Frontrunning: An attacker can monitor the mempool for the oracle returning the randomness and frontrun the oracle, knowing what the random number will be.
  • Manipulation: The randomness oracles themselves might try to manipulate your application. They cannot pick random numbers without consensus from other nodes, but they can withhold and re-order random numbers if your application requests several at the same time.
  • Chain reorgs: Finality is not instant on Ethereum or most other EVM chains. Just because some block is the most recent one, it doesn't mean it won't necessarily stay that way. This is called a "chain re-org." In fact, the chain can alter more than just the final block. This is called the "re-org depth." Etherscan reports re-orgs for various chains, for example Ethereum reorgs and Polygon reorgs. Reorgs can be as deep as 30 or more blocks on Polygon, so waiting fewer blocks can make the application vulnerable.

Here are some other security considerations for using Chainlink randomness:

  • Stale data: There is no SLA (service level agreement) for Chainlink to keep its price oracles up to date within a certain time frame. When the chain is severely congested, the price updates might be delayed. A smart contract that uses a price oracle must explicitly check the data is not stale, i.e. has been updated recently within some threshold. Otherwise, it cannot make a reliable decision with respect to prices.
  • Relying on only one oracle: No matter how secure an oracle seems, an attack may be discovered in the future. The only defense against this is to use multiple independent oracles.
  • Oracles in general are hard to get right: The blockchain can be quite secure, but putting data onto the chain in the first place necessitates some kind of off-chain operation which forgoes all the security guarantees blockchains provides. Even if oracles remain honest, their source of data can be manipulated. For example, an oracle can reliably report prices from a centralized exchange, but those can be manipulated with large buy and sell orders. Similarly, oracles that depend on sensor data or some web2 API are subject to traditional hacking vectors.

A good smart contract architecture avoids the use of oracles altogether where possible.

Oracle Exploit (vs. Market Manipulation)

An oracle exploit occurs when an oracle reports inaccurate data about an event or state of the external world. This can happen because the oracle purposefully acts maliciously or negligently, or the oracle’s data source is compromised.

There are two main types of oracle exploits:

  • Misreporting: This is when an oracle reports a price that differs from the correct market-wide price of an asset. Regardless of whether misreporting occurs due to malicious or negligent behavior, any protocol relying on a faulty oracle for price data may be at risk of an exploit.
  • Poor market coverage: This is when an oracle relies on only a subset of all trading environments to report the price of an asset. This can lead to the oracle misreporting the price of an asset if that subset is manipulated, even when the majority of trading environments and the market-wide price remain unaffected.

The risk of oracle exploits can be mitigated with more secure oracle design. Features of secure oracles include:

  • Sourcing price data from across all trading environments to provide proper market-wide coverage: This helps to ensure that the oracle is not vulnerable to price manipulation in a single trading environment.
  • Protections from external tampering: This can be achieved by decentralizing the oracle or by using other security measures to prevent unauthorized access to the oracle's data.
  • Economic incentives to report faithfully: This can be achieved by rewarding oracles for reporting accurate data and by penalizing them for reporting inaccurate data.

By following these design principles, oracles can be made more secure and resistant to exploitation.

Here are some of the negative consequences of oracle exploits for DeFi protocols:

  • Depegged stablecoins: If an oracle reports an inaccurate price for a stablecoin, this can lead to the stablecoin becoming depegged from its target asset.
  • Malicious arbitrage trades: An attacker could use an oracle exploit to make malicious arbitrage trades that profit at the expense of other users.
  • Unwarranted liquidations: If an oracle reports an inaccurate price for an asset, this could lead to users being liquidated even though they are not in default.
  • Protocol insolvency: If an oracle exploit leads to a large loss of funds, this could make a protocol insolvent.

Oracle exploits are a serious threat to DeFi protocols. By understanding the risks and taking steps to mitigate them, we can help to protect our funds and ensure the continued security of DeFi.

Frontrunning: Sandwich attacks

A sandwich attack is a type of frontrunning attack that exploits the fact that the price of an asset can move in response to buying and selling pressure.

To understand how a sandwich attack works, it is important to understand how the price of an asset is calculated on the blockchain. The price of an asset is typically calculated as the current exchange rate between assets. For example, if a contract is currently trading 1 USDC for 100 k9coin, then you could say k9coin has a price of 0.01 USDC.

However, prices generally move in response to buying and selling pressure. If a large order is sitting in the mempool, traders have an incentive to copy the order but with a higher gas price. That way, they can purchase the asset before the large order, let the large order move the price up, and then sell the asset right away.

The sell order is sometimes called “backrunning.” The sell order can be done with by placing a sell order with a lower gas price so that the sequence looks like this:

  1. Frontrun buy
  2. Large buy
  3. Sell

The primary defense against this attack is to provide a “slippage” parameter. If the “frontrun buy” itself pushes the price up past a certain threshold, the “large buy” order will revert, making the frontrunner fail on the trade.

It’s called a sandwich, because the large buy is sandwiched by the frontrun buy and the backrun sell. This attack also works with large sell orders, just in the opposite direction.

Here are some tips for avoiding sandwich attacks:

  • Use a slippage parameter.
  • Monitor the mempool for large orders.
  • Use a decentralized exchange (DEX) that uses a fair ordering mechanism.

By following these tips, you can help to protect yourself from sandwich attacks.

Market Manipulation

Market manipulation is when the price of an asset is artificially influenced by a malicious actor. This is done by altering the natural supply and demand forces of the market. Market manipulation can be used to make a profit at the expense of other traders.

Low-liquidity assets are more vulnerable to market manipulation. Liquidity refers to how easy it is to buy or sell an asset without impacting its price. The greater an asset's liquidity, the more capital it takes to manipulate its price. Conversely, the lower the liquidity, the less expensive it can be. This is why thinly traded assets with low liquidity are more vulnerable to market manipulation.

The liquidity of an asset depends on its trading volume, market depth, and trading markets. Here's the definition of each and why they matter:

  • Trading volume: Trading volume is the amount an asset was traded during a specific period. High trading volume is an indicator of a highly liquid market. However, assets can have high trading volume while lacking market depth.
  • Market depth: Market depth refers to the number of standing buy and sell bids at various price levels. It indicates the ability of a market to absorb unusually large orders without correspondingly large price swings.
  • Trading markets: Assets trade on various platforms simultaneously, including centralized exchanges, decentralized exchanges (DEXs), and OTC (over-the-counter) desks. The liquidity on an individual platform may be lower than the market as a whole, especially when there's insufficient volume to incentivize the arbitrage traders that help create cross-platform markets. This means that even if a specific asset may be highly liquid in the market as a whole, it could have low liquidity on specific exchanges.

Market manipulation methods to protect against:

  • Spoofing: Posting trade orders without the intention of executing them. A bot may be used to post a large number of orders that impact the behavior of buyers and sellers and then cancel those orders before they go through.
  • Ramping: Trades that artificially increase the market price of an asset to drive demand from actual buyers, who the malicious actor then sells to.
  • Bear raid: Attempts to artificially lower the market price of an asset through heavy selling or short selling.
  • Cross-market manipulation: Making trades in a trade environment to manipulate the price in another environment in order to make additional trades that profit from the divergence in price.
  • Wash trading: Both buying and selling an asset to give the impression of a higher trade volume in order to attract legitimate traders to the market.
  • Frontrunning: Making trades based on insider information that the rest of the market doesn’t have access to or before it can respond. In crypto, frontrunning is part of MEV.

These tactics can target either a subset of an asset’s trading environment or the market-wide price. Targeting a smaller sector of an overall market can be less costly to a malicious actor, although these attacks can be easier to defend against and the opportunity to extract funds can be smaller than manipulating the market-wide price.

Risks of market manipulation for DeFi protocols:

  • Money markets: Lending protocols can accrue toxic debt and even become insolvent if they don’t liquidate undercollateralized positions in a timely and efficient manner. Users’ funds can also be unfairly liquidated based on artificial price changes, resulting in losses.
  • Options markets: Similar to money markets, options markets must trigger liquidations efficiently to stay solvent. These liquidations can be unwarranted.
  • Synthetic assets: Liquidity providers can lose their assets if positions based on artificial prices are paid out or used to create bad loans.
  • Algorithmic stablecoins: Stablecoins can lose their peg if the stabilization mechanism and implicit incentives are broken due to artificial price movements.
  • Automated asset management: Trading algorithms that respond to artificial prices can make trades that lead to poor investment outcomes.

Here are some tips for avoiding market manipulation:

  • Use a reputable exchange: Reputable exchanges have measures in place to detect and prevent market manipulation.
  • Be aware of the risks of low-liquidity assets: Low-liquidity assets are more vulnerable to market manipulation.
  • Use market-making tools: Market-making tools can help you to identify and avoid artificial price movements.
  • Stay informed: Stay up-to-date on the latest market manipulation techniques.

By following these tips, you can help to protect yourself from market manipulation.

 

 

How do you rate this article?

52


Michael @ CryptoEQ
Michael @ CryptoEQ

I am a Co-Founder and Lead Analyst at CryptoEQ. Gain the market insights you need to grow your cryptocurrency portfolio. Our team's supportive and interactive approach helps you refine your crypto investing and trading strategies.


CryptoEQ
CryptoEQ

Gain the market insights you need to grow your cryptocurrency portfolio. Our team's supportive and interactive approach helps you refine your crypto investing and trading strategies.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.

Page not displaying correctly?