菜单

Aleer
Aleer
发布于 2026-11-24 / 0 阅读
0
0

CRYPTO - 嗯

Flag

PCTF{every_yes_was_one_byte_and_i_counted_them}

1. 题目与文件

题面说机器一辈子只会说三句话:太长了、不是这样、嗯。日志里只记录了它说「嗯」的 80 次,顺序是乱的;另有一个 96 字节(192 个 hex 字符)的目标密文。

两个文件的内容特征:

文件

内容

长度

ciphertext.txt

1 行 hex

96 字节 = 6 个 16 字节分组

log.txt

80 行「hex + 嗯」

每行 32 字节 = 2 个分组

三句话正好对应一个 padding oracle 的三种回答:

  • 太长了:提交的密文长度不是整块 / 超长;

  • 不是这样:解密后填充不合法;

  • 嗯:解密后 PKCS#7 填充合法。

2. 结构观察

把 80 条记录的第二个分组和目标的 6 个分组比对,会发现一个非常规整的现象:

  • 第二块只可能是目标的第 1~5 块(C1~C5),不会出现第 0 块;

  • 每个 Cj 恰好出现 16 次,共 5 × 16 = 80 条;

  • 第一块几乎等于前一块 C(j-1),只有末尾若干字节被改动。

把每条记录的第一块与对应的 C(j-1) 做异或,得到差分 D:D 总是形如「前若干个字节为 0,后面一段非 0」。统计每个组内前导零的个数 p,会发现 p 恰好取遍 0~15(有一组是 p=16,即完全未改动)。

这说明这 16 条记录是逐字节 padding oracle 攻击的全套探针:第 p 个探针猜对了前 p 个字节,并通过篡改后面的字节去凑出一个合法的填充。

3. 数学推导

设目标密文分组为 C0, C1, …, C5,其中 C0 是 IV,明文分组为 P1…P5(共 80 字节)。CBC 解密:

Pj = Dec(Cj) XOR C(j-1)

日志里的记录是二分组密文 (A, Cj):把第一块当作 IV 替换掉。此时解密得到的「伪造明文」是

P'j = Dec(Cj) XOR A

两式异或,密钥 Dec(Cj) 被消去:

A XOR C(j-1) = Pj XOR P'j =: D

攻击者保留 A 的前 p 个字节与 C(j-1) 相同,并让其余字节使 P'j 的末尾 n = 16 - p 个字节全部等于 n(合法 PKCS#7 填充)。于是:

  • k < p:A[k] = C(j-1)[k],故 D[k] = 0;

  • k >= p:P'j[k] = n,故 D[k] = Pj[k] XOR n,n = 16 - p。

这就是本题的全部关键:每个探针的差分直接泄漏了对应位置的真实明文字节,只要把差分再异或回去:

Pj[k] = D[k] XOR (16 - p)

同一组内不同 p 的探针在公共位置算出的 Pj[k] 完全一致(脚本中做了断言校验),可以互相验证。

4. 恢复过程

对 5 组各 16 条记录:

  1. 解析出 (A, Cj),定位 j;

  2. 计算 D = A XOR C(j-1),数出前导零个数 p;

  3. 对 k >= p,用 Pj[k] = D[k] XOR (16-p) 还原;

  4. 拼上 5 个 16 字节分组,得到 80 字节完整明文。

第 5 组有一条 D 全零的记录,那是未篡改的原始二分组密文(其最后一个字节本身正好是 0x01,天然合法填充),跳过即可;缺 p=15 的探针不影响,因为 p=14 的探针已经覆盖到第 15 字节。

5. 结果

还原出的 80 字节明文:

{"when": "0321", "what": "PCTF{every_yes_was_one_byte_and_i_counted_them}"}    \x01

即 79 字节有效内容 + 4 个空格 + 0x01 填充字节,共 80 字节,与 PKCS#7 完全自洽。

其中 "when": "0321" 正是在暗示每组探针的 padding 长度按 3、2、1 逐字节倒数,也呼应了 flag 内容「every yes was one byte and I counted them」——她每次只回一个字节的「嗯」,而攻击者一个字节一个字节地数了出来。

6. 完整Exp

#!/usr/bin/env python3
"""Solve the PCTF '嗯' challenge: CBC + PKCS#7 padding oracle."""
import re
from collections import defaultdict
from pathlib import Path

BASE = Path(__file__).resolve().parent / "attachments"
target = bytes.fromhex((BASE / "ciphertext.txt").read_text().strip())
blocks = [target[i:i + 16] for i in range(0, len(target), 16)]
print(f"target: {len(target)} bytes = {len(blocks)} AES blocks (block 0 is the IV)")

# Each log line is a 2-block ciphertext (probe_block, target_block).
# The machine answers 嗯 only when the PKCS#7 padding of the decrypted
# second block is valid.
groups = defaultdict(dict)
for line in (BASE / "log.txt").read_text(encoding="utf-8").splitlines():
    line = line.strip()
    if not line or line.startswith("#"):
        continue
    hexpart, answer = line.split()
    assert answer == "嗯", answer
    ct = bytes.fromhex(hexpart)
    probe, block = ct[:16], ct[16:]
    assert len(ct) == 32
    if block not in blocks[1:]:
        continue
    j = blocks.index(block)
    d = bytes(x ^ y for x, y in zip(probe, blocks[j - 1]))
    # Leading zero bytes = number of bytes the probe got right.
    p = next((k for k in range(16) if d[k]), 16)
    if p < 16:                      # p == 16 is the untouched original pair
        groups[j][p] = d

plaintext = bytearray()
for j in sorted(groups):
    recovered = bytearray()
    for k in range(16):
        # n = 16 - p is the PKCS#7 padding length used by this probe.
        # For every position k >= p: d[k] = P_j[k] XOR n.
        candidates = {d[k] ^ (16 - p) for p, d in groups[j].items() if p <= k}
        assert len(candidates) == 1, (j, k, candidates)
        recovered.append(candidates.pop())
    plaintext += recovered
    print(f"P{j}: {bytes(recovered)!r}")

message = bytes(plaintext)
print(f"\nfull plaintext ({len(message)} bytes): {message!r}")
flag = re.search(rb"PCTF\{[^}]*\}", message).group().decode()
print(f"\nFLAG: {flag}")


评论