Original title: "How to hack SushiSwap through constant product?" A Brief Analysis of SushiSwap’s Second Attack”
Original Source: yudan@ Slow Mist Security Team
< br>
January 27, 2021, according to slow Fog area intelligence, SushiSwap was attacked again. This time, the problem was that the transaction fee of the DIGG-WBTC transaction pair was stolen by the attacker through special means. After receiving the information, the SlowMist security team immediately intervened in the analysis of related incidents. The details of the attack are as follows.
SushiMaker is an important component in the SushiSwap protocol, which is used to collect the handling fee for each transaction pair of SushiSwap, and by setting the route of each token, The handling fees of different trading pairs are finally converted into sushi tokens and given back to the holders of sushi tokens. This process happens on the SushiMaker contract.
The formula of the constant product is very simple, without calculating the handling fee, the formula of the constant product is

That is to say, every exchange is actually followed This formula and the K value before and after the transaction remain unchanged. During the exchange process, since the K value must be kept unchanged, the form of the formula will look like this
< br>

Where X stands for tokens to sell and Y stands for tokens to buy , then the number of tokens that can be exchanged each time will look like this (the specific derivation process will not be demonstrated: D)

It can be seen from the formula that when the upper limit of the exchange amount of the output token Y depends on the number of Y tokens, and the upper limit of the number of X tokens Size has nothing to do, conversely, if the number of X tokens to be sold is large, but the number of Y tokens is small, then a large number of X tokens can only be exchanged for a small amount of Y tokens, and this exchange The price will deviate a lot from the normal transaction price. This is the so-called slippage, which is the key in this attack.
On November 30, 2020, SushiSwap had There was an attack on the problem (see: A brief analysis of the Sushi Swap attack from a small perspective), this attack is similar to the first attack, But the process is different. Compared with the old contract, in the new contract, the service fee will find a specific exchange route for the tokens in different trading pairs through the bridgeFor function during the exchange process, and then perform the exchange.

Among them, the logic of the bridgeFor function is as follows:

According to the logic of bridgeFor, we It is not difficult to find that if the bridge of a specific currency has not been manually set, the default bridge is WETH. That is to say, if the bridge is not set, the default is to convert the handling fee into WETH. As for the DIGG coin, it happens that the corresponding bridge has not been set through setBridge.

But there is still a problem here, that is, during the swap process, if the transaction pair does not exist, the exchange process will fail. In this attack, the DIGG-WETH transaction pair did not exist at the beginning, so the attacker pre-created a DIGG-WETH transaction pair, and then added a small amount of liquidity. If there is a transaction fee exchange at this time, according to the characteristics of the constant product mentioned above, because the liquidity of DIGG-WETH is very small, that is, the upper limit of WETH in DIGG-WETH is very small, and the procedures to be converted in SushiMaker The fee amount is relatively large, such an exchange can cause huge slippage. The conversion process will increase the price of WETH to DIGG in the DIGG-WETH transaction pair, and all the DIGG fee income of DIGG-WETH will go to the DIGG-WETH transaction. By observing the liquidity of the DIGG-WETH trading pair, the liquidity was only less than $2,800 when the liquidity was the largest. This result can also be mutually verified with the derivation of the formula.

After the SushiMaker completed the fee conversion, the price of WETH against DIGG in the DIGG-WETH transaction pair has been raised, resulting in a small amount of WETH A large amount of DIGG can be exchanged, and this amount of DIGG is the majority of the service fee income of the DIGG-WBTC transaction pair.

This attack is similar to SushiSwap’s first attack, They all generate profits by manipulating the exchange price of trading pairs. But the process is different. The first attack was because the attacker created a new trading pair using the LP token itself and other tokens, and manipulated the price of this new trading pair by manipulating the initial liquidity to make a profit. This attack DIGG itself does not have a transaction pair for WETH, but the attacker created this transaction pair and manipulated the initial transaction price, resulting in a huge slippage during the exchange of fees. The attacker only needs to use a small amount of DIGG and WETH Huge profits can be obtained by providing initial liquidity.
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia