[WIP] move juiceloader

This commit is contained in:
xiaozhou233 committed 2025-11-09 15:33:07 +08:00
1 parent c8c255b233
commit 386dc14a01
9 files changed
+279 -139

No files matched your search

-111
View File
@@ -1,111 +0,0 @@
// JVM
#include <jni.h>
#include <jvmti.h>
#include <Logger.hpp>
#include <juiceloader.h>
JuiceLoaderNativeType JuiceLoaderNative;
static int RegisterNatives(JNIEnv *env) {
const char* native_class = "cn/xiaozhou233/juiceloader/JuiceLoader";
JNINativeMethod methods[] = {
{(char*)"init", (char*)"()Z", (void *)&loader_init},
{(char*)"injectJar", (char*)"(Ljava/lang/String;)Z", (void *)&loader_injectJar},
{(char*)"redefineClass", (char*)"(Ljava/lang/Class;[BI)Z", (void *)&loader_redefineClass_clazz},
{(char*)"redefineClassByName", (char*)"(Ljava/lang/String;[BI)Z", (void *)&loader_redefineClass_className},
{(char*)"getLoadedClasses", (char*)"()[Ljava/lang/Class;", (void *)&loader_getLoadedClasses},
{(char*)"getClassBytes", (char*)"(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes},
{(char*)"getClassBytesByName", (char*)"(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName},
{(char*)"retransformClass", (char*)"(Ljava/lang/Class;[BI)Z", (void *)&loader_retransformClass},
{(char*)"retransformClassByName", (char*)"(Ljava/lang/String;[BI)Z", (void *)&loader_retransformClassByName},
{(char*)"getClassByName", (char*)"(Ljava/lang/String;)Ljava/lang/Class;", (void *)&loader_getClassByName},
// public static native Thread nativeGetThreadByName(String var0);
{(char*)"nativeGetThreadByName", (char*)"(Ljava/lang/String;)Ljava/lang/Thread;", (void *)&loader_nativeGetThreadByName},
// public static native ClassLoader nativeInjectJarToThread(Thread var0, String var1);
{(char*)"nativeInjectJarToThread", (char*)"(Ljava/lang/Thread;Ljava/lang/String;)Ljava/lang/ClassLoader;", (void *)&loader_nativeInjectJarToThread}
};
// Find Native(JNI) Class
jclass clazz = env->FindClass("cn/xiaozhou233/juiceloader/JuiceLoader");
if (clazz == NULL) {
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
PLOGE.printf("[-] Failed to initialize libjuiceloader: JuiceLoader class not found");
return -1;
}
// Dynamicly register native methods
jint result = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
if (result != JNI_OK) {
PLOGE << "Failed to register native methods: " << result;
return JNI_ERR;
}
return JNI_OK;
}
static int ConfigureJVMTI(JNIEnv *env, _jvmtiEnv *jvmti) {
// Retransform Class Cache : to store classes that need to be retransformed
RetransformClassCache.arr = NULL;
RetransformClassCache.size = 0;
RetransformClassCache.capacity = 0;
// Application ability
jvmtiCapabilities caps;
memset(&caps, 0, sizeof(caps));
caps.can_get_bytecodes = 1;
caps.can_redefine_classes = 1;
caps.can_redefine_any_class = 1;
caps.can_retransform_classes = 1;
caps.can_retransform_any_class = 1;
caps.can_generate_all_class_hook_events = 1;
jint result = jvmti->AddCapabilities(&caps);
if (result != JNI_OK) {
PLOGE << "Failed to add capabilities: " << result;
return JNI_ERR;
}
// Register callbacks
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
// Enable events
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
return JNI_OK;
}
int InitJuiceLoader(JNIEnv *env, _jvmtiEnv *jvmti) {
PLOGI.printf("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
// Step 1: Check if env/jvmti is null
PLOGD.printf("env: %p, jvmti: %p", env, jvmti);
if (env == nullptr) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: env is null");
return -1;
}
if (jvmti == nullptr) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: jvmti is null");
return -1;
}
JuiceLoaderNative.jvmti = jvmti;
// Step 2: Register Native Methods
if (RegisterNatives(env) != JNI_OK) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: RegisterNatives failed");
return -1;
}
// Step 3: Configure JVMTI
if (ConfigureJVMTI(env, jvmti) != JNI_OK) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: ConfigureJVMTI failed");
return -1;
}
return 0;
}
+2 -9
View File
@@ -1,7 +1,6 @@
#include <windows.h>
#include "Logger.hpp"
#include <JuiceAgent.h>
#include <juiceloader.h>
#include <tinytoml/toml.h>
// For Java VM
@@ -217,19 +216,13 @@ int InitJuiceAgent(const char* ConfigDir) {
return 1;
}
// Step 3: Invoke JuiceLoader Native Init
if (InitJuiceLoader(JuiceAgent.env, JuiceAgent.jvmti) != JNI_OK) {
PLOGE << "Failed to invoke JuiceLoader Native Init: " << result;
return 1;
}
// Step 4: Invoke JuiceLoader Init
// Step 3: Invoke JuiceLoader Init
if (InvokeJuiceLoaderInit(ConfigDir) != 0) {
PLOGE << "Failed to invoke JuiceLoader Init code=" << result;
return 1;
}
// Step 5: Done, cleanup
// Step 4: Done, cleanup
return 0;
}
+1
View File
@@ -0,0 +1 @@
# src/juiceloader/CMakeLists.txt
@@ -1,4 +1,5 @@
#include <juiceloader.h>
#include <cn_xiaozhou233_juiceloader_JuiceLoader.h>
#include <jni.h>
#include <jvmti.h>
#include <string.h>
@@ -97,12 +98,40 @@ void JNICALL ClassFileLoadHook(
/// ============ JNI Function ============ ///
JNIEXPORT jboolean JNICALL loader_init(JNIEnv *env, jclass loader_class) {
PLOGW << "JuiceLoaderNative.init() is deprecated, init will be done automatically.";
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_init
(JNIEnv *env, jclass loader_class) {
PLOGI << "Invoked!";
// Get JavaVM
JavaVM* jvm = nullptr;
env->GetJavaVM(&jvm);
if (jvm == nullptr) {
PLOGE << "Failed to get JavaVM instance";
return JNI_FALSE;
}
// Get JVMTI
jvmtiEnv* jvmti = nullptr;
jvmtiError err = static_cast<jvmtiError>(jvm->GetEnv(reinterpret_cast<void**>(&jvmti), JVMTI_VERSION_1_2));
if (err != JVMTI_ERROR_NONE || jvmti == nullptr) {
PLOGE << "Failed to get JVMTI environment, error: " << err;
return JNI_FALSE;
}
int result = InitJuiceLoader(env, jvmti);
if (result != 0) {
PLOGE << "Failed to initialize JuiceLoader, error code: " << result;
return JNI_FALSE;
}
PLOGI << "JuiceLoader initialized successfully";
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, jstring jarPath) {
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_injectJar
(JNIEnv *env, jclass loader_class, jstring jarPath) {
PLOGI << "Invoked!";
const char *pathStr = env->GetStringUTFChars(jarPath, NULL);
if (!pathStr) {
@@ -127,7 +156,10 @@ JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, js
PLOGI << "Jar injected: " << pathStr;
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass
(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
PLOGI << "Invoked!";
if (!JuiceLoaderNative.jvmti) {
PLOGE.printf("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
@@ -157,7 +189,9 @@ JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClassByName
(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
PLOGI << "redefineClass_className Invoked!";
if (!JuiceLoaderNative.jvmti) {
PLOGE.printf("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
@@ -177,12 +211,14 @@ JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass lo
return JNI_FALSE;
}
jboolean ret = loader_redefineClass_clazz(env, loader_class, clazz, classBytes, length);
jboolean ret = Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass(env, loader_class, clazz, classBytes, length);
env->ReleaseStringUTFChars(className, cname);
return ret;
}
JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loader_class) {
C_EXPORT
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getLoadedClasses
(JNIEnv *env, jclass loader_class) {
PLOGI << "loader_getLoadedClasses invoked!";
if (!JuiceLoaderNative.jvmti) {
@@ -209,7 +245,9 @@ JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loade
return classArray;
}
JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz) {
C_EXPORT
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes
(JNIEnv *env, jclass loader_class, jclass clazz) {
PLOGI << "getClassBytes Invoked!";
if (!JuiceLoaderNative.jvmti) {
PLOGE << "JuiceLoaderNative.jvmti is NULL!";
@@ -254,7 +292,9 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_cla
return result;
}
JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass loader_class, jstring className) {
C_EXPORT
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytesByName
(JNIEnv *env, jclass loader_class, jstring className) {
PLOGI << "getClassBytesByName Invoked!";
if (!JuiceLoaderNative.jvmti) {
PLOGE << "JuiceLoaderNative.jvmti is NULL!";
@@ -270,9 +310,12 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass load
return NULL;
}
return loader_getClassBytes(env, loader_class, clazz);
return Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes(env, loader_class, clazz);
}
JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass
(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
if (!JuiceLoaderNative.jvmti) {
PLOGE << "JuiceLoaderNative.jvmti is NULL! (retransformClass)";
return JNI_FALSE;
@@ -312,7 +355,9 @@ JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_cl
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
C_EXPORT
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClassByName
(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
const char *name_dot = env->GetStringUTFChars(className, NULL);
char *name = _strdup(name_dot);
for (char *p = name; *p; ++p) if (*p == '.') *p = '/';
@@ -326,10 +371,12 @@ JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loa
return JNI_FALSE;
}
return loader_retransformClass(env, loader_class, target, classBytes, length);
return Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass(env, loader_class, target, classBytes, length);
}
JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class, jstring className) {
C_EXPORT
JNIEXPORT jclass JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassByName
(JNIEnv *env, jclass loader_class, jstring className) {
if (className == NULL) {
return NULL;
@@ -354,8 +401,8 @@ JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class,
}
// static native Thread getThreadByName(String name)
JNIEXPORT jobject JNICALL
loader_nativeGetThreadByName
C_EXPORT
JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeGetThreadByName
(JNIEnv* env, jclass, jstring jName)
{
// Convert jstring to std::string
@@ -420,9 +467,9 @@ loader_nativeGetThreadByName
// static native ClassLoader injectJarToThread(Thread thread, String jarPath)
JNIEXPORT jobject JNICALL
loader_nativeInjectJarToThread
(JNIEnv* env, jclass, jobject threadObj, jstring jJarPath)
C_EXPORT
JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeInjectJarToThread
(JNIEnv* env, jclass, jobject threadObj, jstring jJarPath)
{
// Convert jstring -> std::string
const char* cJarPath = env->GetStringUTFChars(jJarPath, nullptr);
@@ -23,6 +23,7 @@ typedef struct {
extern RetransformClassCacheType RetransformClassCache;
#define C_EXPORT extern "C" __declspec(dllexport)
int InitJuiceLoader(JNIEnv *env, jvmtiEnv *jvmti);
+66
View File
@@ -0,0 +1,66 @@
// JVM
#include <jni.h>
#include <jvmti.h>
#include <Logger.hpp>
#include <juiceloader.h>
JuiceLoaderNativeType JuiceLoaderNative;
static int ConfigureJVMTI(JNIEnv *env, _jvmtiEnv *jvmti) {
// Retransform Class Cache : to store classes that need to be retransformed
RetransformClassCache.arr = NULL;
RetransformClassCache.size = 0;
RetransformClassCache.capacity = 0;
// Application ability
jvmtiCapabilities caps;
memset(&caps, 0, sizeof(caps));
caps.can_get_bytecodes = 1;
caps.can_redefine_classes = 1;
caps.can_redefine_any_class = 1;
caps.can_retransform_classes = 1;
caps.can_retransform_any_class = 1;
caps.can_generate_all_class_hook_events = 1;
jint result = jvmti->AddCapabilities(&caps);
if (result != JNI_OK) {
PLOGE << "Failed to add capabilities: " << result;
return JNI_ERR;
}
// Register callbacks
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
// Enable events
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
return JNI_OK;
}
int InitJuiceLoader(JNIEnv *env, _jvmtiEnv *jvmti) {
PLOGI.printf("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
// Step 1: Check if env/jvmti is null
PLOGD.printf("env: %p, jvmti: %p", env, jvmti);
if (env == nullptr) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: env is null");
return -1;
}
if (jvmti == nullptr) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: jvmti is null");
return -1;
}
JuiceLoaderNative.jvmti = jvmti;
// Step 2: Configure JVMTI
if (ConfigureJVMTI(env, jvmti) != JNI_OK) {
PLOGE.printf("[-] Failed to initialize libjuiceloader: ConfigureJVMTI failed");
return -1;
}
return 0;
}