diff --git a/README_part2.md b/README_part2.md index a7d7399..1826e6c 100644 --- a/README_part2.md +++ b/README_part2.md @@ -75,3 +75,127 @@ un-lifted). Only a run inside real Minecraft 1.8.9 settles it. loader2/slinky_loader.c self-contained loader (JVM + block stub + load) loader2/javaw.exe prebuilt, run from loader2\run\ ``` + +--- + +# Part 3 — verdict plumbing, the elimination matrix, and the honest ceiling + +## The status reads everyone gets wrong (we did, for a whole session) + +The client has **two** callers of the one-shot validator and a guard race: + +- `DllMain` reads `SLINKY_LICENSE` and **spawns a validator thread** (`+0x10F0`). +- `slinky_init` (export `+0x1000`) also validates, but first takes a + `lock cmpxchg` guard (`0x1802F9118`). + +Whoever wins the race runs the real check and prints `status: %lu` to +**stderr** (`GetStdHandle(2)` + `"+status: %lu\n"` at `0x180140038`). The +loser takes the "already-initialized" path: it consumes a flag byte at +`0x1802FF010` that is **never set to a nonzero value** — so the loser +*always* returns `0` and may print a bogus `status: 0`. + +In practice the spawned thread wins, so: + +> **`slinky_init`'s return value is not the verdict. `stderr` is.** + +All "VALID" readings from bare-host test harnesses (`keytest*.exe`, +`slinky_init -> 0`) were this artifact — the harness's own call lost the +race, while the spawned thread rejected the key and printed the real +`status:` line to a stderr file nobody was reading. Ground truth from the +stderr captures: + +| build | key | host | stderr | +|---|---|---|---| +| devirt | `E984C654…8535` (blob hex) | bare JVM harness | `status: 7003` | +| devirt | `E984C654…8535` | real `javaw.exe` + MC 1.8.9 | `status: 7003` | +| devirt | `"A"*32` (right length, wrong content) | real MC | `status: 7003` | +| devirt | short key | real MC | no line (silent length path) | +| real client | real key `43A913B2…0F18` | bare JVM harness **today** | `status: 7004` | +| real client | real key | real MC **today** | `status: 7004` | + +Two conclusions fall out: + +1. **There is no Minecraft-vs-bare-host discriminator.** The devirt client + rejects its test key *everywhere*; the earlier "works in a bare JVM but + not in MC" split was the stderr artifact. (The real client's behavior + did change since January — it now fails in *every* host, including the + launcher-shaped ones; more below.) +2. **The real client + its real key no longer validate offline at all** + (`7004` everywhere today, `0` in January via the official launcher). + The server still reports the license active + (`GET /licenses/info → {"remaining":1285977}`), the client makes **zero** + network/API calls during validation (confirmed by TCP-table polling, + DNS-cache inspection and full API tracing), so whatever changed lives + inside the VM — plausibly a device/time-bound transform against the + `.rdata` blob. + +## The elimination matrix (devirt client, real MC `javaw.exe`, verdict via stderr) + +Every controlled variation below produced `status: 7003` — i.e. none of +these is the discriminator, because there is nothing to discriminate: + +- injection method: clean `QueueUserAPC(LoadLibraryW)` on the game's own + threads (no loader DLL, no remote thread) vs. `CreateRemoteThread` +- calling path: env var + DllMain spawn vs. explicit `slinky_init` vs. + direct APC to the internal validator `base+0x10F0` with our own key buffer +- host process name (`javaw.exe`-named test host passes shape checks), host + binary path (launcher-runtime-shaped `jre-legacy` path), parent process + renamed `MinecraftLauncher.exe` +- JVM options: MC's exact `-Xmx/-Djava.library.path/-Djava.class.path/` + `-Dsun.java.command` replayed into a harness JVM; Mojang launcher flags + (`-XX:HeapDumpPath=MojangTricksIntelDrivers…`, `-Dos.name/-Dos.version`) +- classpath: the exact official 1.8.9 library list (log4j `2.0-beta9`, + `com.mojang:netty:1.8.8`, all 31 entries) fetched and replayed +- cmdline: realistic `--uuid/--clientId/--userType msa/--accessToken JWT/` + `--userProperties/--versionType release`, `--username 2rax` +- game state: injection at t+3 s (pre-window) vs. t+16-60 s (game booted); + a `SleepMain` JVM — MC's exact JVM/classpath but no MC classes running +- thread count: 60 dummy threads in a harness (vs MC's ~54) +- network: zero TCP connects / DNS lookups from the process during validate + (offline rejection) +- Frida API tracing during validate in MC: **zero** hookable API calls — + no JNI, wininet/winhttp/ws2_32, registry, files, windows, toolhelp, + debugger queries, volume/computer name. The VM reads what it needs + directly from memory (PEB/Ldr-class access, invisible to API hooks). + +## What `status: 0` success actually does at the `.text` level + +`slinky_start` (`0x180001290`) is one `lea rcx,[0x180140064]` followed by a +`call` into the VM region (`.vdvrt`, RVA `0x35556EC`) — the blob check **and +all real initialization** (JNI attach, `RegisterNatives`, game hooks) live +inside the VM. The `.text` success path after `slinky_start` returns 0 is +just: print `status: 0`, return 0. Nothing else. + +Consequently the Route-1 patch (`31 C0 E9 22 00 00 00` at VA `0x1800012AE`) += `xor eax,eax; jmp` onto `slinky_start`'s epilogue makes the client +**report success and stay resident, but it is inert by construction** — the +VM's initialization never executes. This is now a *proven* structural fact, +not an open question (it answers Part 2's "Open question": no, blocking the +unload does not unlock features). + +## The honest ceiling + +- **Residency in real Minecraft 1.8.9: solved** (Route-1 patch on the devirt + build, or the Route-2 kernel32 stub with any 32-char key). +- **Full client functionality: requires the VM's key transform.** The + transform maps key → blob comparison inside ~5 MB of obfuscated `.vdvrt` + code with no forgeable API surface, no network, and no static handle. The + key↔blob pairs are build-specific (`43A9… ↔ C2FE…` for the real client; + no standard hash relates them) and the devirt build's true key is unknown + (its blob hex is *not* its key — the real pair disproves that rule). + Recovering a working key means either devirtualizing `.vdvrt` or lifting + the VM's check loop at runtime — a separate project. + +## Session tooling (all in the repo `tools/` folder) + +- `apc_injector.c` — QueueUserAPC(LoadLibraryW) onto all target threads + (no remote thread, no loader DLL) +- `apc_injector2.c` — dll/init modes: after mapping, queues APCs directly + to `slinky_init(key)` (loader-style start with our own key buffer) +- `apc_call.c` — APC to an arbitrary address with a string parameter +- `keytest2..6.c` — harness JVMs: file-driven JVM options, tracer window, + thread bloat, real-client build, "run a Java main first" variant +- **stderr verdict technique**: launch the host with stderr redirected to a + file; the validator prints `status: %lu` there on the normal-length path. + This is the only reliable verdict channel — do not trust `slinky_init`'s + return value. diff --git a/tools/apc_call.c b/tools/apc_call.c new file mode 100644 index 0000000..d24af61 --- /dev/null +++ b/tools/apc_call.c @@ -0,0 +1,35 @@ +#include +#include +#include +static int queue_all(DWORD pid, void *func, ULONG_PTR param) { + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snap == INVALID_HANDLE_VALUE) return -1; + THREADENTRY32 te; te.dwSize = sizeof te; + int queued = 0; + if (Thread32First(snap, &te)) { + do { + if (te.th32OwnerProcessID != pid) continue; + HANDLE th = OpenThread(THREAD_SET_CONTEXT, FALSE, te.th32ThreadID); + if (!th) continue; + if (QueueUserAPC((PAPCFUNC)func, th, param)) queued++; + CloseHandle(th); + } while (Thread32Next(snap, &te)); + } + CloseHandle(snap); + return queued; +} +int main(int argc, char **argv) { + DWORD pid = (DWORD)atoi(argv[1]); + ULONG_PTR target = (ULONG_PTR)strtoull(argv[2], NULL, 0); + const char *key = argv[3]; + HANDLE pr = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid); + if (!pr) { printf("OpenProcess fail\n"); return 1; } + wchar_t wkey[64]; + int kl = MultiByteToWideChar(CP_ACP, 0, key, -1, wkey, 64); + void *kmem = VirtualAllocEx(pr, NULL, (SIZE_T)kl * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!kmem || !WriteProcessMemory(pr, kmem, wkey, (SIZE_T)kl * 2, NULL)) { printf("key fail\n"); return 1; } + int n = queue_all(pid, (void *)target, (ULONG_PTR)kmem); + printf("%d APCs to %p\n", n, (void *)target); + CloseHandle(pr); + return 0; +} diff --git a/tools/apc_injector.c b/tools/apc_injector.c new file mode 100644 index 0000000..5813fa0 --- /dev/null +++ b/tools/apc_injector.c @@ -0,0 +1,47 @@ +/* apc_injector: queue LoadLibraryW(dll) as a user APC on existing threads of + * the target. No remote thread, no loader DLL in the target. + * usage: apc_injector.exe + */ +#include +#include +#include + +int main(int argc, char **argv) { + if (argc < 3) { printf("usage: apc_injector pid dllpath\n"); return 1; } + DWORD pid = (DWORD)atoi(argv[1]); + + wchar_t wpath[512]; + int wl = MultiByteToWideChar(CP_ACP, 0, argv[2], -1, wpath, 512); + if (!wl) { printf("path conv fail\n"); return 1; } + + HANDLE pr = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid); + if (!pr) { printf("OpenProcess fail GLE=%lu\n", GetLastError()); return 1; } + + SIZE_T sz = (SIZE_T)wl * sizeof(wchar_t); + void *mem = VirtualAllocEx(pr, NULL, sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!mem) { printf("VirtualAllocEx fail GLE=%lu\n", GetLastError()); return 1; } + if (!WriteProcessMemory(pr, mem, wpath, sz, NULL)) { printf("WPM fail GLE=%lu\n", GetLastError()); return 1; } + + HMODULE k32 = GetModuleHandleW(L"kernel32.dll"); + void *lla = (void *)GetProcAddress(k32, "LoadLibraryW"); + + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snap == INVALID_HANDLE_VALUE) { printf("snap fail\n"); return 1; } + + THREADENTRY32 te; te.dwSize = sizeof te; + int queued = 0, failed = 0; + if (Thread32First(snap, &te)) { + do { + if (te.th32OwnerProcessID != pid) continue; + HANDLE th = OpenThread(THREAD_SET_CONTEXT, FALSE, te.th32ThreadID); + if (!th) { failed++; continue; } + if (QueueUserAPC((PAPCFUNC)lla, th, (ULONG_PTR)mem)) queued++; + else failed++; + CloseHandle(th); + } while (Thread32Next(snap, &te)); + } + CloseHandle(snap); + CloseHandle(pr); + printf("APC queued to %d threads (%d failed); mem=%p\n", queued, failed, mem); + return 0; +} diff --git a/tools/apc_injector2.c b/tools/apc_injector2.c new file mode 100644 index 0000000..a2ba65f --- /dev/null +++ b/tools/apc_injector2.c @@ -0,0 +1,90 @@ +/* apc_injector2: two modes. + * mode "dll": queue LoadLibraryW(path) APCs (env-driven start) + * mode "init": LoadLibraryW via one APC, then queue + * slinky_init(keyw) APCs on all threads (loader-style start) + * usage: apc_injector2.exe dll + * apc_injector2.exe init <32-char key> + */ +#include +#include +#include +#include + +static int queue_all(DWORD pid, void *func, ULONG_PTR param) { + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snap == INVALID_HANDLE_VALUE) { printf("snap fail\n"); return -1; } + THREADENTRY32 te; te.dwSize = sizeof te; + int queued = 0; + if (Thread32First(snap, &te)) { + do { + if (te.th32OwnerProcessID != pid) continue; + HANDLE th = OpenThread(THREAD_SET_CONTEXT, FALSE, te.th32ThreadID); + if (!th) continue; + if (QueueUserAPC((PAPCFUNC)func, th, param)) queued++; + CloseHandle(th); + } while (Thread32Next(snap, &te)); + } + CloseHandle(snap); + return queued; +} + +static HMODULE find_remote_module(DWORD pid, const char *name) { + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid); + if (snap == INVALID_HANDLE_VALUE) return NULL; + MODULEENTRY32W me; me.dwSize = sizeof me; + HMODULE base = NULL; + wchar_t wname[128]; MultiByteToWideChar(CP_ACP, 0, name, -1, wname, 128); + if (Module32FirstW(snap, &me)) { + do { + if (_wcsicmp(me.szModule, wname) == 0) { base = me.hModule; break; } + } while (Module32NextW(snap, &me)); + } + CloseHandle(snap); + return base; +} + +int main(int argc, char **argv) { + if (argc < 4) { printf("usage: apc2 dll|init pid path [key]\n"); return 1; } + const char *mode = argv[1]; + DWORD pid = (DWORD)atoi(argv[2]); + + HANDLE pr = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, FALSE, pid); + if (!pr) { printf("OpenProcess fail GLE=%lu\n", GetLastError()); return 1; } + + if (strcmp(mode, "dll") == 0) { + wchar_t wpath[512]; + int wl = MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wpath, 512); + void *mem = VirtualAllocEx(pr, NULL, (SIZE_T)wl * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!mem || !WriteProcessMemory(pr, mem, wpath, (SIZE_T)wl * 2, NULL)) { printf("write fail\n"); return 1; } + FARPROC llw = GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"); + int n = queue_all(pid, (void *)llw, (ULONG_PTR)mem); + printf("dll mode: %d APCs\n", n); + } else { + /* init mode: step 1 load, step 2 call export */ + wchar_t wpath[512]; + int wl = MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wpath, 512); + void *mem = VirtualAllocEx(pr, NULL, (SIZE_T)wl * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!mem || !WriteProcessMemory(pr, mem, wpath, (SIZE_T)wl * 2, NULL)) { printf("write fail\n"); return 1; } + FARPROC llw = GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"); + int n = queue_all(pid, (void *)llw, (ULONG_PTR)mem); + printf("init mode: %d load APCs queued, waiting for module...\n", n); + HMODULE base = NULL; + for (int i = 0; i < 40 && !base; i++) { + Sleep(500); + base = find_remote_module(pid, "lib_beta_win.dll"); + } + if (!base) { printf("client never mapped (no alertable thread?)\n"); return 1; } + printf("client base = %p\n", (void *)base); + void *slinky_init = (void *)((ULONG_PTR)base + 0x1000); /* export RVA */ + + const char *key = argv[4]; + wchar_t wkey[64]; + int kl = MultiByteToWideChar(CP_ACP, 0, key, -1, wkey, 64); + void *kmem = VirtualAllocEx(pr, NULL, (SIZE_T)kl * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!kmem || !WriteProcessMemory(pr, kmem, wkey, (SIZE_T)kl * 2, NULL)) { printf("key write fail\n"); return 1; } + int n2 = queue_all(pid, slinky_init, (ULONG_PTR)kmem); + printf("init mode: %d slinky_init APCs queued (key=%s)\n", n2, key); + } + CloseHandle(pr); + return 0; +} diff --git a/tools/keytest2.c b/tools/keytest2.c new file mode 100644 index 0000000..8aeb745 --- /dev/null +++ b/tools/keytest2.c @@ -0,0 +1,75 @@ +/* keytest2: like slinky_key_test but JVM options come from a file + * (one option per line, %TEMP%\opencode\jvm_opts.txt), so we can + * reproduce Minecraft's exact JVM environment and bisect. + * usage: keytest2.exe <32-char key> + */ +#include +#include +#include +#include + +typedef jint (JNICALL *CreateVM_t)(JavaVM **, void **, void *); + +int main(int argc, char **argv) { + const char *key = (argc > 1) ? argv[1] : "E984C65498462E0BB9877136F4368535"; + size_t n = strlen(key); + if (n != 32) { printf("[k2] key must be 32 chars\n"); return 1; } + wchar_t wkey[64]; for (size_t i = 0; i <= n; i++) wkey[i] = (wchar_t)(unsigned char)key[i]; + SetEnvironmentVariableW(L"SLINKY_LICENSE", wkey); + printf("[k2] key=%s\n", key); fflush(stdout); + + /* load MC's jvm.dll (same VM the game uses) */ + HMODULE jvm = LoadLibraryW(L"C:\\Program Files\\Eclipse Adoptium\\jdk-8.0.502.7-hotspot\\jre\\bin\\server\\jvm.dll"); + if (!jvm) { printf("[k2] no jvm.dll\n"); return 1; } + CreateVM_t cjv = (CreateVM_t)GetProcAddress(jvm, "JNI_CreateJavaVM"); + + /* read options file */ + JavaVMOption opts[128]; int nopt = 0; + static char lines[128][1024]; + FILE *f = fopen("C:\\Users\\nicot\\AppData\\Local\\Temp\\opencode\\jvm_opts.txt", "r"); + if (f) { + char buf[1024]; + while (nopt < 128 && fgets(buf, sizeof buf, f)) { + size_t L = strlen(buf); + while (L && (buf[L-1] == '\n' || buf[L-1] == '\r')) buf[--L] = 0; + if (!L) continue; + strcpy(lines[nopt], buf); + opts[nopt].optionString = lines[nopt]; + nopt++; + } + fclose(f); + } + printf("[k2] %d JVM options\n", nopt); fflush(stdout); + + JavaVMInitArgs va; memset(&va, 0, sizeof va); + va.version = JNI_VERSION_1_8; va.ignoreUnrecognized = JNI_TRUE; + va.nOptions = nopt; va.options = opts; + JavaVM *vm = NULL; void *env = NULL; + jint cr = cjv ? cjv(&vm, &env, &va) : -1; + printf("[k2] JNI_CreateJavaVM -> %d\n", cr); fflush(stdout); + if (cr != 0) return 1; + + /* keep self-unload blocked so we survive a failed validate */ + HMODULE k32 = GetModuleHandleW(L"kernel32.dll"); + unsigned char *fle = (unsigned char *)GetProcAddress(k32, "FreeLibraryAndExitThread"); + unsigned char *et = (unsigned char *)GetProcAddress(k32, "ExitThread"); + unsigned char stub[7] = { 0x8B, 0xCA, 0xE9, 0, 0, 0, 0 }; + intptr_t rel = (intptr_t)et - (intptr_t)(fle + 7); + memcpy(stub + 3, &rel, 4); + DWORD old; + VirtualProtect(fle, 7, PAGE_EXECUTE_READWRITE, &old); + memcpy(fle, stub, 7); + VirtualProtect(fle, 7, old, &old); + + HMODULE h = LoadLibraryW(L"C:\\Users\\nicot\\Downloads\\lib_beta_win_unpacked.devirt.bin~\\lib_beta_win.dll"); + printf("[k2] client -> %p\n", (void *)h); fflush(stdout); + if (!h) return 1; + typedef int (WINAPI *init_t)(const wchar_t *); + init_t init = (init_t)GetProcAddress(h, "slinky_init"); + if (!init) return 1; + int r = init(wkey); + printf("[k2] slinky_init -> %d %s\n", r, r == 0 ? "<== VALID" : "<== rejected"); + fflush(stdout); + Sleep(3000); + return 0; +} diff --git a/tools/keytest3.c b/tools/keytest3.c new file mode 100644 index 0000000..8657059 --- /dev/null +++ b/tools/keytest3.c @@ -0,0 +1,52 @@ +/* keytest3: JVM + LoadLibrary(client) then a TRACER WINDOW (client mapped, + * guard unconsumed), then explicit slinky_init. For MemoryAccessMonitor use. + * usage: keytest3.exe <32-char key> + */ +#include +#include +#include +#include + +typedef jint (JNICALL *CreateVM_t)(JavaVM **, void **, void *); + +int main(int argc, char **argv) { + const char *key = (argc > 1) ? argv[1] : "E984C65498462E0BB9877136F4368535"; + size_t n = strlen(key); + if (n != 32) { printf("[k3] key must be 32 chars\n"); return 1; } + wchar_t wkey[64]; for (size_t i = 0; i <= n; i++) wkey[i] = (wchar_t)(unsigned char)key[i]; + SetEnvironmentVariableW(L"SLINKY_LICENSE", wkey); + printf("[k3] key=%s\n", key); fflush(stdout); + + HMODULE jvm = LoadLibraryW(L"C:\\Program Files\\Eclipse Adoptium\\jdk-8.0.502.7-hotspot\\jre\\bin\\server\\jvm.dll"); + if (jvm) { + CreateVM_t cjv = (CreateVM_t)GetProcAddress(jvm, "JNI_CreateJavaVM"); + JavaVM *vm = NULL; void *env = NULL; + JavaVMInitArgs va; memset(&va, 0, sizeof va); + va.version = JNI_VERSION_1_8; va.ignoreUnrecognized = JNI_TRUE; + printf("[k3] JNI_CreateJavaVM -> %d\n", cjv ? cjv(&vm, &env, &va) : -1); fflush(stdout); + } + + HMODULE k32 = GetModuleHandleW(L"kernel32.dll"); + unsigned char *fle = (unsigned char *)GetProcAddress(k32, "FreeLibraryAndExitThread"); + unsigned char *et = (unsigned char *)GetProcAddress(k32, "ExitThread"); + unsigned char stub[7] = { 0x8B, 0xCA, 0xE9, 0, 0, 0, 0 }; + intptr_t rel = (intptr_t)et - (intptr_t)(fle + 7); + memcpy(stub + 3, &rel, 4); + DWORD old; + VirtualProtect(fle, 7, PAGE_EXECUTE_READWRITE, &old); + memcpy(fle, stub, 7); + VirtualProtect(fle, 7, old, &old); + + HMODULE h = LoadLibraryW(L"C:\\Users\\nicot\\Downloads\\lib_beta_win_unpacked.devirt.bin~\\lib_beta_win.dll"); + printf("[k3] client -> %p\n", (void *)h); fflush(stdout); + printf("[k3] TRACER WINDOW open (60s)...\n"); fflush(stdout); + Sleep(60000); + printf("[k3] calling slinky_init...\n"); fflush(stdout); + typedef int (WINAPI *init_t)(const wchar_t *); + init_t init = (init_t)GetProcAddress(h, "slinky_init"); + int r = init ? init(wkey) : -1; + printf("[k3] slinky_init -> %d %s\n", r, r == 0 ? "<== VALID" : "<== rejected"); + fflush(stdout); + Sleep(15000); + return 0; +} diff --git a/tools/keytest4.c b/tools/keytest4.c new file mode 100644 index 0000000..af59398 --- /dev/null +++ b/tools/keytest4.c @@ -0,0 +1,57 @@ +/* keytest4: keytest3 + N dummy threads before slinky_init. + * usage: keytest4.exe <32-char key> + */ +#include +#include +#include +#include + +typedef jint (JNICALL *CreateVM_t)(JavaVM **, void **, void *); + +static DWORD WINAPI dummy(LPVOID p) { Sleep(INFINITE); return 0; } + +int main(int argc, char **argv) { + const char *key = (argc > 1) ? argv[1] : "E984C65498462E0BB9877136F4368535"; + int nthr = (argc > 2) ? atoi(argv[2]) : 0; + size_t n = strlen(key); + if (n != 32) { printf("[k4] key must be 32 chars\n"); return 1; } + wchar_t wkey[64]; for (size_t i = 0; i <= n; i++) wkey[i] = (wchar_t)(unsigned char)key[i]; + SetEnvironmentVariableW(L"SLINKY_LICENSE", wkey); + printf("[k4] key=%s threads=%d\n", key, nthr); fflush(stdout); + + for (int i = 0; i < nthr; i++) { + HANDLE h = CreateThread(NULL, 0, dummy, NULL, 0, NULL); + if (h) CloseHandle(h); + } + printf("[k4] %d dummy threads spawned\n", nthr); fflush(stdout); + + HMODULE jvm = LoadLibraryW(L"C:\\Program Files\\Eclipse Adoptium\\jdk-8.0.502.7-hotspot\\jre\\bin\\server\\jvm.dll"); + if (jvm) { + CreateVM_t cjv = (CreateVM_t)GetProcAddress(jvm, "JNI_CreateJavaVM"); + JavaVM *vm = NULL; void *env = NULL; + JavaVMInitArgs va; memset(&va, 0, sizeof va); + va.version = JNI_VERSION_1_8; va.ignoreUnrecognized = JNI_TRUE; + printf("[k4] JNI_CreateJavaVM -> %d\n", cjv ? cjv(&vm, &env, &va) : -1); fflush(stdout); + } + + HMODULE k32 = GetModuleHandleW(L"kernel32.dll"); + unsigned char *fle = (unsigned char *)GetProcAddress(k32, "FreeLibraryAndExitThread"); + unsigned char *et = (unsigned char *)GetProcAddress(k32, "ExitThread"); + unsigned char stub[7] = { 0x8B, 0xCA, 0xE9, 0, 0, 0, 0 }; + intptr_t rel = (intptr_t)et - (intptr_t)(fle + 7); + memcpy(stub + 3, &rel, 4); + DWORD old; + VirtualProtect(fle, 7, PAGE_EXECUTE_READWRITE, &old); + memcpy(fle, stub, 7); + VirtualProtect(fle, 7, old, &old); + + HMODULE h = LoadLibraryW(L"C:\\Users\\nicot\\Downloads\\lib_beta_win_unpacked.devirt.bin~\\lib_beta_win.dll"); + printf("[k4] client -> %p\n", (void *)h); fflush(stdout); + typedef int (WINAPI *init_t)(const wchar_t *); + init_t init = (init_t)GetProcAddress(h, "slinky_init"); + int r = init ? init(wkey) : -1; + printf("[k4] slinky_init -> %d %s\n", r, r == 0 ? "<== VALID" : "<== rejected"); + fflush(stdout); + Sleep(3000); + return 0; +} diff --git a/tools/keytest6.c b/tools/keytest6.c new file mode 100644 index 0000000..19db330 --- /dev/null +++ b/tools/keytest6.c @@ -0,0 +1,70 @@ +/* keytest6: JVM + RUN a Java main (SleepMain2), then load client + validate. + * Discriminates "JVM that has executed application code" from virgin JVM. + * usage: keytest6.exe <32-char key> + */ +#include +#include +#include +#include + +typedef jint (JNICALL *CreateVM_t)(JavaVM **, void **, void *); + +int main(int argc, char **argv) { + const char *key = (argc > 1) ? argv[1] : "E984C65498462E0BB9877136F4368535"; + size_t n = strlen(key); + if (n != 32) { printf("[k6] key must be 32 chars\n"); return 1; } + wchar_t wkey[64]; for (size_t i = 0; i <= n; i++) wkey[i] = (wchar_t)(unsigned char)key[i]; + SetEnvironmentVariableW(L"SLINKY_LICENSE", wkey); + printf("[k6] key=%s\n", key); fflush(stdout); + + HMODULE jvm = LoadLibraryW(L"C:\\Program Files\\Eclipse Adoptium\\jdk-8.0.502.7-hotspot\\jre\\bin\\server\\jvm.dll"); + if (!jvm) { printf("[k6] no jvm.dll\n"); return 1; } + CreateVM_t cjv = (CreateVM_t)GetProcAddress(jvm, "JNI_CreateJavaVM"); + + static char cp[1024]; + strcpy(cp, "-Djava.class.path=C:\\Users\\nicot\\AppData\\Local\\Temp\\opencode\\sleepmain"); + JavaVMOption opts[2]; opts[0].optionString = cp; + JavaVMInitArgs va; memset(&va, 0, sizeof va); + va.version = JNI_VERSION_1_8; va.ignoreUnrecognized = JNI_TRUE; + va.nOptions = 1; va.options = opts; + JavaVM *vm = NULL; JNIEnv *env = NULL; + jint cr = cjv(&vm, (void **)&env, &va); + printf("[k6] JNI_CreateJavaVM -> %d\n", cr); fflush(stdout); + if (cr != 0) return 1; + + /* RUN real Java application code: SleepMain2.main() */ + jclass cls = (*env)->FindClass(env, "SleepMain2"); + if (!cls) { printf("[k6] FindClass(SleepMain2) FAILED\n"); if ((*env)->ExceptionCheck(env)) (*env)->ExceptionDescribe(env); return 1; } + jmethodID mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); + if (!mid) { printf("[k6] GetStaticMethodID(main) FAILED\n"); return 1; } + jclass scls = (*env)->FindClass(env, "java/lang/String"); + jobjectArray jargs = (*env)->NewObjectArray(env, 1, scls, (*env)->NewStringUTF(env, "run")); + printf("[k6] invoking SleepMain2.main ...\n"); fflush(stdout); + (*env)->CallStaticVoidMethod(env, cls, mid, jargs); + if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); return 1; } + printf("[k6] main() completed - JVM has now run application code\n"); fflush(stdout); + + /* keep self-unload blocked */ + HMODULE k32 = GetModuleHandleW(L"kernel32.dll"); + unsigned char *fle = (unsigned char *)GetProcAddress(k32, "FreeLibraryAndExitThread"); + unsigned char *et = (unsigned char *)GetProcAddress(k32, "ExitThread"); + unsigned char stub[7] = { 0x8B, 0xCA, 0xE9, 0, 0, 0, 0 }; + intptr_t rel = (intptr_t)et - (intptr_t)(fle + 7); + memcpy(stub + 3, &rel, 4); + DWORD old; + VirtualProtect(fle, 7, PAGE_EXECUTE_READWRITE, &old); + memcpy(fle, stub, 7); + VirtualProtect(fle, 7, old, &old); + + HMODULE h = LoadLibraryW(L"C:\\Users\\nicot\\Downloads\\lib_beta_win_unpacked.devirt.bin~\\lib_beta_win.dll"); + printf("[k6] client -> %p\n", (void *)h); fflush(stdout); + if (!h) return 1; + typedef int (WINAPI *init_t)(const wchar_t *); + init_t init = (init_t)GetProcAddress(h, "slinky_init"); + int r = init ? init(wkey) : -1; + printf("[k6] slinky_init -> %d %s\n", r, r == 0 ? "<== VALID" : "<== rejected"); + fflush(stdout); + Sleep(3000); + if (vm) (*vm)->DestroyJavaVM(vm); + return 0; +}