← 返回列表

第二届CCF智能汽车大赛 Writeup by Mini-Venom

单站 ChaMd5安全团队 发布 Mon, 10 Aug 2026 08:00:00 +0800 入库 2026-11-18T04:40:13

原创 Mini-Venom 2026-08-10 08:00 辽宁

诚招web、re、crypto、pwn、misc、合约方向的师傅,长期招新IOT+Car+工控+样本分析多个组招人有意向的师傅请联系邮箱 admin@chamd5.org(带上简历和想加入的小组)

数字钥匙取证 SQLite 日志,目标是恢复受保护车端取证结果。

meta 表提示:

auth_signatures 表中两条签名复用了相同 r_hex:

ECDSA 签名公式:

s = k^-1 * (z + r*d) mod n
两个签名复用同一 nonce k 时:

k = (z1 - z2) * (s1 - s2)^-1 mod n
d = (s1*k - z1) * r^-1 mod n

恢复私钥后,根据 protected_vehicle_blob.key_hint:

sha256(recovered_ecdsa_private_key_32bytes_big_endian)[:16]
派生 SM4 key,再使用 SM4-CBC-PKCS7 解密受保护 blob。

import base64
import hashlib
import sqlite3
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.padding import PKCS7
DB_PATH = Path(".work/digital_key_uwb/digital_key_trace.sqlite")
P256_ORDER = int(
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
16,
)
defmodinv(value: int, modulus: int) -> int:
return pow(value % modulus, -1, modulus)
defmain() -> None:
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    rows = conn.execute(
"""
        select *
        from auth_signatures
        where r_hex in (
            select r_hex
            from auth_signatures
            group by r_hex
            having count(*) > 1
        )
        order by session_id
        """    ).fetchall()
if len(rows) != 2:
raise RuntimeError(f"expected 2 repeated-nonce signatures, got {len(rows)}")
    r = int(rows[0]["r_hex"], 16)
    s1 = int(rows[0]["s_hex"], 16)
    s2 = int(rows[1]["s_hex"], 16)
    z1 = int(rows[0]["digest_hex"], 16)
    z2 = int(rows[1]["digest_hex"], 16)
    k = ((z1 - z2) * modinv(s1 - s2, P256_ORDER)) % P256_ORDER
    private_key = ((s1 * k - z1) * modinv(r, P256_ORDER)) % P256_ORDER
    private_key_bytes = private_key.to_bytes(32, "big")
    blob = conn.execute("select * from protected_vehicle_blob where id = 1").fetchone()
    sm4_key = hashlib.sha256(private_key_bytes).digest()[:16]
    iv = bytes.fromhex(blob["iv_hex"])
    ciphertext = base64.b64decode(blob["ciphertext_b64"])
    decryptor = Cipher(algorithms.SM4(sm4_key), modes.CBC(iv)).decryptor()
    padded = decryptor.update(ciphertext) + decryptor.finalize()
    unpadder = PKCS7(128).unpadder()
    plaintext = unpadder.update(padded) + unpadder.finalize()
    print("reused_nonce_tag:", rows[0]["nonce_tag"])
    print("reused_r_hex:", rows[0]["r_hex"])
    print("ecdsa_k_hex:", f"{k:064x}")
    print("private_key_hex:", private_key_bytes.hex())
    print("sm4_key_hex:", sm4_key.hex())
    print("plaintext:", plaintext.decode("utf-8"))
if __name__ == "__main__":
    main()

flag{digital_key_uwb_nonce_reuse}

题目给出 T-Box 固件和 NVM 配置区,目标是离线恢复 22 EF 01 对应 DID 数据。

固件字符串直接提示:

UDS request 22 EF 01 returns 7F 22 33 online; recover EF01 offline from NVM NVM file marker: EF01DATA EF01 payload codec: XOR with DIAG_KEY DIAG_KEY=TBOX2026

NVM 中存在结构:

