mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Update: Loader
This commit is contained in:
1 parent
681a86cb36
commit
e2a3119af6
6 files changed
+792
-1
No files matched your search
@@ -0,0 +1,20 @@
|
||||
file(
|
||||
GLOB_RECURSE
|
||||
libjuiceloader_sources
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/*.c
|
||||
)
|
||||
|
||||
add_library(juiceloader SHARED ${libjuiceloader_sources})
|
||||
|
||||
target_include_directories(juiceloader PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/includes
|
||||
${CMAKE_SOURCE_DIR}/includes/jvm
|
||||
${CMAKE_SOURCE_DIR}/includes/jvm/win32
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/includes
|
||||
)
|
||||
|
||||
target_link_libraries(juiceloader PRIVATE
|
||||
ReflectiveDLLInjection
|
||||
jvm
|
||||
)
|
||||
@@ -0,0 +1,530 @@
|
||||
#include <juiceloader.h>
|
||||
#include <cn_xiaozhou233_juiceloader_JuiceLoader.h>
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
|
||||
RetransformClassCacheType RetransformClassCache = {NULL, 0, 0};
|
||||
|
||||
static const char* g_bytecodes_classname = NULL;
|
||||
static unsigned char* g_bytecodes = NULL;
|
||||
static jint g_bytecodes_len = 0;
|
||||
|
||||
/// ================= Utility ================= ///
|
||||
|
||||
char* get_class_name(JNIEnv* env, jclass clazz) {
|
||||
jclass clsClass = env->FindClass("java/lang/Class");
|
||||
jmethodID mid_getName = env->GetMethodID(clsClass, "getName", "()Ljava/lang/String;");
|
||||
jstring nameStr = (jstring)env->CallObjectMethod(clazz, mid_getName);
|
||||
|
||||
const char* temp = env->GetStringUTFChars(nameStr, NULL);
|
||||
char* result = _strdup(temp);
|
||||
env->ReleaseStringUTFChars(nameStr, temp);
|
||||
env->DeleteLocalRef(nameStr);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Helper: macro to throw RuntimeException with message
|
||||
static void throwRuntimeException(JNIEnv *env, const char *msg) {
|
||||
jclass rte = env->FindClass("java/lang/RuntimeException");
|
||||
if (rte != NULL) {
|
||||
env->ThrowNew(rte, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// ================= ClassFileLoadHook ================= ///
|
||||
|
||||
void JNICALL ClassFileLoadHook(
|
||||
jvmtiEnv* jvmti_env,
|
||||
JNIEnv* jni_env,
|
||||
jclass class_being_redefined,
|
||||
jobject loader,
|
||||
const char* name,
|
||||
jobject protection_domain,
|
||||
jint class_data_len,
|
||||
const unsigned char* classbytes,
|
||||
jint* new_class_data_len,
|
||||
unsigned char** new_classbytes) {
|
||||
|
||||
// ---- capture class bytes ---- //
|
||||
if (g_bytecodes_classname != NULL && g_bytecodes == NULL) {
|
||||
if (strcmp(name, g_bytecodes_classname) == 0) {
|
||||
g_bytecodes_len = class_data_len;
|
||||
g_bytecodes = (unsigned char*)malloc(class_data_len);
|
||||
memcpy(g_bytecodes, classbytes, class_data_len);
|
||||
|
||||
*new_classbytes = NULL;
|
||||
*new_class_data_len = 0;
|
||||
|
||||
PLOGD.printf("[Hook:get] Captured class bytes of %s, len=%d", name, class_data_len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- set class bytes from cache ---- //
|
||||
if (RetransformClassCache.size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < RetransformClassCache.size; i++) {
|
||||
ClassBytesCacheType *entry = &RetransformClassCache.arr[i];
|
||||
if (strcmp(entry->name, name) == 0) {
|
||||
|
||||
unsigned char *newBytes = nullptr;
|
||||
jvmtiError err = jvmti_env->Allocate(entry->len, &newBytes);
|
||||
if (err != JVMTI_ERROR_NONE || newBytes == nullptr) {
|
||||
PLOGE.printf("[Hook:set] jvmti->Allocate failed for class %s, err=%d", name, err);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(newBytes, entry->bytes, entry->len);
|
||||
*new_classbytes = newBytes;
|
||||
*new_class_data_len = entry->len;
|
||||
|
||||
PLOGI.printf("[Hook:set] Patched class: %s, new_len=%d", name, entry->len);
|
||||
|
||||
free(entry->name);
|
||||
free(entry->bytes);
|
||||
for (int j = i; j < RetransformClassCache.size - 1; j++) {
|
||||
RetransformClassCache.arr[j] = RetransformClassCache.arr[j + 1];
|
||||
}
|
||||
RetransformClassCache.size--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// ============ JNI Function ============ ///
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
PLOGE << "Path is NULL!";
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
if (!JuiceLoaderNative.jvmti) {
|
||||
PLOGE.printf("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||
env->ReleaseStringUTFChars(jarPath, pathStr);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
jvmtiError result = JuiceLoaderNative.jvmti->AddToSystemClassLoaderSearch(pathStr);
|
||||
if (result != JVMTI_ERROR_NONE) {
|
||||
PLOGE.printf("Cannot inject jar [result=%d]", result);
|
||||
env->ReleaseStringUTFChars(jarPath, pathStr);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
env->ReleaseStringUTFChars(jarPath, pathStr);
|
||||
PLOGI << "Jar injected: " << pathStr;
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
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);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
jbyte* buf = env->GetByteArrayElements(classBytes, NULL);
|
||||
if (!buf) {
|
||||
PLOGE << "Cannot get class bytes";
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
jvmtiClassDefinition defs[1];
|
||||
defs[0].klass = clazz;
|
||||
defs[0].class_byte_count = length;
|
||||
defs[0].class_bytes = reinterpret_cast<const unsigned char*>(buf);
|
||||
|
||||
jvmtiError result = JuiceLoaderNative.jvmti->RedefineClasses(1, defs);
|
||||
env->ReleaseByteArrayElements(classBytes, buf, JNI_ABORT);
|
||||
|
||||
if (result != JVMTI_ERROR_NONE) {
|
||||
PLOGE.printf("Cannot redefine class [result=%d]", result);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
PLOGI.printf("Class redefined successfully!");
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
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);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
const char* cname = env->GetStringUTFChars(className, NULL);
|
||||
if (!cname) {
|
||||
PLOGE << "Class name is NULL!";
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
jclass clazz = env->FindClass(cname);
|
||||
if (!clazz) {
|
||||
PLOGE.printf("Class not found! [ClassName=%s]", cname);
|
||||
env->ReleaseStringUTFChars(className, cname);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
jboolean ret = Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass(env, loader_class, clazz, classBytes, length);
|
||||
env->ReleaseStringUTFChars(className, cname);
|
||||
return ret;
|
||||
}
|
||||
|
||||
C_EXPORT
|
||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getLoadedClasses
|
||||
(JNIEnv *env, jclass loader_class) {
|
||||
PLOGI << "loader_getLoadedClasses invoked!";
|
||||
|
||||
if (!JuiceLoaderNative.jvmti) {
|
||||
PLOGE << "JuiceLoaderNative.jvmti is NULL!";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jint count = 0;
|
||||
jclass* classes = NULL;
|
||||
jvmtiError result = JuiceLoaderNative.jvmti->GetLoadedClasses(&count, &classes);
|
||||
if (result != JVMTI_ERROR_NONE || count == 0) {
|
||||
if (classes) JuiceLoaderNative.jvmti->Deallocate((unsigned char*)classes);
|
||||
return env->NewObjectArray(0, env->FindClass("java/lang/Class"), NULL);
|
||||
}
|
||||
|
||||
jclass classClass = env->FindClass("java/lang/Class");
|
||||
jobjectArray classArray = env->NewObjectArray(count, classClass, NULL);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
env->SetObjectArrayElement(classArray, i, classes[i]);
|
||||
}
|
||||
|
||||
if (classes) JuiceLoaderNative.jvmti->Deallocate((unsigned char*)classes);
|
||||
return classArray;
|
||||
}
|
||||
|
||||
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!";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* className = get_class_name(env, clazz);
|
||||
if (!className) {
|
||||
PLOGE << "Failed to get class name!";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (char* p = className; *p; ++p) if (*p == '.') *p = '/';
|
||||
|
||||
g_bytecodes_classname = className;
|
||||
g_bytecodes = NULL;
|
||||
g_bytecodes_len = 0;
|
||||
|
||||
jclass classes[1] = { clazz };
|
||||
jvmtiError err = JuiceLoaderNative.jvmti->RetransformClasses(1, classes);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
PLOGE.printf("RetransformClasses failed: %d", err);
|
||||
free(className);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!g_bytecodes) {
|
||||
PLOGW.printf("No bytecode captured for class: %s", className);
|
||||
free(className);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jbyteArray result = env->NewByteArray(g_bytecodes_len);
|
||||
env->SetByteArrayRegion(result, 0, g_bytecodes_len, (jbyte*)g_bytecodes);
|
||||
|
||||
free(g_bytecodes);
|
||||
free(className);
|
||||
g_bytecodes = NULL;
|
||||
g_bytecodes_classname = NULL;
|
||||
g_bytecodes_len = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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!";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* cname = env->GetStringUTFChars(className, NULL);
|
||||
jclass clazz = env->FindClass(cname);
|
||||
env->ReleaseStringUTFChars(className, cname);
|
||||
|
||||
if (!clazz) {
|
||||
PLOGE.printf("Failed to find class %s", cname);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes(env, loader_class, clazz);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const char *name_dot = get_class_name(env, clazz);
|
||||
char* name = _strdup(name_dot);
|
||||
for (char* p = name; *p; ++p) if (*p == '.') *p = '/';
|
||||
|
||||
jbyte* bytes = env->GetByteArrayElements(classBytes, NULL);
|
||||
jint len = env->GetArrayLength(classBytes);
|
||||
|
||||
if (RetransformClassCache.size >= RetransformClassCache.capacity) {
|
||||
int new_capacity = RetransformClassCache.capacity == 0 ? 4 : RetransformClassCache.capacity * 2;
|
||||
RetransformClassCache.arr = (ClassBytesCacheType*)realloc(RetransformClassCache.arr, new_capacity * sizeof(ClassBytesCacheType));
|
||||
RetransformClassCache.capacity = new_capacity;
|
||||
}
|
||||
|
||||
ClassBytesCacheType *entry = &RetransformClassCache.arr[RetransformClassCache.size++];
|
||||
entry->name = _strdup(name);
|
||||
entry->bytes = (unsigned char*)malloc(len);
|
||||
memcpy(entry->bytes, bytes, len);
|
||||
entry->len = len;
|
||||
|
||||
env->ReleaseByteArrayElements(classBytes, bytes, JNI_ABORT);
|
||||
|
||||
jclass classes[1] = { clazz };
|
||||
jvmtiError err = JuiceLoaderNative.jvmti->RetransformClasses(1, classes);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
PLOGE.printf("RetransformClasses failed! [err=%d]", err);
|
||||
free(name);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
PLOGI.printf("[retransformClass] Class retransformed: %s (len=%d)", entry->name, entry->len);
|
||||
free(name);
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
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 = '/';
|
||||
env->ReleaseStringUTFChars(className, name_dot);
|
||||
|
||||
jclass target = env->FindClass(name);
|
||||
free(name);
|
||||
|
||||
if (!target) {
|
||||
PLOGE << "FindClass failed in retransformClassByName";
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
return Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass(env, loader_class, target, classBytes, length);
|
||||
}
|
||||
|
||||
C_EXPORT
|
||||
JNIEXPORT jclass JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassByName
|
||||
(JNIEnv *env, jclass loader_class, jstring className) {
|
||||
|
||||
if (className == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// jstring -> const char*
|
||||
const char *name_utf = env->GetStringUTFChars(className, NULL);
|
||||
if (name_utf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// find class
|
||||
jclass clazz = env->FindClass(name_utf);
|
||||
|
||||
// clean up
|
||||
env->ReleaseStringUTFChars(className, name_utf);
|
||||
|
||||
if (clazz == NULL) {
|
||||
PLOGE.printf("Failed to find class %s", name_utf);
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
|
||||
// static native Thread getThreadByName(String name)
|
||||
C_EXPORT
|
||||
JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeGetThreadByName
|
||||
(JNIEnv* env, jclass, jstring jName)
|
||||
{
|
||||
// Convert jstring to std::string
|
||||
const char* cName = env->GetStringUTFChars(jName, nullptr);
|
||||
std::string targetName = cName;
|
||||
env->ReleaseStringUTFChars(jName, cName);
|
||||
|
||||
// Thread class
|
||||
jclass threadClass = env->FindClass("java/lang/Thread");
|
||||
|
||||
// Call Thread.getAllStackTraces(), signature: ()Ljava/util/Map;
|
||||
jmethodID midGetAll = env->GetStaticMethodID(
|
||||
threadClass,
|
||||
"getAllStackTraces",
|
||||
"()Ljava/util/Map;"
|
||||
);
|
||||
|
||||
jobject mapObj = env->CallStaticObjectMethod(threadClass, midGetAll);
|
||||
|
||||
// Map.keySet() -> Set
|
||||
jclass mapClass = env->FindClass("java/util/Map");
|
||||
jmethodID midKeySet = env->GetMethodID(
|
||||
mapClass,
|
||||
"keySet",
|
||||
"()Ljava/util/Set;"
|
||||
);
|
||||
jobject setObj = env->CallObjectMethod(mapObj, midKeySet);
|
||||
|
||||
// Set.iterator() -> Iterator
|
||||
jclass setClass = env->FindClass("java/util/Set");
|
||||
jmethodID midIterator = env->GetMethodID(
|
||||
setClass,
|
||||
"iterator",
|
||||
"()Ljava/util/Iterator;"
|
||||
);
|
||||
jobject iteratorObj = env->CallObjectMethod(setObj, midIterator);
|
||||
|
||||
// Iterator.hasNext() / next()
|
||||
jclass iteratorClass = env->FindClass("java/util/Iterator");
|
||||
jmethodID midHasNext = env->GetMethodID(iteratorClass, "hasNext", "()Z");
|
||||
jmethodID midNext = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
|
||||
|
||||
// Thread.getName()
|
||||
jmethodID midGetName = env->GetMethodID(threadClass, "getName", "()Ljava/lang/String;");
|
||||
|
||||
while (env->CallBooleanMethod(iteratorObj, midHasNext)) {
|
||||
jobject threadObj = env->CallObjectMethod(iteratorObj, midNext);
|
||||
jstring jThreadName = (jstring)env->CallObjectMethod(threadObj, midGetName);
|
||||
|
||||
const char* cThreadName = env->GetStringUTFChars(jThreadName, nullptr);
|
||||
bool match = (targetName == cThreadName);
|
||||
env->ReleaseStringUTFChars(jThreadName, cThreadName);
|
||||
|
||||
if (match) {
|
||||
return threadObj; // Found target
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr; // Not found
|
||||
}
|
||||
|
||||
|
||||
|
||||
// static native ClassLoader injectJarToThread(Thread thread, String jarPath)
|
||||
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);
|
||||
|
||||
// new File(jarPath)
|
||||
jclass fileClass = env->FindClass("java/io/File");
|
||||
jmethodID fileCtor = env->GetMethodID(fileClass, "<init>", "(Ljava/lang/String;)V");
|
||||
jstring jPathCopy = env->NewStringUTF(cJarPath);
|
||||
jobject fileObj = env->NewObject(fileClass, fileCtor, jPathCopy);
|
||||
|
||||
// File.toURI()
|
||||
jmethodID midToURI = env->GetMethodID(fileClass, "toURI", "()Ljava/net/URI;");
|
||||
jobject uriObj = env->CallObjectMethod(fileObj, midToURI);
|
||||
|
||||
// URI.toURL()
|
||||
jclass uriClass = env->FindClass("java/net/URI");
|
||||
jmethodID midToURL = env->GetMethodID(uriClass, "toURL", "()Ljava/net/URL;");
|
||||
jobject urlObj = env->CallObjectMethod(uriObj, midToURL);
|
||||
|
||||
// Thread.getContextClassLoader()
|
||||
jclass threadClass = env->FindClass("java/lang/Thread");
|
||||
jmethodID midGetCL = env->GetMethodID(
|
||||
threadClass, "getContextClassLoader", "()Ljava/lang/ClassLoader;"
|
||||
);
|
||||
jobject originalCL = env->CallObjectMethod(threadObj, midGetCL);
|
||||
|
||||
// URLClassLoader(URL[] urls, ClassLoader parent)
|
||||
jclass urlClassLoaderClass = env->FindClass("java/net/URLClassLoader");
|
||||
jmethodID urlCLCtor = env->GetMethodID(
|
||||
urlClassLoaderClass,
|
||||
"<init>",
|
||||
"([Ljava/net/URL;Ljava/lang/ClassLoader;)V"
|
||||
);
|
||||
|
||||
// Create URL[] array with 1 element
|
||||
jobjectArray urlArray = env->NewObjectArray(1, env->FindClass("java/net/URL"), nullptr);
|
||||
env->SetObjectArrayElement(urlArray, 0, urlObj);
|
||||
|
||||
// Create new URLClassLoader
|
||||
jobject newClassLoader = env->NewObject(
|
||||
urlClassLoaderClass,
|
||||
urlCLCtor,
|
||||
urlArray,
|
||||
originalCL
|
||||
);
|
||||
|
||||
// thread.setContextClassLoader(newCL)
|
||||
jmethodID midSetCL = env->GetMethodID(
|
||||
threadClass,
|
||||
"setContextClassLoader",
|
||||
"(Ljava/lang/ClassLoader;)V"
|
||||
);
|
||||
env->CallVoidMethod(threadObj, midSetCL, newClassLoader);
|
||||
|
||||
env->ReleaseStringUTFChars(jJarPath, cJarPath);
|
||||
|
||||
return newClassLoader;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class cn_xiaozhou233_juiceloader_JuiceLoader */
|
||||
|
||||
#ifndef _Included_cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
#define _Included_cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: init
|
||||
* Signature: ()Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_init
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: injectJar
|
||||
* Signature: (Ljava/lang/String;)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_injectJar
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: redefineClass
|
||||
* Signature: (Ljava/lang/Class;[BI)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass
|
||||
(JNIEnv *, jclass, jclass, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: redefineClassByName
|
||||
* Signature: (Ljava/lang/String;[BI)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClassByName
|
||||
(JNIEnv *, jclass, jstring, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: getLoadedClasses
|
||||
* Signature: ()[Ljava/lang/Class;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getLoadedClasses
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: getClassBytes
|
||||
* Signature: (Ljava/lang/Class;)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes
|
||||
(JNIEnv *, jclass, jclass);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: getClassBytesByName
|
||||
* Signature: (Ljava/lang/String;)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytesByName
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: retransformClass
|
||||
* Signature: (Ljava/lang/Class;[BI)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass
|
||||
(JNIEnv *, jclass, jclass, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: retransformClassByName
|
||||
* Signature: (Ljava/lang/String;[BI)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClassByName
|
||||
(JNIEnv *, jclass, jstring, jbyteArray, jint);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: getClassByName
|
||||
* Signature: (Ljava/lang/String;)Ljava/lang/Class;
|
||||
*/
|
||||
JNIEXPORT jclass JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassByName
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: nativeGetThreadByName
|
||||
* Signature: (Ljava/lang/String;)Ljava/lang/Thread;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeGetThreadByName
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
/*
|
||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoader
|
||||
* Method: nativeInjectJarToThread
|
||||
* Signature: (Ljava/lang/Thread;Ljava/lang/String;)Ljava/lang/ClassLoader;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeInjectJarToThread
|
||||
(JNIEnv *, jclass, jobject, jstring);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef JUICELOADER_H
|
||||
#define JUICELOADER_H
|
||||
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
|
||||
typedef struct _JuiceLoaderNative {
|
||||
jvmtiEnv *jvmti;
|
||||
} JuiceLoaderNativeType;
|
||||
extern JuiceLoaderNativeType JuiceLoaderNative;
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
unsigned char *bytes;
|
||||
int len;
|
||||
} ClassBytesCacheType;
|
||||
|
||||
typedef struct {
|
||||
ClassBytesCacheType *arr;
|
||||
int size;
|
||||
int capacity;
|
||||
} RetransformClassCacheType;
|
||||
|
||||
extern RetransformClassCacheType RetransformClassCache;
|
||||
|
||||
#define C_EXPORT extern "C" __declspec(dllexport)
|
||||
|
||||
extern "C" __declspec(dllexport)
|
||||
int InitJuiceLoader(JNIEnv *env, jvmtiEnv *jvmti);
|
||||
|
||||
void JNICALL ClassFileLoadHook(
|
||||
jvmtiEnv* jvmti_env,
|
||||
JNIEnv* jni_env,
|
||||
jclass class_being_redefined,
|
||||
jobject loader,
|
||||
const char* name,
|
||||
jobject protection_domain,
|
||||
jint class_data_len,
|
||||
const unsigned char* classbytes,
|
||||
jint* new_class_data_len,
|
||||
unsigned char** new_classbytes);
|
||||
|
||||
// JNI Functions
|
||||
// public native static boolean init();
|
||||
JNIEXPORT jboolean JNICALL loader_init(JNIEnv *env, jclass loader_class);
|
||||
// public native static boolean injectJar(String jarPath);
|
||||
JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, jstring jarPath);
|
||||
// public native static boolean redefineClass(Class<?> clazz, byte[] classBytes, int length);
|
||||
JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length);
|
||||
// public native static boolean redefineClass(String className, byte[] classBytes, int length);
|
||||
JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length);
|
||||
// public native static Class<?>[] getLoadedClasses();
|
||||
JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loader_class);
|
||||
// public static native byte[] getClassBytes(Class<?> clazz);
|
||||
JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz);
|
||||
// public static native byte[] getClassBytesByName(String className);
|
||||
JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass loader_class, jstring className);
|
||||
// public static native boolean retransformClass(Class<?> clazz, byte[] classBytes, int length);
|
||||
JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length);
|
||||
// public static native boolean retransformClassByName(String className, byte[] classBytes, int length);
|
||||
JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length);
|
||||
// public static native Class<?> getClassByName(String className);
|
||||
JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class, jstring className);
|
||||
JNIEXPORT jobject JNICALL loader_nativeGetThreadByName (JNIEnv* env, jclass, jstring jName);
|
||||
JNIEXPORT jobject JNICALL loader_nativeInjectJarToThread (JNIEnv* env, jclass, jobject threadObj, jstring jJarPath);
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
// JVM
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <JuiceAgent/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;
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport)
|
||||
int InitJuiceLoader(JNIEnv *env, _jvmtiEnv *jvmti) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user