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…
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
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.
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-registrymirror.
Vulnerability classes:
vuln/logic/price-calculation·vuln/defi/slippage
AuditVault taxonomy: severity/high · platform/shieldify · sector/dex
Key info#
| Field | Value |
|---|---|
| Loss | 1 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 contract | BerabotRouter._executeV2Swap |
| Attacker EOA | 0x1111111111111111111111111111111111111111 |
| Attack contract | Exploit |
| Attack tx | Exploit.run() |
| Chain / block / date | Ethereum model · block 0 · 2025-01 |
| Compiler | solc 0.8.27, cancun, via IR |
| Bug class | Multi-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#
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:
- 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
amountOutMinhigh enough to revert the dust output. With a correct minimum the same bug reverts the swap instead of underpaying.
Attack walkthrough#
- The chained quote, which does replace the input, returns 1949209071948685202240 wei of DAI.
- The router pulls 1 WETH into the first pair and quotes hop 1 with that 1 WETH.
- Hop 2 still uses 1 WETH against the DOGE/DAI reserves.
amountOutis 996995029979775550 wei. - 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.
- The control router chains
amountOutinto the next input and pays the full quote.
Diagrams#
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#
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#
- AuditVault finding: https://github.com/Auditware/AuditVault/blob/main/findings/45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is.md
- Shieldify report: https://github.com/shieldify-security/audits-portfolio-md/blob/main/Berabot-Security-Review.md
- Portfolio entry: Shieldify audits portfolio, Berabot, January 2025, DEX aggregator
Sources & further analysis#
Reproductions & code
- Standalone PoC + full trace: 45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is_exp (evm-hack-registry mirror).
- AuditVault finding: 45354-h-01-berabotrouter-calculation-for-paths-longer-than-two-is.
Alerts & third-party analyses
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.