diff --git a/.gitignore b/.gitignore index a85b27a..f7232a2 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,6 @@ temp/ # Trae .trae/ + +# 壳源码的本地构建产物 +shell/**/out/ diff --git a/backend/internal/protect/assets/shell32.bin b/backend/internal/protect/assets/shell32.bin index 225164a..31408ea 100644 Binary files a/backend/internal/protect/assets/shell32.bin and b/backend/internal/protect/assets/shell32.bin differ diff --git a/backend/internal/protect/assets/shell64.bin b/backend/internal/protect/assets/shell64.bin index 62e6d43..45c05b0 100644 Binary files a/backend/internal/protect/assets/shell64.bin and b/backend/internal/protect/assets/shell64.bin differ diff --git a/shell/README.md b/shell/README.md new file mode 100644 index 0000000..b9741ef --- /dev/null +++ b/shell/README.md @@ -0,0 +1,92 @@ +# 验证壳源码(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 窗口不映射、截图全是黑屏。 diff --git a/shell/boot/blob.ld b/shell/boot/blob.ld new file mode 100644 index 0000000..587bbff --- /dev/null +++ b/shell/boot/blob.ld @@ -0,0 +1,14 @@ +SECTIONS +{ + .blob : + { + *(.blobhead) + *(.text) + *(.text.*) + *(.rdata) + *(.rdata.*) + *(.data) + *(.data.*) + } + /DISCARD/ : { *(.pdata) *(.xdata) *(.reloc) *(.bss) *(.comment) *(.eh_frame) } +} diff --git a/shell/boot/bootstrap.c b/shell/boot/bootstrap.c new file mode 100644 index 0000000..319e536 --- /dev/null +++ b/shell/boot/bootstrap.c @@ -0,0 +1,203 @@ +/* vfy 引导 stub(x64)—— 裸代码,无 CRT、无导入表 + * 解析自身 PE → 找 .vfy → PEB 拿 kernel32 → 解析导出 → 释放并加载 shell DLL + * → VerifyMain(oep, cfg) → 退出 + * 头布局 40 字节(.vfy 节起始): magic"VFY3"|ver|dllOff|dllSize|cfgOff|cfgSize|oepRva|blobSize|flags|rsv + */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; +typedef void *HMODULE; +typedef void *HANDLE; +typedef int BOOL; + +typedef HMODULE (*fnLoadLibraryA)(const char *); +typedef void *(*fnGetProcAddress)(HMODULE, const char *); +typedef u32 (*fnGetTempPathA)(u32, char *); +typedef HANDLE (*fnCreateFileA)(const char *, u32, u32, void *, u32, u32, HANDLE); +typedef BOOL (*fnWriteFile)(HANDLE, const void *, u32, u32 *, void *); +typedef BOOL (*fnCloseHandle)(HANDLE); +typedef u32 (*fnGetCurrentProcessId)(void); +typedef void (*fnExitProcess)(u32); + +typedef struct { + char magic[4]; + u32 ver; + u32 dllOff, dllSize; + u32 cfgOff, cfgSize; + u32 oepRva; + u32 blobSize; + u32 flags; + u32 reserved; +} VFYHDR; + +/* UTF-16 模块名 vs ASCII 常量比较(不区分大小写) */ +static int uname_eq(const u16 *w, const char *a, int n) { + for (int i = 0; i < n; i++) { + u16 c = w[i]; + if (c >= 'A' && c <= 'Z') c = (u16)(c + 32); + if (c != (u8)a[i]) return 0; + } + return 1; +} + +__attribute__((section(".blobhead"), used)) int blob_main(void) { + u64 peb = 0; + __asm__ volatile("movq %%gs:0x60, %0" : "=r"(peb)); + if (!peb) return 1; + u64 imageBase = *(u64 *)(peb + 0x10); + if (!imageBase) return 1; + + u8 *img = (u8 *)imageBase; + u32 lfanew = *(u32 *)(img + 0x3C); + u8 *nt = img + lfanew; + u16 numSec = *(u16 *)(nt + 6); + u16 optSize = *(u16 *)(nt + 20); + u8 *sec = nt + 24 + optSize; + VFYHDR *hdr = 0; + for (u16 i = 0; i < numSec; i++) { + u8 *s = sec + i * 40; + if (s[0] == '.' && s[1] == 'v' && s[2] == 'f' && s[3] == 'y') { + hdr = (VFYHDR *)(img + *(u32 *)(s + 12)); + break; + } + } + if (!hdr) return 2; + if (!(hdr->magic[0] == 'V' && hdr->magic[1] == 'F' && hdr->magic[2] == 'Y' && hdr->magic[3] == '3')) return 2; + + u8 *secBase = (u8 *)hdr; + u8 *dllBytes = secBase + hdr->dllOff; + const char *cfg = (const char *)(secBase + hdr->cfgOff); + + /* kernel32 from PEB->Ldr->InMemoryOrderModuleList */ + u64 ldr = *(u64 *)(peb + 0x18); + u64 head = ldr + 0x20; /* InMemoryOrderModuleList */ + u64 cur = *(u64 *)(head); + u64 kern = 0; + for (int guard = 0; guard < 64 && cur && cur != head; guard++) { + u8 *entry = (u8 *)(cur - 0x10); + u64 dllBase = *(u64 *)(entry + 0x30); + u16 nameLen = *(u16 *)(entry + 0x58); + u64 nameBuf = *(u64 *)(entry + 0x60); + if (nameLen == 24 && nameBuf && uname_eq((const u16 *)nameBuf, "kernel32.dll", 12)) { + kern = dllBase; + break; + } + cur = *(u64 *)(cur); + } + if (!kern) return 3; + + /* kernel32 export table */ + u8 *kb = (u8 *)kern; + u32 kl = *(u32 *)(kb + 0x3C); + u8 *knt = kb + kl; + u32 expRVA = *(u32 *)(knt + 24 + 112); + if (!expRVA) return 4; + u8 *exp = kb + expRVA; + u32 numNames = *(u32 *)(exp + 24); + u32 *names = (u32 *)(kb + *(u32 *)(exp + 32)); + u16 *ords = (u16 *)(kb + *(u32 *)(exp + 36)); + u32 *funcs = (u32 *)(kb + *(u32 *)(exp + 28)); + + fnGetProcAddress pGPA = 0; + fnLoadLibraryA pLLA = 0; + static const char nGPA[] = "GetProcAddress"; + static const char nLLA[] = "LoadLibraryA"; + for (u32 i = 0; i < numNames; i++) { + const char *nm = (const char *)(kb + names[i]); + int ok1 = 1, ok2 = 1; + for (int k = 0; k < 14; k++) { + if (nm[k] != nGPA[k]) { ok1 = 0; break; } + if (!nGPA[k]) break; + } + for (int k = 0; k < 12; k++) { + if (nm[k] != nLLA[k]) { ok2 = 0; break; } + if (!nLLA[k]) break; + } + if (ok1) pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]); + if (ok2) pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]); + if (pGPA && pLLA) break; + } + if (!pGPA || !pLLA) return 4; + + static const char nTemp[] = "GetTempPathA"; + static const char nCreate[] = "CreateFileA"; + static const char nWrite[] = "WriteFile"; + static const char nClose[] = "CloseHandle"; + static const char nPid[] = "GetCurrentProcessId"; + static const char nExit[] = "ExitProcess"; + fnGetTempPathA pTemp = (fnGetTempPathA)pGPA((HMODULE)kern, nTemp); + fnCreateFileA pCreate = (fnCreateFileA)pGPA((HMODULE)kern, nCreate); + fnWriteFile pWrite = (fnWriteFile)pGPA((HMODULE)kern, nWrite); + fnCloseHandle pClose = (fnCloseHandle)pGPA((HMODULE)kern, nClose); + fnGetCurrentProcessId pPid = (fnGetCurrentProcessId)pGPA((HMODULE)kern, nPid); + fnExitProcess pExit = (fnExitProcess)pGPA((HMODULE)kern, nExit); + if (!pTemp || !pCreate || !pWrite || !pClose || !pPid || !pExit) return 5; + + /* 阶段打点:写到 %TEMP%\vfy_boot.txt(覆盖) */ + char tdir[260]; + u32 tl = pTemp(260, tdir); + char dbg[300]; + int dp = 0; + if (tl && tl < 250) { + for (u32 i = 0; i < tl; i++) dbg[dp++] = tdir[i]; + static const char dn[] = "vfy_boot.txt"; + for (int i = 0; dn[i]; i++) dbg[dp++] = dn[i]; + dbg[dp] = 0; + } else dp = 0; + + /* 释放 DLL */ + char path[300]; + int p = 0; + for (u32 i = 0; i < tl; i++) path[p++] = tdir[i]; + static const char pre[] = "vfycore_"; + for (int i = 0; pre[i]; i++) path[p++] = pre[i]; + u32 pid = pPid(); + char digits[12]; + int dn2 = 0; + if (!pid) digits[dn2++] = '0'; + while (pid && dn2 < 11) { digits[dn2++] = (char)('0' + pid % 10); pid /= 10; } + for (int i = dn2 - 1; i >= 0; i--) path[p++] = digits[i]; + static const char ext[] = ".dll"; + for (int i = 0; ext[i]; i++) path[p++] = ext[i]; + path[p] = 0; + + HANDLE hf = pCreate(path, 0x40000000u, 0, 0, 2, 0x80, 0); + if (hf == (HANDLE)-1 || !hf) return 7; + u32 wrote = 0; + if (!pWrite(hf, dllBytes, hdr->dllSize, &wrote, 0) || wrote != hdr->dllSize) { + pClose(hf); + return 7; + } + pClose(hf); + + /* 加载并调用 */ + HMODULE mod = pLLA(path); + static const char nMain[] = "VerifyMain"; + typedef int (*PFN)(void *, const char *); + PFN fn = 0; + if (mod) fn = (PFN)pGPA(mod, nMain); + + if (dp) { + HANDLE hd = pCreate(dbg, 0x40000000u, 0, 0, 2, 0x80, 0); + if (hd && hd != (HANDLE)-1) { + char line[8]; + line[0] = mod ? 'M' : 'm'; + line[1] = fn ? 'F' : 'f'; + line[2] = '\n'; + u32 w2 = 0; + pWrite(hd, line, 3, &w2, 0); + pClose(hd); + } + } + if (!mod) return 8; + if (!fn) return 9; + + void *oep = (void *)(imageBase + hdr->oepRva); + int rc = fn(oep, cfg); + if (rc != 0) { + if (hdr->flags & 1) ((void (*)(void))oep)(); + pExit(0); + } + return 0; +} diff --git a/shell/boot/bootstrap2.c b/shell/boot/bootstrap2.c new file mode 100644 index 0000000..534d9e3 --- /dev/null +++ b/shell/boot/bootstrap2.c @@ -0,0 +1,257 @@ +/* vfy 引导 stub(x64)v2 —— 反射式加载:壳 DLL 从 .vfy 节直接映射进内存,不落盘 + * + * 流程:解析自身 PE → 找 .vfy → 校验 CRC32 → 手动映射内嵌 DLL(头/节/重定位/导入) + * → 调 DllMain(ATTACH) → 找导出 VerifyMain → 调用(oep, cfg) → 退出 + * + * 头布局 40 字节(.vfy 节起始): + * 0..3 magic "VFY3" | 4..7 ver + * 8..11 dllOff | 12..15 dllSize + * 16..19 cfgOff | 20..23 cfgSize + * 24..27 oepRva | 28..31 blobSize + * 32..35 flags | 36..39 dllCrc32 + */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; +typedef void *HMODULE; +typedef void *HANDLE; +typedef int BOOL; + +typedef HMODULE (*fnLoadLibraryA)(const char *); +typedef void *(*fnGetProcAddress)(HMODULE, const char *); +typedef void *(*fnVirtualAlloc)(void *, u64, u32, u32); +typedef BOOL (*fnVirtualProtect)(void *, u64, u32, u32 *); +typedef void (*fnExitProcess)(u32); + +typedef struct { + char magic[4]; + u32 ver; + u32 dllOff, dllSize; + u32 cfgOff, cfgSize; + u32 oepRva; + u32 blobSize; + u32 flags; + u32 dllCrc32; +} VFYHDR; + + +/* ---------- 小工具 ---------- */ +static int mem_eq(const u8 *a, const char *b) { + for (int i = 0; b[i]; i++) if (a[i] != (u8)b[i]) return 0; + return 1; +} +static void mem_cpy(u8 *d, const u8 *s, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = s[i]; +} +static void mem_zero(u8 *d, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = 0; +} +static u32 crc32_of(const u8 *p, u32 n) { + u32 c = 0xFFFFFFFFu; + for (u32 i = 0; i < n; i++) { + c ^= p[i]; + for (int k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + } + return c ^ 0xFFFFFFFFu; +} + +__attribute__((section(".blobhead"), used)) int blob_main(void) { + u64 peb = 0; + __asm__ volatile("movq %%gs:0x60, %0" : "=r"(peb)); + if (!peb) return 1; + u64 imageBase = *(u64 *)(peb + 0x10); + if (!imageBase) return 1; + + /* 自身 PE → .vfy */ + u8 *img = (u8 *)imageBase; + u32 lfanew = *(u32 *)(img + 0x3C); + u8 *nt = img + lfanew; + u16 numSec = *(u16 *)(nt + 6); + u16 optSize = *(u16 *)(nt + 20); + u8 *sec = nt + 24 + optSize; + VFYHDR *hdr = 0; + for (u16 i = 0; i < numSec; i++) { + u8 *s = sec + i * 40; + if (s[0] == '.' && s[1] == 'v' && s[2] == 'f' && s[3] == 'y') { + hdr = (VFYHDR *)(img + *(u32 *)(s + 12)); + break; + } + } + if (!hdr) return 2; + if (!(hdr->magic[0] == 'V' && hdr->magic[1] == 'F' && hdr->magic[2] == 'Y' && hdr->magic[3] == '3')) return 2; + + u8 *secBase = (u8 *)hdr; + u8 *dllSrc = secBase + hdr->dllOff; + const char *cfg = (const char *)(secBase + hdr->cfgOff); + + /* kernel32 */ + u64 ldr = *(u64 *)(peb + 0x18); + u64 head = ldr + 0x20; + u64 cur = *(u64 *)(head); + u64 kern = 0; + for (int guard = 0; guard < 64 && cur && cur != head; guard++) { + u8 *entry = (u8 *)(cur - 0x10); + u64 dllBase = *(u64 *)(entry + 0x30); + u16 nameLen = *(u16 *)(entry + 0x58); + u64 nameBuf = *(u64 *)(entry + 0x60); + if (nameLen == 24 && nameBuf) { + const u16 *w = (const u16 *)nameBuf; + int ok = 1; + for (int k = 0; k < 12; k++) { + u16 c = w[k]; + if (c >= 'A' && c <= 'Z') c = (u16)(c + 32); + if (c != (u16)"kernel32.dll"[k]) { ok = 0; break; } + } + if (ok) { kern = dllBase; break; } + } + cur = *(u64 *)(cur); + } + if (!kern) return 3; + + u8 *kb = (u8 *)kern; + u32 kl = *(u32 *)(kb + 0x3C); + u8 *knt = kb + kl; + u32 expRVA = *(u32 *)(knt + 24 + 112); + if (!expRVA) return 4; + u8 *exp = kb + expRVA; + u32 numNames = *(u32 *)(exp + 24); + u32 *names = (u32 *)(kb + *(u32 *)(exp + 32)); + u16 *ords = (u16 *)(kb + *(u32 *)(exp + 36)); + u32 *funcs = (u32 *)(kb + *(u32 *)(exp + 28)); + + fnGetProcAddress pGPA = 0; + fnLoadLibraryA pLLA = 0; + for (u32 i = 0; i < numNames; i++) { + const u8 *nm = kb + names[i]; + if (mem_eq(nm, "GetProcAddress")) pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]); + if (mem_eq(nm, "LoadLibraryA")) pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]); + if (pGPA && pLLA) break; + } + if (!pGPA || !pLLA) return 4; + + fnVirtualAlloc pAlloc = (fnVirtualAlloc)pGPA((HMODULE)kern, "VirtualAlloc"); + fnVirtualProtect pProt = (fnVirtualProtect)pGPA((HMODULE)kern, "VirtualProtect"); + fnExitProcess pExit = (fnExitProcess)pGPA((HMODULE)kern, "ExitProcess"); + if (!pAlloc || !pExit) return 5; + + /* 完整性:.vfy 里内嵌 DLL 的 CRC32 */ + if (hdr->dllCrc32) { + u32 got = crc32_of(dllSrc, hdr->dllSize); + if (got != hdr->dllCrc32) return 6; + } + + /* ---- 手动映射 DLL ---- */ + u8 *src = dllSrc; + u32 dl = *(u32 *)(src + 0x3C); + u8 *dnt = src + dl; + u16 dsecs = *(u16 *)(dnt + 6); + u16 doptSize = *(u16 *)(dnt + 20); + u8 *dopt = dnt + 24; + u64 prefBase = *(u64 *)(dopt + 24); + u32 sizeOfImage = *(u32 *)(dopt + 56); + u32 sizeOfHeaders = *(u32 *)(dopt + 60); + u32 entryRVA = *(u32 *)(dopt + 16); + u32 importRVA = *(u32 *)(dopt + 112 + 8); + u32 relocRVA = *(u32 *)(dopt + 112 + 40); + u32 relocSize = *(u32 *)(dopt + 112 + 44); + u8 *dsec = dopt + doptSize; + + u8 *base = (u8 *)pAlloc((void *)0, sizeOfImage, 0x3000 /*COMMIT|RESERVE*/, 0x40 /*RWX*/); + if (!base) return 7; + + /* 头 + 节 */ + mem_cpy(base, src, sizeOfHeaders); + for (u16 i = 0; i < dsecs; i++) { + u8 *s = dsec + i * 40; + u32 va = *(u32 *)(s + 12); + u32 rawSize = *(u32 *)(s + 16); + u32 rawPtr = *(u32 *)(s + 20); + if (rawSize && rawPtr) mem_cpy(base + va, src + rawPtr, rawSize); + } + + /* 重定位 */ + if (relocRVA && relocSize && (u64)base != prefBase) { + u64 delta = (u64)base - prefBase; + u8 *r = base + relocRVA; + u8 *rend = r + relocSize; + while (r + 8 <= rend) { + u32 pageRVA = *(u32 *)r; + u32 blockSize = *(u32 *)(r + 4); + if (!blockSize) break; + u16 *ent = (u16 *)(r + 8); + int cnt = (int)(blockSize - 8) / 2; + for (int i = 0; i < cnt; i++) { + u16 e = ent[i]; + u16 type = (u16)(e >> 12); + u16 off = (u16)(e & 0x0FFF); + if (type == 10) { /* DIR64 */ + u64 *p = (u64 *)(base + pageRVA + off); + *p += delta; + } else if (type == 0) { + /* pad */ + } + } + r += blockSize; + } + } + + /* 导入 */ + if (importRVA) { + u8 *desc = base + importRVA; + for (;;) { + u32 oft = *(u32 *)desc; + u32 nameRVA = *(u32 *)(desc + 12); + u32 ft = *(u32 *)(desc + 16); + if (!nameRVA && !ft && !oft) break; + const char *dllName = (const char *)(base + nameRVA); + HMODULE hm = pLLA(dllName); + if (!hm) return 8; + u64 *thunk = (u64 *)(base + (oft ? oft : ft)); + u64 *iat = (u64 *)(base + ft); + for (int k = 0; thunk[k]; k++) { + u64 t = thunk[k]; + if (t & 0x8000000000000000ull) { + iat[k] = (u64)pGPA(hm, (const char *)(t & 0xFFFF)); + } else { + const char *fnName = (const char *)(base + (u32)t + 2); /* 跳过 Hint */ + iat[k] = (u64)pGPA(hm, fnName); + } + if (!iat[k]) return 9; + } + desc += 20; + } + } + + /* DllMain(DLL_PROCESS_ATTACH) */ + typedef BOOL (*fnDllMain)(HMODULE, u32, void *); + if (entryRVA) { + fnDllMain dm = (fnDllMain)(base + entryRVA); + dm((HMODULE)base, 1, 0); + } + + /* 导出 VerifyMain(在映射镜像里查导出表) */ + u32 dexpRVA = *(u32 *)(dopt + 112); + if (!dexpRVA) return 10; + u8 *dexp = base + dexpRVA; + u32 dnumNames = *(u32 *)(dexp + 24); + u32 *dnames = (u32 *)(base + *(u32 *)(dexp + 32)); + u16 *dords = (u16 *)(base + *(u32 *)(dexp + 36)); + u32 *dfuncs = (u32 *)(base + *(u32 *)(dexp + 28)); + u32 vmRVA = 0; + for (u32 i = 0; i < dnumNames; i++) { + const u8 *nm = base + dnames[i]; + if (mem_eq(nm, "VerifyMain")) { vmRVA = dfuncs[dords[i]]; break; } + } + if (!vmRVA) return 11; + + typedef int (*PFN)(void *, const char *); + PFN fn = (PFN)(base + vmRVA); + void *oep = (void *)(imageBase + hdr->oepRva); + int rc = fn(oep, cfg); + if (rc != 0) { + if (hdr->flags & 1) ((void (*)(void))oep)(); + pExit(0); + } + return 0; +} diff --git a/shell/boot/bootstrap4.c b/shell/boot/bootstrap4.c new file mode 100644 index 0000000..916e566 --- /dev/null +++ b/shell/boot/bootstrap4.c @@ -0,0 +1,374 @@ +/* vfy 引导 stub v4 —— x64 / x86 通用,反射加载内嵌 DLL,全程零落盘 + * + * 头布局 48 字节(.vfy 节起始): + * 0..3 magic "VFY4" 4..7 ver + * 8..11 dllOff 12..15 dllSize + * 16..19 cfgOff 20..23 cfgSize + * 24..27 oepRva 28..31 blobSize + * 32..35 flags 36..39 crcVfy (.vfy 内容,跳过 36..44) + * 40..43 crcFile 44..47 reserved + */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +#if defined(_WIN64) +typedef unsigned long long u64; +typedef unsigned long long uptr; +#else +typedef unsigned int uptr; +#endif +typedef void *HMODULE; +typedef void *HANDLE; +typedef int BOOL; + +/* x86 上 Win32 API 是 __stdcall,指针类型必须带上,否则栈被吃偏 */ +#if defined(_WIN64) +#define API +#else +#define API __stdcall +#endif + +typedef HMODULE(API *fnLoadLibraryA)(const char *); +typedef void *(API *fnGetProcAddress)(HMODULE, const char *); +typedef void *(API *fnVirtualAlloc)(void *, uptr, u32, u32); +typedef BOOL(API *fnVirtualFree)(void *, uptr, u32); +typedef void(API *fnExitProcess)(u32); +typedef u32(API *fnGetModuleFileNameA)(HMODULE, char *, u32); +typedef HANDLE(API *fnCreateFileA)(const char *, u32, u32, void *, u32, u32, HANDLE); +typedef BOOL(API *fnReadFile)(HANDLE, void *, u32, u32 *, void *); +typedef u32(API *fnGetFileSize)(HANDLE, u32 *); +typedef BOOL(API *fnCloseHandle)(HANDLE); +typedef u32(API *fnGetTempPathA)(u32, char *); +typedef BOOL(API *fnWriteFile)(HANDLE, const void *, u32, u32 *, void *); + +typedef struct { + char magic[4]; + u32 ver; + u32 dllOff, dllSize; + u32 cfgOff, cfgSize; + u32 oepRva; + u32 blobSize; + u32 flags; + u32 crcVfy; + u32 crcFile; + u32 reserved; +} VFYHDR; + +#define HDRSZ 48 +#define CRCOFF 36 /* 跳过 8 字节的校验字段 */ + +static uptr rd(const u8 *p) { +#if defined(_WIN64) + return *(const u64 *)p; +#else + return *(const u32 *)p; +#endif +} +static int mem_eq(const u8 *a, const char *b) { + for (int i = 0; b[i]; i++) if (a[i] != (u8)b[i]) return 0; + return 1; +} +static void mem_cpy(u8 *d, const u8 *s, uptr n) { + for (uptr i = 0; i < n; i++) d[i] = s[i]; +} +static u32 crc_feed(u32 c, const u8 *p, u32 n) { + for (u32 i = 0; i < n; i++) { + c ^= p[i]; + for (int k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + } + return c; +} + +__attribute__((section(".blobhead"), used)) int blob_main(void) { + uptr peb = 0; +#if defined(_WIN64) + __asm__ volatile("movq %%gs:0x60, %0" : "=r"(peb)); +#else + __asm__ volatile("movl %%fs:0x30, %0" : "=r"(peb)); +#endif + if (!peb) return 1; +#if defined(_WIN64) + uptr imageBase = rd((const u8 *)peb + 0x10); +#else + uptr imageBase = rd((const u8 *)peb + 0x08); +#endif + if (!imageBase) return 1; + + u8 *img = (u8 *)imageBase; + u32 lfanew = *(u32 *)(img + 0x3C); + u8 *nt = img + lfanew; + u16 numSec = *(u16 *)(nt + 6); + u16 optSize = *(u16 *)(nt + 20); + u8 *sec = nt + 24 + optSize; + VFYHDR *hdr = 0; + u8 *secHdr = 0; + for (u16 i = 0; i < numSec; i++) { + u8 *s = sec + i * 40; + if (s[0] == '.' && s[1] == 'v' && s[2] == 'f' && s[3] == 'y') { + secHdr = s; + hdr = (VFYHDR *)(img + *(u32 *)(s + 12)); + break; + } + } + if (!hdr) return 2; + if (!(hdr->magic[0] == 'V' && hdr->magic[1] == 'F' && hdr->magic[2] == 'Y' && hdr->magic[3] == '4')) return 2; + + u8 *secBase = (u8 *)hdr; + u32 secVSize = *(u32 *)(secHdr + 8); + u32 secRawPtr = *(u32 *)(secHdr + 20); + u8 *dllSrc = secBase + hdr->dllOff; + const char *cfg = (const char *)(secBase + hdr->cfgOff); + + /* ---- kernel32 ---- */ + uptr ldr = 0; +#if defined(_WIN64) + ldr = rd((const u8 *)peb + 0x18); +#else + ldr = rd((const u8 *)peb + 0x0C); +#endif +#if defined(_WIN64) + uptr head = ldr + 0x20, entryOff = 0x10, dllBaseOff = 0x30, nameLenOff = 0x58, nameBufOff = 0x60; +#else + uptr head = ldr + 0x14, entryOff = 0x08, dllBaseOff = 0x18, nameLenOff = 0x2C, nameBufOff = 0x30; +#endif + uptr cur = rd((const u8 *)head); + uptr kern = 0; + for (int guard = 0; guard < 64 && cur && cur != head; guard++) { + u8 *entry = (u8 *)(cur - entryOff); + uptr dllBase = rd(entry + dllBaseOff); + u16 nameLen = *(u16 *)(entry + nameLenOff); + uptr nameBuf = rd(entry + nameBufOff); + if (nameLen == 24 && nameBuf) { + const u16 *w = (const u16 *)nameBuf; + int ok = 1; + for (int k = 0; k < 12; k++) { + u16 c = w[k]; + if (c >= 'A' && c <= 'Z') c = (u16)(c + 32); + if (c != (u16)"kernel32.dll"[k]) { ok = 0; break; } + } + if (ok) { kern = dllBase; break; } + } + cur = rd((const u8 *)cur); + } + if (!kern) return 3; + + u8 *kb = (u8 *)kern; + u32 kl = *(u32 *)(kb + 0x3C); + u8 *knt = kb + kl; + u32 ddOff = 112; +#if !defined(_WIN64) + ddOff = 96; +#endif + u32 expRVA = *(u32 *)(knt + 24 + ddOff); + if (!expRVA) return 4; + u8 *exp = kb + expRVA; + u32 numNames = *(u32 *)(exp + 24); + u32 *names = (u32 *)(kb + *(u32 *)(exp + 32)); + u16 *ords = (u16 *)(kb + *(u32 *)(exp + 36)); + u32 *funcs = (u32 *)(kb + *(u32 *)(exp + 28)); + + fnGetProcAddress pGPA = 0; + fnLoadLibraryA pLLA = 0; + for (u32 i = 0; i < numNames; i++) { + const u8 *nm = kb + names[i]; + if (mem_eq(nm, "GetProcAddress")) pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]); + if (mem_eq(nm, "LoadLibraryA")) pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]); + if (pGPA && pLLA) break; + } + if (!pGPA || !pLLA) return 4; + + fnVirtualAlloc pAlloc = (fnVirtualAlloc)pGPA((HMODULE)kern, "VirtualAlloc"); + fnExitProcess pExit = (fnExitProcess)pGPA((HMODULE)kern, "ExitProcess"); + fnGetModuleFileNameA pModName = (fnGetModuleFileNameA)pGPA((HMODULE)kern, "GetModuleFileNameA"); + fnCreateFileA pCreate = (fnCreateFileA)pGPA((HMODULE)kern, "CreateFileA"); + fnReadFile pRead = (fnReadFile)pGPA((HMODULE)kern, "ReadFile"); + fnGetFileSize pSize = (fnGetFileSize)pGPA((HMODULE)kern, "GetFileSize"); + fnCloseHandle pClose = (fnCloseHandle)pGPA((HMODULE)kern, "CloseHandle"); + fnGetTempPathA pTemp = (fnGetTempPathA)pGPA((HMODULE)kern, "GetTempPathA"); + fnWriteFile pWrite = (fnWriteFile)pGPA((HMODULE)kern, "WriteFile"); + if (!pAlloc || !pExit || !pModName || !pCreate || !pRead || !pSize || !pClose) return 5; +#define LOG(ch) do { \ + if (pTemp && pCreate && pWrite && pClose) { \ + char lp[300]; u32 tl_ = pTemp(300, lp); \ + if (tl_ && tl_ < 280) { \ + const char *nm_ = "vfy_b4.txt"; \ + int i_ = 0; while (nm_[i_]) { lp[tl_ + i_] = nm_[i_]; i_++; } \ + lp[tl_ + i_] = 0; \ + HANDLE h_ = pCreate(lp, 0x40000000u, 0, 0, 2, 0x80, 0); \ + if (h_ && h_ != (HANDLE)-1) { char cc_ = (char)(ch); u32 w_ = 0; pWrite(h_, &cc_, 1, &w_, 0); pClose(h_); } \ + } \ + } \ +} while (0) + + /* ---- 完整性 1:.vfy 内容(跳过 36..44)---- */ + if (hdr->crcVfy) { + u32 c = 0xFFFFFFFFu; + c = crc_feed(c, secBase, CRCOFF); + c = crc_feed(c, secBase + CRCOFF + 8, secVSize - CRCOFF - 8); + c ^= 0xFFFFFFFFu; + if (c != hdr->crcVfy) { LOG('1'); return 6; } + LOG('2'); + } + + /* ---- 完整性 2:整个文件(跳过同一段 8 字节)---- */ + if (hdr->crcFile) { + char path[512]; + u32 n = pModName(0, path, 512); + if (!n) return 12; + HANDLE hf = pCreate(path, 0x80000000u, 1, 0, 3, 0x80, 0); + if (hf == (HANDLE)-1 || !hf) return 12; + u32 fsize = pSize(hf, 0); + u8 *buf = (u8 *)pAlloc(0, fsize, 0x3000, 0x04); + if (!buf) { pClose(hf); return 12; } + u32 got = 0; + if (!pRead(hf, buf, fsize, &got, 0) || got != fsize) { pClose(hf); return 12; } + pClose(hf); + u32 skipAt = secRawPtr + CRCOFF; + u32 c = 0xFFFFFFFFu; + if (skipAt > fsize) return 12; + c = crc_feed(c, buf, skipAt); + c = crc_feed(c, buf + skipAt + 8, fsize - skipAt - 8); + c ^= 0xFFFFFFFFu; + if (c != hdr->crcFile) { LOG('3'); return 13; } + LOG('4'); + } + + /* ---- 反射加载 DLL ---- */ + u8 *src = dllSrc; + u32 dl = *(u32 *)(src + 0x3C); + u8 *dnt = src + dl; + u16 dsecs = *(u16 *)(dnt + 6); + u16 doptSize = *(u16 *)(dnt + 20); + u8 *dopt = dnt + 24; + uptr prefBase = 0; + u32 sizeOfImage = 0, sizeOfHeaders = 0, entryRVA = 0, importRVA = 0, relocRVA = 0, relocSize = 0; +#if defined(_WIN64) + prefBase = *(u64 *)(dopt + 24); + sizeOfImage = *(u32 *)(dopt + 56); + sizeOfHeaders = *(u32 *)(dopt + 60); + entryRVA = *(u32 *)(dopt + 16); + importRVA = *(u32 *)(dopt + 112 + 8); + relocRVA = *(u32 *)(dopt + 112 + 40); + relocSize = *(u32 *)(dopt + 112 + 44); +#else + prefBase = *(u32 *)(dopt + 28); + sizeOfImage = *(u32 *)(dopt + 56); + sizeOfHeaders = *(u32 *)(dopt + 60); + entryRVA = *(u32 *)(dopt + 16); + importRVA = *(u32 *)(dopt + 96 + 8); + relocRVA = *(u32 *)(dopt + 96 + 40); + relocSize = *(u32 *)(dopt + 96 + 44); +#endif + u8 *dsec = dopt + doptSize; + + LOG('5'); + u8 *base = (u8 *)pAlloc(0, sizeOfImage, 0x3000, 0x40); + if (!base) { LOG('6'); return 7; } + LOG('7'); + mem_cpy(base, src, sizeOfHeaders); + for (u16 i = 0; i < dsecs; i++) { + u8 *s = dsec + i * 40; + u32 va = *(u32 *)(s + 12); + u32 rawSize = *(u32 *)(s + 16); + u32 rawPtr = *(u32 *)(s + 20); + if (rawSize && rawPtr) mem_cpy(base + va, src + rawPtr, rawSize); + } + + LOG('8'); + if (relocRVA && relocSize && (uptr)base != prefBase) { + uptr delta = (uptr)base - prefBase; + u8 *r = base + relocRVA; + u8 *rend = r + relocSize; + while (r + 8 <= rend) { + u32 pageRVA = *(u32 *)r; + u32 blockSize = *(u32 *)(r + 4); + if (!blockSize) break; + u16 *ent = (u16 *)(r + 8); + int cnt = (int)(blockSize - 8) / 2; + for (int i = 0; i < cnt; i++) { + u16 e = ent[i]; + u16 type = (u16)(e >> 12); + u16 off = (u16)(e & 0x0FFF); +#if defined(_WIN64) + if (type == 10) { + u64 *p = (u64 *)(base + pageRVA + off); + *p += delta; + } +#else + if (type == 3) { + u32 *p = (u32 *)(base + pageRVA + off); + *p += (u32)delta; + } +#endif + } + r += blockSize; + } + } + + LOG('9'); + if (importRVA) { + u8 *desc = base + importRVA; + for (;;) { + u32 oft = *(u32 *)desc; + u32 nameRVA = *(u32 *)(desc + 12); + u32 ft = *(u32 *)(desc + 16); + if (!nameRVA && !ft && !oft) break; + const char *dllName = (const char *)(base + nameRVA); + HMODULE hm = pLLA(dllName); + if (!hm) { LOG('A'); return 8; } + u8 *thunk = base + (oft ? oft : ft); + u8 *iat = base + ft; + for (u32 k = 0;; k++) { + uptr t = rd(thunk + k * (u32)sizeof(uptr)); + if (!t) break; + void *fn = 0; + if (t & ((uptr)1 << (sizeof(uptr) * 8 - 1))) { + fn = pGPA(hm, (const char *)(t & 0xFFFF)); + } else { + fn = pGPA(hm, (const char *)(base + (u32)t + 2)); + } + if (!fn) { LOG('B'); return 9; } + *(uptr *)(iat + k * sizeof(uptr)) = (uptr)fn; + } + desc += 20; + } + } + + LOG('C'); + typedef BOOL(API *fnDllMain)(HMODULE, u32, void *); + if (entryRVA) { + fnDllMain dm = (fnDllMain)(base + entryRVA); + dm((HMODULE)base, 1, 0); + } + + u32 dexpRVA = 0; +#if defined(_WIN64) + dexpRVA = *(u32 *)(dopt + 112); +#else + dexpRVA = *(u32 *)(dopt + 96); +#endif + LOG('D'); + if (!dexpRVA) return 10; + u8 *dexp = base + dexpRVA; + u32 dnumNames = *(u32 *)(dexp + 24); + u32 *dnames = (u32 *)(base + *(u32 *)(dexp + 32)); + u16 *dords = (u16 *)(base + *(u32 *)(dexp + 36)); + u32 *dfuncs = (u32 *)(base + *(u32 *)(dexp + 28)); + u32 vmRVA = 0; + for (u32 i = 0; i < dnumNames; i++) { + const u8 *nm = base + dnames[i]; + if (mem_eq(nm, "VerifyMain")) { vmRVA = dfuncs[dords[i]]; break; } + } + if (!vmRVA) { LOG('E'); return 11; } + LOG('F'); + + typedef int (*PFN)(void *, const char *); + PFN fn = (PFN)(base + vmRVA); + void *oep = (void *)((u8 *)imageBase + hdr->oepRva); + int rc = fn(oep, cfg); + if (rc != 0) { + if (hdr->flags & 1) ((void (*)(void))oep)(); + pExit(0); + } + return 0; +} diff --git a/shell/boot/bootstrap5.c b/shell/boot/bootstrap5.c new file mode 100644 index 0000000..722bb86 --- /dev/null +++ b/shell/boot/bootstrap5.c @@ -0,0 +1,386 @@ +/* vfy 引导 stub v5 —— x64/x86 通用;**不含任何字符串字面量**(全用立即数打包), + * 因此 x86 下也是位置无关的:32 位没有 RIP 相对寻址,字面量会变成绝对地址而错位。 + * + * 头布局 48 字节(.vfy 节起始): + * 0..3 magic "VFY4" | 4..7 ver | 8..11 dllOff | 12..15 dllSize + * 16..19 cfgOff | 20..23 cfgSize | 24..27 oepRva | 28..31 blobSize + * 32..35 flags | 36..39 crcVfy | 40..43 crcFile | 44..47 reserved + */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +#if defined(_WIN64) +typedef unsigned long long u64; +typedef unsigned long long uptr; +#else +typedef unsigned int uptr; +#endif +typedef void *HMODULE; +typedef void *HANDLE; +typedef int BOOL; + +#if defined(_WIN64) +#define API +#else +#define API __stdcall +#endif + +typedef HMODULE(API *fnLoadLibraryA)(const char *); +typedef void *(API *fnGetProcAddress)(HMODULE, const char *); +typedef void *(API *fnVirtualAlloc)(void *, uptr, u32, u32); +typedef void(API *fnExitProcess)(u32); +typedef u32(API *fnGetModuleFileNameA)(HMODULE, char *, u32); +typedef HANDLE(API *fnCreateFileA)(const char *, u32, u32, void *, u32, u32, HANDLE); +typedef BOOL(API *fnReadFile)(HANDLE, void *, u32, u32 *, void *); +typedef u32(API *fnGetFileSize)(HANDLE, u32 *); +typedef BOOL(API *fnCloseHandle)(HANDLE); +typedef u32(API *fnGetTempPathA)(u32, char *); +typedef BOOL(API *fnWriteFile)(HANDLE, const void *, u32, u32 *, void *); + +typedef struct { + char magic[4]; + u32 ver; + u32 dllOff, dllSize; + u32 cfgOff, cfgSize; + u32 oepRva; + u32 blobSize; + u32 flags; + u32 crcVfy; + u32 crcFile; + u32 reserved; +} VFYHDR; + +#define CRCOFF 36 + +/* ---------- 位置无关的字符串工具(不用 .rdata) ---------- */ +static void putw(u32 *w, u32 w0, u32 w1, u32 w2, u32 w3, u32 w4, u32 w5) { + w[0] = w0; w[1] = w1; w[2] = w2; w[3] = w3; w[4] = w4; w[5] = w5; +} +static void mk(char *d, u32 w0, u32 w1, u32 w2, u32 w3, u32 w4, u32 w5, int n) { + u32 w[6]; putw(w, w0, w1, w2, w3, w4, w5); + for (int i = 0; i < n; i++) d[i] = (char)((w[i >> 2] >> ((i & 3) * 8)) & 0xFF); + d[n] = 0; +} +static int eq_s(const u8 *a, u32 w0, u32 w1, u32 w2, u32 w3, u32 w4, u32 w5, int n) { + u32 w[6]; putw(w, w0, w1, w2, w3, w4, w5); + for (int i = 0; i < n; i++) + if (a[i] != (u8)((w[i >> 2] >> ((i & 3) * 8)) & 0xFF)) return 0; + return 1; +} +static int eq_u16ci(const u16 *a, u32 w0, u32 w1, u32 w2, u32 w3, u32 w4, u32 w5, int n) { + u32 w[6]; putw(w, w0, w1, w2, w3, w4, w5); + for (int i = 0; i < n; i++) { + u16 c = a[i]; + if (c >= 'A' && c <= 'Z') c = (u16)(c + 32); + if (c != (u16)((w[i >> 2] >> ((i & 3) * 8)) & 0xFF)) return 0; + } + return 1; +} +static void *api(void *kern, fnGetProcAddress gpa, u32 w0, u32 w1, u32 w2, u32 w3, u32 w4, u32 w5, int n) { + char nm[32]; + mk(nm, w0, w1, w2, w3, w4, w5, n); + return gpa((HMODULE)kern, nm); +} +static uptr rd(const u8 *p) { +#if defined(_WIN64) + return *(const u64 *)p; +#else + return *(const u32 *)p; +#endif +} +static void mem_cpy(u8 *d, const u8 *s, uptr n) { + for (uptr i = 0; i < n; i++) d[i] = s[i]; +} +static u32 crc_feed(u32 c, const u8 *p, u32 n) { + for (u32 i = 0; i < n; i++) { + c ^= p[i]; + for (int k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + } + return c; +} + +__attribute__((section(".blobhead"), used)) int blob_main(void) { + uptr peb = 0; +#if defined(_WIN64) + __asm__ volatile("movq %%gs:0x60, %0" : "=r"(peb)); +#else + __asm__ volatile("movl %%fs:0x30, %0" : "=r"(peb)); +#endif + if (!peb) return 1; +#if defined(_WIN64) + uptr imageBase = rd((const u8 *)peb + 0x10); +#else + uptr imageBase = rd((const u8 *)peb + 0x08); +#endif + if (!imageBase) return 1; + + u8 *img = (u8 *)imageBase; + u32 lfanew = *(u32 *)(img + 0x3C); + u8 *nt = img + lfanew; + u16 numSec = *(u16 *)(nt + 6); + u16 optSize = *(u16 *)(nt + 20); + u8 *sec = nt + 24 + optSize; + VFYHDR *hdr = 0; + u8 *secHdr = 0; + for (u16 i = 0; i < numSec; i++) { + u8 *s = sec + i * 40; + if (s[0] == '.' && s[1] == 'v' && s[2] == 'f' && s[3] == 'y') { + secHdr = s; + hdr = (VFYHDR *)(img + *(u32 *)(s + 12)); + break; + } + } + if (!hdr) return 2; + if (!(hdr->magic[0] == 'V' && hdr->magic[1] == 'F' && hdr->magic[2] == 'Y' && hdr->magic[3] == '4')) return 2; + + u8 *secBase = (u8 *)hdr; + u32 secVSize = *(u32 *)(secHdr + 8); + u32 secRawPtr = *(u32 *)(secHdr + 20); + u8 *dllSrc = secBase + hdr->dllOff; + const char *cfg = (const char *)(secBase + hdr->cfgOff); + + /* kernel32 */ + uptr ldr = 0; +#if defined(_WIN64) + ldr = rd((const u8 *)peb + 0x18); + uptr head = ldr + 0x20, entryOff = 0x10, dllBaseOff = 0x30, nameLenOff = 0x58, nameBufOff = 0x60; +#else + ldr = rd((const u8 *)peb + 0x0C); + uptr head = ldr + 0x14, entryOff = 0x08, dllBaseOff = 0x18, nameLenOff = 0x2C, nameBufOff = 0x30; +#endif + uptr cur = rd((const u8 *)head); + uptr kern = 0; + for (int guard = 0; guard < 64 && cur && cur != head; guard++) { + u8 *entry = (u8 *)(cur - entryOff); + uptr dllBase = rd(entry + dllBaseOff); + u16 nameLen = *(u16 *)(entry + nameLenOff); + uptr nameBuf = rd(entry + nameBufOff); + if (nameLen == 24 && nameBuf && + eq_u16ci((const u16 *)nameBuf, 0x6E72656B, 0x32336C65, 0x6C6C642E, 0, 0, 0, 12)) { + kern = dllBase; + break; + } + cur = rd((const u8 *)cur); + } + if (!kern) return 3; + + u8 *kb = (u8 *)kern; + u32 kl = *(u32 *)(kb + 0x3C); + u8 *knt = kb + kl; +#if defined(_WIN64) + u32 ddOff = 112; +#else + u32 ddOff = 96; +#endif + u32 expRVA = *(u32 *)(knt + 24 + ddOff); + if (!expRVA) return 4; + u8 *exp = kb + expRVA; + u32 numNames = *(u32 *)(exp + 24); + u32 *names = (u32 *)(kb + *(u32 *)(exp + 32)); + u16 *ords = (u16 *)(kb + *(u32 *)(exp + 36)); + u32 *funcs = (u32 *)(kb + *(u32 *)(exp + 28)); + + fnGetProcAddress pGPA = 0; + fnLoadLibraryA pLLA = 0; + for (u32 i = 0; i < numNames; i++) { + const u8 *nm = kb + names[i]; + if (eq_s(nm, 0x50746547, 0x41636F72, 0x65726464, 0x00007373, 0, 0, 14)) + pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]); + if (eq_s(nm, 0x64616F4C, 0x7262694C, 0x41797261, 0x00000000, 0, 0, 12)) + pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]); + if (pGPA && pLLA) break; + } + if (!pGPA || !pLLA) return 4; + + fnVirtualAlloc pAlloc = (fnVirtualAlloc)api((void *)kern, pGPA, 0x74726956, 0x416C6175, 0x636F6C6C, 0, 0, 0, 12); + fnExitProcess pExit = (fnExitProcess)api((void *)kern, pGPA, 0x74697845, 0x636F7250, 0x00737365, 0, 0, 0, 11); + fnGetModuleFileNameA pModName = (fnGetModuleFileNameA)api((void *)kern, pGPA, 0x4D746547, 0x6C75646F, 0x6C694665, 0x6D614E65, 0x00004165, 0, 18); + fnCreateFileA pCreate = (fnCreateFileA)api((void *)kern, pGPA, 0x61657243, 0x69466574, 0x0041656C, 0, 0, 0, 11); + fnReadFile pRead = (fnReadFile)api((void *)kern, pGPA, 0x64616552, 0x656C6946, 0, 0, 0, 0, 8); + fnGetFileSize pSize = (fnGetFileSize)api((void *)kern, pGPA, 0x46746547, 0x53656C69, 0x00657A69, 0, 0, 0, 11); + fnCloseHandle pClose = (fnCloseHandle)api((void *)kern, pGPA, 0x736F6C43, 0x6E614865, 0x00656C64, 0, 0, 0, 11); + fnGetTempPathA pTemp = (fnGetTempPathA)api((void *)kern, pGPA, 0x54746547, 0x50706D65, 0x41687461, 0, 0, 0, 12); + fnWriteFile pWrite = (fnWriteFile)api((void *)kern, pGPA, 0x74697257, 0x6C694665, 0x00000065, 0, 0, 0, 9); + if (!pAlloc || !pExit || !pModName || !pCreate || !pRead || !pSize || !pClose) return 5; + +#define LOG(ch) do { \ + if (pTemp && pCreate && pWrite && pClose) { \ + char lp[300]; \ + u32 tl_ = pTemp(300, lp); \ + if (tl_ && tl_ < 280) { \ + char fn_[16]; \ + mk(fn_, 0x5F796676, 0x742E3462, 0x00007478, 0, 0, 0, 10); \ + int i_ = 0; \ + while (fn_[i_]) { lp[tl_ + i_] = fn_[i_]; i_++; } \ + lp[tl_ + i_] = 0; \ + HANDLE h_ = pCreate(lp, 0x40000000u, 0, 0, 2, 0x80, 0); \ + if (h_ && h_ != (HANDLE)-1) { char cc_ = (char)(ch); u32 w_ = 0; pWrite(h_, &cc_, 1, &w_, 0); pClose(h_); } \ + } \ + } \ +} while (0) + + /* 完整性 1:.vfy 内容 */ + if (hdr->crcVfy) { + u32 c = 0xFFFFFFFFu; + c = crc_feed(c, secBase, CRCOFF); + c = crc_feed(c, secBase + CRCOFF + 8, secVSize - CRCOFF - 8); + c ^= 0xFFFFFFFFu; + if (c != hdr->crcVfy) { LOG('1'); return 6; } + LOG('2'); + } + + /* 完整性 2:整个文件 */ + if (hdr->crcFile) { + char path[512]; + u32 n = pModName(0, path, 512); + if (!n) return 12; + HANDLE hf = pCreate(path, 0x80000000u, 1, 0, 3, 0x80, 0); + if (hf == (HANDLE)-1 || !hf) return 12; + u32 fsize = pSize(hf, 0); + u8 *buf = (u8 *)pAlloc(0, fsize, 0x3000, 0x04); + if (!buf) { pClose(hf); return 12; } + u32 got = 0; + if (!pRead(hf, buf, fsize, &got, 0) || got != fsize) { pClose(hf); return 12; } + pClose(hf); + u32 skipAt = secRawPtr + CRCOFF; + if (skipAt > fsize) return 12; + u32 c = 0xFFFFFFFFu; + c = crc_feed(c, buf, skipAt); + c = crc_feed(c, buf + skipAt + 8, fsize - skipAt - 8); + c ^= 0xFFFFFFFFu; + if (c != hdr->crcFile) { LOG('3'); return 13; } + LOG('4'); + } + + /* 反射加载 */ + u8 *src = dllSrc; + u32 dl = *(u32 *)(src + 0x3C); + u8 *dnt = src + dl; + u16 dsecs = *(u16 *)(dnt + 6); + u16 doptSize = *(u16 *)(dnt + 20); + u8 *dopt = dnt + 24; + uptr prefBase = 0; + u32 sizeOfImage = 0, sizeOfHeaders = 0, entryRVA = 0, importRVA = 0, relocRVA = 0, relocSize = 0; +#if defined(_WIN64) + prefBase = *(u64 *)(dopt + 24); + sizeOfImage = *(u32 *)(dopt + 56); + sizeOfHeaders = *(u32 *)(dopt + 60); + entryRVA = *(u32 *)(dopt + 16); + importRVA = *(u32 *)(dopt + 112 + 8); + relocRVA = *(u32 *)(dopt + 112 + 40); + relocSize = *(u32 *)(dopt + 112 + 44); +#else + prefBase = *(u32 *)(dopt + 28); + sizeOfImage = *(u32 *)(dopt + 56); + sizeOfHeaders = *(u32 *)(dopt + 60); + entryRVA = *(u32 *)(dopt + 16); + importRVA = *(u32 *)(dopt + 96 + 8); + relocRVA = *(u32 *)(dopt + 96 + 40); + relocSize = *(u32 *)(dopt + 96 + 44); +#endif + u8 *dsec = dopt + doptSize; + + LOG('5'); + u8 *base = (u8 *)pAlloc(0, sizeOfImage, 0x3000, 0x40); + if (!base) { LOG('6'); return 7; } + LOG('7'); + mem_cpy(base, src, sizeOfHeaders); + for (u16 i = 0; i < dsecs; i++) { + u8 *s = dsec + i * 40; + u32 va = *(u32 *)(s + 12); + u32 rawSize = *(u32 *)(s + 16); + u32 rawPtr = *(u32 *)(s + 20); + if (rawSize && rawPtr) mem_cpy(base + va, src + rawPtr, rawSize); + } + + LOG('8'); + if (relocRVA && relocSize && (uptr)base != prefBase) { + uptr delta = (uptr)base - prefBase; + u8 *r = base + relocRVA; + u8 *rend = r + relocSize; + while (r + 8 <= rend) { + u32 pageRVA = *(u32 *)r; + u32 blockSize = *(u32 *)(r + 4); + if (!blockSize) break; + u16 *ent = (u16 *)(r + 8); + int cnt = (int)(blockSize - 8) / 2; + for (int i = 0; i < cnt; i++) { + u16 e = ent[i]; + u16 type = (u16)(e >> 12); + u16 off = (u16)(e & 0x0FFF); +#if defined(_WIN64) + if (type == 10) { u64 *p = (u64 *)(base + pageRVA + off); *p += delta; } +#else + if (type == 3) { u32 *p = (u32 *)(base + pageRVA + off); *p += (u32)delta; } +#endif + } + r += blockSize; + } + } + + LOG('9'); + if (importRVA) { + u8 *desc = base + importRVA; + for (;;) { + u32 oft = *(u32 *)desc; + u32 nameRVA = *(u32 *)(desc + 12); + u32 ft = *(u32 *)(desc + 16); + if (!nameRVA && !ft && !oft) break; + const char *dllName = (const char *)(base + nameRVA); + HMODULE hm = pLLA(dllName); + if (!hm) { LOG('A'); return 8; } + u8 *thunk = base + (oft ? oft : ft); + u8 *iat = base + ft; + for (u32 k = 0;; k++) { + uptr t = rd(thunk + k * (u32)sizeof(uptr)); + if (!t) break; + void *fn = 0; + if (t & ((uptr)1 << (sizeof(uptr) * 8 - 1))) + fn = pGPA(hm, (const char *)(t & 0xFFFF)); + else + fn = pGPA(hm, (const char *)(base + (u32)t + 2)); + if (!fn) { LOG('B'); return 9; } + *(uptr *)(iat + k * sizeof(uptr)) = (uptr)fn; + } + desc += 20; + } + } + + LOG('C'); + typedef BOOL(API *fnDllMain)(HMODULE, u32, void *); + if (entryRVA) { + fnDllMain dm = (fnDllMain)(base + entryRVA); + dm((HMODULE)base, 1, 0); + } + + LOG('D'); + u32 dexpRVA = 0; +#if defined(_WIN64) + dexpRVA = *(u32 *)(dopt + 112); +#else + dexpRVA = *(u32 *)(dopt + 96); +#endif + if (!dexpRVA) return 10; + u8 *dexp = base + dexpRVA; + u32 dnumNames = *(u32 *)(dexp + 24); + u32 *dnames = (u32 *)(base + *(u32 *)(dexp + 32)); + u16 *dords = (u16 *)(base + *(u32 *)(dexp + 36)); + u32 *dfuncs = (u32 *)(base + *(u32 *)(dexp + 28)); + u32 vmRVA = 0; + for (u32 i = 0; i < dnumNames; i++) { + const u8 *nm = base + dnames[i]; + if (eq_s(nm, 0x69726556, 0x614D7966, 0x00006E69, 0, 0, 0, 10)) { vmRVA = dfuncs[dords[i]]; break; } + } + if (!vmRVA) { LOG('E'); return 11; } + LOG('F'); + + typedef int (*PFN)(void *, const char *); + PFN fn = (PFN)(base + vmRVA); + void *oep = (void *)((u8 *)imageBase + hdr->oepRva); + int rc = fn(oep, cfg); + if (rc != 0) { + if (hdr->flags & 1) ((void (*)(void))oep)(); + pExit(0); + } + return 0; +} diff --git a/shell/boot/build-blob.sh b/shell/boot/build-blob.sh new file mode 100755 index 0000000..e35e756 --- /dev/null +++ b/shell/boot/build-blob.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# 从源码重建引导 blob +# 用法: ./build-blob.sh [x64|x86] [输出文件] +# 例: ./build-blob.sh bootstrap5.c x64 blob64.bin +# 详见 ../README.md「构建引导 blob」——重编产物与仓库里的预编译 blob 行为不一致,替换前务必实测。 +set -euo pipefail + +SRC=${1:?用法: $0 [x64|x86] [out.bin]} +ARCH=${2:-x64} +OUT=${3:-blob64.bin} +DIR=$(cd "$(dirname "$0")" && pwd) + +DEFS="" +if [ "$ARCH" = "x64" ]; then DEFS="-D_WIN64"; fi + +TMP=$(mktemp -d) +trap 'rm -rf "$TMP"' EXIT + +gcc -c $DEFS -ffreestanding -fno-stack-protector -fno-asynchronous-unwind-tables \ + -mno-stack-arg-probe "$SRC" -o "$TMP/blob.o" + +# 别用 ld --oformat=binary(会报 cannot perform PE operations on non PE output file), +# 走链接脚本把 .text/.rdata/.data 合成单节再 objcopy 提取 +ld -e blob_main -nostdlib -T "$DIR/blob.ld" "$TMP/blob.o" -o "$TMP/blob.elf" 2>/dev/null + +objcopy -O binary -j .blob "$TMP/blob.elf" "$OUT" + +echo "生成 $OUT($(stat -c%s "$OUT") 字节,arch=$ARCH)" +echo "注意:本机 gcc 重编的 blob 与 assets/ 里的预编译 blob 行为不同(详见 ../README.md)," +echo " 替换资产前先把「单层 / 双层 / 篡改 .vfy & PE 头」四类用例实测一遍。" diff --git a/shell/classic/build-classic.sh b/shell/classic/build-classic.sh new file mode 100755 index 0000000..1594808 --- /dev/null +++ b/shell/classic/build-classic.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# 交叉编译 Direct2D 界面壳 → shell64.dll / shell32.dll +# 用法: ./build-classic.sh [源码] (默认 shell6.cpp) +# 依赖: x86_64-w64-mingw32-g++ / i686-w64-mingw32-g++ +# 注意: 必须带 -Wl,--kill-at,否则导出名带 @N 后缀,GetProcAddress("VerifyMain") 取不到 +set -euo pipefail + +DIR=$(cd "$(dirname "$0")" && pwd) +SRC=${1:-$DIR/shell6.cpp} +OUTDIR=${2:-$DIR/out} +mkdir -p "$OUTDIR" + +COMMON=(-std=c++17 -O2 -shared -s -DUNICODE -D_UNICODE -D_WIN32_WINNT=0x0601 -D_WIN32_LEAN_AND_MEAN) +# 导入表按实测产物取:ADVAPI32/bcrypt/d2d1/DWrite/GDI32/KERNEL32/msvcrt/ole32/USER32/WINHTTP +LIBS=(-static -ladvapi32 -lbcrypt -ld2d1 -ldwrite -lgdi32 -lole32 -luser32 -lwinhttp -Wl,--kill-at) + +x86_64-w64-mingw32-g++ "${COMMON[@]}" -o "$OUTDIR/shell64.dll" "$SRC" "${LIBS[@]}" +echo "生成 $OUTDIR/shell64.dll($(stat -c%s "$OUTDIR/shell64.dll") 字节)" + +if command -v i686-w64-mingw32-g++ >/dev/null 2>&1; then + i686-w64-mingw32-g++ "${COMMON[@]}" -o "$OUTDIR/shell32.dll" "$SRC" "${LIBS[@]}" + echo "生成 $OUTDIR/shell32.dll($(stat -c%s "$OUTDIR/shell32.dll") 字节)" +else + echo "跳过 x86:未安装 i686-w64-mingw32-g++" +fi + +echo "产物是 DLL,加壳器会把它嵌进 .vfy 节;要更新资产请替换 assets/shell64.bin 并重新打壳验证。" +echo "logf_ 的落盘日志补丁见 ../README.md(改完源码重编会自动带上/不带日志,先确认要哪种)。" diff --git a/shell/classic/shell5.cpp b/shell/classic/shell5.cpp new file mode 100644 index 0000000..ffdda78 --- /dev/null +++ b/shell/classic/shell5.cpp @@ -0,0 +1,832 @@ +// vfy shell —— VS 2026 暗黑风(Direct2D + DirectWrite) +// 两种形态:独立 exe(内嵌加密载荷)/ 注入用 DLL(导出 VerifyMain,验证后跳原入口 OEP) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CARD_W 420.0f +#define CARD_H 404.0f +#define PAD 20.0f +#define D2D_VER 1 + +static float g_S = 1.0f; +static inline float L(float v) { return v * g_S; } + +/* VS 2026 dark */ +static D2D1_COLOR_F C_CARD, C_BAR, C_BORDER, C_INPUT, C_INPUT_BD, C_FOCUS, C_TXT, C_TXT2, + C_ACCENT, C_ACCENT_H, C_ACCENT_P, C_LINK, C_RED, C_GREEN, C_WHITE, C_TAG; + +static ID2D1Factory *g_d2d = NULL; +static IDWriteFactory *g_dw = NULL; +static ID2D1DCRenderTarget *g_rt = NULL; +static const wchar_t *g_famCJK = L"Microsoft YaHei UI"; +static const wchar_t *g_famLat = L"Segoe UI"; + +static HWND g_hwnd, g_hUser, g_hPass; +static HFONT g_fontEdit = NULL; +static HBRUSH g_brInput = NULL; +static int g_focus = 0; +static int g_hBtn, g_hChk, g_hTag, g_hEye, g_hClose, g_hMin, g_hLink; +static int g_pressBtn, g_checked, g_showPass; +static volatile LONG g_busy = 0; +static int g_statusKind = 0; +static wchar_t g_status[256] = L"\x5C31\x7EEA"; +static int g_tick = 0; +static int g_done = 0; + +static const RECT LR_CLOSE = {420 - 44, 0, 420, 32}; +static const RECT LR_MIN = {420 - 88, 0, 420 - 44, 32}; +static const RECT LR_USER = {20, 124, 400, 156}; +static const RECT LR_PASS = {20, 186, 400, 218}; +static const RECT LR_EYE = {368, 190, 392, 214}; +static const RECT LR_CHK = {20, 233, 34, 247}; +static const RECT LR_REM = {20, 228, 150, 252}; +static const RECT LR_TAG = {244, 228, 400, 252}; +static const RECT LR_BTN = {20, 264, 400, 298}; +static const RECT LR_L1 = {20, 316, 116, 338}; +static const RECT LR_L2 = {128, 316, 224, 338}; +static const RECT LR_L3 = {236, 316, 332, 338}; + +/* ---------- 载荷(仅 exe 形态使用) ---------- */ +static unsigned char *g_file = NULL; +static DWORD g_fileSize = 0; +static const unsigned char *g_payload = NULL; +static DWORD g_payloadSize = 0; +static unsigned char g_nonce[12], g_tag[16]; +static char g_cfg[4096], g_api[512]; +static char g_name[128] = "\xE6\xBC\x94\xE7\xA4\xBA\xE7\xA8\x8B\xE5\xBA\x8F"; +static char g_device[64] = "A1B2-3C4D"; +static char g_auto[128], g_offline[64]; +static const char TAIL_MAGIC[8] = {'V', 'F', 'Y', 'D', '1', 0, 0, 0}; + +static void logf_(const char *fmt, ...) { + char tmp[MAX_PATH] = {0}; GetTempPathA(MAX_PATH, tmp); + char path[MAX_PATH]; snprintf(path, sizeof(path), "%svfy_debug.log", tmp); + FILE *f = fopen(path, "a"); + if (!f) return; + va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); + fputc('\n', f); fclose(f); +} + +static int json_str(const char *src, const char *key, char *out, int cap) { + char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key); + const char *p = strstr(src, pat); + if (!p) return 0; + p = strchr(p + strlen(pat), ':'); + if (!p) return 0; + p = strchr(p, '"'); + if (!p) return 0; + p++; + int i = 0; + while (*p && *p != '"' && i < cap - 1) out[i++] = *p++; + out[i] = 0; + return 1; +} + +static void load_cfg(const char *json) { + snprintf(g_cfg, sizeof(g_cfg), "%s", json ? json : ""); + json_str(g_cfg, "api", g_api, sizeof(g_api)); + json_str(g_cfg, "name", g_name, sizeof(g_name)); + json_str(g_cfg, "device", g_device, sizeof(g_device)); + json_str(g_cfg, "offline_pass", g_offline, sizeof(g_offline)); +} + +/* ---------- 文本:CJK/拉丁分段 ---------- */ +static int is_cjk(wchar_t c) { + return (c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xF900 && c <= 0xFAFF) || + (c >= 0xFF00 && c <= 0xFF65) || (c >= 0x3000 && c <= 0x303F); +} + +static void draw_run(const wchar_t *s, int n, const wchar_t *fam, float x, float y, float h, + float px, D2D1_COLOR_F c, int bold) { + IDWriteTextFormat *f = NULL; + if (FAILED(g_dw->CreateTextFormat(fam, NULL, bold ? DWRITE_FONT_WEIGHT_SEMI_BOLD : DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, L(px), L"zh-cn", &f))) + return; + f->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); + f->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); + f->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); + wchar_t sub[512]; + int k = n > 511 ? 511 : n; + for (int i = 0; i < k; i++) sub[i] = (s[i] == 0x20) ? (wchar_t)0x00A0 : s[i]; + sub[k] = 0; + IDWriteTextLayout *lay = NULL; + if (FAILED(g_dw->CreateTextLayout(sub, (UINT32)k, f, 4000.0f, L(h), &lay))) { f->Release(); return; } + ID2D1SolidColorBrush *b = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(c, &b))) { + g_rt->DrawTextLayout(D2D1::Point2F(L(x), L(y)), lay, b, D2D1_DRAW_TEXT_OPTIONS_NONE); + b->Release(); + } + lay->Release(); + f->Release(); +} + +static float run_width(const wchar_t *s, int n, const wchar_t *fam, float px, int bold) { + IDWriteTextFormat *f = NULL; + if (FAILED(g_dw->CreateTextFormat(fam, NULL, bold ? DWRITE_FONT_WEIGHT_SEMI_BOLD : DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, L(px), L"zh-cn", &f))) + return 0; + f->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); + IDWriteTextLayout *lay = NULL; + float w = 0; + if (SUCCEEDED(g_dw->CreateTextLayout(s, (UINT32)n, f, 4000.0f, L(px * 2), &lay))) { + DWRITE_TEXT_METRICS m; + if (SUCCEEDED(lay->GetMetrics(&m)) && m.width > 0.5f) w = m.width / g_S - 0.6f; + lay->Release(); + } + if (w <= 0.5f) + for (int i = 0; i < n; i++) + w += is_cjk(s[i]) ? px : (s[i] == 0x20 ? px * 0.28f : px * 0.56f); + f->Release(); + return w; +} + +static void textL(const wchar_t *s, float x, float y, float w, float h, float px, + D2D1_COLOR_F c, int bold, int align) { + int len = (int)wcslen(s); + if (!len) return; + int starts[64], lens[64], fams[64], cnt = 0, i = 0; + while (i < len && cnt < 64) { + int cjk = is_cjk(s[i]); + int j = i; + while (j < len && is_cjk(s[j]) == cjk) j++; + starts[cnt] = i; lens[cnt] = j - i; fams[cnt] = cjk; cnt++; + i = j; + } + float total = 0; + for (int k = 0; k < cnt; k++) total += run_width(s + starts[k], lens[k], fams[k] ? g_famCJK : g_famLat, px, bold); + float ox = x; + if (align == 1) ox = x + (w - total) * 0.5f; + else if (align == 2) ox = x + w - total; + for (int k = 0; k < cnt; k++) { + const wchar_t *fam = fams[k] ? g_famCJK : g_famLat; + draw_run(s + starts[k], lens[k], fam, ox, y, h, px, c, bold); + ox += run_width(s + starts[k], lens[k], fam, px, bold); + } +} + +/* ---------- 形状 ---------- */ +static void fillRound(float x, float y, float w, float h, float r, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->FillRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), L(r), L(r)), b); + b->Release(); +} + +static void strokeRound(float x, float y, float w, float h, float r, float wd, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->DrawRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), L(r), L(r)), b, L(wd)); + b->Release(); +} + +static void fillRect(float x, float y, float w, float h, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->FillRectangle(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), b); + b->Release(); +} + +static void line(float x1, float y1, float x2, float y2, float wd, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->DrawLine(D2D1::Point2F(L(x1), L(y1)), D2D1::Point2F(L(x2), L(y2)), b, L(wd)); + b->Release(); +} + +static D2D1_COLOR_F status_color(void) { + return g_statusKind == 1 ? C_GREEN : (g_statusKind == 2 ? C_RED : C_TXT2); +} + +/* ---------- 画面 ---------- */ +static void paint(void) { + for (int i = 14; i >= 1; i--) + fillRound(PAD - i, PAD - i + 2 + i / 8.0f, CARD_W + i * 2, CARD_H + i * 2, 6 + i, + D2D1::ColorF(0, 0, 0, 0.02f + (15 - i) * 0.006f)); + fillRound(PAD, PAD, CARD_W, CARD_H, 6, C_CARD); + strokeRound(PAD, PAD, CARD_W, CARD_H, 6, 1.0f, C_BORDER); + + g_rt->SetTransform(D2D1::Matrix3x2F::Translation(L(PAD), L(PAD))); + + /* 标题栏 */ + fillRound(0, 0, CARD_W, 32 + 6, 6, C_BAR); + fillRect(0, 32, CARD_W, 6, C_CARD); + fillRect(0, 31.6f, CARD_W, 0.6f, C_BORDER); + + if (g_hClose) fillRect(LR_CLOSE.left, 0, 44, 32, D2D1::ColorF(0.91f, 0.07f, 0.14f, 1)); + else if (g_hMin) fillRect(LR_MIN.left, 0, 44, 32, D2D1::ColorF(1, 1, 1, 0.07f)); + { + D2D1_COLOR_F ic = C_TXT; + float cx = (LR_CLOSE.left + LR_CLOSE.right) / 2.0f, cy = 16; + line(cx - 4.5f, cy - 4.5f, cx + 4.5f, cy + 4.5f, 1.0f, ic); + line(cx + 4.5f, cy - 4.5f, cx - 4.5f, cy + 4.5f, 1.0f, ic); + float mx = (LR_MIN.left + LR_MIN.right) / 2.0f; + line(mx - 4.5f, cy, mx + 4.5f, cy, 1.0f, ic); + } + fillRound(12, 8, 16, 16, 3, C_ACCENT); + { + wchar_t t[256] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_name, -1, t, 256); + textL(t, 36, 0, 260, 32, 12, C_TXT, 0, 0); + } + + /* 标题区 */ + textL(L"\x767B\x5F55\x4EE5\x7EE7\x7EED", 20, 46, 300, 26, 15, C_TXT, 1, 0); + textL(L"\x8BF7\x4F7F\x7528\x8D26\x53F7\x6216\x5361\x5BC6\x767B\x5F55", 20, 72, 320, 16, 11, C_TXT2, 0, 0); + + /* 账号 */ + textL(L"\x8D26\x53F7", 20, 102, 100, 16, 11, C_TXT2, 0, 0); + fillRound(20, 124, 380, 32, 3, C_INPUT); + strokeRound(20, 124, 380, 32, 3, 1.0f, g_focus == 1 ? C_FOCUS : C_INPUT_BD); + if (g_focus == 1) strokeRound(21.5f, 125.5f, 377, 29, 2.4f, 1.0f, C_FOCUS); + if (GetWindowTextLengthW(g_hUser) == 0 && g_focus != 1) + textL(L"\x8BF7\x8F93\x5165\x8D26\x53F7", 32, 124, 340, 32, 12, D2D1::ColorF(0.52f, 0.52f, 0.55f, 1), 0, 0); + + /* 密码 */ + textL(L"\x5BC6\x7801", 20, 164, 100, 16, 11, C_TXT2, 0, 0); + fillRound(20, 186, 380, 32, 3, C_INPUT); + strokeRound(20, 186, 380, 32, 3, 1.0f, g_focus == 2 ? C_FOCUS : C_INPUT_BD); + if (g_focus == 2) strokeRound(21.5f, 187.5f, 377, 29, 2.4f, 1.0f, C_FOCUS); + if (GetWindowTextLengthW(g_hPass) == 0 && g_focus != 2) + textL(L"\x8BF7\x8F93\x5165\x5BC6\x7801", 32, 186, 320, 32, 12, D2D1::ColorF(0.52f, 0.52f, 0.55f, 1), 0, 0); + { + D2D1_COLOR_F ec = g_hEye ? C_FOCUS : D2D1::ColorF(0.6f, 0.6f, 0.63f, 1); + ID2D1SolidColorBrush *b = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(ec, &b))) { + g_rt->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(L(376), L(202)), L(8.5f), L(5.5f)), b, L(1.0f)); + if (g_showPass) line(368, 210, 384, 194, 1.0f, ec); + else { + ID2D1SolidColorBrush *pb = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(ec, &pb))) { + g_rt->FillEllipse(D2D1::Ellipse(D2D1::Point2F(L(376), L(202)), L(2.4f), L(2.4f)), pb); + pb->Release(); + } + } + b->Release(); + } + } + + /* 记住账号 */ + fillRound(20, 233, 14, 14, 2, g_checked ? C_FOCUS : C_INPUT); + strokeRound(20, 233, 14, 14, 2, 1.0f, g_checked ? C_FOCUS : (g_hChk ? C_TXT2 : C_INPUT_BD)); + if (g_checked) { + line(23.2f, 240.2f, 25.6f, 242.8f, 1.4f, C_WHITE); + line(25.6f, 242.8f, 31.0f, 236.8f, 1.4f, C_WHITE); + } + textL(L"\x8BB0\x4F4F\x8D26\x53F7", 40, 233, 100, 14, 11, C_TXT2, 0, 0); + + /* 设备码(VS 标签样式:小圆角 + 细边) */ + fillRound(244, 228, 156, 24, 2, C_TAG); + strokeRound(244, 228, 156, 24, 2, 1.0f, C_INPUT_BD); + { + wchar_t tg[160], dev[64] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_device, -1, dev, 64); + swprintf(tg, 160, L"\x8BBE\x5907 %ls", dev); + textL(tg, 244, 228, 156, 24, 10.5f, C_TXT2, 0, 1); + } + + /* 主按钮 */ + if (g_busy) { + fillRound(20, 264, 380, 34, 3, D2D1::ColorF(0.22f, 0.22f, 0.24f, 1)); + wchar_t d[16] = L"\x9A8C\x8BC1\x4E2D"; + int k = g_tick % 4; + for (int i = 0; i < k; i++) d[3 + i] = L'.'; + d[3 + k] = 0; + textL(d, 20, 264, 380, 34, 12.5f, D2D1::ColorF(0.62f, 0.62f, 0.65f, 1), 0, 1); + } else { + fillRound(20, 264, 380, 34, 3, g_pressBtn ? C_ACCENT_P : (g_hBtn ? C_ACCENT_H : C_ACCENT)); + textL(L"\x767B\x5F55", 20, 264, 380, 34, 12.5f, C_WHITE, 0, 1); + } + + /* 链接 */ + textL(L"\x6CE8\x518C\x8D26\x53F7", 20, 316, 96, 22, 11, g_hLink == 1 ? C_LINK : C_TXT2, 0, 0); + textL(L"\x5361\x5BC6\x5145\x503C", 128, 316, 96, 22, 11, g_hLink == 2 ? C_LINK : C_TXT2, 0, 0); + textL(L"\x5FD8\x8BB0\x5BC6\x7801", 236, 316, 96, 22, 11, g_hLink == 3 ? C_LINK : C_TXT2, 0, 0); + + /* 状态 + 页脚 */ + fillRound(20, 356, 5, 5, 2.5f, status_color()); + textL(g_status, 32, 348, 340, 20, 11, status_color(), 0, 0); + line(20, 374, 400, 374, 0.6f, C_BORDER); + textL(L"v1.0.0 \xB7 \x53EF\x4FE1\x8BBE\x5907\x7ED1\x5B9A \xB7 \x5F02\x5E38\x767B\x5F55\x81EA\x52A8\x9501\x5B9A", + 20, 380, 380, 16, 10, D2D1::ColorF(0.55f, 0.55f, 0.58f, 1), 0, 0); + + g_rt->SetTransform(D2D1::Matrix3x2F::Identity()); +} + +/* ---------- 离屏渲染 + 提交 ---------- */ +static void render(void) { + if (!g_rt || !g_hwnd) return; + int w = (int)((CARD_W + PAD * 2) * g_S), h = (int)((CARD_H + PAD * 2) * g_S); + HDC screen = GetDC(NULL); + HDC mem = CreateCompatibleDC(screen); + BITMAPINFO bi; ZeroMemory(&bi, sizeof(bi)); + bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bi.bmiHeader.biWidth = w; + bi.bmiHeader.biHeight = -h; + bi.bmiHeader.biPlanes = 1; + bi.bmiHeader.biBitCount = 32; + bi.bmiHeader.biCompression = BI_RGB; + void *bits = NULL; + HBITMAP dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &bits, NULL, 0); + if (!dib) { DeleteDC(mem); ReleaseDC(NULL, screen); return; } + HGDIOBJ old = SelectObject(mem, dib); + memset(bits, 0, (size_t)w * h * 4); + RECT rc = {0, 0, w, h}; + g_rt->BindDC(mem, &rc); + g_rt->SetDpi(96.0f, 96.0f); + g_rt->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE); + g_rt->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE); + g_rt->BeginDraw(); + g_rt->Clear(D2D1::ColorF(0, 0, 0, 0)); + paint(); + HRESULT hr = g_rt->EndDraw(); + if (FAILED(hr)) logf_("EndDraw hr=0x%lx", (unsigned long)hr); + RECT wr; GetWindowRect(g_hwnd, &wr); + POINT dst = {wr.left, wr.top}, src = {0, 0}; + SIZE sz = {w, h}; + BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA}; + UpdateLayeredWindow(g_hwnd, screen, &dst, &sz, mem, &src, 0, &bf, ULW_ALPHA); + SelectObject(mem, old); + DeleteObject(dib); + DeleteDC(mem); + ReleaseDC(NULL, screen); +} + +/* ---------- 字体自检 ---------- */ +static int font_cov(const wchar_t *fam, const wchar_t *sample) { + const int W = 200, H = 40; + HDC screen = GetDC(NULL); + HDC mem = CreateCompatibleDC(screen); + BITMAPINFO bi; ZeroMemory(&bi, sizeof(bi)); + bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bi.bmiHeader.biWidth = W; bi.bmiHeader.biHeight = -H; + bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = BI_RGB; + void *bits = NULL; + HBITMAP dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &bits, NULL, 0); + if (!dib) { DeleteDC(mem); ReleaseDC(NULL, screen); return 0; } + HGDIOBJ old = SelectObject(mem, dib); + memset(bits, 0, W * H * 4); + RECT rc = {0, 0, W, H}; + g_rt->BindDC(mem, &rc); + g_rt->SetDpi(96.0f, 96.0f); + g_rt->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE); + g_rt->BeginDraw(); + g_rt->Clear(D2D1::ColorF(0, 0, 0, 0)); + draw_run(sample, (int)wcslen(sample), fam, 4, 2, 30, 15, D2D1::ColorF(1, 1, 1, 1), 0); + g_rt->EndDraw(); + int n = 0; + unsigned char *p = (unsigned char *)bits; + for (int i = 0; i < W * H; i++) if (p[i * 4 + 3] > 16) n++; + SelectObject(mem, old); + DeleteObject(dib); + DeleteDC(mem); + ReleaseDC(NULL, screen); + return n; +} + +static void detect_fonts(void) { + const wchar_t *cjk[] = {L"Microsoft YaHei UI", L"Microsoft YaHei", L"WenQuanYi Micro Hei", L"Noto Sans CJK SC", L"DejaVu Sans"}; + const wchar_t *lat[] = {L"Segoe UI", L"Microsoft YaHei UI", L"DejaVu Sans", L"Arial", L"Tahoma"}; + int best = 0; + for (int i = 0; i < 5; i++) { + int c = font_cov(cjk[i], L"\x8BBE\x5907\x8D26\x53F7"); + if (c > best) { best = c; g_famCJK = cjk[i]; } + } + best = 0; + for (int i = 0; i < 5; i++) { + int c = font_cov(lat[i], L"A1B2-3C4D v1.0.0"); + if (c > best) { best = c; g_famLat = lat[i]; } + } + logf_("fonts: cjk/ lat ok"); +} + +/* ---------- 鉴权 + 载荷 ---------- */ +static int aes_decrypt(const unsigned char *in, DWORD inLen, unsigned char **out, DWORD *outLen) { + static const char secret[] = "vfy-demo-secret-2026"; + BCRYPT_ALG_HANDLE hAlg = NULL; BCRYPT_HASH_HANDLE hHash = NULL; BCRYPT_KEY_HANDLE hKey = NULL; + unsigned char key[32]; int ok = 0; + unsigned char *buf = NULL; ULONG got = 0; NTSTATUS st = 0; + BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info; + if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, NULL, 0) != 0) return 0; + if (BCryptCreateHash(hAlg, &hHash, NULL, 0, NULL, 0, 0) != 0) goto done; + BCryptHashData(hHash, (PUCHAR)secret, (ULONG)strlen(secret), 0); + BCryptFinishHash(hHash, key, 32, 0); + BCryptCloseAlgorithmProvider(hAlg, 0); hAlg = NULL; + if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0) != 0) goto done; + if (BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0) != 0) goto done; + if (BCryptGenerateSymmetricKey(hAlg, &hKey, NULL, 0, key, 32, 0) != 0) goto done; + BCRYPT_INIT_AUTH_MODE_INFO(info); + info.pbNonce = g_nonce; info.cbNonce = 12; info.pbTag = g_tag; info.cbTag = 16; + buf = (unsigned char *)malloc(inLen + 16); + st = BCryptDecrypt(hKey, (PUCHAR)in, inLen, &info, NULL, 0, buf, inLen, &got, 0); + logf_("bcrypt st=0x%lx len=%lu", (unsigned long)st, (unsigned long)got); + if (st != 0) { free(buf); goto done; } + *out = buf; *outLen = got; ok = 1; +done: + if (hKey) BCryptDestroyKey(hKey); + if (hAlg) BCryptCloseAlgorithmProvider(hAlg, 0); + if (hHash) BCryptDestroyHash(hHash); + return ok; +} + +static int http_login(const char *user, const char *pass, char *msg, int cap) { + wchar_t wHost[256], wPath[512]; + char host[256] = {0}, path[512] = {0}; + snprintf(path, sizeof(path), "%s", g_api); + char *p = strstr(path, "://"); + if (!p) return 0; + p += 3; + char *slash = strchr(p, '/'); + if (slash) { *slash = 0; snprintf(path, sizeof(path), "%s", slash + 1); } + snprintf(host, sizeof(host), "%s", p); + unsigned short port = 80; + char *colon = strrchr(host, ':'); + if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); } + MultiByteToWideChar(CP_UTF8, 0, host, -1, wHost, 256); + MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, 512); + char body[512]; + snprintf(body, sizeof(body), "{\"user\":\"%s\",\"pass\":\"%s\",\"device\":\"%s\"}", user, pass, g_device); + int ok = 0; + HINTERNET hS = WinHttpOpen(L"vfy/5.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0); + if (!hS) return 0; + HINTERNET hC = WinHttpConnect(hS, wHost, port, 0); + HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath, NULL, NULL, NULL, 0) : NULL; + if (hR) { + const wchar_t *hdr = L"Content-Type: application/json\r\n"; + WinHttpSendRequest(hR, hdr, (DWORD)-1L, body, (DWORD)strlen(body), (DWORD)strlen(body), 0); + WinHttpReceiveResponse(hR, NULL); + char resp[1024] = {0}; + DWORD cl = 0; + if (WinHttpReadData(hR, resp, sizeof(resp) - 1, &cl)) resp[cl < sizeof(resp) ? cl : sizeof(resp) - 1] = 0; + logf_("http resp=%.300s", resp); + if (strstr(resp, "\"ok\":true")) ok = 1; + json_str(resp, "msg", msg, cap); + if (!ok && !msg[0]) snprintf(msg, cap, "\xE9\xAA\x8C\xE8\xAF\x81\xE5\xA4\xB1\xE8\xB4\xA5"); + } + if (hR) WinHttpCloseHandle(hR); + if (hC) WinHttpCloseHandle(hC); + WinHttpCloseHandle(hS); + return ok; +} + +/* exe 形态:解密内嵌载荷并启动 */ +static int launch_payload(void) { + unsigned char *plain = NULL; DWORD plainLen = 0; + if (!aes_decrypt(g_payload, g_payloadSize, &plain, &plainLen)) return 0; + wchar_t tmp[MAX_PATH]; GetTempPathW(MAX_PATH, tmp); + wchar_t dir[MAX_PATH], out[MAX_PATH]; + swprintf(dir, MAX_PATH, L"%lsvfy_demo_%lu", tmp, (unsigned long)GetCurrentProcessId()); + CreateDirectoryW(dir, NULL); + swprintf(out, MAX_PATH, L"%ls\\app.exe", dir); + HANDLE h = CreateFileW(out, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); + if (h == INVALID_HANDLE_VALUE) { free(plain); return 0; } + DWORD w = 0; WriteFile(h, plain, plainLen, &w, NULL); CloseHandle(h); + free(plain); + STARTUPINFOW si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); + PROCESS_INFORMATION pi; + if (!CreateProcessW(out, NULL, NULL, NULL, FALSE, 0, NULL, dir, &si, &pi)) return 0; + logf_("child pid=%lu", (unsigned long)pi.dwProcessId); + CloseHandle(pi.hThread); CloseHandle(pi.hProcess); + return 1; +} + +static void set_status(int kind, const wchar_t *s) { + g_statusKind = kind; + wcsncpy(g_status, s, 255); g_status[255] = 0; + InvalidateRect(g_hwnd, NULL, FALSE); +} + +static void set_status_u8(int kind, const char *u8) { + wchar_t w[256] = {0}; + MultiByteToWideChar(CP_UTF8, 0, u8, -1, w, 256); + set_status(kind, w); +} + +/* 验证通过后的动作 */ +typedef void (*PFN_OEP)(void); +static PFN_OEP g_oep = NULL; + +static void on_success(void) { + if (g_oep) { + logf_("calling original entry %p", (void *)g_oep); + g_oep(); /* 原程序自己的入口,正常会自己退出 */ + ExitProcess(0); + } + if (launch_payload()) ExitProcess(0); + set_status(2, L"\x542F\x52A8\x5931\x8D25"); +} + +static DWORD WINAPI login_thread(LPVOID) { + char user[128] = {0}, pass[128] = {0}, msg[256] = {0}; + GetWindowTextA(g_hUser, user, sizeof(user)); + GetWindowTextA(g_hPass, pass, sizeof(pass)); + if (!user[0]) { set_status(2, L"\x8BF7\x8F93\x5165\x8D26\x53F7"); g_busy = 0; return 0; } + set_status_u8(0, "\xE9\xAA\x8C\xE8\xAF\x81\xE4\xB8\xAD"); + int passed = http_login(user, pass, msg, sizeof(msg)); + if (!passed && g_offline[0] && strcmp(pass, g_offline) == 0) { logf_("offline mode"); passed = 1; } + if (passed) { + set_status_u8(1, "\xE9\xAA\x8C\xE8\xAF\x81\xE9\x80\x9A\xE8\xBF\x87"); + Sleep(180); + g_done = 1; + on_success(); + return 0; + } + if (msg[0]) set_status_u8(2, msg); + else set_status(2, L"\x9A8C\x8BC1\x5931\x8D25"); + g_busy = 0; + return 0; +} + +static void do_login(void) { + if (InterlockedExchange(&g_busy, 1) != 0) return; + InvalidateRect(g_hwnd, NULL, FALSE); + CreateThread(NULL, 0, login_thread, NULL, 0, NULL); +} + +static int in_rect(const RECT &r, int x, int y) { return x >= r.left && x < r.right && y >= r.top && y < r.bottom; } + +static LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l) { + switch (m) { + case WM_CREATE: + g_hwnd = h; + g_hUser = CreateWindowExW(0, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, + (int)L(32), (int)L(125), (int)L(356), (int)L(30), h, (HMENU)201, NULL, NULL); + g_hPass = CreateWindowExW(0, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_PASSWORD | ES_AUTOHSCROLL, + (int)L(32), (int)L(187), (int)L(324), (int)L(30), h, (HMENU)202, NULL, NULL); + SendMessage(g_hUser, WM_SETFONT, (WPARAM)g_fontEdit, TRUE); + SendMessage(g_hPass, WM_SETFONT, (WPARAM)g_fontEdit, TRUE); + if (g_auto[0]) { + SetWindowTextA(g_hUser, "demo"); + SetWindowTextA(g_hPass, g_auto); + SetTimer(h, 1, 500, NULL); + } + SetTimer(h, 2, 200, NULL); + return 0; + case WM_TIMER: + if (w == 1) { KillTimer(h, 1); do_login(); } + if (w == 2) { g_tick++; if (g_busy) InvalidateRect(h, NULL, FALSE); } + return 0; + case WM_CTLCOLOREDIT: { + HDC dc = (HDC)w; + SetTextColor(dc, RGB(230, 230, 230)); + SetBkColor(dc, RGB(37, 37, 38)); + return (LRESULT)g_brInput; + } + case WM_ERASEBKGND: + return 1; + case WM_PAINT: { + PAINTSTRUCT ps; BeginPaint(h, &ps); + render(); + EndPaint(h, &ps); + return 0; + } + case WM_NCHITTEST: { + POINT pt = {GET_X_LPARAM(l), GET_Y_LPARAM(l)}; + ScreenToClient(h, &pt); + int lx = (int)(pt.x / g_S) - (int)PAD, ly = (int)(pt.y / g_S) - (int)PAD; + if (lx >= 0 && lx < (int)CARD_W && ly >= 0 && ly < 32 && + !in_rect(LR_CLOSE, lx, ly) && !in_rect(LR_MIN, lx, ly)) + return HTCAPTION; + if (!(lx >= 0 && lx < (int)CARD_W && ly >= 0 && ly < (int)CARD_H)) + return HTCAPTION; + break; + } + case WM_MOUSEMOVE: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + int nb = in_rect(LR_BTN, lx, ly), nc = in_rect(LR_REM, lx, ly), nt = in_rect(LR_TAG, lx, ly); + int ne = in_rect(LR_EYE, lx, ly), nx = in_rect(LR_CLOSE, lx, ly), nm = in_rect(LR_MIN, lx, ly); + int nl = in_rect(LR_L1, lx, ly) ? 1 : (in_rect(LR_L2, lx, ly) ? 2 : (in_rect(LR_L3, lx, ly) ? 3 : 0)); + if (nb != g_hBtn || nc != g_hChk || nt != g_hTag || ne != g_hEye || nx != g_hClose || nm != g_hMin || nl != g_hLink) { + g_hBtn = nb; g_hChk = nc; g_hTag = nt; g_hEye = ne; g_hClose = nx; g_hMin = nm; g_hLink = nl; + InvalidateRect(h, NULL, FALSE); + } + TRACKMOUSEEVENT tme = {sizeof(tme), TME_LEAVE, h, 0}; + TrackMouseEvent(&tme); + return 0; + } + case WM_MOUSELEAVE: + g_hBtn = g_hChk = g_hTag = g_hEye = g_hClose = g_hMin = g_hLink = 0; + InvalidateRect(h, NULL, FALSE); + return 0; + case WM_SETCURSOR: { + POINT pt; GetCursorPos(&pt); ScreenToClient(h, &pt); + int lx = (int)(pt.x / g_S) - (int)PAD, ly = (int)(pt.y / g_S) - (int)PAD; + if (in_rect(LR_BTN, lx, ly) || in_rect(LR_REM, lx, ly) || in_rect(LR_TAG, lx, ly) || + in_rect(LR_EYE, lx, ly) || in_rect(LR_CLOSE, lx, ly) || in_rect(LR_MIN, lx, ly) || + in_rect(LR_L1, lx, ly) || in_rect(LR_L2, lx, ly) || in_rect(LR_L3, lx, ly)) { + SetCursor(LoadCursor(NULL, IDC_HAND)); + return TRUE; + } + break; + } + case WM_LBUTTONDOWN: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + if (in_rect(LR_CLOSE, lx, ly)) { DestroyWindow(h); return 0; } + if (in_rect(LR_MIN, lx, ly)) { ShowWindow(h, SW_MINIMIZE); return 0; } + if (in_rect(LR_BTN, lx, ly)) { g_pressBtn = 1; InvalidateRect(h, NULL, FALSE); return 0; } + if (in_rect(LR_REM, lx, ly)) { g_checked = !g_checked; InvalidateRect(h, NULL, FALSE); return 0; } + if (in_rect(LR_EYE, lx, ly)) { + g_showPass = !g_showPass; + SendMessage(g_hPass, EM_SETPASSWORDCHAR, g_showPass ? 0 : 0x25CF, 0); + InvalidateRect(h, NULL, FALSE); + return 0; + } + if (in_rect(LR_TAG, lx, ly)) { + if (OpenClipboard(h)) { + EmptyClipboard(); + int n = MultiByteToWideChar(CP_UTF8, 0, g_device, -1, NULL, 0); + HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, n * 2); + if (hg) { + wchar_t *d = (wchar_t *)GlobalLock(hg); + MultiByteToWideChar(CP_UTF8, 0, g_device, -1, d, n); + GlobalUnlock(hg); + SetClipboardData(CF_UNICODETEXT, hg); + } + CloseClipboard(); + } + set_status(1, L"\x8BBE\x5907\x7801\x5DF2\x590D\x5236"); + return 0; + } + if (in_rect(LR_L1, lx, ly)) { set_status(0, L"\x6CE8\x518C\xFF1A\x6253\x5F00\x5B98\x7F51"); return 0; } + if (in_rect(LR_L2, lx, ly)) { set_status(0, L"\x5361\x5BC6\x5145\x503C\xFF1A\x5F39\x51FA\x5145\x503C\x7A97"); return 0; } + if (in_rect(LR_L3, lx, ly)) { set_status(0, L"\x5FD8\x8BB0\x5BC6\x7801\xFF1A\x8BF7\x8054\x7CFB\x5BA2\x670D"); return 0; } + break; + } + case WM_LBUTTONUP: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + if (g_pressBtn) { + g_pressBtn = 0; + InvalidateRect(h, NULL, FALSE); + if (in_rect(LR_BTN, lx, ly)) do_login(); + return 0; + } + break; + } + case WM_COMMAND: + if (HIWORD(w) == EN_SETFOCUS) { g_focus = (LOWORD(w) == 201) ? 1 : 2; InvalidateRect(h, NULL, FALSE); } + else if (HIWORD(w) == EN_KILLFOCUS) { g_focus = 0; InvalidateRect(h, NULL, FALSE); } + else if (HIWORD(w) == EN_CHANGE) InvalidateRect(h, NULL, FALSE); + break; + case WM_CLOSE: + DestroyWindow(h); + return 0; + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProcW(h, m, w, l); +} + +/* ---------- 启动壳(DLL 与 exe 共用) ---------- */ +static const wchar_t *CLS_NAME = L"VfyShellV5"; + +static void make_colors(void) { + C_CARD = D2D1::ColorF(0.118f, 0.118f, 0.118f, 1); /* #1E1E1E */ + C_BAR = D2D1::ColorF(0.145f, 0.145f, 0.153f, 1); /* #252527 */ + C_BORDER = D2D1::ColorF(0.247f, 0.247f, 0.275f, 1); /* #3F3F46 */ + C_INPUT = D2D1::ColorF(0.145f, 0.145f, 0.149f, 1); /* #252526 */ + C_INPUT_BD = D2D1::ColorF(0.247f, 0.247f, 0.275f, 1); + C_FOCUS = D2D1::ColorF(0.0f, 0.471f, 0.831f, 1); /* #0078D4 */ + C_TXT = D2D1::ColorF(0.902f, 0.902f, 0.902f, 1); /* #E6E6E6 */ + C_TXT2 = D2D1::ColorF(0.616f, 0.616f, 0.616f, 1); /* #9D9D9D */ + C_ACCENT = D2D1::ColorF(0.055f, 0.388f, 0.612f, 1); /* #0E639C */ + C_ACCENT_H = D2D1::ColorF(0.067f, 0.467f, 0.733f, 1); /* #1177BB */ + C_ACCENT_P = D2D1::ColorF(0.039f, 0.306f, 0.478f, 1); /* #0A4E7A */ + C_LINK = D2D1::ColorF(0.216f, 0.58f, 1.0f, 1); /* #3794FF */ + C_RED = D2D1::ColorF(0.96f, 0.35f, 0.35f, 1); + C_GREEN = D2D1::ColorF(0.35f, 0.78f, 0.45f, 1); + C_WHITE = D2D1::ColorF(1, 1, 1, 1); + C_TAG = D2D1::ColorF(1, 1, 1, 0.05f); +} + +static int shell_main(PFN_OEP oep) { + g_oep = oep; + typedef BOOL(WINAPI * PFN_SetCtx)(HANDLE); + PFN_SetCtx setCtx = (PFN_SetCtx)GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetProcessDpiAwarenessContext"); + if (setCtx) setCtx((HANDLE)-4); + HDC sdc = GetDC(NULL); + g_S = GetDeviceCaps(sdc, LOGPIXELSX) / 96.0f; + ReleaseDC(NULL, sdc); + if (g_S < 1.0f) g_S = 1.0f; + + make_colors(); + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_d2d))) return -1; + if (FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown **)&g_dw))) return -1; + + g_fontEdit = CreateFontW((int)-L(13), 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, 0, 0, CLEARTYPE_QUALITY, 0, L"Microsoft YaHei UI"); + g_brInput = CreateSolidBrush(RGB(37, 37, 38)); + + WNDCLASSEXW wc; ZeroMemory(&wc, sizeof(wc)); + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = WndProc; + wc.hInstance = GetModuleHandleW(NULL); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.lpszClassName = CLS_NAME; + wc.hbrBackground = NULL; + RegisterClassExW(&wc); + + int w = (int)((CARD_W + PAD * 2) * g_S), h = (int)((CARD_H + PAD * 2) * g_S); + HWND hwnd = CreateWindowExW(WS_EX_LAYERED | WS_EX_APPWINDOW | WS_EX_TOPMOST, CLS_NAME, L"\x9A8C\x8BC1\x767B\x5F55", + WS_POPUP, 0, 0, w, h, NULL, NULL, wc.hInstance, NULL); + if (!hwnd) return -1; + SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, w, h, SWP_NOMOVE | SWP_NOACTIVATE); + + D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties( + D2D1_RENDER_TARGET_TYPE_DEFAULT, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), + 0, 0, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_DEFAULT); + if (FAILED(g_d2d->CreateDCRenderTarget(&props, &g_rt))) return -1; + + detect_fonts(); + + RECT wa; SystemParametersInfoW(SPI_GETWORKAREA, 0, &wa, 0); + int sx = wa.left + ((wa.right - wa.left) - w) / 2; + int sy = wa.top + ((wa.bottom - wa.top) - h) / 2; + SetWindowPos(hwnd, HWND_TOPMOST, sx, sy, w, h, SWP_NOACTIVATE); + render(); + ShowWindow(hwnd, SW_SHOW); + SetForegroundWindow(hwnd); + + MSG msg; + while (GetMessageW(&msg, NULL, 0, 0)) { + if (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN) { do_login(); continue; } + if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE) { ShowWindow(hwnd, SW_MINIMIZE); continue; } + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + return g_done ? 0 : -2; +} + +/* ================= 导出(DLL 形态) ================= */ +#ifdef SHELL_DLL +extern "C" __declspec(dllexport) int VerifyMain(void *oep, const char *cfgjson) { + load_cfg(cfgjson); + return shell_main((PFN_OEP)oep); +} +extern "C" __declspec(dllexport) int VerifyShellVersion(void) { return 5; } +BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID) { + if (reason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(h); + return TRUE; +} +#else +/* ================= 独立 exe 形态 ================= */ +static int read_self(void) { + wchar_t path[MAX_PATH]; DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH); + if (!n) return 0; + HANDLE h = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + if (h == INVALID_HANDLE_VALUE) return 0; + LARGE_INTEGER sz; GetFileSizeEx(h, &sz); + g_fileSize = (DWORD)sz.QuadPart; + g_file = (unsigned char *)malloc(g_fileSize); + DWORD got = 0; + BOOL ok = ReadFile(h, g_file, g_fileSize, &got, NULL); + CloseHandle(h); + return ok && got == g_fileSize; +} + +static unsigned int le32(const unsigned char *p) { + return (unsigned int)p[0] | ((unsigned int)p[1] << 8) | ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24); +} + +static int parse_tail(void) { + if (g_fileSize < 64) return 0; + const unsigned char *t = g_file + g_fileSize - 64; + if (memcmp(t, TAIL_MAGIC, 8) != 0) return 0; + unsigned int poff = le32(t + 8), psize = le32(t + 12), coff = le32(t + 16), csize = le32(t + 20); + memcpy(g_nonce, t + 24, 12); + memcpy(g_tag, t + 36, 16); + if (poff + psize > g_fileSize) return 0; + g_payload = g_file + poff; g_payloadSize = psize; + DWORD cs = csize < sizeof(g_cfg) - 1 ? csize : sizeof(g_cfg) - 1; + if (coff + cs > g_fileSize) return 0; + char buf[4096] = {0}; + memcpy(buf, g_file + coff, cs); + buf[cs] = 0; + load_cfg(buf); + return 1; +} + +int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) { + if (!read_self() || !parse_tail()) { + MessageBoxW(NULL, L"\x7A0B\x5E8F\x5DF2\x635F\x574F", L"\x9519\x8BEF", MB_ICONERROR); + return 1; + } + int argc = 0; + LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); + for (int i = 1; i + 1 < argc; i++) + if (!wcscmp(argv[i], L"--auto")) + WideCharToMultiByte(CP_UTF8, 0, argv[i + 1], -1, g_auto, sizeof(g_auto), NULL, NULL); + int r = shell_main(NULL); + return r == 0 ? 0 : 1; +} +#endif diff --git a/shell/classic/shell6.cpp b/shell/classic/shell6.cpp new file mode 100644 index 0000000..8d1d9d7 --- /dev/null +++ b/shell/classic/shell6.cpp @@ -0,0 +1,1050 @@ +// vfy shell v6 —— VS 2026 暗黑 / 3× 超采样抗锯齿 / QQ 式设备管理踢线 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CARD_W 420.0f +#define CARD_H 404.0f +#define PAD 20.0f +#define SS 3 +#define MAXDEV 6 + +static float g_S = 1.0f; // DPI 缩放 +static float g_SSf = (float)SS; // 超采样倍数(字体探测时临时设为 1) +static inline float L(float v) { return v * g_S * g_SSf; } +static inline float L1(float v) { return v * g_S; } + +static D2D1_COLOR_F C_CARD, C_BAR, C_BORDER, C_INPUT, C_INPUT_BD, C_FOCUS, C_TXT, C_TXT2, + C_ACCENT, C_ACCENT_H, C_ACCENT_P, C_LINK, C_RED, C_GREEN, C_WHITE, C_TAG, C_ROW, C_ROW_H; + +static ID2D1Factory *g_d2d = NULL; +static IDWriteFactory *g_dw = NULL; +static ID2D1DCRenderTarget *g_rt = NULL; +static const wchar_t *g_famCJK = L"Microsoft YaHei UI"; +static const wchar_t *g_famLat = L"Segoe UI"; + +static HWND g_hwnd, g_hUser, g_hPass; +static HFONT g_fontEdit = NULL; +static HBRUSH g_brInput = NULL; +static int g_focus = 0; +static int g_hBtn, g_hChk, g_hTag, g_hEye, g_hClose, g_hMin, g_hLink, g_hRow, g_hKick, g_hBack; +static int g_pressBtn, g_checked, g_showPass; +static volatile LONG g_busy = 0; +static int g_statusKind = 0; +static wchar_t g_status[256] = L"\x5C31\x7EEA"; +static int g_tick = 0; +static int g_done = 0; +static int g_screen = 0; // 0=登录 1=设备管理 + +/* 设备列表 */ +struct DevInfo { char id[48], name[96], last[40]; int cur; }; +static DevInfo g_devs[MAXDEV]; +static int g_devCount = 0, g_devLimit = 1; +static char g_kickPending[48] = {0}; +static int g_autoKick = 0; + +static const RECT LR_CLOSE = {420 - 44, 0, 420, 32}; +static const RECT LR_MIN = {420 - 88, 0, 420 - 44, 32}; +static const RECT LR_USER = {20, 124, 400, 156}; +static const RECT LR_PASS = {20, 186, 400, 218}; +static const RECT LR_EYE = {368, 190, 392, 214}; +static const RECT LR_CHK = {20, 233, 34, 247}; +static const RECT LR_REM = {20, 228, 150, 252}; +static const RECT LR_TAG = {244, 228, 400, 252}; +static const RECT LR_BTN = {20, 264, 400, 298}; +static const RECT LR_L1 = {20, 316, 116, 338}; +static const RECT LR_L2 = {128, 316, 224, 338}; +static const RECT LR_L3 = {236, 316, 332, 338}; +static const RECT LR_BACK = {20, 330, 190, 352}; + +/* 载荷(独立 exe 形态) */ +static unsigned char *g_file = NULL; +static DWORD g_fileSize = 0; +static const unsigned char *g_payload = NULL; +static DWORD g_payloadSize = 0; +static unsigned char g_nonce[12], g_tag[16]; +static char g_cfg[4096], g_api[512]; +static char g_name[128] = "\xE6\xBC\x94\xE7\xA4\xBA\xE7\xA8\x8B\xE5\xBA\x8F"; +static char g_device[64] = "A1B2-3C4D"; +static char g_auto[128], g_offline[64]; +static const char TAIL_MAGIC[8] = {'V', 'F', 'Y', 'D', '1', 0, 0, 0}; + +static void logf_(const char *fmt, ...) { + char tmp[MAX_PATH] = {0}; GetTempPathA(MAX_PATH, tmp); + char path[MAX_PATH]; snprintf(path, sizeof(path), "%svfy_debug.log", tmp); + FILE *f = fopen(path, "a"); + if (!f) return; + va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); + fputc('\n', f); fclose(f); +} + +static int json_str(const char *src, const char *key, char *out, int cap) { + char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key); + const char *p = strstr(src, pat); + if (!p) return 0; + p = strchr(p + strlen(pat), ':'); + if (!p) return 0; + p = strchr(p, '"'); + if (!p) return 0; + p++; + int i = 0; + while (*p && *p != '"' && i < cap - 1) out[i++] = *p++; + out[i] = 0; + return 1; +} + +static int json_int(const char *src, const char *key, int defv) { + char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key); + const char *p = strstr(src, pat); + if (!p) return defv; + p = strchr(p + strlen(pat), ':'); + if (!p) return defv; + p++; + while (*p == ' ' || *p == '"') p++; + return atoi(p); +} + +static void load_cfg(const char *json) { + snprintf(g_cfg, sizeof(g_cfg), "%s", json ? json : ""); + json_str(g_cfg, "api", g_api, sizeof(g_api)); + json_str(g_cfg, "name", g_name, sizeof(g_name)); + json_str(g_cfg, "device", g_device, sizeof(g_device)); + json_str(g_cfg, "offline_pass", g_offline, sizeof(g_offline)); + json_str(g_cfg, "auto_pass", g_auto, sizeof(g_auto)); + g_autoKick = json_int(g_cfg, "auto_kick", 0); +} + +/* ---------- 文本(CJK/拉丁分段) ---------- */ +static int is_cjk(wchar_t c) { + return (c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xF900 && c <= 0xFAFF) || + (c >= 0xFF00 && c <= 0xFF65) || (c >= 0x3000 && c <= 0x303F); +} + +static void draw_run(const wchar_t *s, int n, const wchar_t *fam, float x, float y, float h, + float px, D2D1_COLOR_F c, int bold) { + IDWriteTextFormat *f = NULL; + if (FAILED(g_dw->CreateTextFormat(fam, NULL, bold ? DWRITE_FONT_WEIGHT_SEMI_BOLD : DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, L(px), L"zh-cn", &f))) + return; + f->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); + f->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); + f->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); + wchar_t sub[512]; + int k = n > 511 ? 511 : n; + for (int i = 0; i < k; i++) sub[i] = (s[i] == 0x20) ? (wchar_t)0x00A0 : s[i]; + sub[k] = 0; + IDWriteTextLayout *lay = NULL; + if (FAILED(g_dw->CreateTextLayout(sub, (UINT32)k, f, 4000.0f, L(h), &lay))) { f->Release(); return; } + ID2D1SolidColorBrush *b = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(c, &b))) { + g_rt->DrawTextLayout(D2D1::Point2F(L(x), L(y)), lay, b, D2D1_DRAW_TEXT_OPTIONS_NONE); + b->Release(); + } + lay->Release(); + f->Release(); +} + +static float run_width(const wchar_t *s, int n, const wchar_t *fam, float px, int bold) { + IDWriteTextFormat *f = NULL; + if (FAILED(g_dw->CreateTextFormat(fam, NULL, bold ? DWRITE_FONT_WEIGHT_SEMI_BOLD : DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, L(px), L"zh-cn", &f))) + return 0; + f->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); + IDWriteTextLayout *lay = NULL; + float w = 0; + if (SUCCEEDED(g_dw->CreateTextLayout(s, (UINT32)n, f, L(4000), L(px * 2), &lay))) { + DWRITE_TEXT_METRICS m; + if (SUCCEEDED(lay->GetMetrics(&m)) && m.width > 0.5f) w = m.width / (g_S * g_SSf) - 0.6f; + lay->Release(); + } + if (w <= 0.5f) + for (int i = 0; i < n; i++) + w += is_cjk(s[i]) ? px : (s[i] == 0x20 ? px * 0.28f : px * 0.56f); + f->Release(); + return w; +} + +static void textL(const wchar_t *s, float x, float y, float w, float h, float px, + D2D1_COLOR_F c, int bold, int align) { + int len = (int)wcslen(s); + if (!len) return; + int starts[64], lens[64], fams[64], cnt = 0, i = 0; + while (i < len && cnt < 64) { + int cjk = is_cjk(s[i]); + int j = i; + while (j < len && is_cjk(s[j]) == cjk) j++; + starts[cnt] = i; lens[cnt] = j - i; fams[cnt] = cjk; cnt++; + i = j; + } + float total = 0; + for (int k = 0; k < cnt; k++) total += run_width(s + starts[k], lens[k], fams[k] ? g_famCJK : g_famLat, px, bold); + float ox = x; + if (align == 1) ox = x + (w - total) * 0.5f; + else if (align == 2) ox = x + w - total; + for (int k = 0; k < cnt; k++) { + const wchar_t *fam = fams[k] ? g_famCJK : g_famLat; + draw_run(s + starts[k], lens[k], fam, ox, y, h, px, c, bold); + ox += run_width(s + starts[k], lens[k], fam, px, bold); + } +} + +static void fillRound(float x, float y, float w, float h, float r, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->FillRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), L(r), L(r)), b); + b->Release(); +} + +static void strokeRound(float x, float y, float w, float h, float r, float wd, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->DrawRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), L(r), L(r)), b, L(wd)); + b->Release(); +} + +static void fillRect(float x, float y, float w, float h, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->FillRectangle(D2D1::RectF(L(x), L(y), L(x + w), L(y + h)), b); + b->Release(); +} + +static void line(float x1, float y1, float x2, float y2, float wd, D2D1_COLOR_F c) { + ID2D1SolidColorBrush *b = NULL; + if (FAILED(g_rt->CreateSolidColorBrush(c, &b))) return; + g_rt->DrawLine(D2D1::Point2F(L(x1), L(y1)), D2D1::Point2F(L(x2), L(y2)), b, L(wd)); + b->Release(); +} + +static D2D1_COLOR_F status_color(void) { + return g_statusKind == 1 ? C_GREEN : (g_statusKind == 2 ? C_RED : C_TXT2); +} + +/* 行矩形 */ +static RECT rowRect(int i) { RECT r = {20, 100 + i * 56, 400, 100 + i * 56 + 52}; return r; } +static RECT kickRect(int i) { RECT r = {308, 100 + i * 56 + 13, 392, 100 + i * 56 + 39}; return r; } + +/* ---------- 画面 ---------- */ +static void paint_chrome(void) { + for (int i = 14; i >= 1; i--) + fillRound(PAD - i, PAD - i + 2 + i / 8.0f, CARD_W + i * 2, CARD_H + i * 2, 6 + i, + D2D1::ColorF(0, 0, 0, 0.02f + (15 - i) * 0.006f)); + fillRound(PAD, PAD, CARD_W, CARD_H, 6, C_CARD); + strokeRound(PAD, PAD, CARD_W, CARD_H, 6, 1.0f, C_BORDER); + + g_rt->SetTransform(D2D1::Matrix3x2F::Translation(L(PAD), L(PAD))); + fillRound(0, 0, CARD_W, 32 + 6, 6, C_BAR); + fillRect(0, 32, CARD_W, 6, C_CARD); + fillRect(0, 31.6f, CARD_W, 0.6f, C_BORDER); + + if (g_hClose) fillRect(LR_CLOSE.left, 0, 44, 32, D2D1::ColorF(0.91f, 0.07f, 0.14f, 1)); + else if (g_hMin) fillRect(LR_MIN.left, 0, 44, 32, D2D1::ColorF(1, 1, 1, 0.07f)); + { + float cx = (LR_CLOSE.left + LR_CLOSE.right) / 2.0f, cy = 16; + line(cx - 4.5f, cy - 4.5f, cx + 4.5f, cy + 4.5f, 1.0f, C_TXT); + line(cx + 4.5f, cy - 4.5f, cx - 4.5f, cy + 4.5f, 1.0f, C_TXT); + float mx = (LR_MIN.left + LR_MIN.right) / 2.0f; + line(mx - 4.5f, cy, mx + 4.5f, cy, 1.0f, C_TXT); + } + fillRound(12, 8, 16, 16, 3, C_ACCENT); + { + wchar_t t[256] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_name, -1, t, 256); + textL(t, 36, 0, 260, 32, 12, C_TXT, 0, 0); + } +} + +static void paint_footer(void) { + fillRound(20, 356, 5, 5, 2.5f, status_color()); + textL(g_status, 32, 348, 340, 20, 11, status_color(), 0, 0); + line(20, 374, 400, 374, 0.6f, C_BORDER); + if (g_screen == 0) + textL(L"v1.0.0 \xB7 \x53EF\x4FE1\x8BBE\x5907\x7ED1\x5B9A \xB7 \x5F02\x5E38\x767B\x5F55\x81EA\x52A8\x9501\x5B9A", + 20, 380, 380, 16, 10, D2D1::ColorF(0.55f, 0.55f, 0.58f, 1), 0, 0); + else + textL(L"\x8D85\x51FA\x4E0A\x9650\x65F6\x8BF7\x4E0B\x7EBF\x4E00\x53F0\x8BBE\x5907\xFF0C\x5426\x5219\x65E0\x6CD5\x767B\x5F55", + 20, 380, 380, 16, 10, D2D1::ColorF(0.55f, 0.55f, 0.58f, 1), 0, 0); +} + +static void paint_login(void) { + textL(L"\x767B\x5F55\x4EE5\x7EE7\x7EED", 20, 46, 300, 26, 15, C_TXT, 1, 0); + textL(L"\x8BF7\x4F7F\x7528\x8D26\x53F7\x6216\x5361\x5BC6\x767B\x5F55", 20, 72, 320, 16, 11, C_TXT2, 0, 0); + + textL(L"\x8D26\x53F7", 20, 102, 100, 16, 11, C_TXT2, 0, 0); + fillRound(20, 124, 380, 32, 3, C_INPUT); + strokeRound(20, 124, 380, 32, 3, 1.0f, g_focus == 1 ? C_FOCUS : C_INPUT_BD); + if (GetWindowTextLengthW(g_hUser) == 0 && g_focus != 1) + textL(L"\x8BF7\x8F93\x5165\x8D26\x53F7", 32, 124, 340, 32, 12, D2D1::ColorF(0.52f, 0.52f, 0.55f, 1), 0, 0); + + textL(L"\x5BC6\x7801", 20, 164, 100, 16, 11, C_TXT2, 0, 0); + fillRound(20, 186, 380, 32, 3, C_INPUT); + strokeRound(20, 186, 380, 32, 3, 1.0f, g_focus == 2 ? C_FOCUS : C_INPUT_BD); + if (GetWindowTextLengthW(g_hPass) == 0 && g_focus != 2) + textL(L"\x8BF7\x8F93\x5165\x5BC6\x7801", 32, 186, 320, 32, 12, D2D1::ColorF(0.52f, 0.52f, 0.55f, 1), 0, 0); + { + D2D1_COLOR_F ec = g_hEye ? C_FOCUS : D2D1::ColorF(0.6f, 0.6f, 0.63f, 1); + ID2D1SolidColorBrush *b = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(ec, &b))) { + g_rt->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(L(376), L(202)), L(8.5f), L(5.5f)), b, L(1.0f)); + if (g_showPass) line(368, 210, 384, 194, 1.0f, ec); + else { + ID2D1SolidColorBrush *pb = NULL; + if (SUCCEEDED(g_rt->CreateSolidColorBrush(ec, &pb))) { + g_rt->FillEllipse(D2D1::Ellipse(D2D1::Point2F(L(376), L(202)), L(2.4f), L(2.4f)), pb); + pb->Release(); + } + } + b->Release(); + } + } + + fillRound(20, 233, 14, 14, 2, g_checked ? C_FOCUS : C_INPUT); + strokeRound(20, 233, 14, 14, 2, 1.0f, g_checked ? C_FOCUS : (g_hChk ? C_TXT2 : C_INPUT_BD)); + if (g_checked) { + line(23.2f, 240.2f, 25.6f, 242.8f, 1.4f, C_WHITE); + line(25.6f, 242.8f, 31.0f, 236.8f, 1.4f, C_WHITE); + } + textL(L"\x8BB0\x4F4F\x8D26\x53F7", 40, 233, 100, 14, 11, C_TXT2, 0, 0); + + fillRound(244, 228, 156, 24, 2, C_TAG); + strokeRound(244, 228, 156, 24, 2, 1.0f, C_INPUT_BD); + { + wchar_t tg[160], dev[64] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_device, -1, dev, 64); + swprintf(tg, 160, L"\x8BBE\x5907 %ls", dev); + textL(tg, 244, 228, 156, 24, 10.5f, C_TXT2, 0, 1); + } + + if (g_busy) { + fillRound(20, 264, 380, 34, 3, D2D1::ColorF(0.22f, 0.22f, 0.24f, 1)); + wchar_t d[16] = L"\x9A8C\x8BC1\x4E2D"; + int k = g_tick % 4; + for (int i = 0; i < k; i++) d[3 + i] = L'.'; + d[3 + k] = 0; + textL(d, 20, 264, 380, 34, 12.5f, D2D1::ColorF(0.62f, 0.62f, 0.65f, 1), 0, 1); + } else { + fillRound(20, 264, 380, 34, 3, g_pressBtn ? C_ACCENT_P : (g_hBtn ? C_ACCENT_H : C_ACCENT)); + textL(L"\x767B\x5F55", 20, 264, 380, 34, 12.5f, C_WHITE, 0, 1); + } + + textL(L"\x6CE8\x518C\x8D26\x53F7", 20, 316, 96, 22, 11, g_hLink == 1 ? C_LINK : C_TXT2, 0, 0); + textL(L"\x5361\x5BC6\x5145\x503C", 128, 316, 96, 22, 11, g_hLink == 2 ? C_LINK : C_TXT2, 0, 0); + textL(L"\x5FD8\x8BB0\x5BC6\x7801", 236, 316, 96, 22, 11, g_hLink == 3 ? C_LINK : C_TXT2, 0, 0); +} + +static void paint_devices(void) { + textL(L"\x8BBE\x5907\x5DF2\x8FBE\x4E0A\x9650", 20, 46, 320, 26, 15, C_TXT, 1, 0); + { + wchar_t sub[160]; + swprintf(sub, 160, L"\x6700\x591A %d \x53F0\x8BBE\x5907\x540C\x65F6\x5728\x7EBF\xFF0C\x8BF7\x4E0B\x7EBF\x4E00\x53F0", g_devLimit); + textL(sub, 20, 72, 380, 16, 11, C_TXT2, 0, 0); + } + + for (int i = 0; i < g_devCount; i++) { + RECT r = rowRect(i); + float y = (float)r.top; + fillRound(20, y, 380, 52, 3, g_hRow == i ? C_ROW_H : C_ROW); + strokeRound(20, y, 380, 52, 3, 1.0f, C_INPUT_BD); + + wchar_t nm[128] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_devs[i].name, -1, nm, 128); + textL(nm, 34, y + 9, 260, 20, 11.5f, C_TXT, 0, 0); + + wchar_t lt[80] = {0}; + MultiByteToWideChar(CP_UTF8, 0, g_devs[i].last, -1, lt, 80); + textL(lt, 34, y + 28, 200, 16, 10, C_TXT2, 0, 0); + + if (g_devs[i].cur) { + fillRound(320, y + 15, 72, 22, 2, D2D1::ColorF(0.0f, 0.471f, 0.831f, 0.18f)); + textL(L"\x672C\x673A\x5728\x7EBF", 320, y + 15, 72, 22, 10, C_LINK, 0, 1); + } else { + int hov = (g_hKick == i); + fillRound(308, y + 13, 84, 26, 2, hov ? C_ACCENT_H : D2D1::ColorF(1, 1, 1, 0.06f)); + strokeRound(308, y + 13, 84, 26, 2, 1.0f, hov ? C_ACCENT_H : C_INPUT_BD); + textL(L"\x4E0B\x7EBF", 308, y + 13, 84, 26, 10.5f, C_TXT, 0, 1); + } + } + + textL(L"\x53D6\x6D88\xFF0C\x8FD4\x56DE\x767B\x5F55", 20, 330, 170, 22, 11, + g_hBack ? C_LINK : C_TXT2, 0, 0); +} + +static void paint(void) { + paint_chrome(); + if (g_screen == 0) paint_login(); + else paint_devices(); + paint_footer(); + g_rt->SetTransform(D2D1::Matrix3x2F::Identity()); +} + +/* ---------- 超采样渲染 + 手写降采样 ---------- */ +static HDC g_mem3 = NULL; +static HBITMAP g_dib3 = NULL, g_dib1 = NULL; +static HDC g_mem1 = NULL; +static void *g_bits3 = NULL, *g_bits1 = NULL; +static int g_curW = 0, g_curH = 0; + +static void free_surfaces(void) { + if (g_dib3) { DeleteObject(g_dib3); g_dib3 = NULL; g_bits3 = NULL; } + if (g_dib1) { DeleteObject(g_dib1); g_dib1 = NULL; g_bits1 = NULL; } + if (g_mem3) { DeleteDC(g_mem3); g_mem3 = NULL; } + if (g_mem1) { DeleteDC(g_mem1); g_mem1 = NULL; } + g_curW = g_curH = 0; +} + +static int ensure_surfaces(int w1, int h1, HDC screen) { + if (g_curW == w1 && g_curH == h1 && g_dib3 && g_dib1) return 1; + free_surfaces(); + BITMAPINFO bi; ZeroMemory(&bi, sizeof(bi)); + bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bi.bmiHeader.biPlanes = 1; + bi.bmiHeader.biBitCount = 32; + bi.bmiHeader.biCompression = BI_RGB; + bi.bmiHeader.biWidth = w1 * SS; + bi.bmiHeader.biHeight = -(h1 * SS); + g_dib3 = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &g_bits3, NULL, 0); + bi.bmiHeader.biWidth = w1; + bi.bmiHeader.biHeight = -h1; + g_dib1 = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &g_bits1, NULL, 0); + if (!g_dib3 || !g_dib1) { free_surfaces(); return 0; } + g_mem3 = CreateCompatibleDC(screen); + g_mem1 = CreateCompatibleDC(screen); + SelectObject(g_mem3, g_dib3); + SelectObject(g_mem1, g_dib1); + g_curW = w1; g_curH = h1; + return 1; +} + +static void render(void) { + if (!g_rt || !g_hwnd) return; + int w1 = (int)((CARD_W + PAD * 2) * g_S), h1 = (int)((CARD_H + PAD * 2) * g_S); + HDC screen = GetDC(NULL); + if (!ensure_surfaces(w1, h1, screen)) { ReleaseDC(NULL, screen); return; } + + /* 1) 3× 面渲染 */ + memset(g_bits3, 0, (size_t)w1 * h1 * SS * SS * 4); + RECT rc3 = {0, 0, w1 * SS, h1 * SS}; + g_rt->BindDC(g_mem3, &rc3); + g_rt->SetDpi(96.0f, 96.0f); + g_rt->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE); + g_rt->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE); + g_rt->BeginDraw(); + g_rt->Clear(D2D1::ColorF(0, 0, 0, 0)); + g_SSf = (float)SS; + paint(); + g_rt->EndDraw(); + + /* 2) 手写降采样(9 像素盒式平均,预乘 alpha 直接平均即可) */ + unsigned char *s3 = (unsigned char *)g_bits3; + unsigned char *d1 = (unsigned char *)g_bits1; + for (int y = 0; y < h1; y++) { + for (int x = 0; x < w1; x++) { + int sr = 0, sg = 0, sb = 0, sa = 0; + for (int dy = 0; dy < SS; dy++) { + unsigned char *row = s3 + ((size_t)(y * SS + dy) * (w1 * SS) + x * SS) * 4; + for (int dx = 0; dx < SS; dx++) { + unsigned char *p = row + dx * 4; + sb += p[0]; sg += p[1]; sr += p[2]; sa += p[3]; + } + } + unsigned char *o = d1 + ((size_t)y * w1 + x) * 4; + int n = SS * SS; + o[0] = (unsigned char)(sb / n); o[1] = (unsigned char)(sg / n); + o[2] = (unsigned char)(sr / n); o[3] = (unsigned char)(sa / n); + } + } + + /* 3) 提交 */ + RECT wr; GetWindowRect(g_hwnd, &wr); + POINT dst = {wr.left, wr.top}, src = {0, 0}; + SIZE sz = {w1, h1}; + BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA}; + UpdateLayeredWindow(g_hwnd, screen, &dst, &sz, g_mem1, &src, 0, &bf, ULW_ALPHA); + ReleaseDC(NULL, screen); +} + +/* ---------- 字体自检 ---------- */ +static int font_cov(const wchar_t *fam, const wchar_t *sample) { + const int W = 200, H = 40; + HDC screen = GetDC(NULL); + HDC mem = CreateCompatibleDC(screen); + BITMAPINFO bi; ZeroMemory(&bi, sizeof(bi)); + bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bi.bmiHeader.biWidth = W; bi.bmiHeader.biHeight = -H; + bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = BI_RGB; + void *bits = NULL; + HBITMAP dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &bits, NULL, 0); + if (!dib) { DeleteDC(mem); ReleaseDC(NULL, screen); return 0; } + HGDIOBJ old = SelectObject(mem, dib); + memset(bits, 0, W * H * 4); + RECT rc = {0, 0, W, H}; + g_rt->BindDC(mem, &rc); + g_rt->SetDpi(96.0f, 96.0f); + g_rt->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE); + float save = g_SSf; + g_SSf = 1.0f; + g_rt->BeginDraw(); + g_rt->Clear(D2D1::ColorF(0, 0, 0, 0)); + draw_run(sample, (int)wcslen(sample), fam, 4, 2, 30, 15, D2D1::ColorF(1, 1, 1, 1), 0); + g_rt->EndDraw(); + g_SSf = save; + int n = 0; + unsigned char *p = (unsigned char *)bits; + for (int i = 0; i < W * H; i++) if (p[i * 4 + 3] > 16) n++; + SelectObject(mem, old); + DeleteObject(dib); + DeleteDC(mem); + ReleaseDC(NULL, screen); + return n; +} + +static void detect_fonts(void) { + const wchar_t *cjk[] = {L"Microsoft YaHei UI", L"Microsoft YaHei", L"WenQuanYi Micro Hei", L"Noto Sans CJK SC", L"DejaVu Sans"}; + const wchar_t *lat[] = {L"Segoe UI", L"Microsoft YaHei UI", L"DejaVu Sans", L"Arial", L"Tahoma"}; + int best = 0; + for (int i = 0; i < 5; i++) { + int c = font_cov(cjk[i], L"\x8BBE\x5907\x8D26\x53F7"); + if (c > best) { best = c; g_famCJK = cjk[i]; } + } + best = 0; + for (int i = 0; i < 5; i++) { + int c = font_cov(lat[i], L"A1B2-3C4D v1.0.0"); + if (c > best) { best = c; g_famLat = lat[i]; } + } + logf_("fonts ok"); +} + +/* ---------- 网络 ---------- */ +static int api_call(const char *suffix, const char *body, char *resp, int cap) { + wchar_t wHost[256], wPath[512]; + char full[600]; + snprintf(full, sizeof(full), "%s", g_api); + char *p = strstr(full, "://"); + if (!p) return 0; + p += 3; + char *slash = strchr(p, '/'); + char dir[400] = {0}; + if (slash) { + snprintf(dir, sizeof(dir), "%s", slash); // 含前导 / + *slash = 0; + char *last = strrchr(dir, '/'); + if (last) *last = 0; + } + char host[256]; + snprintf(host, sizeof(host), "%s", p); + unsigned short port = 80; + char *colon = strrchr(host, ':'); + if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); } + char path[512]; + snprintf(path, sizeof(path), "%s/%s", dir[0] ? dir : "", suffix); + MultiByteToWideChar(CP_UTF8, 0, host, -1, wHost, 256); + MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, 512); + + resp[0] = 0; + HINTERNET hS = WinHttpOpen(L"vfy/6.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0); + if (!hS) return 0; + HINTERNET hC = WinHttpConnect(hS, wHost, port, 0); + HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath, NULL, NULL, NULL, 0) : NULL; + int ok = 0; + if (hR) { + const wchar_t *hdr = L"Content-Type: application/json\r\n"; + WinHttpSendRequest(hR, hdr, (DWORD)-1L, (LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0); + WinHttpReceiveResponse(hR, NULL); + DWORD cl = 0; + if (WinHttpReadData(hR, resp, cap - 1, &cl)) resp[cl < (DWORD)cap ? cl : (DWORD)cap - 1] = 0; + if (strstr(resp, "\"ok\":true")) ok = 1; + logf_("api %s -> %.240s", suffix, resp); + } + if (hR) WinHttpCloseHandle(hR); + if (hC) WinHttpCloseHandle(hC); + WinHttpCloseHandle(hS); + return ok; +} + +static void parse_devices(const char *resp) { + g_devCount = 0; + int lim = json_int(resp, "limit", 0); + if (lim > 0) g_devLimit = lim; + const char *p = strstr(resp, "\"devices\":["); + if (!p) return; + p += 11; + while (*p && g_devCount < MAXDEV) { + const char *ob = strchr(p, '{'); + if (!ob) break; + const char *oe = strchr(ob, '}'); + if (!oe) break; + char blk[640]; + int n = (int)(oe - ob) + 1; + if (n > 639) n = 639; + memcpy(blk, ob, n); + blk[n] = 0; + DevInfo &d = g_devs[g_devCount]; + ZeroMemory(&d, sizeof(d)); + json_str(blk, "id", d.id, sizeof(d.id)); + json_str(blk, "name", d.name, sizeof(d.name)); + json_str(blk, "last", d.last, sizeof(d.last)); + d.cur = json_int(blk, "cur", 0); + if (d.id[0]) g_devCount++; + p = oe + 1; + } + logf_("devices parsed: %d limit=%d", g_devCount, g_devLimit); +} + +/* ---------- 流程 ---------- */ +typedef void (*PFN_OEP)(void); +static PFN_OEP g_oep = NULL; + +static int launch_payload(void) { + unsigned char *plain = NULL; DWORD plainLen = 0; + static const char secret[] = "vfy-demo-secret-2026"; + BCRYPT_ALG_HANDLE hAlg = NULL; BCRYPT_HASH_HANDLE hHash = NULL; BCRYPT_KEY_HANDLE hKey = NULL; + unsigned char key[32]; + BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info; + unsigned char *buf = NULL; ULONG got = 0; NTSTATUS st = 0; + int okd = 0; + if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, NULL, 0) != 0) return 0; + if (BCryptCreateHash(hAlg, &hHash, NULL, 0, NULL, 0, 0) != 0) goto done; + BCryptHashData(hHash, (PUCHAR)secret, (ULONG)strlen(secret), 0); + BCryptFinishHash(hHash, key, 32, 0); + BCryptCloseAlgorithmProvider(hAlg, 0); hAlg = NULL; + if (BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0) != 0) goto done; + if (BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0) != 0) goto done; + if (BCryptGenerateSymmetricKey(hAlg, &hKey, NULL, 0, key, 32, 0) != 0) goto done; + BCRYPT_INIT_AUTH_MODE_INFO(info); + info.pbNonce = g_nonce; info.cbNonce = 12; info.pbTag = g_tag; info.cbTag = 16; + buf = (unsigned char *)malloc(g_payloadSize + 16); + st = BCryptDecrypt(hKey, (PUCHAR)g_payload, g_payloadSize, &info, NULL, 0, buf, g_payloadSize, &got, 0); + okd = (st == 0); +done: + if (hKey) BCryptDestroyKey(hKey); + if (hAlg) BCryptCloseAlgorithmProvider(hAlg, 0); + if (hHash) BCryptDestroyHash(hHash); + if (!okd) return 0; + plainLen = got; + + wchar_t tmp[MAX_PATH]; GetTempPathW(MAX_PATH, tmp); + wchar_t dir[MAX_PATH], out[MAX_PATH]; + swprintf(dir, MAX_PATH, L"%lsvfy_demo_%lu", tmp, (unsigned long)GetCurrentProcessId()); + CreateDirectoryW(dir, NULL); + swprintf(out, MAX_PATH, L"%ls\\app.exe", dir); + HANDLE h = CreateFileW(out, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); + if (h == INVALID_HANDLE_VALUE) { free(buf); return 0; } + DWORD w = 0; WriteFile(h, buf, plainLen, &w, NULL); CloseHandle(h); + free(buf); + STARTUPINFOW si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); + PROCESS_INFORMATION pi; + if (!CreateProcessW(out, NULL, NULL, NULL, FALSE, 0, NULL, dir, &si, &pi)) return 0; + CloseHandle(pi.hThread); CloseHandle(pi.hProcess); + return 1; +} + +static void set_status(int kind, const wchar_t *s) { + g_statusKind = kind; + wcsncpy(g_status, s, 255); g_status[255] = 0; + InvalidateRect(g_hwnd, NULL, FALSE); +} +static void set_status_u8(int kind, const char *u8) { + wchar_t w[256] = {0}; + MultiByteToWideChar(CP_UTF8, 0, u8, -1, w, 256); + set_status(kind, w); +} + +static void on_success(void) { + if (g_oep) { + logf_("calling original entry %p", (void *)g_oep); + g_oep(); + ExitProcess(0); + } + if (launch_payload()) ExitProcess(0); + set_status(2, L"\x542F\x52A8\x5931\x8D25"); +} + +static DWORD WINAPI login_thread(LPVOID); + +static void do_login(void) { + if (InterlockedExchange(&g_busy, 1) != 0) return; + InvalidateRect(g_hwnd, NULL, FALSE); + CreateThread(NULL, 0, login_thread, NULL, 0, NULL); +} + +static DWORD WINAPI kick_thread(LPVOID) { + char user[128] = {0}, body[512] = {0}, resp[2048] = {0}; + GetWindowTextA(g_hUser, user, sizeof(user)); + snprintf(body, sizeof(body), "{\"user\":\"%s\",\"device\":\"%s\",\"kick\":\"%s\"}", user, g_device, g_kickPending); + api_call("kick", body, resp, sizeof(resp)); + logf_("kicked %s", g_kickPending); + g_kickPending[0] = 0; + g_busy = 0; + do_login(); // 踢完自动重试登录 + return 0; +} + +static void do_kick(int idx) { + if (idx < 0 || idx >= g_devCount) return; + if (g_devs[idx].cur) return; + snprintf(g_kickPending, sizeof(g_kickPending), "%s", g_devs[idx].id); + if (InterlockedExchange(&g_busy, 1) != 0) return; + set_status(0, L"\x6B63\x5728\x4E0B\x7EBF\x8BE5\x8BBE\x5907"); + CreateThread(NULL, 0, kick_thread, NULL, 0, NULL); +} + +static DWORD WINAPI login_thread(LPVOID) { + char user[128] = {0}, pass[128] = {0}, msg[256] = {0}, body[512] = {0}, resp[2048] = {0}; + GetWindowTextA(g_hUser, user, sizeof(user)); + GetWindowTextA(g_hPass, pass, sizeof(pass)); + if (!user[0]) { set_status(2, L"\x8BF7\x8F93\x5165\x8D26\x53F7"); g_busy = 0; return 0; } + set_status_u8(0, "\xE9\xAA\x8C\xE8\xAF\x81\xE4\xB8\xAD"); + snprintf(body, sizeof(body), "{\"user\":\"%s\",\"pass\":\"%s\",\"device\":\"%s\"}", user, pass, g_device); + int passed = api_call("login", body, resp, sizeof(resp)); + if (!passed && g_offline[0] && strcmp(pass, g_offline) == 0 && !strstr(resp, "DEVICE_LIMIT")) { + logf_("offline mode"); + passed = 1; + } + if (passed) { + set_status_u8(1, "\xE9\xAA\x8C\xE8\xAF\x81\xE9\x80\x9A\xE8\xBF\x87"); + Sleep(180); + g_done = 1; + on_success(); + return 0; + } + if (strstr(resp, "DEVICE_LIMIT")) { + parse_devices(resp); + g_screen = 1; + set_status(2, L"\x8BBE\x5907\x8DF3\x8FC7\x4E0A\x9650\xFF0C\x8BF7\x9009\x62E9\x4E00\x53F0\x4E0B\x7EBF"); + if (g_autoKick) SetTimer(g_hwnd, 7, 700, NULL); + } else { + json_str(resp, "msg", msg, sizeof(msg)); + if (msg[0]) set_status_u8(2, msg); + else set_status(2, L"\x9A8C\x8BC1\x5931\x8D25"); + } + g_busy = 0; + InvalidateRect(g_hwnd, NULL, FALSE); + return 0; +} + +static int in_rect(const RECT &r, int x, int y) { return x >= r.left && x < r.right && y >= r.top && y < r.bottom; } +static int row_at(int x, int y) { + for (int i = 0; i < g_devCount; i++) { + RECT r = rowRect(i); + if (in_rect(r, x, y)) return i; + } + return -1; +} +static int kick_at(int x, int y) { + for (int i = 0; i < g_devCount; i++) + if (in_rect(kickRect(i), x, y) && !g_devs[i].cur) return i; + return -1; +} + +static LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l) { + switch (m) { + case WM_CREATE: + g_hwnd = h; + g_hUser = CreateWindowExW(0, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, + (int)L1(32), (int)L1(125), (int)L1(356), (int)L1(30), h, (HMENU)201, NULL, NULL); + g_hPass = CreateWindowExW(0, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_PASSWORD | ES_AUTOHSCROLL, + (int)L1(32), (int)L1(187), (int)L1(324), (int)L1(30), h, (HMENU)202, NULL, NULL); + SendMessage(g_hUser, WM_SETFONT, (WPARAM)g_fontEdit, TRUE); + SendMessage(g_hPass, WM_SETFONT, (WPARAM)g_fontEdit, TRUE); + if (g_auto[0]) { + SetWindowTextA(g_hUser, "demo"); + SetWindowTextA(g_hPass, g_auto); + SetTimer(h, 1, 500, NULL); + } + SetTimer(h, 2, 200, NULL); + return 0; + case WM_TIMER: + if (w == 1) { KillTimer(h, 1); do_login(); } + if (w == 2) { g_tick++; if (g_busy) InvalidateRect(h, NULL, FALSE); } + if (w == 7) { + KillTimer(h, 7); + for (int i = 0; i < g_devCount; i++) + if (!g_devs[i].cur) { do_kick(i); break; } + } + return 0; + case WM_CTLCOLOREDIT: { + HDC dc = (HDC)w; + SetTextColor(dc, RGB(230, 230, 230)); + SetBkColor(dc, RGB(37, 37, 38)); + return (LRESULT)g_brInput; + } + case WM_ERASEBKGND: + return 1; + case WM_PAINT: { + PAINTSTRUCT ps; BeginPaint(h, &ps); + render(); + EndPaint(h, &ps); + return 0; + } + case WM_NCHITTEST: { + POINT pt = {GET_X_LPARAM(l), GET_Y_LPARAM(l)}; + ScreenToClient(h, &pt); + int lx = (int)(pt.x / g_S) - (int)PAD, ly = (int)(pt.y / g_S) - (int)PAD; + if (lx >= 0 && lx < (int)CARD_W && ly >= 0 && ly < 32 && + !in_rect(LR_CLOSE, lx, ly) && !in_rect(LR_MIN, lx, ly)) + return HTCAPTION; + if (!(lx >= 0 && lx < (int)CARD_W && ly >= 0 && ly < (int)CARD_H)) + return HTCAPTION; + break; + } + case WM_MOUSEMOVE: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + int nb = 0, nc = 0, nt = 0, ne = 0, nx = in_rect(LR_CLOSE, lx, ly), nm = in_rect(LR_MIN, lx, ly); + int nl = 0, nrow = -1, nkick = -1, nback = 0; + if (g_screen == 0) { + nb = in_rect(LR_BTN, lx, ly); nc = in_rect(LR_REM, lx, ly); + nt = in_rect(LR_TAG, lx, ly); ne = in_rect(LR_EYE, lx, ly); + nl = in_rect(LR_L1, lx, ly) ? 1 : (in_rect(LR_L2, lx, ly) ? 2 : (in_rect(LR_L3, lx, ly) ? 3 : 0)); + } else { + nrow = row_at(lx, ly); + nkick = kick_at(lx, ly); + nback = in_rect(LR_BACK, lx, ly); + } + if (nb != g_hBtn || nc != g_hChk || nt != g_hTag || ne != g_hEye || nx != g_hClose || nm != g_hMin || + nl != g_hLink || nrow != g_hRow || nkick != g_hKick || nback != g_hBack) { + g_hBtn = nb; g_hChk = nc; g_hTag = nt; g_hEye = ne; g_hClose = nx; g_hMin = nm; g_hLink = nl; + g_hRow = nrow; g_hKick = nkick; g_hBack = nback; + InvalidateRect(h, NULL, FALSE); + } + TRACKMOUSEEVENT tme = {sizeof(tme), TME_LEAVE, h, 0}; + TrackMouseEvent(&tme); + return 0; + } + case WM_MOUSELEAVE: + g_hBtn = g_hChk = g_hTag = g_hEye = g_hClose = g_hMin = g_hLink = g_hBack = 0; + g_hRow = g_hKick = -1; + InvalidateRect(h, NULL, FALSE); + return 0; + case WM_SETCURSOR: { + POINT pt; GetCursorPos(&pt); ScreenToClient(h, &pt); + int lx = (int)(pt.x / g_S) - (int)PAD, ly = (int)(pt.y / g_S) - (int)PAD; + if (g_screen == 0) { + if (in_rect(LR_BTN, lx, ly) || in_rect(LR_REM, lx, ly) || in_rect(LR_TAG, lx, ly) || + in_rect(LR_EYE, lx, ly) || in_rect(LR_CLOSE, lx, ly) || in_rect(LR_MIN, lx, ly) || + in_rect(LR_L1, lx, ly) || in_rect(LR_L2, lx, ly) || in_rect(LR_L3, lx, ly)) { + SetCursor(LoadCursor(NULL, IDC_HAND)); + return TRUE; + } + } else { + if (kick_at(lx, ly) >= 0 || in_rect(LR_BACK, lx, ly) || in_rect(LR_CLOSE, lx, ly) || in_rect(LR_MIN, lx, ly)) { + SetCursor(LoadCursor(NULL, IDC_HAND)); + return TRUE; + } + } + break; + } + case WM_LBUTTONDOWN: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + if (in_rect(LR_CLOSE, lx, ly)) { DestroyWindow(h); return 0; } + if (in_rect(LR_MIN, lx, ly)) { ShowWindow(h, SW_MINIMIZE); return 0; } + if (g_screen == 0) { + if (in_rect(LR_BTN, lx, ly)) { g_pressBtn = 1; InvalidateRect(h, NULL, FALSE); return 0; } + if (in_rect(LR_REM, lx, ly)) { g_checked = !g_checked; InvalidateRect(h, NULL, FALSE); return 0; } + if (in_rect(LR_EYE, lx, ly)) { + g_showPass = !g_showPass; + SendMessage(g_hPass, EM_SETPASSWORDCHAR, g_showPass ? 0 : 0x25CF, 0); + InvalidateRect(h, NULL, FALSE); + return 0; + } + if (in_rect(LR_TAG, lx, ly)) { + if (OpenClipboard(h)) { + EmptyClipboard(); + int n = MultiByteToWideChar(CP_UTF8, 0, g_device, -1, NULL, 0); + HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, n * 2); + if (hg) { + wchar_t *d = (wchar_t *)GlobalLock(hg); + MultiByteToWideChar(CP_UTF8, 0, g_device, -1, d, n); + GlobalUnlock(hg); + SetClipboardData(CF_UNICODETEXT, hg); + } + CloseClipboard(); + } + set_status(1, L"\x8BBE\x5907\x7801\x5DF2\x590D\x5236"); + return 0; + } + if (in_rect(LR_L1, lx, ly)) { set_status(0, L"\x6CE8\x518C\xFF1A\x6253\x5F00\x5B98\x7F51"); return 0; } + if (in_rect(LR_L2, lx, ly)) { set_status(0, L"\x5361\x5BC6\x5145\x503C\xFF1A\x5F39\x51FA\x5145\x503C\x7A97"); return 0; } + if (in_rect(LR_L3, lx, ly)) { set_status(0, L"\x5FD8\x8BB0\x5BC6\x7801\xFF1A\x8BF7\x8054\x7CFB\x5BA2\x670D"); return 0; } + } else { + int k = kick_at(lx, ly); + if (k >= 0) { do_kick(k); return 0; } + if (in_rect(LR_BACK, lx, ly)) { + g_screen = 0; + set_status(0, L"\x5C31\x7EEA"); + InvalidateRect(h, NULL, FALSE); + return 0; + } + } + break; + } + case WM_LBUTTONUP: { + int lx = (int)(GET_X_LPARAM(l) / g_S) - (int)PAD, ly = (int)(GET_Y_LPARAM(l) / g_S) - (int)PAD; + if (g_pressBtn) { + g_pressBtn = 0; + InvalidateRect(h, NULL, FALSE); + if (in_rect(LR_BTN, lx, ly)) do_login(); + return 0; + } + break; + } + case WM_COMMAND: + if (HIWORD(w) == EN_SETFOCUS) { g_focus = (LOWORD(w) == 201) ? 1 : 2; InvalidateRect(h, NULL, FALSE); } + else if (HIWORD(w) == EN_KILLFOCUS) { g_focus = 0; InvalidateRect(h, NULL, FALSE); } + else if (HIWORD(w) == EN_CHANGE) InvalidateRect(h, NULL, FALSE); + break; + case WM_CLOSE: + DestroyWindow(h); + return 0; + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProcW(h, m, w, l); +} + +static const wchar_t *CLS_NAME = L"VfyShellV6"; + +static void make_colors(void) { + C_CARD = D2D1::ColorF(0.118f, 0.118f, 0.118f, 1); + C_BAR = D2D1::ColorF(0.145f, 0.145f, 0.153f, 1); + C_BORDER = D2D1::ColorF(0.247f, 0.247f, 0.275f, 1); + C_INPUT = D2D1::ColorF(0.145f, 0.145f, 0.149f, 1); + C_INPUT_BD = D2D1::ColorF(0.247f, 0.247f, 0.275f, 1); + C_FOCUS = D2D1::ColorF(0.0f, 0.471f, 0.831f, 1); + C_TXT = D2D1::ColorF(0.902f, 0.902f, 0.902f, 1); + C_TXT2 = D2D1::ColorF(0.616f, 0.616f, 0.616f, 1); + C_ACCENT = D2D1::ColorF(0.055f, 0.388f, 0.612f, 1); + C_ACCENT_H = D2D1::ColorF(0.067f, 0.467f, 0.733f, 1); + C_ACCENT_P = D2D1::ColorF(0.039f, 0.306f, 0.478f, 1); + C_LINK = D2D1::ColorF(0.216f, 0.58f, 1.0f, 1); + C_RED = D2D1::ColorF(0.96f, 0.35f, 0.35f, 1); + C_GREEN = D2D1::ColorF(0.35f, 0.78f, 0.45f, 1); + C_WHITE = D2D1::ColorF(1, 1, 1, 1); + C_TAG = D2D1::ColorF(1, 1, 1, 0.05f); + C_ROW = D2D1::ColorF(0.145f, 0.145f, 0.149f, 1); + C_ROW_H = D2D1::ColorF(0.18f, 0.18f, 0.19f, 1); +} + +static int shell_main(PFN_OEP oep) { + g_oep = oep; + typedef BOOL(WINAPI * PFN_SetCtx)(HANDLE); + PFN_SetCtx setCtx = (PFN_SetCtx)GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetProcessDpiAwarenessContext"); + if (setCtx) setCtx((HANDLE)-4); + HDC sdc = GetDC(NULL); + g_S = GetDeviceCaps(sdc, LOGPIXELSX) / 96.0f; + ReleaseDC(NULL, sdc); + if (g_S < 1.0f) g_S = 1.0f; + + make_colors(); + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_d2d))) return -1; + if (FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown **)&g_dw))) return -1; + + g_fontEdit = CreateFontW((int)-L1(13), 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, 0, 0, CLEARTYPE_QUALITY, 0, L"Microsoft YaHei UI"); + g_brInput = CreateSolidBrush(RGB(37, 37, 38)); + + WNDCLASSEXW wc; ZeroMemory(&wc, sizeof(wc)); + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = WndProc; + wc.hInstance = GetModuleHandleW(NULL); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.lpszClassName = CLS_NAME; + wc.hbrBackground = NULL; + RegisterClassExW(&wc); + + int w = (int)((CARD_W + PAD * 2) * g_S), h = (int)((CARD_H + PAD * 2) * g_S); + HWND hwnd = CreateWindowExW(WS_EX_LAYERED | WS_EX_APPWINDOW | WS_EX_TOPMOST, CLS_NAME, L"\x9A8C\x8BC1\x767B\x5F55", + WS_POPUP, 0, 0, w, h, NULL, NULL, wc.hInstance, NULL); + if (!hwnd) return -1; + + D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties( + D2D1_RENDER_TARGET_TYPE_DEFAULT, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), + 0, 0, D2D1_RENDER_TARGET_USAGE_NONE, D2D1_FEATURE_LEVEL_DEFAULT); + if (FAILED(g_d2d->CreateDCRenderTarget(&props, &g_rt))) return -1; + + detect_fonts(); + + RECT wa; SystemParametersInfoW(SPI_GETWORKAREA, 0, &wa, 0); + int sx = wa.left + ((wa.right - wa.left) - w) / 2; + int sy = wa.top + ((wa.bottom - wa.top) - h) / 2; + SetWindowPos(hwnd, HWND_TOPMOST, sx, sy, w, h, SWP_NOACTIVATE); + render(); + ShowWindow(hwnd, SW_SHOW); + SetForegroundWindow(hwnd); + + MSG msg; + while (GetMessageW(&msg, NULL, 0, 0)) { + if (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN) { do_login(); continue; } + if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE) { ShowWindow(hwnd, SW_MINIMIZE); continue; } + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + return g_done ? 0 : -2; +} + +#ifdef SHELL_DLL +extern "C" __declspec(dllexport) int VerifyMain(void *oep, const char *cfgjson) { + load_cfg(cfgjson); + return shell_main((PFN_OEP)oep); +} +extern "C" __declspec(dllexport) int VerifyShellVersion(void) { return 6; } +BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID) { + if (reason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(h); + return TRUE; +} +#else +static int read_self(void) { + wchar_t path[MAX_PATH]; DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH); + if (!n) return 0; + HANDLE h = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + if (h == INVALID_HANDLE_VALUE) return 0; + LARGE_INTEGER sz; GetFileSizeEx(h, &sz); + g_fileSize = (DWORD)sz.QuadPart; + g_file = (unsigned char *)malloc(g_fileSize); + DWORD got = 0; + BOOL ok = ReadFile(h, g_file, g_fileSize, &got, NULL); + CloseHandle(h); + return ok && got == g_fileSize; +} +static unsigned int le32(const unsigned char *p) { + return (unsigned int)p[0] | ((unsigned int)p[1] << 8) | ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24); +} +static int parse_tail(void) { + if (g_fileSize < 64) return 0; + const unsigned char *t = g_file + g_fileSize - 64; + if (memcmp(t, TAIL_MAGIC, 8) != 0) return 0; + unsigned int poff = le32(t + 8), psize = le32(t + 12), coff = le32(t + 16), csize = le32(t + 20); + memcpy(g_nonce, t + 24, 12); + memcpy(g_tag, t + 36, 16); + if (poff + psize > g_fileSize) return 0; + g_payload = g_file + poff; g_payloadSize = psize; + DWORD cs = csize < sizeof(g_cfg) - 1 ? csize : sizeof(g_cfg) - 1; + if (coff + cs > g_fileSize) return 0; + char buf[4096] = {0}; + memcpy(buf, g_file + coff, cs); + load_cfg(buf); + return 1; +} +int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int) { + if (!read_self() || !parse_tail()) { + MessageBoxW(NULL, L"\x7A0B\x5E8F\x5DF2\x635F\x574F", L"\x9519\x8BEF", MB_ICONERROR); + return 1; + } + int argc = 0; + LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); + for (int i = 1; i + 1 < argc; i++) + if (!wcscmp(argv[i], L"--auto")) + WideCharToMultiByte(CP_UTF8, 0, argv[i + 1], -1, g_auto, sizeof(g_auto), NULL, NULL); + return shell_main(NULL); +} +#endif diff --git a/shell/duilib/build-duilib.sh b/shell/duilib/build-duilib.sh new file mode 100755 index 0000000..027680a --- /dev/null +++ b/shell/duilib/build-duilib.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# 交叉编译 DuiLib 多皮肤壳 → shell64_dui.dll +# 用法: DUILIB_DIR=/tmp/duilib ./build-duilib.sh [源码] (默认 shell9_v6.cpp) +# +# 前置:先编 libduilib.a(老 DuiLib,mingw)。必须做的改动: +# - 排除 UIWebBrowser(ExDisp.h 不存在)/ UIRichEdit(goto 跨初始化)/ UIActiveX(CActiveXUI 未定义) +# - UIlib.h 里注释掉这三个控件的 include,UIDlgBuilder.cpp 删对应 new CXxxUI 分支 +# - stb_image.c 单独按 C 编(header-only,否则 stbi_load_from_memory 未定义) +# - UICombo::GetClassStyle 不要自递归;UIOption 的 goto Label_ForeImage 就地展开 +# - Utils::GetAt 返回 NULL;UIManager 取不到 HFONT 时返回 NULL +# - 链接入口是 WinMain 时不要加 -municode(否则链接器找 wWinMain) +set -euo pipefail + +DIR=$(cd "$(dirname "$0")" && pwd) +SRC=${1:-$DIR/shell9_v6.cpp} +DUILIB_DIR=${DUILIB_DIR:-/tmp/duilib} +OUTDIR=${2:-$DIR/out} +LIBDUILIB=${LIBDUILIB:-$DUILIB_DIR/libduilib.a} + +[ -f "$LIBDUILIB" ] || { echo "缺少 $LIBDUILIB:先按上面注释里的步骤编 libduilib.a,或用 LIBDUILIB=... 指定"; exit 1; } +mkdir -p "$OUTDIR" + +# 导入表按实测产物取:ADVAPI32/bcrypt/COMCTL32/GDI32/gdiplus/KERNEL32/msvcrt/ole32/SHELL32/USER32/WINHTTP +x86_64-w64-mingw32-g++ -std=c++17 -O2 -shared -s \ + -DUNICODE -D_UNICODE -D_WIN32_WINNT=0x0601 -D_WIN32_LEAN_AND_MEAN \ + -I"$DUILIB_DIR" -I"$DUILIB_DIR/DuiLib" -include "$DUILIB_DIR/shim/compat.h" -fpermissive -w \ + -o "$OUTDIR/shell64_dui.dll" "$SRC" "$LIBDUILIB" \ + -static -lcomctl32 -lgdiplus -lole32 -loleaut32 -luuid -lshlwapi -limm32 -lwinmm -lversion \ + -lgdi32 -luser32 -lwinhttp -ladvapi32 -lbcrypt -Wl,--kill-at + +echo "生成 $OUTDIR/shell64_dui.dll($(stat -c%s "$OUTDIR/shell64_dui.dll") 字节)" +echo "皮肤用 XML 字符串内嵌,theme 从壳 cfg 读;加壳任务侧用 shell_theme 选择,非法值请在校验层挡掉。" diff --git a/shell/duilib/shell10.cpp b/shell/duilib/shell10.cpp new file mode 100644 index 0000000..e742149 --- /dev/null +++ b/shell/duilib/shell10.cpp @@ -0,0 +1,403 @@ +// shell10.cpp —— NIM DuiLib 版 Windows 验证壳(多皮肤,含透明 PNG 皮肤) +// +// 对外接口与老壳完全一致: +// int VerifyMain(void *oep, const char *cfgjson); +// int VerifyShellVersion(void); // 10 +// +// 皮肤分两类: +// XML 皮肤(dark / light / industrial)—— 纯属性绘制,零图片 +// PNG 皮肤(nebula / onyx / aurora)—— 半透明玻璃卡片 + 发光 + 星尘,整窗按像素 alpha +// 所有皮肤资源在启动时释放到 %TEMP%\vfytheme\\,进程结束自动清理。 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "UIlib.h" + +#pragma comment(lib, "winhttp.lib") + +using namespace ui; + +/* ============================== cfg ============================== */ +static char g_cfg[4096]; +static char g_api[512]; +static char g_name[128] = "\xE6\xBC\x94\xE7\xA4\xBA\xE7\xA8\x8B\xE5\xBA\x8F"; // 演示程序 +static char g_theme[32] = "dark"; +static char g_device[64] = ""; +static int g_deviceAuto = 1; + +static int json_str(const char *src, const char *key, char *out, int cap) { + char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key); + const char *p = strstr(src, pat); + if (!p) return 0; + p = strchr(p + strlen(pat), ':'); + if (!p) return 0; + while (*p && *p != '"') p++; + if (!*p) return 0; + p++; + int i = 0; + while (*p && *p != '"' && i < cap - 1) out[i++] = *p++; + out[i] = 0; + return 1; +} + +static int json_int(const char *src, const char *key, int def) { + char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key); + const char *p = strstr(src, pat); + if (!p) return def; + p = strchr(p + strlen(pat), ':'); + if (!p) return def; + return atoi(p + 1); +} + +static void make_device_id(char *out, int cap); // 定义在后面 + +static void load_cfg(const char *json) { + snprintf(g_cfg, sizeof(g_cfg), "%s", json ? json : ""); + json_str(g_cfg, "api", g_api, sizeof(g_api)); + json_str(g_cfg, "name", g_name, sizeof(g_name)); + g_deviceAuto = json_int(g_cfg, "device_auto", 1); + if (!json_str(g_cfg, "device", g_device, sizeof(g_device)) || !g_device[0]) { + if (g_deviceAuto) make_device_id(g_device, sizeof(g_device)); + else snprintf(g_device, sizeof(g_device), "%s", "UNKNOWN-DEVICE"); + } + if (!json_str(g_cfg, "theme", g_theme, sizeof(g_theme))) snprintf(g_theme, sizeof(g_theme), "dark"); +} + + +static void LOGF(const char *fmt, ...) { + FILE *f = fopen("C:\\vfy10.log", "a"); + if (!f) return; + va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); + fputc('\n', f); fclose(f); +} + +/* ============================ 工具 ============================ */ +static std::wstring U8(const char *s) { + if (!s) return L""; + int n = ::MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0); + std::wstring w(n > 0 ? n - 1 : 0, L'\0'); + if (n > 1) ::MultiByteToWideChar(CP_UTF8, 0, s, -1, &w[0], n); + return w; +} + +static std::string W2U8(const std::wstring &w) { + if (w.empty()) return std::string(); + int n = ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, NULL, 0, NULL, NULL); + std::string s(n > 0 ? n - 1 : 0, '\0'); + if (n > 1) ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, &s[0], n, NULL, NULL); + return s; +} + +// MachineGuid + 系统盘卷序列号 → 稳定机器码 +static void make_device_id(char *out, int cap) { + wchar_t guid[128] = {0}; + HKEY hk = NULL; + if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography", + 0, KEY_READ | KEY_WOW64_64KEY, &hk) == ERROR_SUCCESS) { + DWORD sz = sizeof(guid) - sizeof(wchar_t), type = 0; + RegQueryValueExW(hk, L"MachineGuid", NULL, &type, (LPBYTE)guid, &sz); + RegCloseKey(hk); + } + DWORD serial = 0; + GetVolumeInformationW(L"C:\\", NULL, 0, &serial, NULL, NULL, NULL, 0); + unsigned int h1 = 2166136261u, h2 = 3735928559u; + for (const wchar_t *p = guid; *p; p++) { + h1 = (h1 ^ (unsigned int)(*p & 0xFF)) * 16777619u; + h2 = (h2 ^ (unsigned int)(*p >> 8)) * 16777619u; + } + h1 = (h1 ^ serial) * 16777619u; + h2 = (h2 ^ (serial >> 16)) * 16777619u; + snprintf(out, cap, "%04X-%04X-%04X", h1 & 0xFFFF, (h1 >> 16) & 0xFFFF, h2 & 0xFFFF); +} + +/* ======================= 皮肤资源(内嵌) ======================= */ +// 每个皮肤 = global.xml + .xml + 可选 PNG。启动时写到临时目录。 +#include "skins_data.h" // 由 gen_skins.py 生成:SkinFile / kSkins[] / kThemeList + +static std::wstring g_skinRoot; + +static bool EnsureDir(const std::wstring &p) { + return ::CreateDirectoryW(p.c_str(), NULL) != 0 || ::GetLastError() == ERROR_ALREADY_EXISTS; +} + +// 递归建目录:%TEMP%\vfytheme\ 这类多层路径要一层层来 +static void MakeDirs(const std::wstring &path) { + std::wstring cur; + for (size_t i = 0; i < path.size(); i++) { + cur += path[i]; + if (path[i] == L'\\' && cur.size() > 3) EnsureDir(cur); + } + EnsureDir(path); +} + +static void WriteFileBytes(const std::wstring &path, const void *data, unsigned int size) { + HANDLE h = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) return; + DWORD wrote = 0; + ::WriteFile(h, data, size, &wrote, NULL); + ::CloseHandle(h); +} + +// 把某套皮肤的 XML/PNG 释放到临时目录 vfytheme// +static bool StageSkin(const char *theme) { + wchar_t tmp[MAX_PATH] = {0}; + DWORD n = ::GetTempPathW(MAX_PATH, tmp); + std::wstring base; + if (n == 0 || tmp[0] == 0) { + // 没有 TEMP:退到 exe 所在目录 + wchar_t exe[MAX_PATH] = {0}; + ::GetModuleFileNameW(NULL, exe, MAX_PATH); + base.assign(exe); + size_t pos = base.find_last_of(L"\\"); + base = (pos != std::wstring::npos) ? base.substr(0, pos + 1) : std::wstring(L".\\"); + } else { + base.assign(tmp); // 已经带尾部反斜杠 + } + g_skinRoot = base + L"vfytheme"; + MakeDirs(g_skinRoot); + + std::wstring dir = g_skinRoot + L"\\" + U8(theme); + MakeDirs(dir); + + bool any = false; + for (int i = 0; kSkins[i].name != NULL; i++) { + std::wstring fn = U8(kSkins[i].name); + // 只释放本皮肤 + 公共 global.xml + std::string nm = kSkins[i].name; + if (nm != "global.xml" && nm.compare(0, strlen(theme), theme) != 0) continue; + WriteFileBytes(dir + L"\\" + fn, kSkins[i].data, kSkins[i].size); + any = true; + } + return any; +} + +static void CleanupSkins() { + if (g_skinRoot.empty()) return; + // %TEMP% 每次启动会重置同名文件,这里只删文件不删目录,留一点余量 + std::wstring pat = g_skinRoot + L"\\*"; + WIN32_FIND_DATAW fd; + HANDLE h = ::FindFirstFileW(pat.c_str(), &fd); + if (h != INVALID_HANDLE_VALUE) { + do { + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + std::wstring sub = g_skinRoot + L"\\" + fd.cFileName + L"\\*"; + WIN32_FIND_DATAW fd2; HANDLE h2 = ::FindFirstFileW(sub.c_str(), &fd2); + if (h2 != INVALID_HANDLE_VALUE) { + do { + ::DeleteFileW((g_skinRoot + L"\\" + fd.cFileName + L"\\" + fd2.cFileName).c_str()); + } while (::FindNextFileW(h2, &fd2)); + ::FindClose(h2); + } + ::RemoveDirectoryW((g_skinRoot + L"\\" + fd.cFileName).c_str()); + } + } while (::FindNextFileW(h, &fd)); + ::FindClose(h); + } + ::RemoveDirectoryW(g_skinRoot.c_str()); +} + +/* ============================ 登录 ============================ */ +static int api_login(const char *code, char *msg, int cap) { + msg[0] = 0; + if (!g_api[0]) { snprintf(msg, cap, "cfg 里没有 api 地址"); return 0; } + + char scheme[16] = "http://"; + const char *rest = g_api; + if (!strncmp(g_api, "https://", 8)) { snprintf(scheme, sizeof(scheme), "https://"); rest = g_api + 8; } + else if (!strncmp(g_api, "http://", 7)) rest = g_api + 7; + + char host[256] = {0}, path[512] = "/"; + const char *slash = strchr(rest, '/'); + if (slash) { + int hl = (int)(slash - rest); + if (hl > 255) hl = 255; + memcpy(host, rest, hl); + snprintf(path, sizeof(path), "%s", slash); + } else { + snprintf(host, sizeof(host), "%s", rest); + } + unsigned short port = (scheme[4] == 's') ? 443 : 80; + char *colon = strrchr(host, ':'); + if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); } + + char body[1024]; + snprintf(body, sizeof(body), "{\"code\":\"%s\",\"device\":\"%s\"}", code, g_device); + + std::wstring wHost = U8(host), wPath = U8(path); + HINTERNET hS = WinHttpOpen(L"vfy/10.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0); + if (!hS) { snprintf(msg, cap, "网络初始化失败"); return 0; } + HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0); + HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath.c_str(), NULL, NULL, NULL, 0) : NULL; + int ok = 0; + if (hR) { + WinHttpSendRequest(hR, L"Content-Type: application/json\r\n", (DWORD)-1, + (LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0); + WinHttpReceiveResponse(hR, NULL); + char resp[2048] = {0}; + DWORD cl = 0; + WinHttpReadData(hR, resp, sizeof(resp) - 1, &cl); + resp[cl < sizeof(resp) ? cl : sizeof(resp) - 1] = 0; + if (strstr(resp, "\"ok\":true") || strstr(resp, "\"code\":200")) ok = 1; + if (!json_str(resp, "message", msg, cap)) { + if (ok) snprintf(msg, cap, "验证成功"); + else if (strstr(resp, "DEVICE_LIMIT")) snprintf(msg, cap, "设备数量已达上限"); + else snprintf(msg, cap, "卡密无效或已过期"); + } + } else { + snprintf(msg, cap, "无法连接验证服务器"); + } + if (hR) WinHttpCloseHandle(hR); + if (hC) WinHttpCloseHandle(hC); + WinHttpCloseHandle(hS); + return ok; +} + +/* ============================ 窗口 ============================ */ +typedef void (*PFN_OEP)(void); +static PFN_OEP g_oep = NULL; +static int g_logged_in = 0; + +class VerifyWnd : public WindowImplBase { +public: + explicit VerifyWnd(const std::string &theme) : theme_(theme) {} + + std::wstring GetSkinFolder() override { return U8(theme_.c_str()); } + std::wstring GetSkinFile() override { return U8((theme_ + ".xml").c_str()); } + std::wstring GetWindowClassName() const override { return L"VfyNimWnd"; } + + // WindowImplBase::OnCreate 会用 GetStyle() 覆盖创建时的样式(默认是有边框的主窗口), + // 验证窗必须是无边框弹窗,否则系统标题栏会盖在皮肤上。 + LONG GetStyle() override { return WS_POPUP | WS_CLIPCHILDREN; } + + void InitWindow() override { + if (Label *p = dynamic_cast