Files
vmprotect/BUILD-LINUX.md
Cmake 64b374fa06 runtime: 让 runtime 在 Linux 上可构建 + 资源打包脚本
- runtime/precompiled.h: sys/sysctl.h 只在 __APPLE__ 引入;补 <cstdarg>
- runtime/lin_runtime.mak: curl 改为 -lcurl + pkg-config(原来是写死 ~/curl-7.35.0-64)
- core/gen_inc_lin.py: 把 lin_runtime.so 打成 lin_runtimeNN.so.inc(key + 位置异或,格式对齐
  IFile::OpenResource(is_enc=true)),_so_code 写空表
- runtime/licensing_manager.cc: SetSerialNumber 加 DOGDBG=1 才输出的 [dbrt] 跟踪
- tools/dogkeygen/testhost_lin.c: Linux 侧 SDK 调用测试宿主
- BUILD-LINUX.md: 补 runtime 构建与 .inc 格式说明(含 _so_code 缺失导致授权调用被打桩的结论)
2026-11-23 17:37:54 +08:00

102 lines
6.4 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.
# 在 Linux 上编译 DogProtect(Console / Core / SDK / Runtime)
本文件记录在 **Debian 13 + clang 19** 上把本仓库的 Linux 目标(`-target x86_64-linux-gnu`)编译通过所需的最小改动。
所有改动已在 `linux-build` 分支上,直接按下面顺序构建即可,不再需要手工补参数。
## 依赖
```bash
apt-get install -y clang make libssl-dev zlib1g-dev
```
`third-party/libffi/libffi64.a` 是仓库自带预编译库,直接用。
## 构建顺序(必须按这个顺序,后者依赖前者的产物)
```bash
cd core/invariant && make -f lin_invariant_core64.mak CFG_DIR=release # → bin/64/invariant_core.a
cd ../../core && make -f lin_core64.mak CFG_DIR=release # → bin/64/release/core.a(约 70MB)
cd ../sdk && make -f lin_sdk64.mak CFG_DIR=release # → bin/libDogProtectSDK64.so
cd ../DogProtectCon && make -f lin_console64.mak CFG_DIR=release # → bin/64/release/dogprotect_con
```
运行:
```bash
LD_LIBRARY_PATH=$PWD/bin ./bin/64/release/dogprotect_con
# DogProtect Professional v 3.5.1 (build 1024)
# Usage: dogprotect_con File [Output File] [-pf Project File] [-sf Script File] [-wm Watermark] [-we]
```
加壳(一份 CLI 同时吃 PE / ELF / Mach-O,取决于是哪种文件):
```bash
/dogprotect_con input.bin output.bin [-pf project.dog] [-sf script.lua]
```
## 为 Linux 编译修掉的 10 个问题
| # | 文件 | 问题 | 处理 |
|---|------|------|------|
| 1 | `core/precompiled.h` | 无条件 `#include <sys/sysctl.h>`(macOS 头) | 用 `#ifdef __APPLE__` 包住 |
| 2 | `lin_common.mak` | `-Werror` 撞 clang19 的 deprecated 警告(`auto_ptr`/`RSA_*`) | 去掉 `-Werror` |
| 3 | `runtime/crypto.h` | 自定义 `__rdtsc()` 与 clang 内建同名不同返回类型 | 改名 `vmp_rdtsc` + `#define __rdtsc() vmp_rdtsc()` |
| 4 | `core/core.cc` | `OutputDebugStringA` 是 Win32 API | `#ifdef _WIN32` 条件编译 |
| 5 | `core/core.cc` | OpenSSL 3 里 `rsa_st` 是残缺类型,`r->n/e/d` 编译失败 | 改 `RSA_get0_key(r, &n, &e, &d)` |
| 6 | `runtime/precompiled.h`、`core/intel.cc`、`core/processors.cc` | `#include <intrin.h>`(MSVC 头) | Linux 走 `#include <x86intrin.h>` |
| 7 | `core/objects.cc` | 用了 `va_start/va_end` 但缺 `<cstdarg>` | 补 include(PCH 里也补一份) |
| 8 | `core/pefile.cc` | MSVC union 写法 `data.u.UnwindData` / `tls.u.Characteristics` | 改成 `data.UnwindInfoAddress` / `tls.Characteristics` |
| 9 | 各 `*.mak` | 默认目标指向 `clean`,`make -f xxx.mak` 会清空产物 | `lin_common.mak` 加 `.DEFAULT_GOAL := all` |
| 10 | `DogProtectCon/lin_console.mak` | 链接静态 `libcrypto.a` 牵出 zlib/zstd;预编译 libffi 是非 PIE;`strlcpy` 与 glibc 2.38+ 重名 | 改 `-lcrypto -lz`、加 `-no-pie`、加 `-Wl,--allow-multiple-definition` |
## 已验证的能力边界(源码写死,非配置)
- **ELF**:`core/elffile.cc` 只接受 `EM_386 / EM_486 / EM_X86_64`,其它 CPU 返回 `osUnsupportedCPU`
- **Mach-O**:x86 / x86_64
- **PE**:x86 / x64,含 .NET(`core/dotnetfile.cc`)
- **不支持**:ARM / AArch64、Android APK/DEX、iOS arm64
## Runtime(注入到产物里的那部分代码)
`runtime/` 现在也能在 Linux 上构建(原来是 Windows 专用,没人动过):
```bash
apt-get install -y libcurl4-openssl-dev # runtime/licensing_manager.cc 的激活接口要 curl
cd runtime && make -f lin_runtime64.mak -j$(nproc) # → bin/64/Release/lin_runtime.so
python3 core/gen_inc_lin.py bin/64/Release/lin_runtime.so core/lin_runtime64.so.inc lin_runtime64
cd core && make -f lin_core64.mak CFG_DIR=release # 把新 blob 编进打包器
```
修掉的三处(都在 `runtime/`):
| 文件 | 问题 | 处理 |
|---|---|---|
| `precompiled.h` | 无条件 `#include <sys/sysctl.h>`(macOS 头) | 用 `#ifdef __APPLE__` 包住 |
| `precompiled.h` | 用了 `va_copy/va_end` 但缺 `<cstdarg>` | 补 include |
| `lin_runtime.mak` | 链接写死 `~/curl-7.35.0-64/lib/libcurl.so` 与对应 include | 改 `-lcurl` + `pkg-config --cflags libcurl` |
`core/*_runtime*.inc` 的格式(`IFile::OpenResource(..., is_enc=true)`):
- `_so_file[]` = 4 字节 key(小端)+ 整份 .so 按位置异或(`out[p] = in[p] ^ uint8(rotl32(key,p) + p)`)
- `_so_code[]` = runtime 的 export/import/map/function 四张元数据表(`ReadFromBuffer` 逐张读)
`gen_inc_lin.py` 只能替 `_so_file`,`_so_code` 写空表。**后果:打包时 `FACE_LICENSE_INFO` 这些常量仍能被扫到并正确链接(实测注入 192 字节授权数据),但宿主对 SDK 授权函数的调用会被打成桩** —— 产物里 `DogProtectSetSerialNumber` 恒返 `2`(INVALID),自签序列号用不上。
调试注入后的 runtime:`runtime/licensing_manager.cc::SetSerialNumber` 里有 `DOGDBG=1` 才输出的 `[dbrt]` 跟踪(序列号长度、CRC、解出的块头、密钥偏移),重建 blob 后 `DOGDBG=1 ./产物 序列号` 即可看到 runtime 内部到底走到哪一步。
真正要打通得生成 `_so_code`,那一步依赖厂商的 runtime 保护流水线(仓库里没有),所以默认仍用厂商的 blob;`gen_inc_lin.py` 与上面三处修复保留,供后续实验。
## 授权(licensing)相关
- Linux 目标统一按 **Ultimate** 版编译(`lin_common.mak` 里追加 `CFLAGS/CXXFLAGS += -DULTIMATE`)。
授权子系统(`LicensingManager`、密钥对、`-lf` 授权参数文件)只在 ULTIMATE 分支里编译;
不定义它会编译出 Professional,打包时直接报 `The licensing system not supported in this version`。
- 工具自身注册状态:把 `core/DogProtectLicense.ini` 复制到 `bin/64/release/`(与 `dogprotect_con` 同目录),
横幅会变成 `Registered to: ...`,此时才给满 10 个 VM executor 并开字节码加密。
未注册时产物只有 1 个 executor、体积差 6 倍(894KB vs 5.3MB)。
- 产物侧的序列号由 `tools/dogkeygen/dogkeygen` 生成:用工程里 `<LicenseManager>` 的 RSA 私钥做
PKCS#1 v1.5 签名,产物端用同一工程的公钥校验。用法见 `tools/dogkeygen/README.md`。
- 已知差异:打包产物里注入的 runtime 是仓库自带的**预编译 blob**(`core/*_runtime*.inc`),
对自签序列号返回 `state=2`(INVALID)。要让产物端也接受自签序列号,需要先用 `runtime/` 源码重建该 blob;
编译器侧(导入/校验序列号)走的是 `core.a` 里的同一个 `LicensingManager`,自签序列号已验证可用。