Reproduced Exploit

Berabot: a path longer than two tokens quotes every hop with the original amountIn

_executeV2Swap loops over path but passes the original amountIn into _calculateAmountOut on every hop. WETH to DOGE is quoted correctly. DOGE to DAI is quoted as if 1 WETH of DOGE arrived, so the router asks the second pair for about 0.997 DAI. The pair accepts it, because the real DOGE input is mu…

Jan 2025•Ethereum•logic•4 min read

Loss

1 WETH taken by the first pair. The user receives 996995029979775550 wei of DAI, about 0.997, instead of 1949…

Chain

Ethereum

Category

logic

Date

Jan 2025

Source

AuditVault

EVM Playground

Source-level debugger — step opcodes and Solidity in sync

evm-hack-analyzer

The attack is replayed in an in-browser EVM preloaded with the exact dumped fork state. The execution tree shows every call; step by Solidity line or by opcode across all depths — source, Stack, Memory, Storage, Balances (native / ERC-20 / NFT), Transient storage and Return value stay in sync. Click a tree node, opcode, or source line to jump. Query opens a search over the trace: previous and next writes, balance changes and calls, breakpoints, and a diff of everything that changed between two steps. No backend, no live RPC.

Loading fork state…

Source & credit. Reproduction of a public audit finding curated by AuditVault — the original finding: 45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is. Standalone Foundry PoC and full write-up: 45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/price-calculation · vuln/defi/slippage

AuditVault taxonomy: severity/high · platform/shieldify · sector/dex

Key info#

FieldValue
Loss1 WETH taken by the first pair. The user receives 996995029979775550 wei of DAI, about 0.997, instead of 1949209071948685202240 wei, about 1949.209 DAI
Vulnerable contractBerabotRouter._executeV2Swap
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2025-01
Compilersolc 0.8.27, cancun, via IR
Bug classMulti-hop swap never replaces amountIn with the previous amountOut

TL;DR#

_executeV2Swap loops over path but passes the original amountIn into _calculateAmountOut on every hop. WETH to DOGE is quoted correctly. DOGE to DAI is quoted as if 1 WETH of DOGE arrived, so the router asks the second pair for about 0.997 DAI. The pair accepts it, because the real DOGE input is much larger. The user spent 1 WETH. A router that writes amountOut back into the next input pays the full quote, 1949.209 DAI.

Background#

Shieldify's Berabot review, January 2025, H-01. BerabotRouter mirrors a Uniswap V2 router. A path such as WETH, DOGE, DAI is meant to chain the output of one pair into the input of the next. The published fix keeps a _amountIn local and sets _amountIn = amountOut at the end of the loop. The team marked the issue fixed.

The vulnerable code#

SOLIDITY
amountOut = _calculateAmountOut(reserveIn, reserveOut, amountIn, _router);
// @> VULN: every hop quotes with the original amountIn, not the previous hop's amountOut

The recommended change from the report:

DIFF
- amountOut = _calculateAmountOut(reserveIn, reserveOut, amountIn, _router);
+ amountOut = _calculateAmountOut(reserveIn, reserveOut, _amountIn, _router);
  Pair(pair).swap(...);
+ _amountIn = amountOut;

Root cause#

amountIn is the function argument. The loop assigns amountOut but never feeds it into the next iteration. Hop 0 is accidentally correct. Every later hop prices a different pair with the first token's input amount.

Preconditions#

  • Two V2 pairs exist: WETH/DOGE with reserves 100 and 200000, and DOGE/DAI with reserves 200000 and 200000. Fee is 0.3 percent.
  • The caller swaps 1 WETH along a path of length 3 and does not set an amountOutMin high enough to revert the dust output. With a correct minimum the same bug reverts the swap instead of underpaying.

Attack walkthrough#

  1. The chained quote, which does replace the input, returns 1949209071948685202240 wei of DAI.
  2. The router pulls 1 WETH into the first pair and quotes hop 1 with that 1 WETH.
  3. Hop 2 still uses 1 WETH against the DOGE/DAI reserves. amountOut is 996995029979775550 wei.
  4. The second pair's K check passes because the DOGE that actually arrived is the hop-1 output, not 1 wei-unit. The user is paid the dust.
  5. The control router chains amountOut into the next input and pays the full quote.

Diagrams#

flowchart TD A["User swaps 1 WETH along WETH, DOGE, DAI"] --> B["Hop 1 quotes DOGE out from 1 WETH"] B --> C["DOGE is sent to the second pair"] C --> D["VULN hop 2 still quotes with 1 WETH"] D --> E["User receives about 0.997 DAI"] E --> F["First pair keeps the 1 WETH"] B --> G["Control feeds hop-1 output into hop 2"] G --> H["User receives about 1949 DAI"]

Impact#

The first pair's WETH balance rises by 1 WETH. The user receives 996995029979775550 wei of DAI. The fair multi-hop output on these reserves is 1949209071948685202240 wei. The difference stays in the pairs. If the caller had set amountOutMin to the fair quote, the swap would revert and the path would be unusable, which is the failure mode the report describes.

Remediation#

Track _amountIn, pass it to _calculateAmountOut, and set _amountIn = amountOut after each hop.

How to reproduce#

BASH
cd 45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is_exp
forge test -vv

test_multihop_pays_dust_and_keeps_weth logs the fair and actual DAI amounts. test_control_chained_amount_in_pays_fair_dai asserts they match once the input is chained. Captured output is output.txt.

Sources#


Sources & further analysis#

Reproductions & code

Alerts & third-party analyses

  • Web3Sec X hacked database: search.
  • Rekt leaderboard: search.
  • Solodit incident search: search.

These dashboards index community alerts tweets, post-mortems, and independent write-ups. Reach them through the protocol name above to cross-check this reproduction against other analyses.