Aleer
Aleer
发布于 2026-11-10 / 7 阅读
0
0

MISC - 小灰灰的情感故事2

平台:Sepolia
目标合约:0xd705E7E570B7e2E96D0BC085004Bf48bF7DD49dD
部署交易:0x517b1c0c235a3f8d736ec64ab1e13ecee1143f090d5dd32f8faa8bfc1411b422
Flag:PCTF{xhh_st0rage_n0t_s3cret}


1. 题目信息

压缩包解出两部分:

文件

内容

info.txt

网络(Sepolia)、合约地址、部署交易哈希

challenge.sol

XhhVault 合约源码

合约很短:

contract XhhVault {
    bytes32 private key;    // storage slot 0
    bytes32 private secret; // storage slot 1
​
    constructor() {
        // NOTE: the real ones are on the blockchain
        key = 0x0000...0000;
        secret = 0x0000...0000;
    }
​
    function hint() external pure returns (string memory) {
        return "xhh: key is at slot 0, secret is at slot 1. key XOR secret = what you want.";
    }
}

题面玩了个双关:"反正 private 的东西别人看不到,对吧?" —— 这正是本题唯一的考点。


2. 关键知识点:private 只骗编译器,不骗区块链

Solidity 的 private / internal / public 只是编译期的可见性修饰符,用来限制其它合约能不能在 Solidity 层面调用或读取它,它不会给数据加密,也不会把数据藏起来。

合约的所有状态变量都保存在 EVM 的 storage 里,而 storage 是完全公开的:只要拿到合约地址,任何人都可以用 eth_getStorageAt 按槽位把原始 32 字节读出来:

eth_getStorageAt(address, slot, blockTag) -> bytes32
  • 静态定长变量(如 bytes32、uint256)按声明顺序从 slot 0 开始,各占一个槽;

  • 所以 key 在 slot 0,secret 在 slot 1 —— 源码注释和 hint() 都已经直接告诉我们了;

  • 读取 storage 不需要私钥、不需要签名、不发交易、不花 gas,只读 RPC 就够。

换句话说:把钥匙和秘密一起锁进"private 保险箱",可这个保险箱是透明的玻璃做的。


3. 解题过程

3.1 确认合约状态

先确认合约确实已部署在 Sepolia 且创建成功:

eth_getCode(0xd705...49dD) -> 0x6080604052...   (406 字节,已部署)
receipt.status             -> 0x1              (创建成功)
receipt.contractAddress    -> 0xd705...49dD
receipt.blockNumber        -> 11961167

部署时间约为 2026-10-21 16:15 (UTC+8),之后没有其它调用,storage 保持构造函数写入的初值。

3.2 读取两个槽位

slot 0 (key)    = 0x8d2424271f65784000023e9e281556775436321cea7300779841a29d64bad75a
slot 1 (secret) = 0xdd677061641d10285f714aae5a7431120b580268b5003314ea24d6e064bad75a

3.3 按提示做异或

hint() 已经把算法写死了:key XOR secret = what you want。逐字节异或:

  8d 24 24 27 1f 65 78 40 00 02 3e 9e 28 15 56 77
^ dd 67 70 61 64 1d 10 28 5f 71 4a ae 5a 74 31 12
---------------------------------------------------
  50 43 54 46 7b 78 68 68 5f 73 74 30 72 61 67 65
  5f 6e 30 74 5f 73 33 63 72 65 74 7d 00 00 00 00

换成 ASCII:

PCTF{xhh_st0rage_n0t_s3cret}

异或结果十六进制:504354467b7868685f737430726167655f6e30745f7333637265747d00000000,尾部 4 个 00 是因为 flag 只有 28 字节。


4. 反向验证:这两个槽位真的是 key 和 secret

直接读 storage 虽然拿到了值,但最好确认没有读错槽位。部署交易的 input 就是构造函数字节码,反汇编可以看到两段完全对称的 PUSH32 ... SSTORE:

7f 8d2424...64bad75a   PUSH32 key
5f 1b                  PUSH0 ; SHL        (key << 0,即 key 本身)
5f 81 55               PUSH0 ; DUP2 ; SSTORE   -> storage[0] = key
50                     POP
7f dd6770...64bad75a   PUSH32 secret
5f 1b                  PUSH0 ; SHL
60 01 81 90 55         PUSH1 1 ; DUP2 ; SWAP1 ; SSTORE  -> storage[1] = secret
50                     POP

字节码里的两个字面量与读到的 slot 0 / slot 1 完全一致;运行时字节码中的 hint() 选择器 fbf552db 和字符串 xhh: key is at slot 0 ... 也能对上,说明我们读的就是本题合约。

还有一个细节可以佐证:两个值的后 4 字节完全相同(都是 64bad75a),因为 secret = key XOR flag,而 flag 只有 28 字节、高位补零,所以尾部异或为 0。只要发现这种规律,不看提示也能猜出两串"随机数"之间的关系。


5. 复现脚本

方式一:一条 curl

ADDR=0xd705E7E570B7e2E96D0BC085004Bf48bF7DD49dD
RPC=https://ethereum-sepolia-rpc.publicnode.com
​
curl -s "$RPC" -H 'Content-Type: application/json' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_getStorageAt\",
       \"params\":[\"$ADDR\",\"0x0\",\"latest\"]}"

把 "0x0" 换成 "0x1" 再读一次即可。

方式二:Foundry cast

export RPC=https://ethereum-sepolia-rpc.publicnode.com
export ADDR=0xd705E7E570B7e2E96D0BC085004Bf48bF7DD49dD
​
cast storage $ADDR 0 --rpc-url $RPC   # 0x8d24...bad75a  <- key
cast storage $ADDR 1 --rpc-url $RPC   # 0xdd67...bad75a  <- secret

cast 负责把槽位读出来(省去手写 JSON-RPC),异或直接把两个十六进制串喂给下面的脚本即可。

方式三:Python(完整脚本,仅标准库)

import json, urllib.request
​
RPC  = "https://ethereum-sepolia-rpc.publicnode.com"
ADDR = "0xd705E7E570B7e2E96D0BC085004Bf48bF7DD49dD"
​
def rpc(method, *params):
    body = json.dumps({"jsonrpc": "2.0", "id": 1,
                       "method": method, "params": list(params)}).encode()
    req = urllib.request.Request(
        RPC, data=body,
        headers={"Content-Type": "application/json", "User-Agent": "Mozilla/5.0"})
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.load(r)["result"]
​
def slot(n):
    return bytes.fromhex(rpc("eth_getStorageAt", ADDR, hex(n), "latest")[2:])
​
key, secret = slot(0), slot(1)
flag = bytes(a ^ b for a, b in zip(key, secret))
print("key    =", key.hex())
print("secret =", secret.hex())
print("flag   =", flag.rstrip(b"\x00").decode())

小坑:publicnode 等公共 RPC 会拦截没有 User-Agent 的 Python 请求(直接返回 403 Forbidden),加上浏览器 UA 即可。

运行结果:

key    = 8d2424271f65784000023e9e281556775436321cea7300779841a29d64bad75a
secret = dd677061641d10285f714aae5a7431120b580268b5003314ea24d6e064bad75a
flag   = PCTF{xhh_st0rage_n0t_s3cret}

6. Flag

PCTF{xhh_st0rage_n0t_s3cret}



评论