From 9a13a10a2323a8c9513948adfaaec67903ea52d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=91=A8XiaoZhou?= Date: Fri, 17 Oct 2025 03:32:49 +0800 Subject: [PATCH] [WIP] [Update & BugFix] Config --- includes/JuiceAgent/InjectParameters.h | 7 +- java/InjectorNative.class | Bin 294 -> 330 bytes java/InjectorNative.java | 7 +- ...ou233_juiceagent_injector_InjectorNative.h | 16 +-- src/agent/CMakeLists.txt | 2 + src/agent/libagent.c | 65 ++++++------ src/injector/include/InjectorNative.h | 16 +-- src/injector/libinjector.c | 99 +++++++----------- 8 files changed, 94 insertions(+), 118 deletions(-) diff --git a/includes/JuiceAgent/InjectParameters.h b/includes/JuiceAgent/InjectParameters.h index 864a29f..5935d65 100644 --- a/includes/JuiceAgent/InjectParameters.h +++ b/includes/JuiceAgent/InjectParameters.h @@ -6,12 +6,7 @@ #define INJECT_PATH_MAX 4096 typedef struct InjectParameters{ - char JuiceLoaderJarPath[INJECT_PATH_MAX]; - char BootstrapApiPath[INJECT_PATH_MAX]; - // Optional: - char JuiceLoaderLibPath[INJECT_PATH_MAX]; - // Optional: - char EntryJarPath[INJECT_PATH_MAX]; + char ConfigPath[INJECT_PATH_MAX]; } InjectParameters; #endif \ No newline at end of file diff --git a/java/InjectorNative.class b/java/InjectorNative.class index 5a02febab99f05ac9a21dea57da851ef8e03704c..ff0c75c181d4c015ad1451e9e6dd6a3f9b9ca6d0 100644 GIT binary patch delta 34 qcmZ3+bc$)hl!>z?MKv@$eXi_`SEDYlS delta 20 bcmX@bw2W!O6fPDGPt7Ps1~CoKiHAA>J~{>d diff --git a/java/InjectorNative.java b/java/InjectorNative.java index fefada9..8bb4fc7 100644 --- a/java/InjectorNative.java +++ b/java/InjectorNative.java @@ -4,11 +4,12 @@ public class InjectorNative { /* * @param pid target process id */ - public native boolean inject(int pid); + public native boolean inject(int pid, String path); /* * @param pid target process id - * @param path libinjector path + * @param path injection dll path + * @param configPath config file path */ - public native boolean inject(int pid, String path); + public native boolean inject(int pid, String path, String configPath); } \ No newline at end of file diff --git a/java/cn_xiaozhou233_juiceagent_injector_InjectorNative.h b/java/cn_xiaozhou233_juiceagent_injector_InjectorNative.h index a69089c..5e30366 100644 --- a/java/cn_xiaozhou233_juiceagent_injector_InjectorNative.h +++ b/java/cn_xiaozhou233_juiceagent_injector_InjectorNative.h @@ -7,14 +7,6 @@ #ifdef __cplusplus extern "C" { #endif -/* - * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative - * Method: inject - * Signature: (I)Z - */ -JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__I - (JNIEnv *, jobject, jint); - /* * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative * Method: inject @@ -23,6 +15,14 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2 (JNIEnv *, jobject, jint, jstring); +/* + * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative + * Method: inject + * Signature: (ILjava/lang/String;Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2Ljava_lang_String_2 + (JNIEnv *, jobject, jint, jstring, jstring); + #ifdef __cplusplus } #endif diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index 0b1fc79..5a794fe 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -1 +1,3 @@ # src/core/CMakeLists.txt + +target_link_libraries(agent PRIVATE shlwapi) \ No newline at end of file diff --git a/src/agent/libagent.c b/src/agent/libagent.c index cbd406a..4b0b8b6 100644 --- a/src/agent/libagent.c +++ b/src/agent/libagent.c @@ -23,11 +23,7 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) { return 1; } - log_info("%s ThreadProc got param: \nJuiceLoaderJarPath: %s\nJuiceLoaderLibPath: %s\nEntryJarPath: %s\nBootstrapApiPath: %s", LOG_PREFIX, - param->JuiceLoaderJarPath, - param->JuiceLoaderLibPath, - param->EntryJarPath, - param->BootstrapApiPath); + log_info("%s ThreadProc got param: ConfigPath: %s", LOG_PREFIX, param->ConfigPath); const char* bootstrapApiJar = param->BootstrapApiPath; if (bootstrapApiJar == NULL) { @@ -147,41 +143,44 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){ log_info("%s DLL_PROCESS_ATTACH", LOG_PREFIX); DisableThreadLibraryCalls(hinstDLL); + + InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters)); + if (localParm == NULL) { + log_error("%s malloc failed for InjectParameters", LOG_PREFIX); + break; + } + memset(localParm, 0, sizeof(InjectParameters)); if (lpReserved != NULL) { InjectParameters *remoteParm = (InjectParameters*)lpReserved; - InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters)); - if (localParm == NULL) { - log_error("%s malloc failed for InjectParameters", LOG_PREFIX); - break; - } - - memset(localParm, 0, sizeof(InjectParameters)); - - safe_copy(localParm->JuiceLoaderJarPath, remoteParm->JuiceLoaderJarPath, sizeof(localParm->JuiceLoaderJarPath)); - safe_copy(localParm->JuiceLoaderLibPath, remoteParm->JuiceLoaderLibPath, sizeof(localParm->JuiceLoaderLibPath)); - safe_copy(localParm->EntryJarPath, remoteParm->EntryJarPath, sizeof(localParm->EntryJarPath)); - safe_copy(localParm->BootstrapApiPath, remoteParm->BootstrapApiPath, sizeof(localParm->BootstrapApiPath)); - - log_info("%s DllMain got param: \nJuiceLoaderJarPath: %s\nJuiceLoaderLibPath: %s\nEntryJarPath: %s\nBootstrapApiPath: %s", LOG_PREFIX, - localParm->JuiceLoaderJarPath, - localParm->JuiceLoaderLibPath, - localParm->EntryJarPath, - localParm->BootstrapApiPath); - - HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL); - if (hThread) { - CloseHandle(hThread); - } else { - log_error("%s CreateThread failed %lu", LOG_PREFIX, GetLastError()); - free(localParm); - } + safe_copy(localParm->ConfigPath, remoteParm->ConfigPath, sizeof(localParm->ConfigPath)); } else { - log_error("%s RemoteParm is NULL!", LOG_PREFIX); + log_info("%s RemoteParm is NULL!", LOG_PREFIX); + + char DllDir[INJECT_PATH_MAX] = {0}; + + if (GetModuleFileNameA(hinstDLL, DllDir, MAX_PATH) != 0) { + PathRemoveFileSpecA(DllDir); + log_info("%s path: %s", LOG_PREFIX, DllDir); + } else { + log_error("GetModuleFileName failed, error=%lu\n", GetLastError()); + return -1; + } + safe_copy(localParm->ConfigPath, DllDir, sizeof(localParm->ConfigPath)); } + + log_info("%s ConfigPath: %s", LOG_PREFIX, localParm->ConfigPath); + + HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL); + if (hThread) { + CloseHandle(hThread); + } else { + log_error("%s CreateThread failed %lu", LOG_PREFIX, GetLastError()); + free(localParm); + } + break; - case DLL_PROCESS_DETACH: log_info("%s DLL_PROCESS_DETACH", LOG_PREFIX); break; diff --git a/src/injector/include/InjectorNative.h b/src/injector/include/InjectorNative.h index a69089c..5e30366 100644 --- a/src/injector/include/InjectorNative.h +++ b/src/injector/include/InjectorNative.h @@ -7,14 +7,6 @@ #ifdef __cplusplus extern "C" { #endif -/* - * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative - * Method: inject - * Signature: (I)Z - */ -JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__I - (JNIEnv *, jobject, jint); - /* * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative * Method: inject @@ -23,6 +15,14 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2 (JNIEnv *, jobject, jint, jstring); +/* + * Class: cn_xiaozhou233_juiceagent_injector_InjectorNative + * Method: inject + * Signature: (ILjava/lang/String;Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2Ljava_lang_String_2 + (JNIEnv *, jobject, jint, jstring, jstring); + #ifdef __cplusplus } #endif diff --git a/src/injector/libinjector.c b/src/injector/libinjector.c index f1249f3..15a3402 100644 --- a/src/injector/libinjector.c +++ b/src/injector/libinjector.c @@ -137,7 +137,7 @@ int inject(int pid, char *path, InjectParameters *params){ InjectParameters verify; SIZE_T read = 0; if (ReadProcessMemory(hProcess, lpRemoteParam, &verify, sizeof(InjectParameters), &read)) { - log_debug("[*] Read back remote param, JuiceLoaderJarPath=%.*s", (int)sizeof(verify.JuiceLoaderJarPath), verify.JuiceLoaderJarPath); + log_debug("[*] Read back remote param, ConfigPath=%.*s", (int)sizeof(verify.ConfigPath), verify.ConfigPath); } else { log_error("[-] ReadProcessMemory failed: %lu", GetLastError()); } @@ -187,65 +187,44 @@ int inject(int pid, char *path, InjectParameters *params){ return bSuccess ? 0 : -1; } -/* wrapper using HOME */ -int inject_pid(int pid){ - const char* home = getenv("HOME"); - if (!home) { - home = getenv("USERPROFILE"); - if (!home) - return -1; - - } - - char dir[INJECT_PATH_MAX]; - snprintf(dir, sizeof(dir), "%s\\JuiceAgent\\", home); - - char path[INJECT_PATH_MAX]; - snprintf(path, sizeof(path), "%s\\libagent.dll", dir); - - char loaderJarPath[INJECT_PATH_MAX]; - snprintf(loaderJarPath, sizeof(loaderJarPath), "%s\\JuiceLoader.jar", dir); - - InjectParameters params; - memset(¶ms, 0, sizeof(params)); - - strncpy(params.JuiceLoaderJarPath, loaderJarPath, sizeof(params.JuiceLoaderJarPath) - 1); - params.JuiceLoaderJarPath[sizeof(params.JuiceLoaderJarPath) - 1] = '\0'; - - strncpy(params.JuiceLoaderLibPath, "", sizeof(params.JuiceLoaderLibPath) - 1); - params.JuiceLoaderLibPath[sizeof(params.JuiceLoaderLibPath) - 1] = '\0'; - - strncpy(params.EntryJarPath, "", sizeof(params.EntryJarPath) - 1); - params.EntryJarPath[sizeof(params.EntryJarPath) - 1] = '\0'; - - return inject(pid, path, ¶ms); -} - -/* JNI interface */ -JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_orangex_injector_InjectorNative_inject - (JNIEnv *env, jobject obj, jint pid,jstring jAgentPth, jstring jJuiceLoaderJarPath, jstring jJuiceLoaderLibPath, jstring jEntryJarPath, jstring jBootStrapApiPath) { - - const char* localAgentPath = (*env)->GetStringUTFChars(env, jAgentPth, NULL); - const char* localLoaderJar = (*env)->GetStringUTFChars(env, jJuiceLoaderJarPath, NULL); - const char* localLoaderLib = (*env)->GetStringUTFChars(env, jJuiceLoaderLibPath, NULL); - const char* localEntryJar = (*env)->GetStringUTFChars(env, jEntryJarPath, NULL); - const char* bootstrapApiPath = (*env)->GetStringUTFChars(env, jBootStrapApiPath, NULL); - - InjectParameters params; - memset(¶ms, 0, sizeof(params)); - - strncpy(params.JuiceLoaderJarPath, localLoaderJar ? localLoaderJar : "", sizeof(params.JuiceLoaderJarPath)-1); - strncpy(params.JuiceLoaderLibPath, localLoaderLib ? localLoaderLib : "", sizeof(params.JuiceLoaderLibPath)-1); - strncpy(params.EntryJarPath, localEntryJar ? localEntryJar : "", sizeof(params.EntryJarPath)-1); - strncpy(params.BootstrapApiPath, bootstrapApiPath ? bootstrapApiPath : "", sizeof(params.BootstrapApiPath)-1); - - int ret = inject(pid, (char*)localAgentPath, ¶ms); - - if (localAgentPath) (*env)->ReleaseStringUTFChars(env, jAgentPth, localAgentPath); - if (localLoaderJar) (*env)->ReleaseStringUTFChars(env, jJuiceLoaderJarPath, localLoaderJar); - if (localLoaderLib) (*env)->ReleaseStringUTFChars(env, jJuiceLoaderLibPath, localLoaderLib); - if (localEntryJar) (*env)->ReleaseStringUTFChars(env, jEntryJarPath, localEntryJar); - if (bootstrapApiPath) (*env)->ReleaseStringUTFChars(env, jBootStrapApiPath, bootstrapApiPath); +/* +* JNI Function: inject(int pid) +* Note: +*/ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2 + (JNIEnv *env, jobject obj, jint pid, jstring path) { + // Injection Path + const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL); + int ret = inject(pid, InjectionDLL, NULL); + (*env)->ReleaseStringUTFChars(env, path, InjectionDLL); return (ret == 0) ? JNI_TRUE : JNI_FALSE; } + + +/* +* JNI Function: inject(int pid, String path) +* Param pid: target process id +* Param path: path of inject dll +* Param configPath: path of config file (toml) +*/ +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 configPath){ + // Injection Path + const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL); + // Config Path + const char* ConfigPath = (*env)->GetStringUTFChars(env, configPath, NULL); + + InjectParameters params; + memset(¶ms, 0, sizeof(params)); + + strncpy(params.ConfigPath, ConfigPath ? ConfigPath : "", sizeof(params.ConfigPath)-1); + + int ret = inject(pid, (char*)InjectionDLL, ¶ms); + + // Clean up + (*env)->ReleaseStringUTFChars(env, path, InjectionDLL); + (*env)->ReleaseStringUTFChars(env, configPath, ConfigPath); + + return (ret == 0) ? JNI_TRUE : JNI_FALSE; +} \ No newline at end of file