EF01DATA | payload_len | encrypted_payload

payload_len = 0x21,后续 33 字节使用 TBOX2026 循环 XOR。

恢复流程:

  1. 定位 EF01DATA。
  2. 读取长度字节。
  3. 取出密文 payload。
  4. 使用 TBOX2026 循环异或恢复明文。
from pathlib import Path
NVM_PATH = Path(".work/remote_diag_config/tbox_firmware_intro/nvm_partition.bin")
MARKER = b"EF01DATA"
DIAG_KEY = b"TBOX2026"
defmain() -> None:
    data = NVM_PATH.read_bytes()
    marker_offset = data.index(MARKER)
    length_offset = marker_offset + len(MARKER)
    payload_len = data[length_offset]
    ciphertext = data[length_offset + 1 : length_offset + 1 + payload_len]
    plaintext = bytes(
        byte ^ DIAG_KEY[index % len(DIAG_KEY)]
for index, byte in enumerate(ciphertext)
    )
    print("marker_offset:", hex(marker_offset))
    print("payload_len:", payload_len)
    print("ciphertext_hex:", ciphertext.hex(" "))
    print("plaintext:", plaintext.decode("utf-8"))
if __name__ == "__main__":
    main()

flag{tbox_firmware_basic_reverse}

题目给出远程诊断会话日志,目标是恢复 UDS 请求 22 EF 10 的受保护响应内容。

日志中泄露了完整加密参数:

日志存在 debug-key-cache 记录,直接暴露生产密钥:

key_ascii: TBOX-SM4-KEY2026

