DeFi has taken the world by storm with its limitless possibilities and applications. This report looks at several common DeFi attack vectors and mitigation measures, including price oracle manipulation, replay attack, and rug manipulation.
- Decentralized finance (DeFi) has caused a stir in the world due to its limitless possibilities and applications. According to DeFi Pulse, the industry as of June is 1600 g. surpassed $ billion Total Reserved Value (TVL).
- DeFi protocols have become an attractive target for hackers because the open and immutable nature of smart contracts makes hacks difficult to prevent. As of June 1600 d. in the last 8 months due to hacks and exploits, assets were stolen for more than $200 million A growing number of attacks could slow down widespread DeFi adoption.
- This report explores several common attack vectors on DeFi, including manipulation of price oracles, re-entry and rug pull attacks, and how to mitigate them.
Price manipulation oracles using flash loans is the most common exploit in recent years. Attackers can use a large number of unsecured loans to manipulate the displayed prices of assets on decentralized exchanges (DEXs). If a protocol relies on just one DEX as the only price oracle, then it becomes vulnerable to price data manipulation.
Reentry, or reentrancy, is one of the most well-known and destructive attacks faced by smart contract developers year after year. When hackers carry out this attack, the balance of the smart contract can be completely erased. By definition, a procedure is said to be reentrant if its “execution can be interrupted in the middle and restarted (relogin) and both runs can complete without any runtime errors.” Therefore, this puts the smart contract in an “inconsistent state” and leads to vulnerabilities.
What is reentrancy b?
Imagine a poorly programmed ATM that only checks your account balance when you withdraw your card. What happens if you request withdrawals again and again? You will sooner or later withdraw all the money from the ATM, as it will not understand that the amount withdrawn potentially exceeds the balance of your account until you withdraw the card. This is the main mechanism of the reentrancy exploit that was used in the famous The DAO hack in 2016 d.
Interaction between smart contracts
Smart contracts should not send more money than they contain. However, by taking advantage of reentrancy, hackers can actually turn a smart contract into a poorly programmed ATM. Before diving into how smart contracts can be exploited through reentrancy, we first need to know some of the basics of how smart contracts interact with each other. basic actions of a smart contract. When developing smart contracts in the Solidity language, two types of functions are mainly called to perform basic tasks:
External call functions allow contracts to interact with each other and call other functions from the same or another contract, for example:
Internal call functions:
one. Internal audit (i.e. balance check).
All smart contracts on Ethereum by default contain a fallback function, which is fully customizable and which the developer can replace with arbitrary code. For example, if it is replaced with a payable function, the smart contract will be able to receive ether and the function will be executed every time this happens. You can program it to request withdrawal of funds from the target contract after receiving the ether.
Imagine contracts A and B, where A is the vulnerable contract and B is the contract used by the attacker. The sequence of actions will be as follows:
Because the real balance of B in contract A will not be updated until the withdrawal cycle is completed, the hacker can recursively call the output function until contract A is completely empty.
The DAO Hack
The DAO – popular decentralized investment fund based on smart contracts. AT 1692 d. The DAO smart contracts accumulated $40 million (at the then exchange rate). June 2016 d. it was hacked and 3.6 million ether was stolen ($ million) using a cross-functional reentry attack. The Ethereum Foundation has released a critical update to roll back the hack. This led to the split of Ethereum into Ethereum and Ethereum Classic. To carry out the attack, the hacker used Ethereum’s fallback feature.
How to prevent
There are a number of common ways to avoid a reentry attack. Solidity supports three ways to transfer ether between wallets and smart contracts: send(), transfer() and call.value(). These methods differ only in the gas limit when executing the function. If you use the send() or transfer() function instead of call.value(), recursive output calls will not be allowed due to the low gas limit. In addition, to avoid recursive output, the contract must update its internal state (for example, the user’s balance) before making any transaction.
Another possible approach is to whitelist certain external contracts in order to limit interaction with unknown contracts.
Rug pull or “rag-pull” (lit. “kidok”, “knocking the soil out from under your feet”) – a popular m a scam in the DeFi ecosystem widely used by unscrupulous developers as it is a technically very simple but extremely lucrative type of attack. Rug pull is when an unscrupulous team suddenly removes all traces in social networks, abandoning the project, and disappears with the funds of investors. For example, the team could withdraw all purchase support from the liquidity pool on the decentralized exchange (DEX). Fraudsters have full control over the project protocol, so they can create and list tokens on the exchange without an audit and even have the right to take liquidity.
What is rug pull
Fraudsters can create a token on a DEX like Uniswap or SushiSwap and pair it with a leading cryptocurrency like Ethereum. They then typically promise retail investors such as “yielding farmers” ridiculously high annual percentage returns. When investors exchange their Ether for new tokens and enough funds are reserved in the smart contract, developers can empty the liquidity pool and disappear with the funds. A sudden loss of liquidity will lead to a massive sale of tokens as their holders want to save their profits.
Case 1: Meerkat Finance
Meerkat Finance is a profitable farming protocol on Binance Smart Chain (BSC), whose developers disappeared a day after the launch in March 2020 g. s approx. million BUSD and 13 BNB, totaling approximately $ million. After the incident, the Meerkat website and Twitter account were deleted. It turned out that shortly before the attack, the developers updated the contract, giving themselves the right to withdraw assets from the pool.
Case 2: TruAmpl token (TMPL)
More one example is the famous rug pull TMPL that happened in August 2020 when the contract creator withdrew all liquidity, including 57 ETH and 2 659 50 TMPL tokens, total through 13 minutes after the start of the public token sale.
The sequence of events can be summarized as follows:
Scammers (0x5d17a2b59328c1e387806ebefaebcf21a03a114e) created a TMPL token pool contract (0x7a114d2310B4cF170963dF2C5dAcb4cD) on Uniswap and provided liquidity in the form of 64 ETH and 3 TMPL for promotion.
Users exchanged their valuable tokens, such as like ETH, to TMPL, increasing the pool participation rate and the price of TMPL.
Scammers took liquidity from the platform and profited from valuable tokens.
How to prevent
To protect yourself from rug pull attacks, you should study the project before investing in it. You should make sure the team is reliable by finding out the goals and the wrong side of the project. In particular, you can check the track record of developers, their social media footprints, experience, etc. You can also use blockchain explorers like Etherscan to check the number of token holders. If tokens have very few holders and are only listed on decentralized exchanges, there is a high possibility of fraud.