feat(collect): 按 docs/tdc-案例.md 重做主链路 —— 四段加密 + 字符串指纹识别 37/37

做的什么:
  1. 判决并修正 collect 结构: 不是「整体一次加密」, 而是
     chunk1 / trajectory / chunk2 / sd **四段独立加密**, 前 3 段
     空格 pad 到 24 字节对齐、sd 不 pad, 4 段 base64 直接拼接。
  2. 按案例 §5.3 实现字符串+结构指纹的模块识别器, 把 37 个采集器
     全部识别且跨构建唯一。
  3. 修 reorder 的恒等映射 bug, 并补强断言。
  4. 在线抓取全新样本作为唯一数据源, 清理探索性脚本。

为什么:
  * 旧「整体加密 + \x00 补齐」与真机数据矛盾 —— 导致多轮实验
    建立在结构错误的 collect 上(这正是此前反复"乱碰"的根因)。
  * 模块 id 每份构建重排, 不能当跨构建锚点; 但模块的**代码**
    (字符串集合、结构)不变 —— 这是案例指明的不变量。
  * reorder 旧实现用「目标槽下标」切「源数组」, 顺序不同即全盘错位;
    原测试只断言"是置换", 恒等函数也满足, 所以 bug 被放过了。

怎么验证:
  * 四段结构(3 条独立证据, 其中一条不需密钥):
      - 真机明文 3 处连续空格串 [+5,+10,+24], 且 pad == 24-len%24 全中
      - 密文长度算术 480+96+416+56 = 1048 == 真实 1048 字符
      - '=' 只出现在 1046/1048, 对应 sd 段 33B→补 7→40B
  * 指纹: 4 份全新在线构建 37/37 识别、0 unknown、跨构建语义名集合
    完全一致、单构建内 0 重名
  * 配对正确性: 独立证据 37/37 一致; 负对照(清单下标 / 模块 id /
    随机配对)均只有 2-3/37 —— 证明归一化没有过拟合
  * 端到端重排 3/3 正确且非恒等
  * selftest 全绿; 强断言自查: 旧实现过不了新断言

新增:
  tools/tdc_modules.py            字符串+结构指纹识别(案例 §5.3)
  scripts/fetch_online_samples.py 在线抓取全新 tdc.js
  scripts/verify/*.py             7 个可复现的验证脚本
  docs/tdc-案例.md                案例原文(看雪, 作者执着的猫)

修改:
  tcaptcha/crypto.py          +build_collect / split_collect_segments(四段)
  tools/tdc_collectgen.py     reorder 改为跨构建锚点对齐 + spec_from_seq
  tools/tdc_layout.py         +static_fingerprint / opcode_histogram
  tools/tdc_program.py        修符号求值插值类型崩溃
  tcaptcha/tests/test_collectgen.py  补「非恒等」强断言 + 在线新样本端到端
  tcaptcha/client.py          debug 目录改到系统临时目录(不再写仓库内 scratch/)
  har/1ms_dianxuan/PROGRESS.md      新增已知 #41~#49, 显式修正被推翻的旧结论
  .gitignore                  +scratch/  +har/online/

删除:
  scratch/(含 10-11 遗留的一次性脚本, 已确认不可恢复后清理)

注意:
  * 账本记录了一次自我纠错: 曾据案例 §5.5 误判 kind=2「不进 cd」,
    实测(cd 38 槽、轨迹在第 10 槽)否定后已撤回。
  * 仍未打通: 纯离线生成需要每个语义名的取值来源(已知 #38)。
This commit is contained in:
yml2213
2026-10-12 02:28:08 +08:00
parent 50734a854b
commit 80a122245a
18 changed files with 2512 additions and 40 deletions
+75
View File
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""用真机 collect 密文的**长度与填充**判决「四段加密」结构。
不需要密钥:段结构在 base64 的长度与 '=' 分布上留下指纹。
判决依据(docs/tdc-案例.md §5.1):
chunk1/trajectory/chunk2 各 pad 到 24 字节 -> 密文长度是 24 的倍数 -> base64 无 '='
sd 不 pad,只被 XTEA 补到 8 的倍数 -> 长度%3 != 0 -> base64 带 '='
用法:
.venv/bin/python scripts/verify/verify_seglen.py <collect 的 base64>
"""
import base64
import re
import sys
from pathlib import Path
if len(sys.argv) < 2:
print("用法: verify_seglen.py <collect 的 base64(文件路径或直接给串)>")
raise SystemExit(2)
_arg = sys.argv[1]
_p = Path(_arg)
b64 = (_p.read_text(encoding="ascii") if _p.exists() else _arg).strip()
print("真机 collect: %d 字符, 含 '=' : %s" % (len(b64), "=" in b64))
print()
raw = base64.b64decode(b64)
print("解码后密文: %d 字节" % len(raw))
print()
# 真机明文(另一份同会话样本)给出的各段长度
segs = [("chunk1", 355, 5), ("trajectory", 62, 10), ("chunk2", 288, 24), ("sd", 33, None)]
print("%-11s %8s %6s %10s %12s %10s" % ("段", "内容", "pad", "密文字节", "base64字符", "含'='"))
print("-" * 66)
total = 0
total_b64 = 0
for name, content, pad in segs:
if pad is not None:
n = content + pad # 空格 pad 到 24
assert n % 24 == 0
else:
n = content + ((8 - content % 8) % 8) # sd: 只被 XTEA 补到 8 的倍数
enc = base64.b64encode(b"\x00" * n).decode()
total += n
total_b64 += len(enc)
print("%-11s %8d %6s %10d %12d %10s"
% (name, content, pad if pad is not None else "(xtea)",
n, len(enc), "=" in enc))
print("-" * 66)
print("%-11s %8s %6s %10d %12d" % ("合计", "", "", total, total_b64))
print()
print(">>> 推算总密文字节 %d == 实际 %d : %s" % (total, len(raw), total == len(raw)))
print(">>> 推算 base64 字符 %d == 实际 %d : %s" % (total_b64, len(b64), total_b64 == len(b64)))
print()
# 反过来看:把整串按 24 字节切,前三段应能整除
print("[反向校验] 把密文按 24 字节切分:")
off = 0
for name, content, pad in segs:
if pad is None:
print(" %-11s 剩余 %3d 字节 (sd, 非 24 倍数)" % (name, len(raw) - off))
break
n = content + pad
print(" %-11s [%4d:%4d] %3d 字节 24倍数=%s"
% (name, off, off + n, n, n % 24 == 0))
off += n
print()
# 关键指纹: '=' 只应出现在最后(来自 sd 段)
idx = b64.find("=")
print("'=' 首次出现位置: %d / 总长 %d -> %s"
% (idx, len(b64), "在末尾区域(符合 sd 段不 pad)"
if idx >= len(b64) - 4 else "⚠️ 在中间(与四段结构矛盾)"))
if idx >= 0:
print(" 尾部填充: %r" % b64[idx:])