解密流程:

  1. 从 JSONL 中提取 debug-key-cache。
  2. 从 protected-response 中提取 IV 和密文。
  3. 使用 SM4-CBC 解密。
  4. 使用 PKCS[#7] 去填充。
import base64
import json
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.padding import PKCS7
LOG_PATH = Path(".work/tsp_diag_session.jsonl")
defmain() -> None:
    records = [json.loads(line) for line in LOG_PATH.read_text(encoding="utf-8").splitlines()]
    key_record = next(r for r in records if r.get("event") == "debug-key-cache")
    response_record = next(r for r in records if r.get("event") == "protected-response")
    key = bytes.fromhex(key_record["key_hex"])
    iv = bytes.fromhex(response_record["iv_hex"])
    ciphertext = base64.b64decode(response_record["ciphertext_b64"])
    decryptor = Cipher(algorithms.SM4(key), modes.CBC(iv)).decryptor()
    padded = decryptor.update(ciphertext) + decryptor.finalize()
    unpadder = PKCS7(128).unpadder()
    plaintext = unpadder.update(padded) + unpadder.finalize()
    print("key_hex:", key.hex())
    print("key_ascii:", key.decode("ascii"))
    print("iv_hex:", iv.hex())
    print("plaintext_hex:", plaintext.hex(" "))
try:
        print("plaintext_ascii:", plaintext.decode("utf-8"))
except UnicodeDecodeError:
pass
if __name__ == "__main__":
    main()

flag{tsp_sm4_diag_log_simple}

车载以太网抓包,包含明文 MKA/epoch 诊断遥测和若干 MACsec XPN AES-GCM-128 保护帧。目标是恢复末尾受保护的 crash-recorder 诊断记录

关键公式

aad_formula: ethernet_header_14 || sectag_14
nonce_formula: salt12 XOR (ssci_be32 || pn64_be64)
pn64_formula: pn_hi_be32 || sectag_pn32_be32
diag_bind_kdf: SHA256(b'MACSEC-DIAG-BIND-V2'||H||S||SCI||SSCI_be32)[:16]

睡眠/唤醒后,ZGW_MAIN 通道出现 MACsec XPN nonce 复用:

frame 6: PN = 1073741847 SCI = 02:42:ac:11:00:90 / port 8230 AN = 1

frame 13: PN = 1073741847 SCI = 02:42:ac:11:00:90 / port 8230 AN = 1

两帧 AAD 完全相同,且都是一块 16 字节密文。AES-GCM tag:

T = S xor GHASH_H(A, C)

在相同 AAD、相同长度、单块密文下:

T1 xor T2 = (C1 xor C2) * H^2 H^2 = (T1 xor T2) / (C1 xor C2) H = sqrt(H^2) S = T1 xor GHASH_H(A, C1)

恢复 H 和 S 后,根据诊断记录中的 KDF 派生 AES-GCM key,解密 ZGW-DIAG/2。

import hashlib
import json
from pathlib import Path
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from scapy.all import raw, rdpcap
PCAP_PATH = Path(".work/sleepwake_macsec/dist/sleepwake_macsec.pcapng")
GCM_R = 0xE1000000000000000000000000000000
GCM_ONE = 1 << 127
defint_from_block(block: bytes) -> int:
return int.from_bytes(block, "big")
defblock_from_int(value: int) -> bytes:
return value.to_bytes(16, "big")
defgf_mul(x: int, y: int) -> int:
    z = 0
    v = x
for bit in range(128):
if (y >> (127 - bit)) & 1:
            z ^= v
if v & 1:
            v = (v >> 1) ^ GCM_R
else:
            v >>= 1
return z
defgf_pow(x: int, exponent: int) -> int:
    result = GCM_ONE
while exponent:
if exponent & 1:
            result = gf_mul(result, x)
        x = gf_mul(x, x)
        exponent >>= 1
return result
defgf_inv(x: int) -> int:
return gf_pow(x, (1 << 128) - 2)
defgf_sqrt(x: int) -> int:
return gf_pow(x, 1 << 127)
defpadded_blocks(data: bytes) -> list[int]:
    result = []
for offset in range(0, len(data), 16):
        block = data[offset : offset + 16]
        result.append(int_from_block(block.ljust(16, b"\x00")))
return result
defghash(h: int, aad: bytes, ciphertext: bytes) -> int:
    y = 0
for block in padded_blocks(aad):
        y = gf_mul(y ^ block, h)
for block in padded_blocks(ciphertext):
        y = gf_mul(y ^ block, h)
    length_block = (len(aad) * 8).to_bytes(8, "big") + (len(ciphertext) * 8).to_bytes(8, "big")
return gf_mul(y ^ int_from_block(length_block), h)
defmacsec_parts(packet: bytes) -> tuple[bytes, bytes, bytes]:
    aad = packet[:28]  # Ethernet header 14 || SecTAG 14
    ciphertext = packet[28:-16]
    tag = packet[-16:]
return aad, ciphertext, tag
defmain() -> None:
    packets = [bytes(raw(packet)) for packet in rdpcap(str(PCAP_PATH))]
    aad_6, ciphertext_6, tag_6 = macsec_parts(packets[5])
    aad_13, ciphertext_13, tag_13 = macsec_parts(packets[12])
if aad_6 != aad_13 or len(ciphertext_6) != 16or len(ciphertext_13) != 16:
raise RuntimeError("expected frames 6 and 13 to reuse identical AAD and one-block ciphertexts")
    tag_delta = int_from_block(tag_6) ^ int_from_block(tag_13)
    ciphertext_delta = int_from_block(ciphertext_6) ^ int_from_block(ciphertext_13)
    h_squared = gf_mul(tag_delta, gf_inv(ciphertext_delta))
    h = gf_sqrt(h_squared)
    s = int_from_block(tag_6) ^ ghash(h, aad_6, ciphertext_6)
    diag_payload = packets[16][14:].decode("utf-8")
    diag = json.loads(diag_payload.split(" ", 1)[1])
    label = diag["bind"]["kdf_label"].encode("ascii")
    sci = bytes.fromhex(diag["bind"]["sci"])
    ssci = bytes.fromhex(diag["bind"]["ssci"])
    diag_key = hashlib.sha256(label + block_from_int(h) + block_from_int(s) + sci + ssci).digest()[:16]
    plaintext = AESGCM(diag_key).decrypt(
        bytes.fromhex(diag["nonce"]),
        bytes.fromhex(diag["ct"]) + bytes.fromhex(diag["tag"]),
        bytes.fromhex(diag["aad"]),
    )
    print("reused_frames: 6,13")
    print("H:", block_from_int(h).hex())
    print("S:", block_from_int(s).hex())
    print("diag_key:", diag_key.hex())
    print("plaintext:", plaintext.decode("utf-8"))
if __name__ == "__main__":
    main()

flag{macsec_xpn_ghash_sleepwake}

diag_log.txt 给了关键协议:

tracepack.bin 头部是:

magic, ntrace, nsamples, key_len, data_off, meta_len, crc = ...
实际解析为:

magic    = HSMTRC02
  ntrace   = 1800
  nsamples = 720
  key_len  = 32
  data_off = 43232
  meta_len = 24

注意一点

前面是所有 trace 的 metadata 区,之后从 data_off 开始才是波形数据,不是 metadata 和波形交错存储。

metadata 结构:

plaintext[16]
  trigger u16
  quality u16
  index u32

筛选 quality >= 80 后做 CPA。

  1. 计算 SecOC MAC 参数:
VIN = LCTF2026SC000042
  ECU_ID = 42
  HSM_PROFILE = scope-prod-v2
  root_key = a0d3b323979bf4acd19dcfdc6ba2f2b7

计算:

import hmac, hashlib
  root = bytes.fromhex("a0d3b323979bf4acd19dcfdc6ba2f2b7")
  vin = "LCTF2026SC000042"
  ecu = "42"
  profile = "scope-prod-v2"
  secoc_key = hmac.new(
      root,
f"{vin}|{ecu}|{profile}".encode(),
      hashlib.sha256
  ).digest()[:16]

得到:

secoc_key = 5e428d1b5933f67f198d184325998662
然后连接 /ws/doip,发送:

1003
  22F190
  22F1A0

服务端返回 counter:

62F1A0086BC3C8
所以:

counter = 086bc3c8
计算工程模式 MAC:

counter = bytes.fromhex("086bc3c8")
  cmd = b"\x01"
  mac = hmac.new(
      secoc_key,
      counter + cmd + vin[-4:].encode(),
      hashlib.sha256
  ).digest()[:8]

mac = 9ae8db27144b5c11
工程模式请求:

31014242086bc3c8019ae8db27144b5c11
服务端返回:

7101424200
最后读 DID EF01:

22EF01
响应:

62EF01666C61677B31666639336265352D383638662D343730342D626235332D6535346465356438396434317D
ASCII 解码为:

flag{1ff93be5-868f-4704-bb53-e54de5d89d41}
拿到题目后先看readme文件

有一个 ARM gateway ECU 固件、一个 NCAL/NVM 校准区、一个 OTA capsule,要恢复受保护的 DID EF01。

再看 uds_diag.asc:

7E0 02 10 03 00 00 00 00 00
7E8 06 50 03 00 32 01 F4 00
7E0 03 22 EF 01 00 00 00 00
7E8 03 7F 22 33 00 00 00 00
# DID EF01 requires engineering level and NCAL protected read.

UDS里 22 EF 01 返回了 7F 22 33,也就是 securityAccessDenied。说明正常诊断读不到,需要从固件逻辑和本地数据里把 EF01 解出来。

直接对nvm_calibration.bin做 hex dump:

00000000  4E 43 41 4C 20 26 07 29 00 00 00 03 00 00 00 18  NCAL &.)........
00000010  00 00 00 90 F3 BA AE 51 F1 90 00 00 00 00 00 90
00000020  00 00 00 15 AF 5A 02 16 B2 6D 65 17 A1 A6 24 BF
...
00000040  F1 A0 00 02 00 00 00 A5 00 00 00 2F 8D 78 1A 12
...
00000068  EF 01 00 07 00 00 00 D4 00 00 00 26 FC 99 89 9B
00000078  20 3E 3F B7 E7 A3 63 12 B9 DC AB FD B9 DC AB FD
00000088  B9 DC AB FD B9 DC AB FD
00000090  4C 43 54 46 32 30 32 36 45 56 2D 41 52 4D 2D 47
000000A0  57 2D 30 34 32 65 6E 67 69 6E 65 65 72 69 6E 67
000000B0  20 73 65 63 75 72 69 74 79 20 6C 65 76 65 6C 20
000000C0  69 73 20 72 65 71 75 69 72 65 64 20 66 6F 72 20
000000D0  45 46 30 31 7E 74 36 68 B5 5D 91 C9 3D E8 91 E9
000000E0  A5 2E FD EF 41 41 25 BB 5B ED AC 22 95 9E 2F 9E
000000F0  D3 6D CA 5A E7 D1 4D 35 D5 37

