Files
verify/shell/README.md

93 lines
5.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 验证壳源码(Windows)
`backend/internal/protect/assets/` 里的 `.bin` 全是**编译产物**,这里放它们的源码与构建方法。
以前这些源码只存在于开发机的临时目录里,仓库只有二进制,导致「壳坏了改不动、想改校验也找不到源」。
## 源码 ↔ 资产对应
| 源码 | 产物(`backend/internal/protect/assets/`) | 说明 |
|---|---|---|
| `boot/bootstrap5.c` + `boot/blob.ld` | `blob64.bin` / `blob32.bin` | 引导:解析 `.vfy` 头 → 两级 CRC → 反射加载壳 DLL → 交回原入口。无导入表、无重定位、纯位置无关 |
| `classic/shell6.cpp` | `shell64.bin` / `shell32.bin`(**同族,非同一版本**) | Direct2D 自绘界面壳(导出 `VerifyMain(oep, cfgjson)`),3× 超采样抗锯齿、设备指纹、登录/注册/卡密三态 |
| `classic/shell5.cpp` | — | shell6 的前一版,保留作对照 |
| `duilib/shell9_v6.cpp` | `shell64_dui.bin` | 老 DuiLib 多皮肤壳(dark/light/industrial/compact/split/card/terminal,XML 内嵌) |
| `duilib/shell9.cpp`~`shell9_v5.cpp` | — | 多皮肤迭代历史版本 |
| `duilib/shell10.cpp` | — | NIM DuiLib 版(PNG 半透明皮肤 nebula/onyx/aurora),尚未作为资产发布 |
**现役壳资产的真实出处**:`shell64.bin` 里残留的编译路径是 `shell8.cpp`,那个版本的源码没保留下来。
`classic/shell6.cpp` 是最近的可用源码,编出来的 DLL 82KB,而资产 332KB;按字符串统计,
资产里 `DEVICE_LIMIT` / `unbind` / `kick` 相关串是 shell6 产物的两倍(10 : 5),说明线上壳比 shell6
多一版设备管理/解绑逻辑。改界面或加校验时**以资产行为为准**,别假设 shell6 就是线上那份。
## 构建引导 blob
```bash
./boot/build-blob.sh boot/bootstrap5.c x64 blob64.bin # x64
./boot/build-blob.sh boot/bootstrap5.c x86 blob32.bin # x86
```
三步等价于(`build-blob.sh` 里就是这条链):
```bash
gcc -c -D_WIN64 -ffreestanding -fno-stack-protector -fno-asynchronous-unwind-tables \
-mno-stack-arg-probe bootstrap5.c -o /tmp/blob.o
ld -e blob_main -nostdlib -T blob.ld /tmp/blob.o -o /tmp/blob.elf
objcopy -O binary -j .blob /tmp/blob.elf blob64.bin
```
**注意:不要用源码重编的 blob 直接替换资产。** 实测本机 gcc 产出的 blob 与仓库里的预编译 blob
行为不一致:`bootstrap4.c` 重编后启动即退出(返回 3,找不到 kernel32),`bootstrap5.c` 重编后
`GetProcAddress` 对全部导出返回空(API 指针掩码为 0);改成手工扫导出表后单层能跑通,
但再套一层外层加壳器会静默挂住。改引导一律**对预编译 blob 打二进制补丁**(见下)。
要重编请先在目标环境把单层、双层、篡改三类用例全部实测过再换资产。
## 构建壳 DLL(mingw 交叉编译)
```bash
./classic/build-classic.sh # → shell64.dll + shell32.dll
./duilib/build-duilib.sh # → shell64_dui.dll(需要 libduilib.a)
```
导出的 `DLL` 必须能被 `GetProcAddress("VerifyMain")` 直接取到,所以链接要带 `-Wl,--kill-at`
(否则名字带 `@8` 后缀)。DuiLib 版还要先按 `references` 里的步骤编出 `libduilib.a`:
排除 `UIWebBrowser` / `UIRichEdit` / `UIActiveX`,`stb_image` 单独按 C 编,`utils/GetAt` 返回 `NULL` 等。
## 已应用的二进制补丁(都只改 1 字节,可逆)
| 位置 | 补丁 | 效果 |
|---|---|---|
| `blob64.bin` 偏移 `0xb21` | `74` → `EB`(`je` → `jmp`,锚点 `39 43 28 74`) | 整文件 CRC 仍计算,但比较后不退出 → 默认产物可直接再套一层加壳器 |
| `blob32.bin` 偏移 `0xab5` | `74` → `EB`(锚点 `39 46 28 74`) | 同上(x86,逻辑相同,未做 wine32 实测) |
| `shell64.bin` 偏移 `0x19d0` | `57` → `C3`(`push rdi` → `ret`) | `logf_` 变空函数:不再往 `%TEMP%\vfy_debug.log` 写设备指纹等调试信息 |
| `shell32.bin` 偏移 `0x1b50` | `57` → `C3` | 同上(`logf_` 是 cdecl 变参,入口直接 `ret` 不破坏调用方栈) |
`shell64_dui.bin` 没有这套日志函数,无需处理。
补丁脚本:
```python
p = 'backend/internal/protect/assets/shell64.bin'
d = bytearray(open(p, 'rb').read())
assert d[0x19d0] in (0x57, 0xC3)
d[0x19d0] = 0xC3 # 0x57 = 原始 push rdi
open(p, 'wb').write(bytes(d))
```
`logf_` 在各架构里的符号名是 `_ZL5logf_PKcz`(x64)/ `__ZL5logf_PKcz`(x86),
符号表里带偏移,可直接定位:`objdump -t shell64.bin | grep logf`。
## 完整性语义(改完之后)
- `crcV`(节级):覆盖 `.vfy` 节内 `[0,36) + [44,len)` —— 防换壳 DLL / 改配置,**仍然生效**,篡改实测退出码 6。
- `crcFile`(整文件):补丁后只计算不拦截。单层产物因此失去「整文件防篡改」,代价与取舍见
`docs/double-pack.md`。想恢复严格校验,把上面第一、二行的字节改回 `0x74` 即可(但双层会回到 `exit=13`)。
## 调试
- 引导的阶段标记写在 `%TEMP%\vfy_b4.txt`(每步一个字符)。退出码含义:
`3` 找不到 kernel32、`4` 导出/GetProcAddress 失败、`5` API 指针为空、`6` `crcV` 不符、
`12` 读文件失败、`13` `crcFile` 不符。空文件说明根本没走到 CRC 阶段。
- 壳 DLL 的日志走 `logf_`(本仓库资产里已禁用)。要排查界面/网络问题时,用未打补丁的 DLL
或在源码里给 `logf_` 加重定向,别直接改 `%TEMP%` 路径——那是唯一能落盘的取证点。
- 验证界面需要窗口管理器:`Xvfb :99` + `openbox`,否则 wine 窗口不映射、截图全是黑屏。