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 72 73 74 75 76 77 78 79 80 81 82 83 84
| const { ethers } = require("hardhat");
async function debugTest() { console.log("🔍 调试自毁攻击...");
const [owner, attacker] = await ethers.getSigners();
console.log("所有者:", owner.address); console.log("攻击者:", attacker.address);
const VulnerableWallet = await ethers.getContractFactory("VulnerableWallet"); const wallet = await VulnerableWallet.deploy(); const walletAddress = await wallet.getAddress(); console.log("💰 钱包合约:", walletAddress);
const initialCode = await ethers.provider.getCode(walletAddress); console.log("📄 初始合约代码长度:", initialCode.length);
console.log("\n💸 测试存款..."); const depositTx = await wallet.deposit({ value: ethers.parseEther("1.0") }); await depositTx.wait();
const [ownerAddr, balance, contractBalance] = await wallet.getInfo(); console.log(" 所有者:", ownerAddr); console.log(" 内部余额:", ethers.formatEther(balance), "ETH"); console.log(" 合约余额:", ethers.formatEther(contractBalance), "ETH");
console.log("\n💥 执行自毁..."); try { const destroyTx = await wallet.destroy(); const receipt = await destroyTx.wait(); console.log(" ✅ 自毁交易成功"); console.log(" 交易哈希:", receipt.hash); console.log(" 区块号:", receipt.blockNumber);
await new Promise(resolve => setTimeout(resolve, 2000));
const finalCode = await ethers.provider.getCode(walletAddress); console.log("📄 自毁后合约代码长度:", finalCode.length); console.log(" 代码是否被删除:", finalCode === "0x" ? "✅ 是" : "❌ 否");
if (finalCode !== "0x") { console.log("⚠️ 警告: 合约代码未被删除!"); }
} catch (error) { console.log("❌ 自毁失败:", error.message); }
console.log("\n🔫 测试攻击合约..."); const SelfDestructAttack = await ethers.getContractFactory("SelfDestructAttack"); const attack = await SelfDestructAttack.connect(attacker).deploy(); const attackAddress = await attack.getAddress(); console.log("🎯 攻击合约:", attackAddress);
const attackCode = await ethers.provider.getCode(attackAddress); console.log("📄 攻击合约代码长度:", attackCode.length);
console.log("\n💣 执行攻击合约自毁..."); try { const attackTx = await attack.connect(attacker).attack(walletAddress, { value: ethers.parseEther("0.1") }); const attackReceipt = await attackTx.wait(); console.log(" ✅ 攻击交易成功");
const attackCodeAfter = await ethers.provider.getCode(attackAddress); console.log("📄 攻击后攻击合约代码长度:", attackCodeAfter.length); console.log(" 攻击合约是否自毁:", attackCodeAfter === "0x" ? "✅ 是" : "❌ 否");
} catch (error) { console.log("❌ 攻击失败:", error.message); } }
debugTest().catch(console.error);
|