>lhzctf

小灰灰的情感故事(2)-小灰灰的合约

1.打开文件

是一个Sepolia测试网 合约只有两个状态变量

bytes32 private key;    // slot 0
bytes32 private secret; // slot 1

小灰灰认为加上private等于别人看不到 事实上并没有用

2.异或解题

使用脚本

import json, urllib.request

CONTRACT = "0xd705E7E570B7e2E96D0BC085004Bf48bF7DD49dD"
RPC = "https://ethereum-sepolia-rpc.publicnode.com"

def rpc(method, params):
    payload = json.dumps({"jsonrpc": "2.0", "id": 1,
                          "method": method, "params": params}).encode()
    req = urllib.request.Request(RPC, data=payload, method="POST",
        headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=20) as resp:
        return json.loads(resp.read().decode())["result"]

def read_slot(slot: int) -> int:
    # position 必须是 hex 字符串;返回 0x 开头的 32 字节
    return int(rpc("eth_getStorageAt", [CONTRACT, hex(slot), "latest"]), 16)

key    = read_slot(0)
secret = read_slot(1)
flag   = (key ^ secret).to_bytes(32, "big").rstrip(b"\x00").decode("utf-8")

print(f"key    = 0x{key:064x}")
print(f"secret = 0x{secret:064x}")
print(f"flag   = {flag}")

得到flag