Skip to content
Smart contracts Audit finding Advanced

Sandwich attack on astroport sweep

The collector contract allows anyone to call a sweep function that swaps asset tokens to ANC through Astroport without setting a belief price or ensuring a maximum spread. An attacker could sandwich this trade to profit at the protocol's expense, causing the protocol to receive fewer tokens than at a fair market price.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

https://github.com/code-423n4/2022-02-anchor/blob/7af353e3234837979a19ddc8093dc9ad3c63ab6b/contracts%2Fanchor-token-contracts%2Fcontracts%2Fcollector%2Fsrc%2Fcontract.rs#L130-L137

The collector contract allows anyone to sweep, swapping an asset token to ANC through astro port. Note that belief_price is not set and config.max_spread might not be set as well or misconfigured.

This allows an attacker to create a contract to perform a sandwich attack to make a profit on this trade.

A common attack in DeFi is the sandwich attack. Upon observing a trade of asset X for asset Y, an attacker frontruns the victim trade by also buying asset Y, lets the victim execute the trade, and then backruns (executes after) the victim by trading back the amount gained in the first trade. Intuitively, one uses the knowledge that someone’s going to buy an asset, and that this trade will increase its price, to make a profit. The attacker’s plan is to buy this asset cheap, let the victim buy at an increased price, and then sell the received amount again at a higher price afterwards.

Trades can happen at a bad price and lead to receiving fewer tokens than at a fair market price. The attacker’s profit is the protocol’s loss.

Proof of Concept

Attacker creates a contract that triggers 3 messages for the sandwich attack:

  • Astroport: buy ANC with asset
  • Call sweep which trades at bad price
  • Astroport: sell assets from the first message for profit

Recommended Mitigation Steps

Consider setting a ANC/asset belief_price from an oracle.

bitn8 (Anchor) disagreed with severity and commented:

While it might certainly be possible to sandwich an attack like this, there is no risk to users’ funds outside a possible bad fill price. There are many other issues at work here too such as UNI v2 that Astro is built on and volatility. This assumes very illiquid markets or huge wallets to move prices like this.

Alex the Entreprenerd (triage) commented:

  • Anyone can call = can set up a price imbalance
  • Is a sweep (prob dust amount)

Looks valid but impact seems reduced.


The original report as published on Code4rena, by cmichel. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the sweep function performs a token swap without setting a belief price or enforcing a maximum spread, allowing the trade to execute at a manipulated price.

Impact

The protocol could receive fewer tokens than at a fair market price, and the attacker's profit is the protocol's loss; however, the Anchor team disputed the severity, noting no risk to users' funds outside a possible bad fill price and that the impact seems reduced.

What the attacker needed

Anyone can call sweep; Attacker can deploy a contract; Astroport liquidity pool exists

The fix

The recommendation is to set a belief price for the ANC/asset pair from an oracle.

How it was found

  1. Attacker creates a contract that sends a message to Astroport to buy ANC with the asset token.
  2. The contract calls sweep, which trades at a bad price.
  3. The contract sends a message to Astroport to sell the assets from the first message for profit.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A sandwich attack is when someone sees a trade coming and places their own trades before and after it to profit from the price change. Here, the contract lets anyone trigger a trade that swaps tokens without checking if the price is fair, so an attacker can manipulate the price and make money at the protocol's expense.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by cmichel

View all

196 more disclosed reports, best first.

More from Anchor contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.