一、题目信息
二、解题思路
层层解压 110 层后,最内层只有两个文件,
flag_bits = 0x0001,即老式 PKWARE 加密(ZipCrypto)。关键线索在
layer_110.zip的 ZIP 注释里:
Layer 110 / 110
Old locks, unchanged bytes. The notice is remembered below.
notice.txt (Base64, exact bytes including LF newlines):
QXJjaGl2ZSBtYWludGVuYW5jZSBub3RpY2UK... 注释把 notice.txt 的明文(Base64,含 LF)直接给了出来 —— 这正是 ZipCrypto 已知明文攻击需要的材料。
"Old locks, unchanged bytes"(老锁 / 字节不变)= ZipCrypto + Store,"Stored bytes never change their shape"= 无压缩,明文长度即密文长度。
拿到 12 字节以上已知明文就可用
bkcrack恢复 96 bit 内部密钥(口令状态);由于同一压缩包里所有条目用的是同一个口令,这组密钥可以解开包内任意条目(-D能整包解密就是这个原理)。
三、关键步骤
1. 递归解压 110 层
while True:
zips = glob.glob(os.path.join(dst, "**", "*.zip"), recursive=True)
if not zips: break
# 逐层 Expand/zipfile 解压,直到没有 zip2. 确认加密方式
Index Encryption Compression CRC32 Uncompressed Packed size Name
0 ZipCrypto Store 4ddafd19 150 162 notice.txt
1 ZipCrypto Store ec97a4d3 43 55 flag.txtflag_bits = 0x0001、compress_type = 0(Store)packed = uncompressed + 12→ 多出的 12 字节是 ZipCrypto 的加密头(11 字节随机 + 1 字节 CRC 高字节)
3. 还原已知明文并校验
注释里的 Base64 解出来是 150 字节:
Archive maintenance notice
Exactly 110 shells protect this message.
The comment remembers what this file says.
Stored bytes never change their shape.它的 CRC32 = 0x4ddafd19,与 ZIP 中 notice.txt 记录的 CRC 完全一致,说明这确实是该条目的明文(不是诱饵)。
4. 已知明文攻击
bkcrack -C layer_110.zip -c notice.txt -p notice_plain.txt约 6 秒出结果:
Keys: 11719cec ab67c662 af9f552c5. 用密钥解 flag.txt
bkcrack -C layer_110.zip -c flag.txt -k 11719cec ab67c662 af9f552c -d flag.txt
# 或整包解密
bkcrack -C layer_110.zip -k 11719cec ab67c662 af9f552c -D layer_110_decrypted.zip解出:
PCTF{110_layers_zipcrypto_known_plaintext}校验(这一点很关键,避免自欺):
用同一组密钥解
notice.txt:解密头第 12 字节 =0x4d=CRC32(notice) >> 24✔解
flag.txt:解密头第 12 字节 =0xec=0xec97a4d3 >> 24✔flag.txt明文 43 字节(末尾带\n),CRC32 = 0xec97a4d3,与 ZIP 记录完全一致 ✔
四、易踩的坑
密钥是"条目起始的口令状态",不是"跳过 12 字节头之后的数据状态"。
所以同一组-k密钥对notice.txt和flag.txt都成立;但如果你自己拿着"notice 数据段"的密钥流去异或 flag,一定是乱码 —— 因为每个条目都会各自消耗掉自己的 12 字节加密头。注释里的 Base64 要按原始字节(含 LF)还原,差一个换行都会让明文不匹配、攻击失败。
packed_size - uncompressed_size = 12是判断 ZipCrypto 的快速特征;Store + 加密是最适合已知明文攻击的组合。
五、拿到 Flag
PCTF{110_layers_zipcrypto_known_plaintext}六、Exp
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
第三届 PCTF2026 新生赛 - Misc - layer
附件: layer_001.zip (110 层嵌套 zip, 最内层 notice.txt / flag.txt 为 ZipCrypto 加密)
思路:
1) 递归解压 110 层 -> 最内层 layer_110.zip
2) ZIP 注释里给了 notice.txt 的 Base64 明文(含 LF) -> 已知明文
3) bkcrack 已知明文攻击恢复 96-bit 内部密钥(口令状态)
4) 同一口令状态可解包内任意条目 -> 解 flag.txt 并校验 CRC32
用法:
python "Misc - layer - exp.py" layer_001.zip [--bkcrack PATH] [--keys X Y Z] [--workdir DIR]
依赖: Python 3.8+; bkcrack 仅在自动攻击时需要。
"""
import argparse
import base64
import glob
import os
import re
import shutil
import subprocess
import sys
import zipfile
import zlib
# 本题目已恢复出的内部密钥(口令状态), 作为无 bkcrack 时的兜底
KNOWN_KEYS = (0x11719CEC, 0xAB67C662, 0xAF9F552C)
# ---------------------------------------------------------------- ZipCrypto
TBL = []
for _i in range(256):
_c = _i
for _ in range(8):
_c = (_c >> 1) ^ 0xEDB88320 if (_c & 1) else _c >> 1
TBL.append(_c & 0xFFFFFFFF)
def zipcrypto_decrypt(keys, data):
"""keys=(k0,k1,k2) 为条目起始(12 字节加密头之前)的内部状态; data 为 packed 字节."""
k0, k1, k2 = keys
out = bytearray()
for c in data:
t = (k2 | 2) & 0xFFFF
p = c ^ (((t * (t ^ 1)) >> 8) & 0xFF)
out.append(p)
k0 = ((k0 >> 8) ^ TBL[(k0 ^ p) & 0xFF]) & 0xFFFFFFFF
k1 = ((k1 + (k0 & 0xFF)) * 134775813 + 1) & 0xFFFFFFFF
k2 = ((k2 >> 8) ^ TBL[(k2 ^ (k1 >> 24)) & 0xFF]) & 0xFFFFFFFF
return bytes(out)
def read_packed_bytes(zip_path, info):
"""绕过加密读取 ZIP 条目的原始 packed 字节(含 12 字节加密头)."""
with open(zip_path, "rb") as f:
f.seek(info.header_offset)
hdr = f.read(30)
if len(hdr) < 30 or hdr[:4] != b"PK\x03\x04":
raise ValueError("bad local header for %s" % info.filename)
nlen = int.from_bytes(hdr[26:28], "little")
elen = int.from_bytes(hdr[28:30], "little")
f.seek(info.header_offset + 30 + nlen + elen)
data = f.read(info.compress_size)
if len(data) != info.compress_size:
raise ValueError("truncated data for %s" % info.filename)
return data
# ---------------------------------------------------------------- unzip layers
def unzip_all(src_zip, workdir):
"""递归解压(平铺, 避免 110 层目录导致 Windows 超长路径), 返回最内层 zip 路径."""
if os.path.isdir(workdir):
shutil.rmtree(workdir)
os.makedirs(workdir, exist_ok=True)
cur = src_zip
layer = 1
while True:
with zipfile.ZipFile(cur) as zf:
names = zf.namelist()
if len(names) == 1 and names[0].lower().endswith(".zip"):
nxt = os.path.join(workdir, os.path.basename(names[0]))
with open(nxt, "wb") as f:
f.write(zf.read(names[0]))
cur = nxt
layer += 1
continue
inner = os.path.join(workdir, "inner_%03d" % layer)
os.makedirs(inner, exist_ok=True)
# 加密条目无法直接 extract, 这里按原始 packed 字节落盘(含 12B 加密头)
for info in zf.infolist():
with open(os.path.join(inner, info.filename), "wb") as f:
f.write(read_packed_bytes(cur, info))
print("[*] 解压完成: 共 %d 层, 最内层 %s" % (layer, os.path.basename(cur)))
return cur, inner
# ---------------------------------------------------------------- main
def main():
ap = argparse.ArgumentParser()
ap.add_argument("zipfile", help="layer_001.zip")
ap.add_argument("--bkcrack", default="bkcrack", help="bkcrack 可执行文件路径")
ap.add_argument("--keys", nargs=3, metavar=("X", "Y", "Z"), help="直接给定内部密钥(十六进制)")
ap.add_argument("--workdir", default="_layer_work", help="解压工作目录")
args = ap.parse_args()
inner_zip, inner_dir = unzip_all(args.zipfile, args.workdir)
with zipfile.ZipFile(inner_zip) as zf:
infos = zf.infolist()
comment = zf.comment
if not comment:
sys.exit("[!] 最内层 ZIP 没有注释, 无法取得已知明文")
text = comment.decode("utf-8", "replace")
print("[*] ZIP 注释:")
for line in text.splitlines():
print(" " + line)
# 注释里的 Base64 = notice.txt 的精确明文
m = re.search(r"Base64[^\n]*:\s*\n([A-Za-z0-9+/=\s]+)", text)
if not m:
sys.exit("[!] 注释中没找到 Base64 明文")
b64 = re.sub(r"\s+", "", m.group(1))
known_plain = base64.b64decode(b64)
print("[*] 已知明文 %d 字节, CRC32=%08x"
% (len(known_plain), zlib.crc32(known_plain) & 0xFFFFFFFF))
# 用明文 CRC 找出"被泄露明文的条目"与"目标条目"
known_info = None
for info in infos:
if (zlib.crc32(known_plain) & 0xFFFFFFFF) == info.CRC:
known_info = info
break
targets = [i for i in infos if i is not known_info]
if known_info is None:
sys.exit("[!] 已知明文与任何条目 CRC 都不匹配")
if not targets:
sys.exit("[!] 没有其他条目可解")
print("[*] 已知明文条目: %s ; 目标条目: %s"
% (known_info.filename, ", ".join(t.filename for t in targets)))
# ---------------- 恢复密钥
if args.keys:
keys = tuple(int(x, 16) for x in args.keys)
print("[*] 使用给定密钥: %08x %08x %08x" % keys)
else:
plain_path = os.path.join(args.workdir, "known_plain.txt")
with open(plain_path, "wb") as f:
f.write(known_plain)
cmd = [args.bkcrack, "-C", inner_zip, "-c", known_info.filename, "-p", plain_path]
print("[*] 运行:", " ".join(cmd))
try:
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=3600)
except FileNotFoundError:
sys.exit("[!] 找不到 bkcrack, 用 --bkcrack 指定路径, 或用 --keys 直接给密钥")
out = proc.stdout + proc.stderr
print(out)
mm = re.findall(r"\b([0-9a-fA-F]{8})\s+([0-9a-fA-F]{8})\s+([0-9a-fA-F]{8})\b", out)
if not mm:
sys.exit("[!] 没能从 bkcrack 输出里解析出密钥")
keys = tuple(int(v, 16) for v in mm[-1])
print("[*] 恢复出密钥: %08x %08x %08x" % keys)
# ---------------- 解密目标
for info in targets:
packed = read_packed_bytes(inner_zip, info)
plain_all = zipcrypto_decrypt(keys, packed)
header, data = plain_all[:12], plain_all[12:]
print()
print("[*] %s" % info.filename)
print(" 加密头 12B : %s" % header.hex())
print(" 头部校验字节 : %02x (CRC32>>24 = %02x) %s"
% (header[11], (info.CRC >> 24) & 0xFF,
"OK" if header[11] == (info.CRC >> 24) & 0xFF else "MISMATCH"))
crc = zlib.crc32(data) & 0xFFFFFFFF
print(" 明文长度/CRC32 : %d / %08x (记录 %08x) %s"
% (len(data), crc, info.CRC, "OK" if crc == info.CRC else "MISMATCH"))
print(" 明文 : %r" % data)
if crc == info.CRC:
flag = data.rstrip(b"\r\n").decode("utf-8", "replace")
print()
print("FLAG: %s" % flag)
if __name__ == "__main__":
main()