这里明显是一个 header 加表项的格式。开头有:

NCAL magic
count  = 3
entry  = 0x18

每个表项长度是 0x28,三个表项刚好在:

0x18
0x40
0x68

每个表项可以按下面结构拆:

structncal_entry {
uint16_t did_be;
uint16_t flags_be;
uint32_t data_offset_be;
uint32_t data_length_be;
uint8_t  key[16];
uint8_t  extra[12];
};

用 Python 拆一下:

from pathlib import Path
nvm = Path("nvm_calibration.bin").read_bytes()
for off in [0x18, 0x40, 0x68]:
    e = nvm[off:off + 0x28]
    did = int.from_bytes(e[0:2], "big")
    flags = int.from_bytes(e[2:4], "big")
    data_off = int.from_bytes(e[4:8], "big")
    data_len = int.from_bytes(e[8:12], "big")
    key = e[12:28]
    extra = e[28:40]
    data = nvm[data_off:data_off + data_len]
    print(hex(off), hex(did), hex(flags), hex(data_off), hex(data_len))
    print("key  =", key.hex())
    print("extra=", extra.hex())
    print("data =", data)
    print()

输出:

前两个 DID 是明文,第三个 EF01 是密文。我们要解的就是:

ct = 7e743668b55d91c93de891e9a52efdef414125bb5bedac22959e2f9ed36dca5ae7d14d35d537
长度 0x26 = 38 字节。

