做了什么:
- 包改为分层: tcaptcha/{protocol,telemetry,solvers,ocr,adapters,models,tests}
* protocol/ client.py transport.py proxy.py
* telemetry/ crypto.py values.py key.py opcodes.py
disasm.py program.py layout.py modules.py
* solvers/ text_click.py icon_click.py icon_click_rotate.py slider.py
* ocr/ engines.py slicer.py
* adapters/ one_ms.py
- tools/ 里原本是**包运行时依赖**的 tdc_{disasm,program,layout,modules}.py 并入 telemetry/
(放在 tools/ 属于分层倒置),tools/ 只留 CLI: tdc_key_pure.py / tdc_profile.py / cdp_*.js
- crypto.py 529 -> 340 行: 切除「成功样本模板回填」与「Node 插桩兜底」两条老路;
generate_telemetry 只剩 python_values 一种模式,传别的会直接报错并说明原因
- 求解器改名分清: solver.py -> solvers/icon_click.py(client 实际在用的 YOLO+ResNet18 ONNX),
icon_click_solver.py -> solvers/icon_click_rotate.py(仅测试在用,留作对照)
- pyproject: 去掉 ultralytics/onnx(训练栈已删),补上真正在用的 onnxruntime
删除了什么:
- 雨云整条线 + dist/rainyun_qinglong(104M 旧快照)
- 模板回填: models/success_collect_template.b64、tdc_worker.js、analyze_collect_layout.py
- 「补环境」时代浏览器 JS: chrome_fetch/manual_captcha/probe_captcha/solve_in_browser/tdc_browser
- 旧 VM 探索工具 10 个(tdc_collectgen/tdc_slotmap/tdc_lift/tdc_vm_run/tdc_query/tdc_probe 等)
+ 8 个基于它们的旧 verify 脚本
- 训练/标注/评估: train_classifier/train_detector/prepare_yolo/web_labeler/collector/
siamese/evaluate/benchmark_verify
- tcaptcha/core_js/(60 个分析产物)、TOOLCHAIN_REPORT.md(整篇在描述已删工具)
- 3 个已无任何引用的 PyTorch 权重:icon_classifier.pt 43M / yolo_detector.pt 5.9M /
siamese_matcher.pt 1.6M —— 线上只跑 onnx,tcaptcha/ 117M -> 66M
为什么:
- 项目后面要专门承载「滑块 / 文字点选 / 图标点选」三种求解器;扁平包 + 运行时依赖混在
tools/ + 两条已废弃的 collect 生成路线,会让后续扩展持续踩坑
怎么验证:
- .venv/bin/python -m tcaptcha.selftest 全绿
- scripts/verify/verify_offline.py 逐槽全等 + 分段几何一致
- scripts/verify/verify_values.py 4 份真机明文 152/152 逐槽全等
- scripts/verify/send_test.py --mode python_values 1ms 签到页文字点选 errorCode=0
- 回滚点: tag backup/pre-refactor-20261012
108 lines
4.4 KiB
Python
108 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
纯 Python XTEA 密钥提取 回归测试 (AGENTS.md §5.9)
|
|
|
|
固化的性质:
|
|
1. **跨构建通用**: opcode 编号每份构建整体重排, 但仍能按构建推导出正确语义表;
|
|
2. **纯 Python == Node 神谕**: 5 份构建(含 1 份新抓的实时 tdc.js)的期望密钥
|
|
全部由 Node `extract_xtea_key` 逐字对照确认过, 此处固化为断言 (不再依赖 Node 跑测试);
|
|
3. **定位不写死 pc**: 断言覆盖 core_js / root / 1ms(旧+两份实时) / 实时新抓 共 5 份
|
|
不同构建, 任一份 opcode 重排或寄存器分配变化都会立刻暴露。
|
|
|
|
运行:
|
|
.venv/bin/python -m tcaptcha.tests.test_tdc_key
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from tcaptcha.telemetry.key import extract_xtea_key_pure # noqa: E402
|
|
from tcaptcha.telemetry.opcodes import derive_opcode_table # noqa: E402
|
|
|
|
# (相对路径, 期望有效密钥, 是否必须存在)
|
|
# 期望值来自 Node 插桩 extract_xtea_key, 逐字一致。
|
|
# 入库样本 (root / 实时新抓 fixture) 为必需门禁; har/** 被 .gitignore 忽略,
|
|
# 本地有这些样本时会一并回归, 缺少时自动跳过。
|
|
FIXTURES = [
|
|
("tcaptcha/tdc_sample.js", [1448563793, 1850559083, 1733513813, 1364613714], True),
|
|
# 2026-10-11 现场从 turing.captcha.qcloud.com 下发的 tdc.js (入库固化, 防"仅本地可验")
|
|
("tcaptcha/tests/fixtures/tdc_fresh_2026-10-11.js",
|
|
[1397244740, 1799507557, 1399740484, 1516463189], True),
|
|
("har/1ms_dianxuan/tdc.js", [1448106085, 1766670948, 1819173482, 1298358636], False),
|
|
("har/1ms_dianxuan/live/tdc_live.js", [1314475846, 1500397933, 1751410275, 1716217423], False),
|
|
("har/1ms_dianxuan/live/tdc_latest.js", [1800883800, 1850828643, 1197042522, 1800823125], False),
|
|
]
|
|
|
|
_fails: list = []
|
|
|
|
|
|
def check(name: str, cond: bool, detail: str = "") -> None:
|
|
print(f" [{'PASS' if cond else 'FAIL'}] {name}" + (f" {detail}" if detail else ""))
|
|
if not cond:
|
|
_fails.append(name)
|
|
|
|
|
|
def main() -> int:
|
|
print("1) 按构建推导 opcode 表 (58 个 handler, 无未识别)")
|
|
tables = {}
|
|
skipped = []
|
|
for rel, _expect, required in FIXTURES:
|
|
p = ROOT / rel
|
|
if not p.exists():
|
|
if required:
|
|
check(f"fixture {rel}", False, "缺失")
|
|
else:
|
|
skipped.append(rel)
|
|
continue
|
|
names, arity, dynamic, diag = derive_opcode_table(p.read_text(encoding="utf-8", errors="replace"))
|
|
tables[rel] = names
|
|
check(f"opcode 表 {Path(rel).name}",
|
|
diag["handler_count"] == 58 and not diag["unknown_slots"],
|
|
f"handlers={diag['handler_count']} unknown={diag['unknown_slots']}")
|
|
if skipped:
|
|
print(f" (跳过本地未提供的样本 {len(skipped)} 份: {[Path(s).name for s in skipped]})")
|
|
|
|
print("\n2) opcode 编号确实每份构建都不同 (证明没有依赖写死的数值)")
|
|
jump_ops = {rel: next(o for o, n in tabs.items() if n == "JUMP") for rel, tabs in tables.items()}
|
|
if len(jump_ops) >= 2:
|
|
check("JUMP 的 opcode 跨构建不统一", len(set(jump_ops.values())) >= 2, str(jump_ops))
|
|
|
|
print("\n3) 纯 Python 提取 == Node 神谕 (逐字)")
|
|
for rel, expect, required in FIXTURES:
|
|
p = ROOT / rel
|
|
if not p.exists():
|
|
continue
|
|
js = p.read_text(encoding="utf-8", errors="replace")
|
|
try:
|
|
rep = extract_xtea_key_pure(js)
|
|
check(Path(rel).name, rep["key"] == expect, f"{rep['key']}")
|
|
except Exception as exc: # noqa: BLE001
|
|
check(Path(rel).name, False, f"{type(exc).__name__}: {exc}")
|
|
|
|
print("\n4) 密钥构造自洽: base + 折盐 == key")
|
|
for rel, expect, required in FIXTURES:
|
|
p = ROOT / rel
|
|
if not p.exists():
|
|
continue
|
|
rep = extract_xtea_key_pure(p.read_text(encoding="utf-8", errors="replace"))
|
|
folded = list(rep["base"])
|
|
for idx, salt in rep["salts"]:
|
|
folded[idx] = (folded[idx] + salt) & 0xFFFFFFFF
|
|
check(f"折盐 {Path(rel).name}", folded == rep["key"] == expect)
|
|
|
|
print()
|
|
if _fails:
|
|
print(f"❌ test_tdc_key 失败 {len(_fails)} 项: {_fails}")
|
|
return 1
|
|
print("✅ test_tdc_key 全部通过")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|