Lessons learned from DeFi, review the attack logic in DeFi

拔丝地瓜
本文约3412字,阅读全文需要约14分钟
More and more people looking to steal funds by attacking smart contracts are exploiting vulnerabilities that arise when smart contracts are put together.

Editor's Note: This article comes fromCrypto Valley Live (ID: cryptovalley), Author: Mika Honkasalo, translation: Liam, reproduced by Odaily with authorization.

Summary

  • Editor's Note: This article comes from

  • Crypto Valley Live (ID: cryptovalley)

  • Crypto Valley Live (ID: cryptovalley)

, Author: Mika Honkasalo, translation: Liam, reproduced by Odaily with authorization.

Summary

More and more people looking to steal funds by attacking smart contracts are exploiting vulnerabilities that arise when smart contracts are put together.

In 2020, in the attacks on DeFi, the total amount of fraud or theft has reached 36 million US dollars. But because the dForce attackers refunded the stolen $25 million, the actual amount was roughly $11 million.

Compared to the early days of Ethereum, the average value lost per hack has dropped significantly. Eight out of 10 attacks in 2020 were for less than $1 million.

Compared to traditional cybercrime, information on the blockchain is more valuable to the attacker because a successful attack on a smart contract can lead to immediate financial rewards. Information that has been traditionally hacked, such as stolen personal information, needs to be sold to make money, but smart contracts store assets directly.

In the early days of Ethereum, most attacks were based on finding individual vulnerabilities that gave attackers the ability to freeze or drain smart contracts. This was the case with the infamous DAO hack in 2016, where $160 million in ETH was stolen and Ethereum was eventually forked. Similarly, the Parity multi-signature attack in 2017 allowed hackers to steal $30 million, and $150 million in Parity wallets was frozen, all as a result of this type of vulnerability.

  1. Vulnerabilities in such smart contracts are still being exploited from time to time. Recently, an attacker managed to steal all VETH from the token contract, making a profit of $900,000 just by draining the VETH-ETH Uniswap pool. But this is a simple mistake made by VETH, because there is a logical error in the way VETH modified the ERC20 token standard.

  2. In general, security is now improved, especially for those projects with a higher profile. Their increased security has been driven by user expectations around auditing and improved tooling around testing. The biggest security issue in DeFi recently was the theft of $25 million in digital assets from dForce in the lending market. However, the funds were returned as the attacker's IP address was discovered and shared with the Singapore police.

  3. The dForce attack can also be considered the result of a particularly serious negligence by the development team, as the exploited vulnerability was re-entrancy. Re-entrancy attacks are the reason DAOs are drained, and this has always been a well-known issue that developers need to consider when interacting with ERC777 tokens.

  4. A distinctive feature of ERC777 tokens is the ability to notify smart contracts that funds will be received or sent - and allow the contracts to act on this information. This example shows how contracts are vulnerable to reentrancy attacks, such as allowing users to withdraw all funds from the contract.

The user invokes the contract, ready to withdraw all funds from the contract."reentry"The contract checks to see if the user has funds in the contract.

The contract renews itself and the user has no funds in the contract.

A reentrancy vulnerability allows a malicious user to call the contract again before the contract is fully executed (

reentry

). In the example above, an attacker could re-enter the contract between steps 3 and 4 and withdraw again before the user's balance was updated. By repeating this process, they can withdraw all existing funds from the contract.

This principle is used to attack dForce, when the contract believes that the user is continuously increasing the amount of imBTC collateral (an ERC777 token)-then the contract will allow the user to borrow more funds from the system. The attackers increased their fake imBTC collateral to well over $25 million, and then withdrew all of the liquidity in dForce in lending transactions.

secondary title

Hacking through composability

A user siphoned $220,000 from the imBTC-ETH Uniswap pool using the same exploit based on the ERC-777 standard used in the dForce attack. In the case of Uniswap, the attacker removed ETH from the ETH-imBTC token pool, but the imBTC token balance did not increase. Such manipulations typically increase the price of ETH.

Unlike the dForce case, Uniswap's development team has no real responsibility here, and it's nearly impossible for them to prevent people from falling into this type of trap. Even if the Uniswap team made sure their interface didn't allow users to add liquidity to a potentially drained pool, they couldn't prevent other interfaces from being built on top of smart contracts that allowed it. In fact, this particular attack has been widely discussed many years ago.

The recent June 18 attack on Balancer is a similar example of exploiting non-standard programming patterns in a specific market. STA is an ERC20 token with an additional deflation model that takes a 1% fee from each transfer. The attacker used smart contracts to automatically perform multiple operations in a single transaction to exchange WETH and STA tokens back and forth 24 times, exhausting the STA tokens in one of the Balancer liquidity pools until only 0.0000000000000000001 STA was left in the pool ( 1 weiSTA). The Balancer pool is unaware of STA's deflationary model and sets a new price for its assets based on 1 weiSTA. The attacker then traded WETH, LINK, SNX, and WBTC in the pool. Through this attack, the funds of the two pools were lost more than 500,000 US dollars.

A day later, Balancer suffered a second vulnerability issue, also caused by unexpected behavior interacting with other smart contracts. Compound's token distribution requires users to claim their COMP by interacting with Compound's smart contracts. Users who want to earn both COMP and BAL (the Balancer token earned by providing liquidity) will need to create a liquidity pool with COMP so that they will be eligible for both tokens.

The attacker realizes that the new COMP generated by the assets in the Balancer liquidity pool does not belong to anyone, so the attacker can enter a pool, obtain these available COMP tokens, and then put them into the liquidity Pool more money and leave. As in the case of the vulnerabilities discovered now, the attackers use flash loans to maximize their own capital, and then obtain the maximum profit.

secondary title"Exploiting the unique properties of the blockchain to carry out attacks

One protocol that has long suffered from such issues is Synthetix. From September 2019 to February 2020, the protocol's arbitrage bots were constantly running ahead of the oracle update. In Synthetix, transactions are not transactions between buyers and sellers through matching, but transactions based on price feeds provided by oracles, and liquidity is provided by SNX token holders.

When Synthetix's oracle publishes a price feed on-chain, the bot pays a high transaction fee to be included in an Ethereum block before the oracle has a chance to update the price. Because of this preemptive arbitrage behavior, the trading volume of Synthetix reached a maximum of 69 million US dollars in a single day, while the average trading volume of Synthetix without arbitrage opportunities today is only 1.5 million US dollars.

On March 12 (

Black Thursday"), the owner of the vault (vault) unexpectedly lost $8.3 million in MakerDAO liquidation, transaction fees were also a factor in the accident. When MakerDAO's vault (where users deposit collateral and generate Dai )'s collateral is insufficient, their collateral (such as ETH) will be auctioned to raise DAI and repay the debt of the liquidated treasury. Typically, the winning bidders (keepers) in the auction will exchange DAI for ETH, then these DAI will be destroyed. Once the debt is paid off, the original owner of the treasury will receive the remaining ETH.

On March 12, due to the sudden drop of more than 50% in the price of ETH, many treasuries entered the liquidation process. At the same time, the Ethereum network became very congested, and the gas soared to 400gwei or 20 times the normal price, which made it impossible for most keepers to quote instantly. A keeper kept triggering the liquidation and won the available ETH with a bid of $0.

secondary title

Governance attacks that exist in the future?