验签逻辑(base64 → BN_mod_exp → PKCS#1 解析 → chunk + SHA-1 校验)放在 DogProtectBegin/End 标记里,实测评测:未加壳与加壳后都能正确接受 dogkeygen 签发的序列号、拒绝被改坏的序列号。 这条路不依赖 SDK 的授权 API,也就不依赖打包器注入的 runtime 与 _so_code 元数据。
193 lines
5.7 KiB
C
193 lines
5.7 KiB
C
// 自签序列号验签演示(宿主侧,不用 SDK 的授权 API)
|
|
//
|
|
// 作用:证明「用户验签」这套 API 可以在你自己的程序里复刻 —— 不依赖打包器注入的
|
|
// runtime,也不依赖厂商 blob。加狗壳只负责虚拟机化/反调试,验签走你自己的代码。
|
|
//
|
|
// 构建(Linux):
|
|
// clang -O1 -o verify_demo verify_demo.c -lcrypto -L<repo>/bin -lDogProtectSDK64 \
|
|
// -Wl,-rpath,<repo>/bin
|
|
//
|
|
// 用法:./verify_demo <序列号> # 由 tools/dogkeygen 生成
|
|
//
|
|
// 序列号格式(与 tools/dogkeygen 一致):
|
|
// serial = base64( (00 02 || 非零填充 || 00 || payload) ^ d mod n )
|
|
// payload = chunk..., 末尾 FF + sha1(前面全部) 前 4 字节倒序
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#include <openssl/bn.h>
|
|
#include <openssl/sha.h>
|
|
|
|
// 打包标记:这段会被加壳器虚拟化
|
|
void DogProtectBegin(const char *marker);
|
|
void DogProtectEnd(void);
|
|
|
|
// ---- 公钥(与 tools/dogkeygen 签名用的私钥成对;十六进制大端)----
|
|
// 实际项目里从工程文件的 <LicenseManager Modulus/PublicExp> 取,或做成资源/固定数组
|
|
static const char *kModulusHex =
|
|
"cb8fe83752562dafae44cb4cb06f116605d98d83c5f2740e56d7e897f14314726c11847c133182d08f7e81e63547b644ca0cd4bbbed0701f39e332c85892d7f91d17c6527c2a775b0c38a9212303e25b2e05d5813c1c2baf2119e092b512c072ca21839c3e8d8a693c25b88e26d8f54515d2a8e96d00634cc26b59cf3c4b7b01";
|
|
|
|
static const char *kPublicExpHex = "010001";
|
|
|
|
// 序列号里的到期日:0 表示不限
|
|
static const unsigned long long kBuildDate = 0; // (年<<16)|(月<<8)|日,0 = 不校验
|
|
|
|
static int b64_val(int c)
|
|
{
|
|
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
|
if (c >= '0' && c <= '9') return c - '0' + 52;
|
|
if (c == '+') return 62;
|
|
if (c == '/') return 63;
|
|
return -1;
|
|
}
|
|
|
|
static size_t b64_decode(const char *src, unsigned char *dst, size_t cap)
|
|
{
|
|
size_t n = 0;
|
|
unsigned int buf = 0;
|
|
int bits = 0;
|
|
for (const char *p = src; *p; p++) {
|
|
int v = b64_val((unsigned char)*p);
|
|
if (v < 0) continue; // 忽略 '=' 和杂字符
|
|
buf = (buf << 6) | (unsigned int)v;
|
|
bits += 6;
|
|
if (bits >= 8) {
|
|
bits -= 8;
|
|
if (n < cap) dst[n++] = (unsigned char)(buf >> bits);
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static unsigned int rd32(const unsigned char *p)
|
|
{
|
|
return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) | ((unsigned int)p[2] << 8) | p[3];
|
|
}
|
|
|
|
// 返回 0 = 验签通过
|
|
static int verify_serial(const char *serial, char *user_out, size_t user_cap, int *expired)
|
|
{
|
|
unsigned char sig[512];
|
|
size_t sig_len = b64_decode(serial, sig, sizeof(sig));
|
|
if (sig_len < 16) return 2;
|
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
|
BIGNUM *n = NULL, *e = NULL, *s = BN_bin2bn(sig, (int)sig_len, NULL), *m = NULL;
|
|
BN_hex2bn(&n, kModulusHex);
|
|
BN_hex2bn(&e, kPublicExpHex);
|
|
m = BN_new();
|
|
BN_mod_exp(m, s, e, n, ctx);
|
|
|
|
int mod_len = BN_num_bytes(n);
|
|
unsigned char *block = malloc(mod_len);
|
|
memset(block, 0, mod_len);
|
|
BN_bn2bin(m, block + mod_len - BN_num_bytes(m));
|
|
|
|
int rc = 3;
|
|
if (mod_len < 16 || block[0] != 0x00 || block[1] != 0x02)
|
|
goto done;
|
|
|
|
size_t pos = 2;
|
|
while (pos < (size_t)mod_len && block[pos] != 0x00) pos++;
|
|
if (pos + 1 >= (size_t)mod_len)
|
|
goto done;
|
|
pos++; // 跳过 0x00 分隔符
|
|
|
|
// ---- 解析 chunk ----
|
|
const unsigned char *payload = block + pos;
|
|
size_t plen = (size_t)mod_len - pos;
|
|
size_t i = 0, end_tag = 0;
|
|
int seen_end = 0;
|
|
if (user_out && user_cap) user_out[0] = 0;
|
|
|
|
while (i < plen) {
|
|
unsigned char tag = payload[i];
|
|
if (tag == 0x01) { // VERSION
|
|
if (payload[i + 1] < 1 || payload[i + 1] > 2) { rc = 4; goto done; }
|
|
i += 2;
|
|
} else if (tag == 0x02 || tag == 0x03) { // USER_NAME / EMAIL
|
|
unsigned char len = payload[i + 1];
|
|
if (tag == 0x02 && user_out && user_cap) {
|
|
size_t k = len < user_cap - 1 ? len : user_cap - 1;
|
|
memcpy(user_out, payload + i + 2, k);
|
|
user_out[k] = 0;
|
|
}
|
|
i += 2 + len;
|
|
} else if (tag == 0x05 || tag == 0x09) { // EXP_DATE / MAX_BUILD
|
|
unsigned int date = rd32(payload + i + 1);
|
|
if (tag == 0x05 && date) {
|
|
time_t t = time(NULL);
|
|
struct tm tmv;
|
|
localtime_r(&t, &tmv);
|
|
unsigned int now = ((unsigned int)(tmv.tm_year + 1900) << 16) |
|
|
((unsigned int)(tmv.tm_mon + 1) << 8) | (unsigned int)tmv.tm_mday;
|
|
if (now > date && expired) *expired = 1;
|
|
}
|
|
i += 5;
|
|
} else if (tag == 0x06) { // RUNNING_TIME_LIMIT
|
|
i += 2;
|
|
} else if (tag == 0x04 || tag == 0x08) { // HWID / USER_DATA
|
|
unsigned char len = payload[i + 1];
|
|
i += 2 + len;
|
|
} else if (tag == 0x07) { // PRODUCT_CODE
|
|
i += 9;
|
|
} else if (tag == 0xFF) { // END + 校验和
|
|
end_tag = i;
|
|
seen_end = 1;
|
|
break;
|
|
} else {
|
|
rc = 5;
|
|
goto done;
|
|
}
|
|
}
|
|
if (!seen_end) { rc = 6; goto done; }
|
|
|
|
// ---- 校验和:sha1(payload 前 end_tag 字节) 的前 4 字节倒序 ----
|
|
{
|
|
unsigned char hash[20];
|
|
SHA1(payload, end_tag, hash);
|
|
if (payload[end_tag + 1] != hash[3] || payload[end_tag + 2] != hash[2] ||
|
|
payload[end_tag + 3] != hash[1] || payload[end_tag + 4] != hash[0]) {
|
|
rc = 7;
|
|
goto done;
|
|
}
|
|
}
|
|
rc = 0;
|
|
|
|
done:
|
|
free(block);
|
|
BN_free(n); BN_free(e); BN_free(s); BN_free(m);
|
|
BN_CTX_free(ctx);
|
|
(void)kBuildDate;
|
|
return rc;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
DogProtectBegin("verify");
|
|
|
|
if (argc < 2) {
|
|
printf("usage: %s <serial>\n", argv[0]);
|
|
DogProtectEnd();
|
|
return 1;
|
|
}
|
|
|
|
char user[64];
|
|
int expired = 0;
|
|
int rc = verify_serial(argv[1], user, sizeof(user), &expired);
|
|
|
|
printf("verify rc = %d\n", rc);
|
|
if (rc == 0) {
|
|
printf("LICENSE OK user='%s'%s\n", user, expired ? " [已过期]" : "");
|
|
} else {
|
|
printf("LICENSE BAD\n");
|
|
}
|
|
|
|
DogProtectEnd();
|
|
return rc == 0 ? 0 : 1;
|
|
}
|