然后再看gateway_fw.elf是 ARM 32-bit 小端目标文件,符号名保留了一些函数名:

machine: EM_ARM
sections:
.text.sub_08001020
.text.sub_08001240
.text.sub_08001490
.text.sub_08001680
.text.sub_08001a10
.text.sub_08001c40
.text.sub_08002010
.text.sub_08002400
.text.sub_080028c0
.text.sub_08002c00_entry
.rodata.g_08004010
.rodata.g_08004040
.rodata.g_08004080
.rodata.g_080040a0
.rodata.g_080040e0

几个.rodata里能直接看到一些标记:

.rodata.g_08004010: DEVSLOT2
.rodata.g_08004040: DIDTAB3
.rodata.g_08004080: KDFTBL1
.rodata.g_080040a0: ARMGWNOTEq
.rodata.g_080040e0: LCTF2026EV-ARM-GW-042

说明固件里确实有 DID 表、KDF 表和一些解密/校验逻辑。

下一步就是解DID表,先看sub_08002010,这个函数用于根据 DID 查表。它引用了 .rodata.g_08004040,开头是 DIDTAB3。

核心逻辑大概是:

for base in [0, 8, 16]:
    tmp = []
for j in range(8):
        b = DIDTAB[9 + base + j]
        v = rol8(b, 7) ^ ((base + j) * 9 - 0x5f)
        tmp.append(v & 0xff)
    did = (tmp[0] << 8) | tmp[1]
if did == target_did:
return tmp[5]

