From 1931072b96d00f51f3c88b92d2f7b252877f9044 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sat, 15 Aug 2026 12:30:49 +0800 Subject: [PATCH] Fix: harden injector/libinject/loader against UB, format bugs and leaks --- includes/JuiceAgent/Config.hpp | 2 +- src/injector/injector.c | 2 +- src/libinject/libinject.c | 32 +++++++++++++++------------- src/libloader/common/Loader.cpp | 16 +++++++------- src/libloader/includes/JvmAttach.hpp | 3 +-- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index 08e93f5..40a78df 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -90,7 +90,7 @@ public: return value; } - catch (const std::exception& e) { + catch (...) { spdlog::warn("Config get failed: {}, using default value", path); return default_value; } diff --git a/src/injector/injector.c b/src/injector/injector.c index ade3e64..adef061 100644 --- a/src/injector/injector.c +++ b/src/injector/injector.c @@ -116,7 +116,7 @@ static int run(int argc, char** argv) return 3; } - inject_fn_t inject = (inject_fn_t)proc; + inject_fn_t inject = (inject_fn_t)(void*)proc; printf("[*] Calling inject(pid=%d, dllPath=\"%s\", currentDir=\"%s\")\n", pid, dll_path, cwd); diff --git a/src/libinject/libinject.c b/src/libinject/libinject.c index b58e4ce..d13a5eb 100644 --- a/src/libinject/libinject.c +++ b/src/libinject/libinject.c @@ -5,8 +5,7 @@ #include #define WIN_X64 -#define BREAK_WITH_ERROR( e ) { printf("[-] %s. Error=%l", e, GetLastError()); break; } -#define MAX_PATH 260 +#define BREAK_WITH_ERROR(e) { printf("[-] %s. Error=%lu\n", e, GetLastError()); break; } __declspec(dllexport) int inject(int pid, char *path, char *params){ @@ -83,21 +82,21 @@ int inject(int pid, char *path, char *params){ } - /* allocate remote memory for InjectParameters */ - lpRemoteParam = VirtualAllocEx(hProcess, NULL, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); - if (!lpRemoteParam) - BREAK_WITH_ERROR("VirtualAllocEx for remote param failed\n"); - - printf("remote param addr = %p\n", lpRemoteParam); - /* write params to remote memory */ SIZE_T written = 0; if (params) { - if (!WriteProcessMemory(hProcess, lpRemoteParam, params, MAX_PATH, &written) || written != MAX_PATH) - BREAK_WITH_ERROR("WriteProcessMemory for remote param failed\n"); + /* allocate remote memory for params */ + lpRemoteParam = VirtualAllocEx(hProcess, NULL, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + if (!lpRemoteParam) + BREAK_WITH_ERROR("VirtualAllocEx for remote param failed"); + + printf("remote param addr = %p\n", lpRemoteParam); + + SIZE_T param_len = strlen(params) + 1; + if (!WriteProcessMemory(hProcess, lpRemoteParam, params, param_len, &written) || written != param_len) + BREAK_WITH_ERROR("WriteProcessMemory for remote param failed"); } else { printf("No parameters provided; using null pointer\n"); - lpRemoteParam = NULL; } /* read back some bytes to verify write */ @@ -124,7 +123,7 @@ int inject(int pid, char *path, char *params){ BREAK_WITH_ERROR("LoadRemoteLibraryR returned NULL"); } - printf("[+] DLL injected: '%s' into PID %d, remote param at %p\n", path, dwProcessId, lpRemoteParam); + printf("[+] DLL injected: '%s' into PID %lu, remote param at %p\n", path, dwProcessId, lpRemoteParam); /* wait for reflective loader thread to complete */ WaitForSingleObject(hRemoteThread, INFINITE); @@ -164,6 +163,7 @@ int inject(int pid, char *path, char *params){ */ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2 (JNIEnv *env, jobject obj, jint pid, jstring path) { + (void)obj; // Injection Path const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL); @@ -181,12 +181,13 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ */ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2Ljava_lang_String_2 (JNIEnv *env, jobject obj, jint pid , jstring path, jstring configDir){ - + (void)obj; const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL); const char* ConfigDir = (*env)->GetStringUTFChars(env, configDir, NULL); char params[MAX_PATH] = {0}; /* use a real buffer */ strncpy(params, ConfigDir ? ConfigDir : "", MAX_PATH - 1); + params[MAX_PATH - 1] = '\0'; int ret = inject(pid, (char*)InjectionDLL, params); @@ -210,6 +211,7 @@ static int g_count = 0; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { + (void)lParam; wchar_t title[256]; GetWindowTextW(hwnd, title, 256); if (wcslen(title) == 0) return TRUE; @@ -229,7 +231,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle (JNIEnv *env, jclass clazz, jstring keyword) { - + (void)clazz; const jchar *input = (*env)->GetStringChars(env, keyword, NULL); wcsncpy(g_keyword, (const wchar_t *)input, 255); g_keyword[255] = L'\0'; diff --git a/src/libloader/common/Loader.cpp b/src/libloader/common/Loader.cpp index 36f8c16..b6d1109 100644 --- a/src/libloader/common/Loader.cpp +++ b/src/libloader/common/Loader.cpp @@ -52,13 +52,13 @@ static bool invoke_juiceagent_init(JNIEnv* env, const LoaderConfig& config) { return true; } -static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) { +static void init(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) { if (!runtime_dir) { spdlog::warn("Runtime directory is not set!"); } // Load configuration - JuiceAgent::Config::Config cfg(runtime_dir); + JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : ""); if (!cfg.is_valid()) { spdlog::error("Failed to load configuration!"); @@ -91,9 +91,9 @@ static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) { spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256); // Load JuiceAgent API to system classloader - jint status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str()); - if (status != JNI_OK) { - spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", status); + jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str()); + if (status != JVMTI_ERROR_NONE) { + spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast(status)); return; } @@ -108,16 +108,16 @@ static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) { void entrypoint(const char* runtime_dir) { // Attach to the JVM - JuiceAgent::Loader::Jvm jvm; + Jvm jvm; if (!jvm.attach()) { - spdlog::error("Failed to attach to JVM!"); + spdlog::error("Failed to attach to JVM!"); return; } // Get jvm and jni env JNIEnv* env = jvm.get_env(); auto* jvmti = jvm.get_jvmti(); - if(!env || !jvmti) { + if (!env || !jvmti) { spdlog::error("Failed to get jni env or jvmti env!"); return; } diff --git a/src/libloader/includes/JvmAttach.hpp b/src/libloader/includes/JvmAttach.hpp index 80acd93..6fcc414 100644 --- a/src/libloader/includes/JvmAttach.hpp +++ b/src/libloader/includes/JvmAttach.hpp @@ -56,10 +56,9 @@ private: attached_ = false; } - bool retry(const char* what, int attempt) { + void retry(const char* what, int attempt) { spdlog::debug("Failed to get {}, attempt {}/{}", what, attempt, max_try_); std::this_thread::sleep_for(std::chrono::milliseconds(retry_delay_ms_)); - return false; } bool acquire_jvm() {