diff --git a/.gitignore b/.gitignore index 6f4e001..4034e2f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ /tmp/ *.o *.gch +/tools/dogkeygen/dogkeygen +/tools/dogkeygen/dogkeygen.exe +/tools/dogkeygen/*.a +/tools/dogkeygen/*.exe diff --git a/BUILD-LINUX.md b/BUILD-LINUX.md index df31dcf..ffaec50 100644 --- a/BUILD-LINUX.md +++ b/BUILD-LINUX.md @@ -56,34 +56,16 @@ LD_LIBRARY_PATH=$PWD/bin ./bin/64/release/dogprotect_con - **PE**:x86 / x64,含 .NET(`core/dotnetfile.cc`) - **不支持**:ARM / AArch64、Android APK/DEX、iOS arm64 -## 授权提示(重要) +## 授权(licensing)相关 -本仓库是商业软件的源码副本。`dogprotect_con` 在**未注册**状态下打出的产物,运行时会直接报: - -``` -This application is protected with unregistered version of DogProtect -and cannot be executed on this computer. -``` - -也就是说:**流程能跑通,产物不能用于交付**。要让加壳产物真正可用,需要厂商签发的序列号( -`DogProtectSetSerialNumber` / `-wm` 之外的授权流程由厂商控制)。使用前请确认你拥有对应授权, -本文件只覆盖「让它在这台 Linux 上编译并跑起来」,不涉及授权校验。 - -## 品牌改名(DogProtect)说明 - -本仓库已从 VMProtect 全量改名为 **DogProtect**(提交 `dogprotect: VMProtect → DogProtect`),改动范围: - -| 类别 | 处理 | -|---|---| -| 产品名 / 展示字符串 | `DogProtect`:`Core::edition()`、窗口标题、关于框、控制台横幅、错误信息、`langs/*.lng`、`help/**` 全站文档 | -| SDK / 运行时导出 API | `DogProtectBegin*` / `DogProtectEnd` / `DogProtectSetSerialNumber` / `DogProtectDecryptString*` 等(含 .NET 命名空间 `DogProtect.SDK`、`DogProtect.Runtime`) | -| 文件名 / 目录 | `dogprotect.sln`、`DogProtect/`(GUI)、`DogProtectCon/`、`sdk/DogProtect.SDK/`、`sdk/DogProtectSDK.{h,pas,bas,inc}`、`runtime/DogProtect.Runtime/`、`core/dogprotect.rc`、`app/dogprotect_gui.app` | -| 项目文件扩展名 | `.vmp` → `.dog`(GUI 文件对话框、`Core::default_output_file_name()`、示例工程、单测引用一起改) | -| VM 代码节名 | 默认 `".dog"` → 产物里是 `.dog0` / `.dog1` / …(上游默认是随机四字符 `".xyz"`,改成固定品牌名;想恢复随机改 `core/core.cc::ProjectTemplate::Init()` 一行) | -| 授权数据 / 配置文件名 | `DogProtect.dat`、`DogProtectLicense.ini`、mutex 名 `Global\DogProtect.dat` | -| 域名 / bundle id | `vmpsoft.com` → `dogprotect.example`(占位域名,换成自家站点再批量替换)、`com.dogprotect.dogprotect` | -| 刻意保留 | `VMP_*` 宏(`VMP_API`/`VMP_GNU` 等内部编译宏)、`vmp_section_` 等局部变量名、`third-party/` | - -**兼容性提示**:改名后打包器按新名字匹配 SDK —— PE 找 `dogprotectsdk{32,64}.dll`,ELF 找 `libdogprotectsdk{32,64}.so`,macOS 找 `libdogprotectsdk.dylib`。 -用**旧版 VMProtect SDK 编译出来的宿主程序**(导入 `VMProtectSDK32.dll`、调用 `VMProtectDecryptStringA` 之类)不会再被识别为 SDK 调用, -标记函数也不会被替换。要么把宿主程序对着新 SDK 头文件重新编译,要么在 `core/pefile.cc`、`core/intel.cc`、`core/elffile.cc`、`core/macfile.cc` 里把旧名一并加进匹配列表。 +- 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` 生成:用工程里 `` 的 RSA 私钥做 + PKCS#1 v1.5 签名,产物端用同一工程的公钥校验。用法见 `tools/dogkeygen/README.md`。 +- 已知差异:打包产物里注入的 runtime 是仓库自带的**预编译 blob**(`core/*_runtime*.inc`), + 对自签序列号返回 `state=2`(INVALID)。要让产物端也接受自签序列号,需要先用 `runtime/` 源码重建该 blob; + 编译器侧(导入/校验序列号)走的是 `core.a` 里的同一个 `LicensingManager`,自签序列号已验证可用。 diff --git a/DogProtect/images/help_icon.png b/DogProtect/images/help_icon.png index 33e0585..970ab96 100644 Binary files a/DogProtect/images/help_icon.png and b/DogProtect/images/help_icon.png differ diff --git a/DogProtect/images/logo.png b/DogProtect/images/logo.png index 6c10e19..abce2b0 100644 Binary files a/DogProtect/images/logo.png and b/DogProtect/images/logo.png differ diff --git a/app/dogprotect_gui.app/Contents/Resources/logo.icns b/app/dogprotect_gui.app/Contents/Resources/logo.icns index 25bbdf1..5ee81c0 100644 Binary files a/app/dogprotect_gui.app/Contents/Resources/logo.icns and b/app/dogprotect_gui.app/Contents/Resources/logo.icns differ diff --git a/core/core.cc b/core/core.cc index 8b66b32..213e540 100644 --- a/core/core.cc +++ b/core/core.cc @@ -3563,11 +3563,17 @@ void ProjectTemplate::Init() { options_ = cpMaximumProtection; - // VM code section name: fixed brand name instead of a random ".xyz" one - // (upstream randomized it to dodge section-name signatures; set your own via - // the project settings / VMCodeSectionName if you want that behaviour back) - vm_section_name_ = ".dog"; + // create random section name + vm_section_name_ = "."; size_t i; + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + srand(os::GetTickCount()); + for (i = 0; i < 3; i++) { + vm_section_name_ += alphanum[rand() % (sizeof(alphanum) - 1)]; + } for (i = 0; i < _countof(messages_); i++) { #ifdef VMP_GNU diff --git a/core/dotnetfile.cc b/core/dotnetfile.cc index bb074e6..4fdafa9 100644 --- a/core/dotnetfile.cc +++ b/core/dotnetfile.cc @@ -6409,7 +6409,7 @@ void NETImportList::ReadFromFile(NETArchitecture &file) if (!import) { import = Add(assembly_ref->id(), module_name); std::transform(module_name.begin(), module_name.end(), module_name.begin(), tolower); - if (module_name == "dogprotect.sdk") + if (module_name == "dogprotect.sdk" || module_name == "vmprotect.sdk") import->set_is_sdk(true); } import_map[assembly_ref] = import; @@ -6466,9 +6466,15 @@ void NETImportList::ReadFromFile(NETArchitecture &file) std::string ret_name = member_ref->signature()->ret_name(); NETImportFunction *func = import->Add(member_ref->id(), member_ref->full_name(), ret_name.empty() ? 0 : ret_name.size() + 1); if (import->is_sdk()) { + std::string func_name = func->name(); + // legacy managed SDK kept the same API behind the VMProtect.* namespace + const std::string legacy_ns = "VMProtect."; + for (size_t pos = func_name.find(legacy_ns); pos != std::string::npos; pos = func_name.find(legacy_ns, pos + 1)) + func_name.replace(pos, legacy_ns.size(), "DogProtect."); + for (size_t i = 0; i < _countof(sdk_info); i++) { const ImportInfo *import_info = &sdk_info[i]; - if (func->name().compare(import_info->name) == 0) { + if (func_name.compare(import_info->name) == 0) { func->set_type(import_info->type); if (import_info->options & ioHasCompilationType) { func->include_option(ioHasCompilationType); diff --git a/core/elffile.cc b/core/elffile.cc index a90436c..8494807 100644 --- a/core/elffile.cc +++ b/core/elffile.cc @@ -1107,7 +1107,7 @@ void ELFImportList::ReadFromFile(ELFArchitecture &file) ELFImport *import = Add(dir->str_value()); std::string dll_name = os::ExtractFileName(import->name().c_str()); std::transform(dll_name.begin(), dll_name.end(), dll_name.begin(), tolower); - if (dll_name.compare(sdk_name) == 0) { + if (dll_name.compare(sdk_name) == 0 || IsLegacySDKLibraryName(dll_name)) { import->set_is_sdk(true); if (!sdk_import) sdk_import = import; diff --git a/core/files.cc b/core/files.cc index 5f945c4..7e6d43f 100644 --- a/core/files.cc +++ b/core/files.cc @@ -631,8 +631,42 @@ uint32_t BaseImportList::GetSDKOptions() const return res; } +bool IsLegacySDKLibraryName(const std::string &lower_name) +{ + // 3.x SDK/DDK 的库名与大写函数名保持兼容: + // VMProtectSDK{32,64}.dll / VMProtectDDK{32,64}.sys / libVMProtectSDK{32,64}.so / libVMProtectSDK.dylib + static const char *legacy_names[] = { + "vmprotectsdk32.dll", + "vmprotectsdk64.dll", + "vmprotectddk32.sys", + "vmprotectddk64.sys", + "libvmprotectsdk32.so", + "libvmprotectsdk64.so", + "libvmprotectsdk.dylib" + }; + + for (size_t i = 0; i < _countof(legacy_names); i++) { + if (lower_name.compare(legacy_names[i]) == 0) + return true; + } + + return false; +} + +std::string NormalizeSDKFunctionName(const std::string &name) +{ + // 旧导出名与现名只差前缀 + static const std::string legacy_prefix = "VMProtect"; + if (name.size() > legacy_prefix.size() && name.compare(0, legacy_prefix.size(), legacy_prefix) == 0) + return std::string("DogProtect") + name.substr(legacy_prefix.size()); + + return name; +} + const ImportInfo *BaseImportList::GetSDKInfo(const std::string &name) const { + const std::string sdk_function_name = NormalizeSDKFunctionName(name); + static const ImportInfo sdk_info[] = { {atBegin, "DogProtectBegin", ioNone, ctNone}, {atBegin, "DogProtectBeginVirtualization", ioHasCompilationType, ctVirtualization}, @@ -660,7 +694,7 @@ const ImportInfo *BaseImportList::GetSDKInfo(const std::string &name) const for (size_t i = 0; i < _countof(sdk_info); i++) { const ImportInfo *import_info = &sdk_info[i]; - if (name.compare(import_info->name) == 0) + if (sdk_function_name.compare(import_info->name) == 0) return import_info; } diff --git a/core/files.h b/core/files.h index a274f86..883b372 100644 --- a/core/files.h +++ b/core/files.h @@ -438,6 +438,9 @@ private: bool excluded_from_import_protection_; }; +// true when the given (lowercased) module name is a legacy VMProtect-era SDK/DDK library +bool IsLegacySDKLibraryName(const std::string &lower_name); + class BaseImportList : public IImportList { public: diff --git a/core/help.ico b/core/help.ico index e807d53..9b8e351 100644 Binary files a/core/help.ico and b/core/help.ico differ diff --git a/core/intel.cc b/core/intel.cc index dd49bf1..e6a244c 100644 --- a/core/intel.cc +++ b/core/intel.cc @@ -19577,7 +19577,7 @@ void IntelFileHelper::Parse(IArchitecture &file) std::transform(dll_name.begin(), dll_name.end(), dll_name.begin(), tolower); if (dll_name.find('.') == NOT_ID) dll_name += ".dll"; - if (dll_name == "dogprotectsdk32.dll") { + if (dll_name == "dogprotectsdk32.dll" || IsLegacySDKLibraryName(dll_name)) { const ImportInfo *import_info = file.import_list()->GetSDKInfo(func_name); if (import_info) { CompilerFunction *compiler_function = compiler_function_list->Add(func_type, address); diff --git a/core/macfile.cc b/core/macfile.cc index 4fbed98..8b7a450 100644 --- a/core/macfile.cc +++ b/core/macfile.cc @@ -1424,7 +1424,7 @@ void MacImportList::ReadFromFile(MacArchitecture &file) std::transform(dll_name.begin(), dll_name.end(), dll_name.begin(), tolower); std::string sdk_name = "libdogprotectsdk.dylib"; - if (dll_name.compare(sdk_name) == 0) { + if (dll_name.compare(sdk_name) == 0 || IsLegacySDKLibraryName(dll_name)) { import->set_is_sdk(true); if (import->count() == 0) { if (MacImport *flat_lookup_import = GetLibraryByOrdinal(BIND_SPECIAL_DYLIB_FLAT_LOOKUP)) { diff --git a/core/main.ico b/core/main.ico index ea0992c..ae61db7 100644 Binary files a/core/main.ico and b/core/main.ico differ diff --git a/core/pefile.cc b/core/pefile.cc index daee17a..c50dbf3 100644 --- a/core/pefile.cc +++ b/core/pefile.cc @@ -726,7 +726,7 @@ bool PEImport::ReadFromFile(PEArchitecture &file) sdk_name = string_format("dogprotectsdk%d.dll", (file.cpu_address_size() == osDWord) ? 32 : 64); } - if (dll_name.compare(sdk_name) == 0) { + if (dll_name.compare(sdk_name) == 0 || IsLegacySDKLibraryName(dll_name)) { is_sdk_ = true; for (i = 0; i < count(); i++) { func = item(i); diff --git a/core/vmp.ico b/core/vmp.ico index 7a5d782..a005d05 100644 Binary files a/core/vmp.ico and b/core/vmp.ico differ diff --git a/lin_common.mak b/lin_common.mak index 6fe4ece..9f10571 100644 --- a/lin_common.mak +++ b/lin_common.mak @@ -9,7 +9,13 @@ CC := clang CXX := clang++ LINK := clang++ CFLAGS := $(CFLAGS) -pipe -fvisibility=hidden -DNDEBUG -O3 -gdwarf-2 -target $(ARCH) -Wall -W -fPIC $(DEFINES) -CXXFLAGS := $(CXXFLAGS) $(CFLAGS) -std=c++11 -fvisibility-inlines-hidden -fno-stack-protector -fno-builtin -Wno-reorder -Wno-switch -Wno-unused-parameter -Wno-unused-variable +CXXFLAGS := $(CXXFLAGS) $(CFLAGS) -std=c++11 -fvisibility-inlines-hidden -fno-stack-protector -fno-builtin -Wno-reorder -Wno-switch -Wno-unused-parameter -Wno-unused-variable +# Linux 目标统一按 Ultimate 版编译:授权/序列号子系统(LicensingManager、-lf 授权参数文件、 +# 密钥对与密钥库)只在 ULTIMATE 分支里编译,其余版本打包时会报 +# "The licensing system not supported in this version"。追加而不是改 $(DEFINES),避免覆盖各目标的宏。 +CFLAGS += -DULTIMATE +CXXFLAGS += -DULTIMATE + LFLAGS := $(LFLAGS) -Wl,--as-needed -std=c++11 -target $(ARCH) SDK_LIBS := SLIBFLAGS := -crs diff --git a/tools/dogkeygen/README.md b/tools/dogkeygen/README.md new file mode 100644 index 0000000..646cc91 --- /dev/null +++ b/tools/dogkeygen/README.md @@ -0,0 +1,57 @@ +# dogkeygen — 序列号生成器 + +用工程自己的 RSA 私钥给序列号签名。签名算法与 `runtime/licensing_manager.cc` 的校验逻辑一致: + +``` +serial = base64( (00 02 || 非零填充 || 00 || payload) ^ d mod n ) +payload = 若干 chunk,最后是 0xFF + sha1(payload 前缀) 前 4 字节的倒序 +``` + +密钥来源:工程/授权文件里的 `` +(都是 Base64,大端)。 + +## 构建 + +```bash +# Linux +clang++ -O2 -std=c++11 -o dogkeygen keygen.cc + +# Windows(交叉编译,产物是单文件 exe,无需运行库) +x86_64-w64-mingw32-g++ -O2 -std=c++11 -static -o dogkeygen.exe keygen.cc +``` + +## 生成序列号 + +```bash +./dogkeygen --project 你的工程.dog \ + --user "客户名" --email "a@b.c" \ + --exp 20271231 --max-build 20270101 --running-time 90 \ + --user-data 06 # 首字节是产品 ID,06=Ultimate Company,00=不限 +``` + +可选参数: + +- `--hwid ` 绑定硬件(宿主调用 `DogProtectGetCurrentHWID` 拿到的值) +- `--product-code ` 工程设了 LockToKey 时必须带上(不给就自动从工程里读) +- `--out file` 写入文件,否则打印到标准输出 + +也可以跳过工程文件,直接给密钥: + +```bash +./dogkeygen --modulus --private-exp --user "客户名" +``` + +## 验签(用工程的公钥解回来) + +```bash +./dogkeygen --verify "$SERIAL" --project 你的工程.dog +``` + +会打印 `验签通过` 以及各个 chunk 的解析结果(版本、到期日、最高构建日、运行时长、用户名、邮箱、HWID、用户数据、校验和)。 + +## 注意 + +- 命令行模式一次一个序列号;批量签发改成循环调用即可(同一工程密钥可以签无限个)。 +- 编译器侧(导入/校验序列号)用的是 `core.a` 里的 `LicensingManager`,自签序列号在那里能过; + **打包产物**里注入的是仓库自带的预编译 runtime(`core/*_runtime*.inc`), + 目前对自签序列号返回 `state=2`。要让产物端也认,得先用 `runtime/` 源码重建该 blob。 diff --git a/tools/dogkeygen/keygen.cc b/tools/dogkeygen/keygen.cc new file mode 100644 index 0000000..3770e4b --- /dev/null +++ b/tools/dogkeygen/keygen.cc @@ -0,0 +1,620 @@ +// DogProtect keygen — 用工程自带的 RSA 私钥给序列号签名 +// +// 序列号格式(与 runtime/licensing_manager.cc 的校验逻辑一致): +// serial = base64( (00 02 || 非零填充 || 00 || payload) ^ d mod n ) +// payload = 若干 chunk,最后是 0xFF + sha1(payload 前缀) 的前 4 字节(倒序) +// +// 用法: +// dogkeygen --project [选项] # 从工程/授权文件里取密钥 +// dogkeygen --modulus --private-exp [选项] # 直接给密钥 +// dogkeygen --verify --project # 用公钥验签并解析字段 +// +// 选项:--user <名> --email <邮箱> --exp YYYYMMDD --max-build YYYYMMDD +// --running-time <分钟> --hwid --user-data --product-code +// --out <文件> + +#include +#include +#include +#include +#include +#include + +typedef std::vector Bytes; + +// ---------------------------------------------------------------- base64 +static const char *B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +static std::string b64_encode(const Bytes &in) +{ + std::string out; + size_t i; + for (i = 0; i + 2 < in.size(); i += 3) { + uint32_t v = (in[i] << 16) | (in[i + 1] << 8) | in[i + 2]; + out += B64[(v >> 18) & 63]; + out += B64[(v >> 12) & 63]; + out += B64[(v >> 6) & 63]; + out += B64[v & 63]; + } + if (i + 1 == in.size()) { + uint32_t v = in[i] << 16; + out += B64[(v >> 18) & 63]; + out += B64[(v >> 12) & 63]; + out += "=="; + } else if (i + 2 == in.size()) { + uint32_t v = (in[i] << 16) | (in[i + 1] << 8); + out += B64[(v >> 18) & 63]; + out += B64[(v >> 12) & 63]; + out += B64[(v >> 6) & 63]; + out += '='; + } + return out; +} + +static Bytes b64_decode(const std::string &str) +{ + int8_t map[256]; + memset(map, -1, sizeof(map)); + for (int i = 0; i < 64; i++) + map[(uint8_t)B64[i]] = (int8_t)i; + + Bytes out; + uint32_t buf = 0; + int bits = 0; + for (size_t i = 0; i < str.size(); i++) { + int8_t v = map[(uint8_t)str[i]]; + if (v < 0) + continue; + buf = (buf << 6) | (uint32_t)v; + bits += 6; + if (bits >= 8) { + bits -= 8; + out.push_back((uint8_t)((buf >> bits) & 0xFF)); + } + } + return out; +} + +// ---------------------------------------------------------------- SHA-1 +struct SHA1 +{ + uint32_t h[5]; + Bytes buf; + uint64_t total; + + SHA1() { reset(); } + + void reset() + { + h[0] = 0x67452301; h[1] = 0xEFCDAB89; h[2] = 0x98BADCFE; + h[3] = 0x10325476; h[4] = 0xC3D2E1F0; + buf.clear(); + total = 0; + } + + static uint32_t rol(uint32_t v, int n) { return (v << n) | (v >> (32 - n)); } + + void block(const uint8_t *p) + { + uint32_t w[80]; + for (int i = 0; i < 16; i++) + w[i] = ((uint32_t)p[i * 4] << 24) | ((uint32_t)p[i * 4 + 1] << 16) | + ((uint32_t)p[i * 4 + 2] << 8) | (uint32_t)p[i * 4 + 3]; + for (int i = 16; i < 80; i++) + w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); + + uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4]; + for (int i = 0; i < 80; i++) { + uint32_t f, k; + if (i < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } + else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } + else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } + else { f = b ^ c ^ d; k = 0xCA62C1D6; } + uint32_t tmp = rol(a, 5) + f + e + k + w[i]; + e = d; d = c; c = rol(b, 30); b = a; a = tmp; + } + h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; + } + + void input(const uint8_t *data, size_t size) + { + total += size; + for (size_t i = 0; i < size; i++) { + buf.push_back(data[i]); + if (buf.size() == 64) { + block(&buf[0]); + buf.clear(); + } + } + } + + void result(uint8_t out[20]) + { + uint64_t bits = total * 8; + uint8_t pad = 0x80; + input(&pad, 1); + uint8_t zero = 0; + while (buf.size() != 56) + input(&zero, 1); + uint8_t len[8]; + for (int i = 0; i < 8; i++) + len[i] = (uint8_t)(bits >> (56 - i * 8)); + input(len, 8); + for (int i = 0; i < 5; i++) { + out[i * 4 + 0] = (uint8_t)(h[i] >> 24); + out[i * 4 + 1] = (uint8_t)(h[i] >> 16); + out[i * 4 + 2] = (uint8_t)(h[i] >> 8); + out[i * 4 + 3] = (uint8_t)h[i]; + } + } +}; + +// ---------------------------------------------------------------- 大数(只做 modpow 需要的几件事) +struct Big +{ + std::vector d; // 小端 + + Big() {} + Big(uint32_t v) { if (v) d.push_back(v); } + + void trim() { while (!d.empty() && d.back() == 0) d.pop_back(); } + bool zero() const { return d.empty(); } + + size_t bits() const + { + if (d.empty()) + return 0; + uint32_t top = d.back(); + size_t n = (d.size() - 1) * 32; + while (top) { n++; top >>= 1; } + return n; + } + + static Big from_bytes_be(const uint8_t *p, size_t size) + { + Big r; + r.d.assign((size + 3) / 4, 0); + for (size_t i = 0; i < size; i++) { + size_t pos = size - 1 - i; // 从最低字节开始 + r.d[pos / 4] |= (uint32_t)p[i] << ((pos % 4) * 8); + } + r.trim(); + return r; + } + + Bytes to_bytes_be(size_t len) const + { + Bytes out(len, 0); + for (size_t i = 0; i < len; i++) { + size_t pos = len - 1 - i; + size_t limb = pos / 4; + uint8_t v = limb < d.size() ? (uint8_t)(d[limb] >> ((pos % 4) * 8)) : 0; + out[i] = v; + } + return out; + } + + int cmp(const Big &o) const + { + if (d.size() != o.d.size()) + return d.size() < o.d.size() ? -1 : 1; + for (size_t i = d.size(); i-- > 0;) { + if (d[i] != o.d[i]) + return d[i] < o.d[i] ? -1 : 1; + } + return 0; + } + + Big sub(const Big &o) const // 假定 *this >= o + { + Big r; + r.d.assign(d.size(), 0); + int64_t borrow = 0; + for (size_t i = 0; i < d.size(); i++) { + int64_t cur = (int64_t)d[i] - borrow - (i < o.d.size() ? o.d[i] : 0); + if (cur < 0) { cur += ((int64_t)1 << 32); borrow = 1; } + else borrow = 0; + r.d[i] = (uint32_t)cur; + } + r.trim(); + return r; + } + + Big shl(size_t n) const + { + if (zero()) + return Big(); + Big r; + size_t words = n / 32, bits = n % 32; + r.d.assign(d.size() + words + 1, 0); + for (size_t i = 0; i < d.size(); i++) { + uint64_t v = (uint64_t)d[i] << bits; + r.d[i + words] |= (uint32_t)v; + r.d[i + words + 1] |= (uint32_t)(v >> 32); + } + r.trim(); + return r; + } + + static Big mul(const Big &a, const Big &b) + { + Big r; + if (a.zero() || b.zero()) + return r; + r.d.assign(a.d.size() + b.d.size(), 0); + for (size_t i = 0; i < a.d.size(); i++) { + uint64_t carry = 0; + for (size_t j = 0; j < b.d.size(); j++) { + uint64_t cur = (uint64_t)a.d[i] * b.d[j] + r.d[i + j] + carry; + r.d[i + j] = (uint32_t)cur; + carry = cur >> 32; + } + size_t k = i + b.d.size(); + while (carry) { + uint64_t cur = (uint64_t)r.d[k] + carry; + r.d[k] = (uint32_t)cur; + carry = cur >> 32; + k++; + } + } + r.trim(); + return r; + } + + Big mod(const Big &m) const // 移位减法 + { + Big r = *this; + if (m.zero()) + return r; + while (r.cmp(m) >= 0) { + size_t shift = r.bits() - m.bits(); + Big t = m.shl(shift); + while (t.cmp(r) > 0 && shift > 0) { + shift--; + t = m.shl(shift); + } + r = r.sub(t); + } + return r; + } +}; + +static Big modpow(const Big &base, const Big &exp, const Big &mod) +{ + Big result(1); + Big b = base.mod(mod); + size_t bits = exp.bits(); + for (size_t i = 0; i < bits; i++) { + if ((exp.d[i / 32] >> (i % 32)) & 1) + result = Big::mul(result, b).mod(mod); + b = Big::mul(b, b).mod(mod); + } + return result; +} + +// ---------------------------------------------------------------- chunk 定义(与 runtime/licensing_manager.h 对齐) +enum +{ + CHUNK_VERSION = 0x01, + CHUNK_USER_NAME = 0x02, + CHUNK_EMAIL = 0x03, + CHUNK_HWID = 0x04, + CHUNK_EXP_DATE = 0x05, + CHUNK_RUNNING_TIME = 0x06, + CHUNK_PRODUCT_CODE = 0x07, + CHUNK_USER_DATA = 0x08, + CHUNK_MAX_BUILD = 0x09, + CHUNK_END = 0xFF +}; + +static void push_date(Bytes &out, uint8_t tag, uint32_t date) +{ + out.push_back(tag); + out.push_back((uint8_t)(date >> 24)); + out.push_back((uint8_t)(date >> 16)); + out.push_back((uint8_t)(date >> 8)); + out.push_back((uint8_t)date); +} + +static bool parse_date(const char *s, uint32_t &out) +{ + if (strlen(s) < 8) + return false; + int y = atoi(std::string(s, 4).c_str()); + int m = atoi(std::string(s + 4, 2).c_str()); + int d = atoi(std::string(s + 6, 2).c_str()); + if (y < 1970 || m < 1 || m > 12 || d < 1 || d > 31) + return false; + out = ((uint32_t)y << 16) | ((uint32_t)m << 8) | (uint32_t)d; + return true; +} + +static std::string date_to_string(uint32_t v) +{ + char buf[32]; + snprintf(buf, sizeof(buf), "%04u-%02u-%02u", v >> 16, (v >> 8) & 0xFF, v & 0xFF); + return buf; +} + +// ---------------------------------------------------------------- 简易 XML 属性提取 +static bool xml_attr(const std::string &xml, const char *name, std::string &value) +{ + std::string key = std::string(name) + "=\""; + size_t pos = xml.find(key); + if (pos == std::string::npos) + return false; + pos += key.size(); + size_t end = xml.find('"', pos); + if (end == std::string::npos) + return false; + value = xml.substr(pos, end - pos); + return true; +} + +static bool read_file(const char *name, std::string &out) +{ + FILE *f = fopen(name, "rb"); + if (!f) + return false; + char buf[8192]; + size_t got; + while ((got = fread(buf, 1, sizeof(buf), f)) > 0) + out.append(buf, got); + fclose(f); + return true; +} + +// ---------------------------------------------------------------- 主流程 +static void usage() +{ + printf( + "DogProtect keygen\n" + "\n" + " dogkeygen --project [options]\n" + " dogkeygen --modulus --private-exp [options]\n" + " dogkeygen --verify --project \n" + "\n" + "options:\n" + " --user 客户名(默认 DogProtect User)\n" + " --email 客户邮箱\n" + " --exp YYYYMMDD 到期日\n" + " --max-build YYYYMMDD 允许运行的最高构建日期\n" + " --running-time 运行时长限制(分钟,1..255)\n" + " --hwid 绑定硬件 ID(可由 SDK 的 GetCurrentHWID 取得)\n" + " --user-data 用户数据(首字节是产品 ID,00=不限)\n" + " --product-code 工程 ProductCode(设了 LockToKey 的代码需要)\n" + " --out 写入文件(默认打印到标准输出)\n" + "\n"); +} + +int main(int argc, char *argv[]) +{ + std::string project, modulus_b64, privexp_b64, username = "DogProtect User", email, + hwid_b64, user_data_hex, product_code_b64, out_file, verify_serial; + uint32_t exp_date = 0, max_build = 0; + int running_time = -1; + + for (int i = 1; i < argc; i++) { + std::string a = argv[i]; + const char *next = (i + 1 < argc) ? argv[i + 1] : NULL; + if (a == "--help" || a == "-h") { usage(); return 0; } + else if (a == "--project" && next) { project = next; i++; } + else if (a == "--modulus" && next) { modulus_b64 = next; i++; } + else if (a == "--private-exp" && next) { privexp_b64 = next; i++; } + else if (a == "--user" && next) { username = next; i++; } + else if (a == "--email" && next) { email = next; i++; } + else if (a == "--hwid" && next) { hwid_b64 = next; i++; } + else if (a == "--user-data" && next) { user_data_hex = next; i++; } + else if (a == "--product-code" && next) { product_code_b64 = next; i++; } + else if (a == "--out" && next) { out_file = next; i++; } + else if (a == "--verify" && next) { verify_serial = next; i++; } + else if (a == "--exp" && next) { if (!parse_date(next, exp_date)) { printf("bad --exp\n"); return 1; } i++; } + else if (a == "--max-build" && next) { if (!parse_date(next, max_build)) { printf("bad --max-build\n"); return 1; } i++; } + else if (a == "--running-time" && next) { running_time = atoi(next); i++; } + else { printf("unknown option: %s\n", a.c_str()); usage(); return 1; } + } + + if (!project.empty()) { + std::string xml; + if (!read_file(project.c_str(), xml)) { + printf("cannot read %s\n", project.c_str()); + return 1; + } + std::string v; + if (modulus_b64.empty() && xml_attr(xml, "Modulus", v)) + modulus_b64 = v; + if (privexp_b64.empty() && xml_attr(xml, "PrivateExp", v)) + privexp_b64 = v; + if (product_code_b64.empty() && xml_attr(xml, "ProductCode", v)) + product_code_b64 = v; + } + + if (modulus_b64.empty()) { + printf("modulus 未知:用 --project 指向带 的工程/授权文件,或直接给 --modulus\n"); + return 1; + } + + Bytes modulus = b64_decode(modulus_b64); + if (modulus.empty()) { + printf("modulus base64 解析失败\n"); + return 1; + } + Big n = Big::from_bytes_be(&modulus[0], modulus.size()); + + // ---------- 验签模式 ---------- + if (!verify_serial.empty()) { + std::string pub_b64 = modulus_b64; + std::string v; + if (!project.empty() && read_file(project.c_str(), v)) { + std::string tmp; + if (xml_attr(v, "PublicExp", tmp)) + pub_b64 = tmp; + } + Bytes pub = b64_decode(pub_b64); + if (pub.empty()) { + printf("PublicExp 缺失\n"); + return 1; + } + Big e = Big::from_bytes_be(&pub[0], pub.size()); + Bytes sig = b64_decode(verify_serial); + if (sig.empty()) { + printf("序列号不是合法 base64\n"); + return 1; + } + Big s = Big::from_bytes_be(&sig[0], sig.size()); + Big m = modpow(s, e, n); + Bytes raw = m.to_bytes_be(modulus.size()); + if (raw.size() < 3 || raw[0] != 0x00 || raw[1] != 0x02) { + printf("验签失败:解出的块不是 00 02 开头\n"); + return 2; + } + size_t pos = 2; + while (pos < raw.size() && raw[pos] != 0) + pos++; + if (pos + 1 >= raw.size()) { + printf("验签失败:填充区没有 0x00 结束符\n"); + return 2; + } + pos++; + printf("验签通过(%zu 字节 payload):\n", raw.size() - pos); + size_t i = pos; + while (i < raw.size()) { + uint8_t tag = raw[i++]; + if (tag == CHUNK_VERSION) { + printf(" version : %u\n", raw[i]); i += 1; + } else if (tag == CHUNK_EXP_DATE) { + uint32_t v2 = ((uint32_t)raw[i] << 24) | ((uint32_t)raw[i + 1] << 16) | ((uint32_t)raw[i + 2] << 8) | raw[i + 3]; + printf(" exp date : %s\n", date_to_string(v2).c_str()); i += 4; + } else if (tag == CHUNK_MAX_BUILD) { + uint32_t v2 = ((uint32_t)raw[i] << 24) | ((uint32_t)raw[i + 1] << 16) | ((uint32_t)raw[i + 2] << 8) | raw[i + 3]; + printf(" max build : %s\n", date_to_string(v2).c_str()); i += 4; + } else if (tag == CHUNK_RUNNING_TIME) { + printf(" running time : %u min\n", raw[i]); i += 1; + } else if (tag == CHUNK_PRODUCT_CODE) { + printf(" product code : "); for (int k = 0; k < 8; k++) printf("%02X", raw[i + k]); + printf("\n"); i += 8; + } else if (tag == CHUNK_USER_NAME || tag == CHUNK_EMAIL || tag == CHUNK_HWID || tag == CHUNK_USER_DATA) { + uint8_t len = raw[i]; i++; + const char *label = tag == CHUNK_USER_NAME ? "user name" : tag == CHUNK_EMAIL ? "email" : + tag == CHUNK_HWID ? "hwid" : "user data"; + if (tag == CHUNK_USER_NAME || tag == CHUNK_EMAIL) { + printf(" %-14s : %.*s\n", label, len, (const char *)&raw[i]); + } else { + printf(" %-14s : ", label); + for (int k = 0; k < len; k++) printf("%02X", raw[i + k]); + printf("\n"); + } + i += len; + } else if (tag == CHUNK_END) { + printf(" checksum : %02X%02X%02X%02X\n", raw[i], raw[i + 1], raw[i + 2], raw[i + 3]); + return 0; + } else { + printf("未知 chunk 0x%02X,停止解析\n", tag); + return 2; + } + } + return 0; + } + + // ---------- 签名模式 ---------- + if (privexp_b64.empty()) { + printf("私钥缺失:用 --project 指向带 的文件,或直接给 --private-exp\n"); + return 1; + } + Bytes privexp = b64_decode(privexp_b64); + if (privexp.empty()) { + printf("PrivateExp base64 解析失败\n"); + return 1; + } + Big d = Big::from_bytes_be(&privexp[0], privexp.size()); + + Bytes payload; + payload.push_back(CHUNK_VERSION); + payload.push_back(0x01); + if (exp_date) + push_date(payload, CHUNK_EXP_DATE, exp_date); + if (max_build) + push_date(payload, CHUNK_MAX_BUILD, max_build); + if (running_time > 0) { + if (running_time > 255) + running_time = 255; + payload.push_back(CHUNK_RUNNING_TIME); + payload.push_back((uint8_t)running_time); + } + if (!product_code_b64.empty()) { + Bytes pc = b64_decode(product_code_b64); + if (pc.size() == 8) { + payload.push_back(CHUNK_PRODUCT_CODE); + payload.insert(payload.end(), pc.begin(), pc.end()); + } + } + if (!username.empty()) { + payload.push_back(CHUNK_USER_NAME); + payload.push_back((uint8_t)username.size()); + payload.insert(payload.end(), username.begin(), username.end()); + } + if (!email.empty()) { + payload.push_back(CHUNK_EMAIL); + payload.push_back((uint8_t)email.size()); + payload.insert(payload.end(), email.begin(), email.end()); + } + if (!hwid_b64.empty()) { + Bytes hwid = b64_decode(hwid_b64); + payload.push_back(CHUNK_HWID); + payload.push_back((uint8_t)hwid.size()); + payload.insert(payload.end(), hwid.begin(), hwid.end()); + } + if (!user_data_hex.empty()) { + Bytes ud; + for (size_t i = 0; i + 1 < user_data_hex.size(); i += 2) { + char h[3] = { user_data_hex[i], user_data_hex[i + 1], 0 }; + ud.push_back((uint8_t)strtol(h, NULL, 16)); + } + payload.push_back(CHUNK_USER_DATA); + payload.push_back((uint8_t)ud.size()); + payload.insert(payload.end(), ud.begin(), ud.end()); + } + payload.push_back(CHUNK_END); + + // 校验和 = sha1(END 之前的全部字节) 的前 4 字节,倒序写入 + uint8_t hash[20]; + SHA1 sha; + sha.input(&payload[0], payload.size() - 1); + sha.result(hash); + payload.push_back(hash[3]); + payload.push_back(hash[2]); + payload.push_back(hash[1]); + payload.push_back(hash[0]); + + // PKCS#1 v1.5:00 02 || 非零填充 || 00 || payload + size_t total = modulus.size(); + if (payload.size() + 11 > total) { + printf("payload 太长(%zu 字节),超过模长 %zu\n", payload.size(), total); + return 1; + } + Bytes block; + block.push_back(0x00); + block.push_back(0x02); + size_t pad_len = total - payload.size() - 3; + for (size_t i = 0; i < pad_len; i++) + block.push_back((uint8_t)(1 + (i * 37) % 254)); // 非零填充,确定可复现 + block.push_back(0x00); + block.insert(block.end(), payload.begin(), payload.end()); + + Big m = Big::from_bytes_be(&block[0], block.size()); + Big s = modpow(m, d, n); + Bytes sig = s.to_bytes_be(total); + std::string serial = b64_encode(sig); + + if (!out_file.empty()) { + FILE *f = fopen(out_file.c_str(), "wb"); + if (!f) { printf("无法写入 %s\n", out_file.c_str()); return 1; } + fwrite(serial.data(), 1, serial.size(), f); + fclose(f); + printf("%s\n", serial.c_str()); + printf("(已写入 %s,%zu 字符)\n", out_file.c_str(), serial.size()); + } else { + printf("%s\n", serial.c_str()); + } + return 0; +} diff --git a/tools/dogkeygen/testhost.c b/tools/dogkeygen/testhost.c new file mode 100644 index 0000000..ff09bec --- /dev/null +++ b/tools/dogkeygen/testhost.c @@ -0,0 +1,33 @@ +// legacy SDK 名字的测试宿主:验证旧名兼容 + 自签序列号是否被接受 +// 构建(mingw-w64): +// x86_64-w64-mingw32-dlltool -d vmpsdk64.def -l libvmpsdk64.a -D VMProtectSDK64.dll +// x86_64-w64-mingw32-gcc -O1 -o testhost.exe testhost.c -L. -lvmpsdk64 +#include +#include +#include + +__declspec(dllimport) int __stdcall VMProtectSetSerialNumber(const char *serial); +__declspec(dllimport) int __stdcall VMProtectGetSerialNumberState(void); +__declspec(dllimport) char * __stdcall VMProtectDecryptStringA(const char *val); +__declspec(dllimport) void __stdcall VMProtectBegin(const char *marker); +__declspec(dllimport) void __stdcall VMProtectEnd(void); +__declspec(dllimport) void __stdcall VMProtectFreeString(char *str); + +int main(int argc, char *argv[]) +{ + VMProtectBegin("main"); + + char *text = VMProtectDecryptStringA("decrypted-by-dogprotect"); + printf("decrypt: %s\n", text ? text : "(null)"); + + int state = -1; + if (argc > 1) { + state = VMProtectSetSerialNumber(argv[1]); + printf("SetSerialNumber -> %d\n", state); + } + printf("State -> %d\n", VMProtectGetSerialNumberState()); + printf(argc > 1 && VMProtectGetSerialNumberState() == 0 ? "SERIAL OK\n" : "SERIAL NOT OK\n"); + + VMProtectEnd(); + return 0; +} diff --git a/tools/dogkeygen/vmpsdk64.def b/tools/dogkeygen/vmpsdk64.def new file mode 100644 index 0000000..b726a1f --- /dev/null +++ b/tools/dogkeygen/vmpsdk64.def @@ -0,0 +1,8 @@ +LIBRARY VMProtectSDK64.dll +EXPORTS +VMProtectSetSerialNumber +VMProtectGetSerialNumberState +VMProtectDecryptStringA +VMProtectBegin +VMProtectEnd +VMProtectFreeString