From cf5d87f7a7abb3822e4f606012d37b65d251f7de Mon Sep 17 00:00:00 2001 From: mkrsym1 Date: Sun, 2 Jul 2023 23:21:17 +0300 Subject: [PATCH] Refuse to launch if the patcher is inside the game directory --- injector/src/dll.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/injector/src/dll.c b/injector/src/dll.c index 46f5f58..10776f5 100644 --- a/injector/src/dll.c +++ b/injector/src/dll.c @@ -5,6 +5,10 @@ #include +typedef char *(*wgufn_t)(wchar_t* path); // wine_get_unix_file_name + +const char *J_MB_TITLE = "Jadeite Launcher Payload"; + BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { // Only listen for attach if (reason != DLL_PROCESS_ATTACH) { @@ -25,6 +29,38 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { strcpy(workdir, targetExe); *(strrchr(workdir, '\\')) = '\0'; + // SAFETY: verify that the injector is not inside the game directory + HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); + wgufn_t wine_get_unix_file_name = (wgufn_t)GetProcAddress(kernel32, "wine_get_unix_file_name"); + + if (wine_get_unix_file_name) { + wchar_t wInjectDll[MAX_PATH], wWorkdir[MAX_PATH]; + MultiByteToWideChar(CP_UTF8, 0, injectDll, strlen(injectDll) + 1, wInjectDll, MAX_PATH); + MultiByteToWideChar(CP_UTF8, 0, workdir, strlen(workdir) + 1, wWorkdir, MAX_PATH); + + char *unixInjectDll = wine_get_unix_file_name(wInjectDll); + char *unixWorkdir = wine_get_unix_file_name(wWorkdir); + + char startsWith = 0; + while (*unixInjectDll != '\0' && *unixWorkdir != '\0') { + startsWith = *unixInjectDll == *unixWorkdir; + if (!startsWith) break; + + unixInjectDll++, unixWorkdir++; + } + + HANDLE heap = GetProcessHeap(); + HeapFree(heap, 0, unixInjectDll); + HeapFree(heap, 0, unixWorkdir); + + if (startsWith) { + MessageBoxA(NULL, "Putting the patcher (or any other foreign PE binaries) inside the game directory is dangerous! Please move it elsewhere.", J_MB_TITLE, MB_OK | MB_ICONERROR); + exit(1); + } + } else { + MessageBoxA(NULL, "Could not find wine_get_unix_file_name! Wine version too old?", J_MB_TITLE, MB_OK | MB_ICONWARNING); + } + // Start the game STARTUPINFO si; ZeroMemory(&si, sizeof(si)); @@ -47,7 +83,7 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { )) { char message[64]; sprintf(message, "Failed to start game process: %ld", GetLastError()); - MessageBoxA(NULL, message, "Jadeite Launcher Payload", MB_OK | MB_ICONERROR); + MessageBoxA(NULL, message, J_MB_TITLE, MB_OK | MB_ICONERROR); exit(1); } @@ -62,7 +98,7 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { if (waitEnabled && strcmp(waitEnabled, "") != 0) { char message[64]; sprintf(message, "PID: %ld. Press OK to continue", pi.dwProcessId); - MessageBoxA(NULL, message, "Jadeite Launcher Payload", MB_OK | MB_ICONINFORMATION); + MessageBoxA(NULL, message, J_MB_TITLE, MB_OK | MB_ICONINFORMATION); } // Resume the process