# RES-Token: pair manipulation via thisAToB drains USDT liquidity

RES token's thisAToB() cost function was manipulated via repeated flash-swap buys on the USDT/RES pair, then dumping RES and ALL tokens for ~290k USDT.

- Payout: Real-world hack
- Severity: high
- Program: RES-Token
- Analysis by: DeFiHackLabs
- Disclosed: 2022-10-06
- Write-up quality: 6/10
- Original: https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-10/RES_exp.sol
- Library page: https://bugbountylibrary.com/r/dhl%3A20221006-res
- Tags: area:web3 domain:web3 impact:funds-loss kind:incident platform:defihacklabs sev:high tech:evm tech:solidity technique:chained vuln:flash-loan vuln:oracle-manipulation

**Target:** BEP-20 token contract and PancakeSwap USDT/RES and USDT/ALL pairs
**Root cause:** RES token's thisAToB() cost/price logic derives value from the manipulable USDT/RES pair reserves, so an attacker who skews the pair via repeated swaps can inflate the credited RES/ALL balances of a victim address.
**Steps:**
1. Flash-swap 10,014,120.886 USDT from USDT_WBNB_PAIR via `swap(10014120886666860414836616, 0, address(this), "borrowusdt")`.
2. In pancakeCall, approve PS_ROUTER and repeatedly transfer USDT into USDT_RES_PAIR then call `USDT_RES_PAIR.swap(0, <RES out>, 0x3F693Effc53908d517F186A20431f756C90c2229, "")` six times with escalating USDT amounts (476862...696, 953725...392, 1430588...088, 1907451...784, 2384314...480, 2861177...176).
3. Call `RES_TOKEN.thisAToB()` to trigger the vulnerable cost logic against the skewed pair.
4. Prank as 0x3F693Effc53908d517F186A20431f756C90c2229 to approve RES and ALL tokens to the attack contract.
5. Transfer all ALL_TOKEN balance from 0x3F69... into USDT_ALL_PAIR and swap out USDT (`USDT_ALL_PAIR.swap(0, getusdamount, address(this), "")`).
6. Transfer all RES balance from 0x3F69... into USDT_RES_PAIR and swap out USDT (`USDT_RES_PAIR.swap(1905851854454828201052166, 0, address(this), "")`).
7. Repay flash swap: `amount0 + amount0*251/100000` USDT to USDT_WBNB_PAIR.
**How found:** Attacker identified that thisAToB() reads live pair reserves as its price/cost input, making it flash-loan manipulable; repeated swaps amplify the skew before the cost function executes.
**Exploitation:** Flash-borrow ~10M USDT from the PancakeSwap USDT/WBNB pair, then perform six successive USDT->RES swaps into the USDT/RES pair to skew its reserves, call thisAToB() to convert the manipulated state into RES/ALL balances on a target address, then dump both tokens into their respective pairs for USDT and repay the flash loan with a 0.251% fee, netting ~290,671 USDT.
**Insight:** When a token's cost/convert function (thisAToB) reads AMM pair reserves directly, treat it as an oracle-manipulation target: skew the pair with successive swaps, then call the function to mint/credit value at the distorted price.

