diff --git a/.gitignore b/.gitignore index 4034e2f..04aff71 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ /tools/dogkeygen/dogkeygen.exe /tools/dogkeygen/*.a /tools/dogkeygen/*.exe +/tools/dogkeygen/testhost_lin +/tools/dogkeygen/testhost_lin.exe diff --git a/BUILD-LINUX.md b/BUILD-LINUX.md index ffaec50..aafbca6 100644 --- a/BUILD-LINUX.md +++ b/BUILD-LINUX.md @@ -56,6 +56,36 @@ LD_LIBRARY_PATH=$PWD/bin ./bin/64/release/dogprotect_con - **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 `(macOS 头) | 用 `#ifdef __APPLE__` 包住 | +| `precompiled.h` | 用了 `va_copy/va_end` 但缺 `` | 补 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`)。 diff --git a/core/gen_inc_lin.py b/core/gen_inc_lin.py new file mode 100644 index 0000000..1847d7e --- /dev/null +++ b/core/gen_inc_lin.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +"""把编译出来的 runtime .so 打包成 core/lin_runtimeNN.so.inc(打包器内置的 runtime 资源)。 + +格式与 core/files.cc::IFile::OpenResource(..., is_enc=true) 对应: + 前 4 字节 = key(uint32 小端),其余字节按位置异或: + out[p] = in[p] ^ uint8(rotl32(key, p) + p) + +`_so_code` 是 runtime 被 VMProtect 处理后导出的函数元数据缓冲(export/import/map/function 四张表)。 +这里写空表({0}),打包器读到的就是 0 条 —— runtime 以整块方式注入,不做函数级映射。 + +用法: + python3 core/gen_inc_lin.py core/lin_runtime64.so.inc lin_runtime64 +""" +import sys, os + +MASK = 0xFFFFFFFF + +def rotl32(v, n): + n &= 31 + v &= MASK + return ((v << n) | (v >> (32 - n))) & MASK if n else v + +def encrypt(data: bytes, key: int) -> bytes: + out = bytearray(len(data)) + for i, b in enumerate(data): + out[i] = b ^ ((rotl32(key, i) + i) & 0xFF) + return bytes(out) + +def emit(fh, name, data): + fh.write('const uint8_t %s[] = {\n' % name) + for i in range(0, len(data), 16): + fh.write('\t' + ', '.join('0x%02x' % b for b in data[i:i + 16]) + ',\n') + fh.write('};\n\n') + +def main(): + if len(sys.argv) != 4: + print(__doc__) + return 1 + so_path, out_path, prefix = sys.argv[1], sys.argv[2], sys.argv[3] + raw = open(so_path, 'rb').read() + key = 0x5A17C0DE # 任意固定 key,打包器从头部读取 + payload = key.to_bytes(4, 'little') + encrypt(raw, key) + with open(out_path, 'w', newline='\n') as fh: + emit(fh, prefix + '_so_code', b'\x00') # 空元数据表 + emit(fh, prefix + '_so_file', payload) # key + 加密后的 .so + print('%s: %d bytes .so -> %d bytes resource (key=0x%08X)' % (so_path, len(raw), len(payload), key)) + return 0 + +if __name__ == '__main__': + sys.exit(main()) diff --git a/runtime/licensing_manager.cc b/runtime/licensing_manager.cc index 559ec73..bc99610 100644 --- a/runtime/licensing_manager.cc +++ b/runtime/licensing_manager.cc @@ -173,6 +173,9 @@ int LicensingManager::SetSerialNumber(const char *serial) { CriticalSection cs(critical_section_); + static int dbg = getenv("DOGDBG") ? 1 : 0; + if (dbg) fprintf(stderr, "[dbrt] SetSerialNumber('%s')\n", serial ? serial : "(null)"); + save_state(SERIAL_STATE_FLAG_INVALID); if (!serial) @@ -193,6 +196,8 @@ int LicensingManager::SetSerialNumber(const char *serial) } // check license data integrity + if (dbg) fprintf(stderr, "[dbrt] serial_len=%zu decoded_len=%zu crc=%d data_size=%zu\n", + len, len, (int)CheckLicenseDataCRC(), license_data_->size()); if (!CheckLicenseDataCRC()) { delete [] binary_serial; return save_state(SERIAL_STATE_FLAG_CORRUPTED); @@ -242,6 +247,12 @@ int LicensingManager::SetSerialNumber(const char *serial) if (!serial_) return SERIAL_STATE_FLAG_INVALID; + if (dbg && serial_->size() > 2) + fprintf(stderr, "[dbrt] block[0..2]=%02X %02X %02X size=%zu exp_off=%u exp_size=%u mod_off=%u mod_size=%u\n", + serial_->GetByte(0), serial_->GetByte(1), serial_->GetByte(2), serial_->size(), + license_data_->GetDWord(FIELD_PUBLIC_EXP_OFFSET * 4), license_data_->GetDWord(FIELD_PUBLIC_EXP_SIZE * 4), + license_data_->GetDWord(FIELD_MODULUS_OFFSET * 4), license_data_->GetDWord(FIELD_MODULUS_SIZE * 4)); + if (serial_->GetByte(0) != 0 || serial_->GetByte(1) != 2) return SERIAL_STATE_FLAG_INVALID; diff --git a/runtime/lin_runtime.mak b/runtime/lin_runtime.mak index f70e323..d576d0c 100644 --- a/runtime/lin_runtime.mak +++ b/runtime/lin_runtime.mak @@ -8,11 +8,11 @@ TMP_DIR := ../tmp/lin/runtime/$(ARCH_DIR)/runtime PCH_DIR := $(TMP_DIR)/runtime.gch DEFINES := LFLAGS := $(LFLAGS) -Wl,--no-undefined -shared -Wl,--wrap=memcpy -Wl,--wrap=__poll_chk -Wl,--wrap=__fdelt_chk -LIBS = ~/curl-7.35.0-$(ARCH_DIR)/lib/libcurl.so -ldl -lrt -L../bin/ -lDogProtectSDK$(ARCH_DIR) +LIBS = -lcurl -ldl -lrt -L../bin/ -lDogProtectSDK$(ARCH_DIR) DYLIBS := ../bin/libDogProtectSDK$(ARCH_DIR).so OBJCOMP := OBJECTS := $(addprefix $(TMP_DIR)/, $(SOURCES:.cc=.o)) -INCFLAGS := -I ~/curl-7.35.0-$(ARCH_DIR)/include +INCFLAGS := $(shell pkg-config --cflags libcurl 2>/dev/null) include ../lin_common.mak diff --git a/runtime/precompiled.h b/runtime/precompiled.h index f5ca3fc..2803439 100644 --- a/runtime/precompiled.h +++ b/runtime/precompiled.h @@ -24,6 +24,8 @@ void __cdecl operator delete[](void *p); #include #include #include +#include +#include #include #include #include @@ -31,7 +33,9 @@ void __cdecl operator delete[](void *p); #include #include #include +#ifdef __APPLE__ #include +#endif #include #include #include diff --git a/tools/dogkeygen/testhost_lin.c b/tools/dogkeygen/testhost_lin.c new file mode 100644 index 0000000..5e29dfc --- /dev/null +++ b/tools/dogkeygen/testhost_lin.c @@ -0,0 +1,29 @@ +// Linux 测试宿主:调 SDK 的序列号/字符串接口,用来验证产物端的授权校验 +// 构建:clang -O1 -o testhost_lin testhost_lin.c -L/bin -lDogProtectSDK64 -Wl,-rpath,/bin +#include +#include + +int DogProtectSetSerialNumber(const char *serial); +int DogProtectGetSerialNumberState(void); +char *DogProtectDecryptStringA(const char *val); +void DogProtectBegin(const char *marker); +void DogProtectEnd(void); + +int main(int argc, char *argv[]) +{ + DogProtectBegin("main"); + + char *text = DogProtectDecryptStringA("decrypted-by-dogprotect"); + printf("decrypt: %s\n", text ? text : "(null)"); + + if (argc > 1) { + int st = DogProtectSetSerialNumber(argv[1]); + printf("SetSerialNumber -> %d\n", st); + } + int state = DogProtectGetSerialNumberState(); + printf("State -> %d\n", state); + printf("%s\n", state == 0 ? "SERIAL OK" : "SERIAL NOT OK"); + + DogProtectEnd(); + return 0; +}