实际解出来的三条记录:

EF01: ef01000702030000
F190: f190000000000000
F1A0: f1a0000201010000

其中 EF01 对应返回值是 3,也和题目提示的 engineering/security level 有关系。这说明EF01的确是固件保护逻辑里的目标 DID,不是 NVM 里随便一段数据。

下一步分析 capsule 里的附加信息,update_capsule.pak 开头是 UCAP:

00000000  55 43 41 50 05 00 03 00 10 00 00 00 2B 1D 4B E2
...
00000070  47 61 74 65 77 61 79 20 66 69 72 6D 77 61 72 65
00000090  6C 65 2E 4C 4D 49 47 10 08 40 E0 D3 98 61 59 09
000000A0  D9 AE 6E 15 25 6D AC 94 00 00 00 00 00 44 45 42
000000B0  55 47 00 56 49 4E 2D 53 45 45 44 2D 4F 4E 4C 59

sub_08002400会解析UCAP,查找类型 0x201的段,里面有LMIG标记。该段数据的解法是:

defrol8(x, n):
return ((x << n) | (x >> (8 - n))) & 0xff
out[i] = rol8(enc[i], 5) ^ ((i * 7 + 0x42) & 0xff)

从 capsule 里解出来:

CAL-MIGRATE-2019
这个信息说明 capsule 是校准迁移相关的,但最终解EF01时,真正核心还是 NVM 里的密文和固件里的两层变换。

sub_08001a10是第一层数据变换。函数前面会把 16 字节 key 和 12 字节 extra 拼起来算 CRC32,然后和常量异或得到 seed:

seed = crc32(key || extra) ^ 0xa5c35a17
后面是 xorshift32 流:

defxorshift32(x):
    x ^= (x << 13) & 0xffffffff
    x ^= (x >> 17) & 0xffffffff
    x ^= (x << 5) & 0xffffffff
return x & 0xffffffff

每轮取一个字节跟密文异或:

x = xorshift32(x)
ks_byte = (x >> (bit & 0x18)) & 0xff
plain[i] = cipher[i] ^ ks_byte
bit += 8

这里的取字节顺序是低 8 位、次低 8 位、次高 8 位、高 8 位循环。

sub_08001c40是一层状态相关异或。反汇编关键位置如下:

08001c4a: mov.w    ip, 
08001c4e: movs     r2, 
08001c50: ldrb     r3, [r0]
08001c52: eor.w    lr, r3, ip
08001c5a: eor.w    ip, r2, r3, lsl 
08001c62: eor.w    r3, r3, ip
08001c66: eor      ip, r3, 
08001c6a: strb     lr, [r0], 
08001c6e: adds     r2, 

