From de971d6481a8ca6bc6d29dcf855de6a74ade279e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=91=A8XiaoZhou?= Date: Sat, 8 Nov 2025 14:12:35 +0800 Subject: [PATCH] [Refactor] CPP --- CMakeLists.txt | 2 +- cmake/JuiceModule.cmake | 2 + includes/JuiceAgent/InjectParameters.h | 12 - includes/JuiceAgent/JuiceAgent.h | 11 + src/agent/{libagent.c => libagent.c.txt} | 0 src/agent/libagent.cpp | 221 ++++++++++++++++++ src/agent/utils/Logger.hpp | 17 ++ .../utils/{datautils.c => datautils.c.txt} | 0 src/agent/utils/jvmutils.c | 49 ---- 9 files changed, 252 insertions(+), 62 deletions(-) delete mode 100644 includes/JuiceAgent/InjectParameters.h create mode 100644 includes/JuiceAgent/JuiceAgent.h rename src/agent/{libagent.c => libagent.c.txt} (100%) create mode 100644 src/agent/libagent.cpp create mode 100644 src/agent/utils/Logger.hpp rename src/agent/utils/{datautils.c => datautils.c.txt} (100%) delete mode 100644 src/agent/utils/jvmutils.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f803109..8c16881 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,4 +34,4 @@ include(cmake/JuiceModule.cmake) # modules add_juice_module(agent) -add_juice_module(injector) \ No newline at end of file +#add_juice_module(injector) \ No newline at end of file diff --git a/cmake/JuiceModule.cmake b/cmake/JuiceModule.cmake index 391cca2..a2b7bc1 100644 --- a/cmake/JuiceModule.cmake +++ b/cmake/JuiceModule.cmake @@ -7,7 +7,9 @@ function(add_juice_module NAME) # Collect all source files in module src and utils file(GLOB MODULE_SOURCES "${CMAKE_SOURCE_DIR}/src/${NAME}/*.c" + "${CMAKE_SOURCE_DIR}/src/${NAME}/*.cpp" "${CMAKE_SOURCE_DIR}/src/${NAME}/utils/*.c" + "${CMAKE_SOURCE_DIR}/src/${NAME}/utils/*.cpp" ) # Create shared library diff --git a/includes/JuiceAgent/InjectParameters.h b/includes/JuiceAgent/InjectParameters.h deleted file mode 100644 index cc07434..0000000 --- a/includes/JuiceAgent/InjectParameters.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef INJECTPARAMETERS_H -#define INJECTPARAMETERS_H - -#include - -#define INJECT_PATH_MAX 4096 - -typedef struct InjectParameters{ - char ConfigDir[INJECT_PATH_MAX]; -} InjectParameters; - -#endif \ No newline at end of file diff --git a/includes/JuiceAgent/JuiceAgent.h b/includes/JuiceAgent/JuiceAgent.h new file mode 100644 index 0000000..4ee91a5 --- /dev/null +++ b/includes/JuiceAgent/JuiceAgent.h @@ -0,0 +1,11 @@ +#ifndef JUICEAGENT_H +#define JUICEAGENT_H + +#define INJECT_PATH_MAX 4096 + +typedef struct InjectParams{ + char JuiceLoaderJarPath[INJECT_PATH_MAX]; + char ConfigDir[INJECT_PATH_MAX]; +} InjectParams; + +#endif // JUICEAGENT_H \ No newline at end of file diff --git a/src/agent/libagent.c b/src/agent/libagent.c.txt similarity index 100% rename from src/agent/libagent.c rename to src/agent/libagent.c.txt diff --git a/src/agent/libagent.cpp b/src/agent/libagent.cpp new file mode 100644 index 0000000..5224a1e --- /dev/null +++ b/src/agent/libagent.cpp @@ -0,0 +1,221 @@ +// For Reflective Inject +#include "ReflectiveLoader.h" +#define WIN_X64 + +#include +#include "Logger.hpp" +#include + +// For Java VM +#include +#include + +struct _JuiceAgent { + bool isAttached; + JavaVM *jvm; + jvmtiEnv *jvmti; + JNIEnv *env; +} JuiceAgent = {false, NULL, NULL, NULL}; + +HINSTANCE hAppInstance = nullptr; + +int init_juiceloader(JNIEnv *env, _jvmtiEnv *jvmti) { + PLOGI << "EXAMPLE INIT JUICELOADER!"; + return 0; +} + +static int GetJavaEnv() { + jint res = JNI_ERR; + JavaVM *jvm = NULL; + jvmtiEnv *jvmti = NULL; + JNIEnv *env = NULL; + + // Get the JVM + res = JNI_GetCreatedJavaVMs(&jvm, 1, NULL); + if (res != JNI_OK) { + PLOGE.printf("JNI_GetCreatedJavaVMs failed: %d", res); + return res; + } + JuiceAgent.jvm = jvm; + PLOGI << "Got the JVM"; + + // Get the JNIEnv + res = jvm->GetEnv((void**)&env, JNI_VERSION_1_8); + if (res != JNI_OK) { + PLOGI << "GetEnv failed, trying to attach."; + res = jvm->AttachCurrentThread((void**)&env, NULL); + if (res != JNI_OK) { + PLOGE.printf("AttachCurrentThread failed: %d", res); + return res; + } + } + JuiceAgent.env = env; + PLOGI << "Got the JNIEnv"; + + // Get the JVMTI + res = jvm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_2); + if (res != JNI_OK) { + PLOGE.printf("GetEnv failed: %d", res); + return res; + } + JuiceAgent.jvmti = jvmti; + PLOGI << "Got the JVMTI"; + + return 0; +} + +static int InvokeJuiceLoaderInit(const char* ConfigDir) { + // Variables + int result = JNI_ERR; + JNIEnv *env = JuiceAgent.env; + jvmtiEnv *jvmti = JuiceAgent.jvmti; + + const char *bootstrap_class = "cn/xiaozhou233/juiceloader/JuiceLoaderBootstrap"; + const char *method_name = "init"; + const char *method_signature = "(Ljava/lang/String;)V"; + + PLOGI << "Invoke JuiceLoader Init"; + PLOGI << "\n\n================ JuiceLoader Init ================="; + // Find class + jclass cls = env->FindClass(bootstrap_class); + if (cls == NULL) { + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + } + PLOGE << "FindClass failed"; + return 1; + } + + // Find method + jmethodID mid = env->GetStaticMethodID(cls, method_name, method_signature); + if (mid == NULL) { + PLOGE << "GetStaticMethodID failed"; + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + } + return 1; + } + + // Invoke method + env->CallStaticVoidMethod(cls, mid, env->NewStringUTF(ConfigDir)); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + } + + PLOGI << "================ JuiceLoader Init =================\n\n"; + + PLOGI << "Invoke JuiceLoader Init done"; + return 0; +} + +int InitJuiceAgent(const char* ConfigDir, const char* JuiceLoaderJarPath) { + // Variables + int result = JNI_ERR; + + // Step 1: Get the Java environment + if (GetJavaEnv() != 0) { + PLOGE << "Failed to get Java environment code=" << result; + return 1; + } + + // Step 2: Inject JuiceLoader.jar + result = JuiceAgent.jvmti->AddToSystemClassLoaderSearch(JuiceLoaderJarPath); + if (result != JNI_OK) { + PLOGE.printf("AddToSystemClassLoaderSearch failed: %d", result); + return 1; + } + + // Step 3: Invoke JuiceLoader Native Init + if (init_juiceloader(JuiceAgent.env, JuiceAgent.jvmti) != JNI_OK) { + PLOGE << "Failed to invoke JuiceLoader Native Init: " << result; + return 1; + } + + // Step 4: Invoke JuiceLoader Init + if (InvokeJuiceLoaderInit(ConfigDir) != 0) { + PLOGE << "Failed to invoke JuiceLoader Init code=" << result; + return 1; + } + + // Step 5: Done, cleanup + + return 0; +} + +DWORD WINAPI ThreadProc(LPVOID lpParam) { + PLOGD << "ThreadProc"; + PLOGD.printf("Recieved lpParam: %p"); + if (lpParam == nullptr) { + PLOGE << "lpParam is NULL"; + return 1; + } + + InjectParams* pParams = (InjectParams*)lpParam; + PLOGD << "ConfigDir: " << pParams->ConfigDir; + PLOGD << "JuiceLoaderJarPath: " << pParams->JuiceLoaderJarPath; + + PLOGI << "Init JuiceAgent..."; + int result = InitJuiceAgent(pParams->ConfigDir, pParams->JuiceLoaderJarPath); + PLOGI.printf("Init JuiceAgent result: %d", result); + + PLOGI << "Exit ThreadProc"; + return 0; +} + +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved) +{ + switch (dwReason) + { + case DLL_PROCESS_ATTACH: { + hAppInstance = hinstDLL; + InitLogger(); + + PLOGI << "DLL_PROCESS_ATTACH"; + PLOGI.printf("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER); + + DisableThreadLibraryCalls(hinstDLL); + + // TODO: ReflectiveLoader Params + InjectParams *testParams = (InjectParams*)malloc(sizeof(InjectParams)); + strcpy(testParams->ConfigDir, "C:\\Users\\xiaozhou\\Desktop\\Inject_V1.0_b1"); + strcpy(testParams->JuiceLoaderJarPath, "C:\\Users\\xiaozhou\\Desktop\\Inject_V1.0_b1\\JuiceLoader.jar"); + lpReserved = (void*)testParams; + + // Check if lpReserved is NULL + if (lpReserved == nullptr) { + PLOGE << "lpReserved is NULL"; + return FALSE; + } + + // Allocate memory for local param + InjectParams* pParams = (InjectParams*)malloc(sizeof(InjectParams)); + if (pParams == nullptr) { + PLOGE << "Failed to allocate memory for local param"; + return FALSE; + } + + // Copy lpReserved to local param + memcpy(pParams, lpReserved, sizeof(InjectParams)); + + // Create a thread to handle the injection + HANDLE hThread = CreateThread(NULL, 0, ThreadProc, pParams, 0, NULL); + if (hThread) { + CloseHandle(hThread); + } else { + PLOGE << "Failed to create thread"; + free(pParams); + } + + break; + } + + case DLL_PROCESS_DETACH: + PLOGI << "DLL_PROCESS_DETACH"; + break; + } + + return TRUE; +} diff --git a/src/agent/utils/Logger.hpp b/src/agent/utils/Logger.hpp new file mode 100644 index 0000000..0ce7318 --- /dev/null +++ b/src/agent/utils/Logger.hpp @@ -0,0 +1,17 @@ +// Logger.hpp +#pragma once +#include +#include +#include +#include +#include + +inline void InitLogger() +{ + static plog::ConsoleAppender consoleAppender; + static plog::DebugOutputAppender debugAppender; + + plog::init(plog::debug, &consoleAppender).addAppender(&debugAppender); + + PLOG_INFO << "Logger initialized: Console + Debug Output"; +} \ No newline at end of file diff --git a/src/agent/utils/datautils.c b/src/agent/utils/datautils.c.txt similarity index 100% rename from src/agent/utils/datautils.c rename to src/agent/utils/datautils.c.txt diff --git a/src/agent/utils/jvmutils.c b/src/agent/utils/jvmutils.c deleted file mode 100644 index 80e7c18..0000000 --- a/src/agent/utils/jvmutils.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "jvmutils.h" - -// Get JavaVM -jint GetCreatedJVM(JavaVM **out_jvm) { - if (!out_jvm) return JNI_ERR; - *out_jvm = NULL; - - jint res = JNI_GetCreatedJavaVMs(out_jvm, 1, NULL); - if (res != JNI_OK || *out_jvm == NULL) { - MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK); - return res; - } - return JNI_OK; -} - -// Get JNIEnv -jint GetJNIEnv(JavaVM *jvm, JNIEnv **out_env) { - if (!jvm || !out_env) return JNI_ERR; - *out_env = NULL; - - void *env = NULL; - jint res = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_8); - if (res == JNI_OK && env != NULL) { - *out_env = (JNIEnv *)env; - return JNI_OK; - } - - // Attach current thread - res = (*jvm)->AttachCurrentThread(jvm, &env, NULL); - if (res != JNI_OK || env == NULL) { - return res; - } - *out_env = (JNIEnv *)env; - return JNI_OK; -} - -// Get JVMTI -jint GetJVMTI(JavaVM *jvm, jvmtiEnv **out_jvmti) { - if (!jvm || !out_jvmti) return JNI_ERR; - *out_jvmti = NULL; - - jint res = (*jvm)->GetEnv(jvm, (void**)out_jvmti, JVMTI_VERSION_1_2); - if (res == JNI_OK && *out_jvmti != NULL) { - return JNI_OK; - } - - *out_jvmti = NULL; - return res; -} \ No newline at end of file