[#0x5a][#0][#1][#0xc3][#1][#0x1d]
整理下,这个函数本身不是简单自反的,所以写了一个反函数用于已知前缀推中间态:

defunveil(buf):
    out = bytearray(buf)
    ip = 0x5a
    r2 = 0
for i, b in enumerate(buf):
        out[i] = b ^ (ip & 0xff)
        ip = (b ^ (r2 ^ ((ip & 0xff) << 1)) ^ 0xc3) & 0xffffffff
        r2 = (r2 + 0x1d) & 0xffffffff
return bytes(out)

整体解密形式:

intermediate = EF01_cipher ^ xorshift_stream(seed)
flag         = veil(intermediate)

经过分析,sub_08001a10 里 seed 是 CRC32 派生出来的。正常路线可以继续追 key、extra、KDF 的组合,但这里有一个更直接的办法。

因为最终明文是 flag,基本可以确定开头是:flag

最终明文前 4 字节经过 unveil 后,就能得到第一层解密后的前 4 字节中间值。keystream[:4] = ct[:4] ^ unveil(b"flag")

而 xorshift32 对 32-bit seed 是 GF(2) 线性的,前 4 个 keystream 字节刚好也是 32 bit 信息。所以构造 32 个基向量:cols = [first4(1 \<\< i) for i in range(32)]

然后对 keystream[:4] 做 GF(2) 高斯消元,就能还原 seed。最后得到:seed = 0x8572ed6d

用这个 seed 生成完整 keystream:

4209c7b74da4001af16ec8d944481c13f4adfd581496c0d85b12431a21fa081e4683bd0e0b71
第一层异或后的中间值:

3c7df1dff8f991d3cc865930e166e1fcb5ecd8e34f7b6cface8c6c84f297c244a152f03bde46
再过veil,就得到 flag。

完整解题脚本:

from pathlib import Path
nvm = Path("nvm_calibration.bin").read_bytes()
ct = nvm[0xD4:0xD4 + 0x26]
defxorshift32(x):
    x ^= (x << 13) & 0xffffffff
    x ^= (x >> 17) & 0xffffffff
    x ^= (x << 5) & 0xffffffff
return x & 0xffffffff
defstream(seed, n):
    out = []
    x = seed
    bit = 0
for _ in range(n):
        x = xorshift32(x)
        out.append((x >> (bit & 0x18)) & 0xff)
        bit += 8
return bytes(out)
defveil(buf):
    a = bytearray(buf)
    ip = 0x5a
    r2 = 0
for i in range(len(a)):
        y = a[i] ^ (ip & 0xff)
        ip = (y ^ (r2 ^ ((ip & 0xff) << 1)) ^ 0xc3) & 0xffffffff
        a[i] = y
        r2 = (r2 + 0x1d) & 0xffffffff
return bytes(a)
defunveil(buf):
    out = bytearray(buf)
    ip = 0x5a
    r2 = 0
for i, b in enumerate(buf):
        out[i] = b ^ (ip & 0xff)
        ip = (b ^ (r2 ^ ((ip & 0xff) << 1)) ^ 0xc3) & 0xffffffff
        r2 = (r2 + 0x1d) & 0xffffffff
return bytes(out)
# first4(seed) 是 GF(2) 线性变换,用 32 个基向量建矩阵。
deffirst4(seed):
return int.from_bytes(stream(seed, 4), "little")
cols = [first4(1 << i) for i in range(32)]
defgf2_solve(y):
    rows = []
for bit in range(32):
        mask = 0
for j, c in enumerate(cols):
if (c >> bit) & 1:
                mask |= 1 << j
        rows.append([mask, (y >> bit) & 1])
    r = 0
    piv = []
for col in range(32):
        p = next((i for i in range(r, 32) if (rows[i][0] >> col) & 1), None)
if p isNone:
continue
        rows[r], rows[p] = rows[p], rows[r]
for i in range(32):
if i != r and ((rows[i][0] >> col) & 1):
                rows[i][0] ^= rows[r][0]
                rows[i][1] ^= rows[r][1]
        piv.append((col, r))
        r += 1
    x = 0
for col, row in piv:
if rows[row][1]:
            x |= 1 << col
return x
prefix = b"flag"
need_intermediate = unveil(prefix)
need_ks4 = bytes(ct[i] ^ need_intermediate[i] for i in range(4))
seed = gf2_solve(int.from_bytes(need_ks4, "little"))
ks = stream(seed, len(ct))
intermediate = bytes(c ^ k for c, k in zip(ct, ks))
flag = veil(intermediate)
print("seed =", hex(seed))
print("keystream =", ks.hex())
print("intermediate =", intermediate.hex())
print("flag =", flag.decode())

运行结果:

flag = flag{3d02c696a47d9e524d37241e33098bd0}

结束

招新小广告

ChaMd5 Venom 招收大佬入圈

新成立组IOT+工控+样本分析 长期招新

欢迎联系admin@chamd5.org

原文链接:https://mp.weixin.qq.com/s?__biz=MzIzMTc1MjExOQ==&mid=2247514357&idx=1&sn=1011e14745c14a80af2d4b9b4421b4d9