mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f2782a126 | ||
|
|
87a1701f22 | ||
|
|
e265e39be9 | ||
|
|
34323a68fe | ||
|
|
39fad40655 | ||
|
|
bf5a6b0804 | ||
|
|
30d379521a | ||
|
|
fe495657d4 | ||
|
|
40707231c0 | ||
|
|
3dc83d83db | ||
|
|
9bb9faacdf | ||
|
|
7cdd3e4b11 | ||
|
|
a44f2d9753 | ||
|
|
7c3f1c302e | ||
|
|
f311b51b43 | ||
|
|
8fd8432032 | ||
|
|
6113ea47c2 | ||
|
|
c5e6f3e180 | ||
|
|
f3c2b7d0e6 | ||
|
|
c642280142 | ||
|
|
390f2c2241 | ||
|
|
8563e2b447 | ||
|
|
8e42780eed | ||
|
|
b46802faaa |
No files matched your search
@@ -1,7 +1,5 @@
|
|||||||
[Injection]
|
[Injection]
|
||||||
BootstrapAPIPath = "D:/Development/JuiceLoader/build/libs/bootstrap-api.jar"
|
|
||||||
JuiceLoaderJarPath = "D:/Development/JuiceLoader/build/libs/JuiceLoader.jar"
|
JuiceLoaderJarPath = "D:/Development/JuiceLoader/build/libs/JuiceLoader.jar"
|
||||||
JuiceLoaderLibPath = ""
|
|
||||||
EntryJarPath = "D:/Development/JuiceLoader/build/libs/Entry.jar"
|
EntryJarPath = "D:/Development/JuiceLoader/build/libs/Entry.jar"
|
||||||
EntryClass = "cn.xiaozhou233.juiceloader.entry.Entry"
|
EntryClass = "cn.xiaozhou233.juiceloader.entry.Entry"
|
||||||
EntryMethod = "start"
|
EntryMethod = "start"
|
||||||
+2
-4
@@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||||||
|
|
||||||
# version
|
# version
|
||||||
set(PROJECT_VERSION_MAJOR 1)
|
set(PROJECT_VERSION_MAJOR 1)
|
||||||
set(PROJECT_VERSION_MINOR 1)
|
set(PROJECT_VERSION_MINOR 3)
|
||||||
set(PROJECT_BUILD_NUMBER 29)
|
set(PROJECT_BUILD_NUMBER 37)
|
||||||
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
|
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
|
||||||
message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ")
|
message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ")
|
||||||
|
|
||||||
@@ -29,11 +29,9 @@ set(INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/includes)
|
|||||||
add_subdirectory(${INCLUDES}/ReflectiveDLLInjection)
|
add_subdirectory(${INCLUDES}/ReflectiveDLLInjection)
|
||||||
add_subdirectory(${INCLUDES}/log.c)
|
add_subdirectory(${INCLUDES}/log.c)
|
||||||
add_subdirectory(${INCLUDES}/tomlc17)
|
add_subdirectory(${INCLUDES}/tomlc17)
|
||||||
add_subdirectory(src/utils)
|
|
||||||
|
|
||||||
include(cmake/JuiceModule.cmake)
|
include(cmake/JuiceModule.cmake)
|
||||||
|
|
||||||
# modules
|
# modules
|
||||||
add_juice_module(agent)
|
add_juice_module(agent)
|
||||||
add_juice_module(juiceloader)
|
|
||||||
add_juice_module(injector)
|
add_juice_module(injector)
|
||||||
@@ -23,7 +23,6 @@ function(add_juice_module NAME)
|
|||||||
${CMAKE_SOURCE_DIR}/includes/log.c
|
${CMAKE_SOURCE_DIR}/includes/log.c
|
||||||
"${CMAKE_SOURCE_DIR}/src/${NAME}/utils"
|
"${CMAKE_SOURCE_DIR}/src/${NAME}/utils"
|
||||||
"${CMAKE_SOURCE_DIR}/src/${NAME}/include"
|
"${CMAKE_SOURCE_DIR}/src/${NAME}/include"
|
||||||
"${CMAKE_SOURCE_DIR}/src/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Link common libraries
|
# Link common libraries
|
||||||
@@ -31,7 +30,6 @@ function(add_juice_module NAME)
|
|||||||
ReflectiveDLLInjection
|
ReflectiveDLLInjection
|
||||||
log.c
|
log.c
|
||||||
jvm
|
jvm
|
||||||
utils
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory("${CMAKE_SOURCE_DIR}/src/${NAME}")
|
add_subdirectory("${CMAKE_SOURCE_DIR}/src/${NAME}")
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ target_include_directories(log.c PUBLIC
|
|||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(log.c PRIVATE LOG_USE_COLOR)
|
# target_compile_definitions(log.c PRIVATE LOG_USE_COLOR)
|
||||||
@@ -9,10 +9,10 @@
|
|||||||
typedef struct _InjectionInfo {
|
typedef struct _InjectionInfo {
|
||||||
char BootstrapAPIPath[INJECT_PATH_MAX];
|
char BootstrapAPIPath[INJECT_PATH_MAX];
|
||||||
char JuiceLoaderJarPath[INJECT_PATH_MAX];
|
char JuiceLoaderJarPath[INJECT_PATH_MAX];
|
||||||
char JuiceLoaderLibPath[INJECT_PATH_MAX];
|
|
||||||
char EntryJarPath[INJECT_PATH_MAX];
|
char EntryJarPath[INJECT_PATH_MAX];
|
||||||
char EntryClass[INJECT_PATH_MAX];
|
char EntryClass[INJECT_PATH_MAX];
|
||||||
char EntryMethod[INJECT_PATH_MAX];
|
char EntryMethod[INJECT_PATH_MAX];
|
||||||
|
char InjectionDir[INJECT_PATH_MAX];
|
||||||
} InjectionInfoType;
|
} InjectionInfoType;
|
||||||
extern InjectionInfoType InjectionInfo;
|
extern InjectionInfoType InjectionInfo;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef JAVAUTILS_H
|
||||||
|
#define JAVAUTILS_H
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include <jvmti.h>
|
||||||
|
|
||||||
|
char* get_class_name(JNIEnv* env, jclass clazz);
|
||||||
|
|
||||||
|
#endif //JAVAUTILS_H
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#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;
|
||||||
|
|
||||||
|
|
||||||
|
jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
#endif
|
||||||
File renamed without changes.
@@ -0,0 +1,478 @@
|
|||||||
|
/*
|
||||||
|
Author: xiaozhou233 (xiaozhou233@vip.qq.com)
|
||||||
|
File: juiceloader.c
|
||||||
|
Desc: JuiceLoader Native, Provide jni function for JuiceLoader
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include "log.h"
|
||||||
|
#include "juiceloader.h"
|
||||||
|
#include "javautils.h"
|
||||||
|
|
||||||
|
JuiceLoaderNativeType JuiceLoaderNative;
|
||||||
|
RetransformClassCacheType RetransformClassCache = {NULL, 0, 0};
|
||||||
|
|
||||||
|
static const char* g_bytecodes_classname = NULL;
|
||||||
|
static unsigned char* g_bytecodes = NULL;
|
||||||
|
static jint g_bytecodes_len = 0;
|
||||||
|
|
||||||
|
static void initLogger() {
|
||||||
|
log_set_level(LOG_TRACE);
|
||||||
|
log_is_log_time(false);
|
||||||
|
log_set_name("juiceloader");
|
||||||
|
log_info("Logger initialized.");
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
||||||
|
// ---- get class bytecode ---- //
|
||||||
|
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;
|
||||||
|
|
||||||
|
log_trace("[Hook:get] Captured class bytes of %s, len=%d", name, class_data_len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ---- get class bytecode ---- //
|
||||||
|
|
||||||
|
// ---- set class bytecode ---- //
|
||||||
|
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 = NULL;
|
||||||
|
jvmtiError err = (*jvmti_env)->Allocate(jvmti_env, entry->len, &newBytes);
|
||||||
|
if (err != JVMTI_ERROR_NONE || newBytes == NULL) {
|
||||||
|
log_error("[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;
|
||||||
|
|
||||||
|
log_info("[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ---- set class bytecode ---- //
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
||||||
|
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||||
|
|
||||||
|
initLogger();
|
||||||
|
if (env == NULL) {
|
||||||
|
log_error("env is NULL! env=%p", env);
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
if (jvmti == NULL) {
|
||||||
|
log_error("jvmti is NULL! jvmti=%p", jvmti);
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
JuiceLoaderNative.jvmti = jvmti;
|
||||||
|
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
||||||
|
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
||||||
|
|
||||||
|
|
||||||
|
// Dynamic register jni functions
|
||||||
|
jclass clazz = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoader");
|
||||||
|
if (clazz == NULL) {
|
||||||
|
log_error("Cannot find JuiceLoader class!");
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
JNINativeMethod methods[] = {
|
||||||
|
// JuiceLoaderNative.init()
|
||||||
|
{"init", "()Z", (void *)&loader_init},
|
||||||
|
// JuiceLoaderNative.injectJar(String(jarPath));
|
||||||
|
{"injectJar", "(Ljava/lang/String;)Z", (void *)&loader_injectJar},
|
||||||
|
// JuiceLoaderNative.redefineClass(Class<?> clazz, byte[] classBytes, int length);
|
||||||
|
{"redefineClass", "(Ljava/lang/Class;[BI)Z", (void *)&loader_redefineClass_clazz},
|
||||||
|
// JuiceLoaderNative.redefineClassByName(String className, byte[] classBytes, int length);
|
||||||
|
{"redefineClassByName", "(Ljava/lang/String;[BI)Z", (void *)&loader_redefineClass_className},
|
||||||
|
// JuiceLoaderNative.getLoadedClasses();
|
||||||
|
{"getLoadedClasses", "()[Ljava/lang/Class;", (void *)&loader_getLoadedClasses},
|
||||||
|
// public static native byte[] getClassBytes(Class<?> clazz);
|
||||||
|
// public static native byte[] getClassBytesByName(String className);
|
||||||
|
{"getClassBytes", "(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes},
|
||||||
|
{ "getClassBytesByName", "(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName},
|
||||||
|
// public static native boolean retransformClass(Class<?> clazz, byte[] classBytes, int length);
|
||||||
|
{"retransformClass", "(Ljava/lang/Class;[BI)Z", (void *)&loader_retransformClass},
|
||||||
|
// public static native boolean retransformClassByName(String className, byte[] classBytes, int length);
|
||||||
|
{"retransformClassByName", "(Ljava/lang/String;[BI)Z", (void *)&loader_retransformClassByName},
|
||||||
|
// public static native Class<?> getClassByName(String className);
|
||||||
|
{"getClassByName", "(Ljava/lang/String;)Ljava/lang/Class;", (void *)&loader_getClassByName}
|
||||||
|
};
|
||||||
|
jint result = (*env)->RegisterNatives(env, clazz, methods, sizeof(methods) / sizeof(methods[0]));
|
||||||
|
if (result != JNI_OK) {
|
||||||
|
log_error("Cannot register JNI methods for JuiceLoaderNative class! [result=%d]", result);
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init RetransformClassCache
|
||||||
|
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;
|
||||||
|
result = (*JuiceLoaderNative.jvmti)->AddCapabilities(JuiceLoaderNative.jvmti, &caps);
|
||||||
|
if (result != JNI_OK) {
|
||||||
|
log_error("Cannot add JVMTI capabilities! [result=%d]", result);
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register callbacks
|
||||||
|
jvmtiEventCallbacks callbacks;
|
||||||
|
memset(&callbacks, 0, sizeof(callbacks));
|
||||||
|
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
|
||||||
|
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
|
||||||
|
|
||||||
|
// Enable events
|
||||||
|
(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
|
||||||
|
|
||||||
|
return JNI_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ============ JNI Function ============ ///
|
||||||
|
|
||||||
|
// JuiceLoaderNative.init()
|
||||||
|
JNIEXPORT jboolean JNICALL loader_init(JNIEnv *env, jclass loader_class) {
|
||||||
|
// Deprcated
|
||||||
|
log_warn("JuiceLoaderNative.init() is deprecated, init will be done automatically.");
|
||||||
|
return JNI_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// JuiceLoaderNative.injectJar(String)
|
||||||
|
JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, jstring jarPath) {
|
||||||
|
log_info("injectJar Invoked!");
|
||||||
|
jboolean return_value = JNI_FALSE;
|
||||||
|
|
||||||
|
const char *pathStr = (*env)->GetStringUTFChars(env, jarPath, NULL);
|
||||||
|
if (pathStr == NULL) {
|
||||||
|
log_error("jar path is NULL!");
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
log_info("Injecting jar: %s", pathStr);
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
jvmtiError result = (*JuiceLoaderNative.jvmti)->AddToSystemClassLoaderSearch(JuiceLoaderNative.jvmti, pathStr);
|
||||||
|
if (result != JNI_OK) {
|
||||||
|
log_error("Cannot inject jar [result=%d]", result);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
return_value = JNI_TRUE;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (pathStr != NULL)
|
||||||
|
(*env)->ReleaseStringUTFChars(env, jarPath, pathStr);
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// JuiceLoaderNative.redefineClass(Class, byte[], int)
|
||||||
|
JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
|
||||||
|
log_info("redefineClass_clazz Invoked!");
|
||||||
|
jboolean return_value = JNI_FALSE;
|
||||||
|
jbyte* buf = NULL;
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info("Creating class definition...");
|
||||||
|
log_trace("class: %p", clazz);
|
||||||
|
log_trace("Class bytes length: %d", length);
|
||||||
|
buf = (*env)->GetByteArrayElements(env, classBytes, NULL);
|
||||||
|
if (buf == NULL) {
|
||||||
|
log_error("Cannot get class bytes");
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
jvmtiClassDefinition defs[1];
|
||||||
|
defs[0].klass = clazz;
|
||||||
|
defs[0].class_byte_count = length;
|
||||||
|
defs[0].class_bytes = (const unsigned char*) buf;
|
||||||
|
|
||||||
|
jvmtiError result = (*JuiceLoaderNative.jvmti)->RedefineClasses(JuiceLoaderNative.jvmti, 1, defs);
|
||||||
|
if (result != JVMTI_ERROR_NONE) {
|
||||||
|
log_error("Cannot redefine class [result=%d]", result);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
return_value = JNI_TRUE;
|
||||||
|
log_info("Class redefined successfully!");
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (buf != NULL) {
|
||||||
|
(*env)->ReleaseByteArrayElements(env, classBytes, buf, JNI_ABORT);
|
||||||
|
}
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
|
||||||
|
log_info("redefineClass_className Invoked!");
|
||||||
|
jboolean return_value = JNI_FALSE;
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* cname = (*env)->GetStringUTFChars(env, className, NULL);
|
||||||
|
log_info("Finding class %s", cname);
|
||||||
|
if (cname == NULL) {
|
||||||
|
log_error("Class name is NULL!");
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
jclass clazz = (*env)->FindClass(env, cname);
|
||||||
|
if (clazz == NULL) {
|
||||||
|
log_error("Class not found! [ClassName=%s, clazz=%p]", cname, clazz);
|
||||||
|
return_value = JNI_FALSE;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
return_value = loader_redefineClass_clazz(env, loader_class, clazz, classBytes, length);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (cname != NULL) {
|
||||||
|
(*env)->ReleaseStringUTFChars(env, className, cname);
|
||||||
|
}
|
||||||
|
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loader_class) {
|
||||||
|
log_info("loader_getLoadedClasses invoked!");
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug
|
||||||
|
jvmtiPhase phase; (*JuiceLoaderNative.jvmti)->GetPhase(JuiceLoaderNative.jvmti, &phase);
|
||||||
|
log_debug("JVMTI Phase=%d [getLoadedClasses]", phase);
|
||||||
|
|
||||||
|
jint count = 0;
|
||||||
|
jclass* classes = NULL;
|
||||||
|
|
||||||
|
jvmtiError result = (*JuiceLoaderNative.jvmti)->GetLoadedClasses(JuiceLoaderNative.jvmti, &count, &classes);
|
||||||
|
if (result != JVMTI_ERROR_NONE || count == 0) {
|
||||||
|
(*JuiceLoaderNative.jvmti)->Deallocate(JuiceLoaderNative.jvmti, (unsigned char*)classes);
|
||||||
|
return (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "java/lang/Class"), NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
jclass classClass = (*env)->FindClass(env, "java/lang/Class");
|
||||||
|
jobjectArray classArray = (*env)->NewObjectArray(env, count, classClass, NULL);
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
(*env)->SetObjectArrayElement(env, classArray, i, classes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (classes != NULL) {
|
||||||
|
(*JuiceLoaderNative.jvmti)->Deallocate(JuiceLoaderNative.jvmti, (unsigned char*)classes);
|
||||||
|
}
|
||||||
|
return classArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz) {
|
||||||
|
log_info("getClassBytes Invoked!");
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* className = get_class_name(env, clazz);
|
||||||
|
if (className == NULL) {
|
||||||
|
log_error("Failed to get class name!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// JVM 内部使用路径名(. 替换为 /)
|
||||||
|
for (char* p = className; *p; p++) {
|
||||||
|
if (*p == '.') *p = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
g_bytecodes = NULL;
|
||||||
|
g_bytecodes_classname = className;
|
||||||
|
g_bytecodes_len = 0;
|
||||||
|
|
||||||
|
jclass classes[1] = { clazz };
|
||||||
|
jvmtiError err = (*JuiceLoaderNative.jvmti)->RetransformClasses(JuiceLoaderNative.jvmti, 1, classes);
|
||||||
|
if (err != JVMTI_ERROR_NONE) {
|
||||||
|
log_error("RetransformClasses failed: %d", err);
|
||||||
|
free((void*)className);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_bytecodes != NULL) {
|
||||||
|
jbyteArray result = (*env)->NewByteArray(env, g_bytecodes_len);
|
||||||
|
(*env)->SetByteArrayRegion(env, result, 0, g_bytecodes_len, (jbyte*)g_bytecodes);
|
||||||
|
|
||||||
|
free((void*)g_bytecodes);
|
||||||
|
free((void*)className);
|
||||||
|
g_bytecodes = NULL;
|
||||||
|
g_bytecodes_classname = NULL;
|
||||||
|
g_bytecodes_len = 0;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_warn("No bytecode captured for class: %s", className);
|
||||||
|
free((void*)className);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass loader_class, jstring className) {
|
||||||
|
log_info("getClassBytesByName Invoked!");
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* cname = (*env)->GetStringUTFChars(env, className, NULL);
|
||||||
|
jclass clazz = (*env)->FindClass(env, cname);
|
||||||
|
(*env)->ReleaseStringUTFChars(env, className, cname);
|
||||||
|
|
||||||
|
if (clazz == NULL) {
|
||||||
|
log_error("Failed to find class %s", cname);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return loader_getClassBytes(env, loader_class, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("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 = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
// get class bytes
|
||||||
|
jbyte *bytes = (*env)->GetByteArrayElements(env, classBytes, NULL);
|
||||||
|
jint len = (*env)->GetArrayLength(env, classBytes);
|
||||||
|
|
||||||
|
if (RetransformClassCache.size >= RetransformClassCache.capacity) {
|
||||||
|
int new_capacity = RetransformClassCache.capacity == 0 ? 4 : RetransformClassCache.capacity * 2;
|
||||||
|
RetransformClassCache.arr = realloc(RetransformClassCache.arr, new_capacity * sizeof(ClassBytesCacheType));
|
||||||
|
RetransformClassCache.capacity = new_capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// save class bytes to cache
|
||||||
|
ClassBytesCacheType *entry = &RetransformClassCache.arr[RetransformClassCache.size++];
|
||||||
|
entry->name = _strdup(name);
|
||||||
|
entry->bytes = malloc(len);
|
||||||
|
memcpy(entry->bytes, bytes, len);
|
||||||
|
entry->len = len;
|
||||||
|
|
||||||
|
// clean up
|
||||||
|
(*env)->ReleaseByteArrayElements(env, classBytes, bytes, JNI_ABORT);
|
||||||
|
|
||||||
|
// retransform classes
|
||||||
|
jclass classes[1] = { clazz };
|
||||||
|
jvmtiError err = (*JuiceLoaderNative.jvmti)->RetransformClasses(JuiceLoaderNative.jvmti, 1, classes);
|
||||||
|
if (err != JVMTI_ERROR_NONE) {
|
||||||
|
log_error("RetransformClasses failed! [err=%d]", err);
|
||||||
|
return JNI_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info("[retransformClass] Class retransformed: %s (len=%d)", entry->name, entry->len);
|
||||||
|
return JNI_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
|
||||||
|
const char *name_dot = (*env)->GetStringUTFChars(env, className, NULL);
|
||||||
|
char *name = strdup(name_dot);
|
||||||
|
for (char *p = name; *p; ++p) if (*p == '.') *p = '/';
|
||||||
|
(*env)->ReleaseStringUTFChars(env, className, name_dot);
|
||||||
|
|
||||||
|
jclass target = (*env)->FindClass(env, name);
|
||||||
|
free(name);
|
||||||
|
|
||||||
|
if (!target) {
|
||||||
|
log_error("FindClass failed in retransformClassByName");
|
||||||
|
return JNI_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return loader_retransformClass(env, loader_class, target, classBytes, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class, jstring className) {
|
||||||
|
if (className == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// jstring -> const char*
|
||||||
|
const char *name_utf = (*env)->GetStringUTFChars(env, className, NULL);
|
||||||
|
if (name_utf == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find class
|
||||||
|
jclass clazz = (*env)->FindClass(env, name_utf);
|
||||||
|
|
||||||
|
// clean up
|
||||||
|
(*env)->ReleaseStringUTFChars(env, className, name_utf);
|
||||||
|
|
||||||
|
if (clazz == NULL) {
|
||||||
|
log_error("Failed to find class %s", name_utf);
|
||||||
|
}
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
+21
-150
@@ -12,16 +12,23 @@ Desc: Entry point for the agent
|
|||||||
#include "jvmti.h"
|
#include "jvmti.h"
|
||||||
#include "InjectParameters.h"
|
#include "InjectParameters.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "GlobalUtils.h"
|
#include "jvmutils.h"
|
||||||
#include "shlwapi.h"
|
#include "shlwapi.h"
|
||||||
#include "tomlc17.h"
|
#include "tomlc17.h"
|
||||||
#include "datautils.h"
|
#include "datautils.h"
|
||||||
|
#include "juiceloader.h"
|
||||||
|
|
||||||
#define WIN_X64
|
#define WIN_X64
|
||||||
extern HINSTANCE hAppInstance;
|
extern HINSTANCE hAppInstance;
|
||||||
|
|
||||||
InjectionInfoType InjectionInfo;
|
InjectionInfoType InjectionInfo;
|
||||||
|
|
||||||
|
struct _JuiceAgent {
|
||||||
|
bool isAttached;
|
||||||
|
JavaVM *jvm;
|
||||||
|
jvmtiEnv *jvmti;
|
||||||
|
} JuiceAgent = {false, NULL, NULL};
|
||||||
|
|
||||||
static void initLogger() {
|
static void initLogger() {
|
||||||
log_set_level(LOG_TRACE);
|
log_set_level(LOG_TRACE);
|
||||||
log_is_log_time(false);
|
log_is_log_time(false);
|
||||||
@@ -58,13 +65,6 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
}
|
}
|
||||||
toml_free(toml_result);
|
toml_free(toml_result);
|
||||||
|
|
||||||
log_trace("Checking bootstrap-api.jar");
|
|
||||||
if (GetFileAttributesA(InjectionInfo.BootstrapAPIPath) == INVALID_FILE_ATTRIBUTES) {
|
|
||||||
log_error("bootstrap-api.jar not found: %s", InjectionInfo.BootstrapAPIPath);
|
|
||||||
// MessageBoxA(NULL, "bootstrap-api.jar not found", "Error", MB_OK);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check JuiceLoader.jar
|
// Check JuiceLoader.jar
|
||||||
|
|
||||||
log_trace("Checking JuiceLoader.jar");
|
log_trace("Checking JuiceLoader.jar");
|
||||||
@@ -94,6 +94,8 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
if (result != JNI_OK) {
|
if (result != JNI_OK) {
|
||||||
log_error("Failed to get JNIEnv (%d)", result);
|
log_error("Failed to get JNIEnv (%d)", result);
|
||||||
// MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
|
// MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
|
||||||
|
log_error("Maybe the JVM havent been init yet.");
|
||||||
|
JuiceAgent.isAttached = false;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
log_info("Got JNIEnv");
|
log_info("Got JNIEnv");
|
||||||
@@ -112,20 +114,23 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
log_info("Enabled jvmti");
|
log_info("Enabled jvmti");
|
||||||
|
|
||||||
// Inject jars
|
// Inject jars
|
||||||
log_info("injecting bootstrap-api jar...");
|
|
||||||
(*jvmti)->AddToSystemClassLoaderSearch(jvmti, InjectionInfo.BootstrapAPIPath);
|
|
||||||
log_info("injected bootstrap-api jar");
|
|
||||||
|
|
||||||
log_info("injecting loader jar...");
|
log_info("injecting loader jar...");
|
||||||
(*jvmti)->AddToSystemClassLoaderSearch(jvmti, InjectionInfo.JuiceLoaderJarPath);
|
(*jvmti)->AddToSystemClassLoaderSearch(jvmti, InjectionInfo.JuiceLoaderJarPath);
|
||||||
log_info("injected loader jar");
|
log_info("injected loader jar");
|
||||||
|
|
||||||
// Invoke loader.init(JuiceLoaderLibPath, EntryJarPath)
|
// init juiceloader
|
||||||
|
log_info("init juice loader...");
|
||||||
|
init_juiceloader(env, jvmti);
|
||||||
|
|
||||||
|
initLogger();
|
||||||
|
log_info("juice loader init done");
|
||||||
|
|
||||||
log_info("invoking loader init()...");
|
log_info("invoking loader init()...");
|
||||||
log_info("============ Loader Info ============\n");
|
log_info("============ Loader Info ============\n");
|
||||||
jclass cls = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoader");
|
jclass cls = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoaderBootstrap");
|
||||||
if (cls == NULL) {
|
if (cls == NULL) {
|
||||||
log_error("FindClass JuiceLoader failed");
|
log_error("FindClass JuiceLoaderBootstrap failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "init", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
|
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "init", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
|
||||||
@@ -139,10 +144,10 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
(*env)->CallStaticVoidMethod(env, cls, mid,
|
(*env)->CallStaticVoidMethod(env, cls, mid,
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.JuiceLoaderLibPath),
|
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryJarPath),
|
(*env)->NewStringUTF(env, InjectionInfo.EntryJarPath),
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryClass),
|
(*env)->NewStringUTF(env, InjectionInfo.EntryClass),
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryMethod));
|
(*env)->NewStringUTF(env, InjectionInfo.EntryMethod),
|
||||||
|
(*env)->NewStringUTF(env, InjectionInfo.InjectionDir));
|
||||||
log_info("============ Loader Info =============\n");
|
log_info("============ Loader Info =============\n");
|
||||||
log_info("invoked. ");
|
log_info("invoked. ");
|
||||||
log_info("done. cleaning up...");
|
log_info("done. cleaning up...");
|
||||||
@@ -177,28 +182,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
log_info("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
log_info("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||||
|
|
||||||
DisableThreadLibraryCalls(hinstDLL);
|
DisableThreadLibraryCalls(hinstDLL);
|
||||||
|
|
||||||
// JVM Options Inject Method Detection
|
|
||||||
// TODO: Remove Duplicated Code: GetCreatedJVM/GetEnv and var jvm/env (Duplicated at: ThreadProc)
|
|
||||||
JavaVM *jvm = NULL;
|
|
||||||
JNIEnv *env = NULL;
|
|
||||||
jint result = GetCreatedJVM(&jvm);
|
|
||||||
// Check if JVM is created
|
|
||||||
if (result != JNI_OK || jvm == NULL) {
|
|
||||||
log_error("Failed to get JVM (%d) [JVM: %p]", result, jvm);
|
|
||||||
MessageBoxA(NULL, "Failed to get JVM [GetCreatedJVM Faliled or JVM is NULL]\nCheck console for more info.", "Error", MB_OK);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
log_info("DllMain GetCreatedJVM OK. Checking if JVM is inited...");
|
|
||||||
// Check if JVM is inited
|
|
||||||
// JNI_EDETACHED: jvm not initialized, means Agent loaded before JVM inited (jvm options: -agentpath)
|
|
||||||
// Will exit DllMain and jvm will invoke Agent_OnLoad
|
|
||||||
if (GetJNIEnv(jvm, &env) == JNI_EDETACHED) {
|
|
||||||
log_warn("Inject Method JVM Options Detected! Exiting...");
|
|
||||||
log_trace("DllMain(DLL_PROCESS_ATTACH) Exiting...");
|
|
||||||
log_info("Agent Will be injected by the Inject Methood JVM Options.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allocate memory for local param
|
// Allocate memory for local param
|
||||||
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
||||||
@@ -256,116 +239,4 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return bReturnValue;
|
return bReturnValue;
|
||||||
}
|
|
||||||
|
|
||||||
/// =========================================================== ///
|
|
||||||
/// Inject Method: JVM Options -agentpath
|
|
||||||
|
|
||||||
/// =========================================================== ///
|
|
||||||
/// Function: vm_init
|
|
||||||
/// Description: Called when the JVM is fully initialized. This is a callback function.
|
|
||||||
/// =========================================================== ///
|
|
||||||
static char *agent_options = NULL;
|
|
||||||
static void JNICALL vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) {
|
|
||||||
log_info("vminit: VM fully initialized!");
|
|
||||||
log_info("Use options: %s", agent_options);
|
|
||||||
|
|
||||||
log_info("JNIEnv: %p", env);
|
|
||||||
log_info("jvmtiEnv: %p", jvmti);
|
|
||||||
|
|
||||||
log_info("Version: %d", (*env)->GetVersion(env));
|
|
||||||
|
|
||||||
// TODO: Remove Duplicated Code
|
|
||||||
|
|
||||||
// Check if agent_options is null
|
|
||||||
if (agent_options == NULL) {
|
|
||||||
// Use DLL path as default
|
|
||||||
log_info("agent_options is null, use DLL path as default");
|
|
||||||
char DllDir[INJECT_PATH_MAX] = {0};
|
|
||||||
|
|
||||||
if (GetModuleFileNameA(hAppInstance, DllDir, INJECT_PATH_MAX) == 0) {
|
|
||||||
// Failed to get module file name
|
|
||||||
DWORD err = GetLastError();
|
|
||||||
if (err == ERROR_MOD_NOT_FOUND) {
|
|
||||||
// Reflective detected, lpReserved must be set.
|
|
||||||
log_fatal("Reflective load detected! Please set ConfigDir manually! Exit.");
|
|
||||||
} else {
|
|
||||||
log_error("Normal Inject: GetModuleFileNameA failed! Error: %lu", err);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
// Normal Inject(e.g. CreateRemoteThread+LoadLibraryA), lpReserved can be NULL.
|
|
||||||
PathRemoveFileSpecA(DllDir);
|
|
||||||
log_info("GetModuleFileNameA: %s", DllDir);
|
|
||||||
agent_options = DllDir;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log_trace("agent_options: %s", agent_options);
|
|
||||||
|
|
||||||
// Allocate memory for InjectParameters
|
|
||||||
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
|
||||||
if (localParm == NULL) {
|
|
||||||
log_error("malloc failed for InjectParameters");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
memset(localParm, 0, sizeof(InjectParameters));
|
|
||||||
|
|
||||||
// safe copy
|
|
||||||
safe_copy(localParm->ConfigDir, agent_options, INJECT_PATH_MAX);
|
|
||||||
|
|
||||||
// Start new thread
|
|
||||||
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL);
|
|
||||||
if (hThread) {
|
|
||||||
CloseHandle(hThread);
|
|
||||||
} else {
|
|
||||||
log_error("CreateThread failed %lu", GetLastError());
|
|
||||||
free(localParm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// =========================================================== ///
|
|
||||||
/// Function: Agent_OnLoad
|
|
||||||
/// Description: Java Agent OnLoad
|
|
||||||
/// Inject Method: JVM Options A) -agentpath:<path-to-agent>.dll=<param-config-dir>
|
|
||||||
/// Example: java -agentpath:./libagent.dll=./config -jar myapp.jar
|
|
||||||
/// =========================================================== ///
|
|
||||||
|
|
||||||
// Notice: This function is called by the JVM when the agent is loaded.
|
|
||||||
// Warning: JVM not initialized yet when this function is called.
|
|
||||||
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
|
|
||||||
initLogger();
|
|
||||||
log_info("Agent_OnLoad");
|
|
||||||
log_info("Agent_OnLoad: options: %s", options);
|
|
||||||
|
|
||||||
// save options
|
|
||||||
if (options != NULL) {
|
|
||||||
agent_options = strdup(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
jvmtiEnv *jvmti;
|
|
||||||
(*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1_2);
|
|
||||||
|
|
||||||
// callbacks
|
|
||||||
jvmtiEventCallbacks callbacks = {0};
|
|
||||||
callbacks.VMInit = &vm_init;
|
|
||||||
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
|
|
||||||
|
|
||||||
// enable events
|
|
||||||
(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
|
|
||||||
|
|
||||||
log_info("Agent_OnLoad: Done, waiting for JVM Init event.");
|
|
||||||
return JNI_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Inject Method: JVM Options -agentpath
|
|
||||||
/// =========================================================== ///
|
|
||||||
|
|
||||||
// Notice: This function is useless but required by the JVM (in Windows).
|
|
||||||
JNIEXPORT jint JNICALL Agent_OnAttach (JavaVM* vm, char *options, void *reserved) {
|
|
||||||
log_info("Agent_OnAttach");
|
|
||||||
#ifdef WIN_X64
|
|
||||||
log_warn("Agent_OnAttach: Windows will invoke DllMain, Exiting Agent_OnAttach.");
|
|
||||||
log_info("Agent_OnAttach will exit in Windows, Loading will be done in DllMain.\n");
|
|
||||||
#endif
|
|
||||||
return JNI_OK;
|
|
||||||
}
|
}
|
||||||
+24
-75
@@ -8,97 +8,46 @@ Desc: data utils
|
|||||||
#include "datautils.h"
|
#include "datautils.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "tomlc17.h"
|
#include "tomlc17.h"
|
||||||
|
#include "juiceloader.h"
|
||||||
|
|
||||||
void safe_copy(char *dest, const char *src, size_t dest_size) {
|
void safe_copy(char *dest, const char *src, size_t dest_size) {
|
||||||
if (!dest || dest_size == 0) return;
|
if (!dest || dest_size == 0) return;
|
||||||
strncpy(dest, src, dest_size - 1);
|
strncpy(dest, src, dest_size - 1);
|
||||||
dest[dest_size - 1] = '\0';
|
dest[dest_size - 1] = '\0';
|
||||||
}
|
}
|
||||||
|
static void GetTomlStringOrDefault(toml_datum_t tableDatum, const char* key, const char* defaultVal, char* outBuf, size_t bufSize) {
|
||||||
|
toml_datum_t datum = toml_seek(tableDatum, key);
|
||||||
|
if (datum.type != TOML_STRING || !datum.u.s || datum.u.s[0] == '\0') {
|
||||||
|
safe_copy(outBuf, defaultVal, bufSize);
|
||||||
|
} else {
|
||||||
|
safe_copy(outBuf, datum.u.s, bufSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool ReadInjectionInfo(toml_result_t result, InjectParameters* params) {
|
bool ReadInjectionInfo(toml_result_t result, InjectParameters* params) {
|
||||||
|
if (!result.ok) return false;
|
||||||
|
|
||||||
toml_datum_t InjectionTree = toml_seek(result.toptab, "Injection");
|
toml_datum_t InjectionTree = toml_seek(result.toptab, "Injection");
|
||||||
|
if (InjectionTree.type != TOML_TABLE) {
|
||||||
// ======= BootstrapAPIPath =======
|
log_error("Injection section missing");
|
||||||
toml_datum_t BootstrapAPIPath = toml_seek(InjectionTree, "BootstrapAPIPath");
|
|
||||||
char BootstrapAPIPathStr[INJECT_PATH_MAX];
|
|
||||||
if (BootstrapAPIPath.type != TOML_STRING) {
|
|
||||||
log_error("BootstrapAPIPath is not a string.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (BootstrapAPIPath.u.s == NULL || strlen(BootstrapAPIPath.u.s) == 0) {
|
|
||||||
sprintf(BootstrapAPIPathStr, "%s\\bootstrap-api.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(BootstrapAPIPathStr, "%s", BootstrapAPIPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.BootstrapAPIPath, BootstrapAPIPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= BootstrapAPIPath =======
|
|
||||||
|
|
||||||
// ======= JuiceLoaderJarPath =======
|
char tmp[INJECT_PATH_MAX];
|
||||||
toml_datum_t JuiceLoaderJarPath = toml_seek(InjectionTree, "JuiceLoaderJarPath");
|
|
||||||
char JuiceLoaderJarPathStr[INJECT_PATH_MAX];
|
|
||||||
if (JuiceLoaderJarPath.type != TOML_STRING) {
|
|
||||||
log_error("JuiceLoaderJarPath is not a string.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JuiceLoaderJarPath.u.s == NULL || strlen(JuiceLoaderJarPath.u.s) == 0) {
|
|
||||||
sprintf(JuiceLoaderJarPathStr, "%s\\JuiceLoader.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(JuiceLoaderJarPathStr, "%s", JuiceLoaderJarPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.JuiceLoaderJarPath, JuiceLoaderJarPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= JuiceLoaderJarPath =======
|
|
||||||
|
|
||||||
// ======= JuiceLoaderLibPath =======
|
snprintf(tmp, sizeof(tmp), "%s\\JuiceLoader.jar", params->ConfigDir);
|
||||||
toml_datum_t JuiceLoaderLibPath = toml_seek(InjectionTree, "JuiceLoaderLibPath");
|
GetTomlStringOrDefault(InjectionTree, "JuiceLoaderJarPath", tmp, InjectionInfo.JuiceLoaderJarPath, INJECT_PATH_MAX);
|
||||||
char JuiceLoaderLibPathStr[INJECT_PATH_MAX];
|
|
||||||
if (JuiceLoaderLibPath.type != TOML_STRING) {
|
|
||||||
log_error("JuiceLoaderLibPath is not a string.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JuiceLoaderLibPath.u.s == NULL || strlen(JuiceLoaderLibPath.u.s) == 0) {
|
|
||||||
sprintf(JuiceLoaderLibPathStr, "%s\\libjuiceloader.dll", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(JuiceLoaderLibPathStr, "%s", JuiceLoaderLibPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.JuiceLoaderLibPath, JuiceLoaderLibPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= JuiceLoaderLibPath =======
|
|
||||||
|
|
||||||
// ======= Entry =======
|
snprintf(tmp, sizeof(tmp), "%s\\entry.jar", params->ConfigDir);
|
||||||
toml_datum_t EntryJarPath = toml_seek(InjectionTree, "EntryJarPath");
|
GetTomlStringOrDefault(InjectionTree, "EntryJarPath", tmp, InjectionInfo.EntryJarPath, INJECT_PATH_MAX);
|
||||||
char EntryJarPathStr[INJECT_PATH_MAX];
|
|
||||||
if (EntryJarPath.type != TOML_STRING) {
|
|
||||||
log_error("EntryJarPath is not a string.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (EntryJarPath.u.s == NULL || strlen(EntryJarPath.u.s) == 0) {
|
|
||||||
sprintf(EntryJarPathStr, "%s\\entry.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(EntryJarPathStr, "%s", EntryJarPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.EntryJarPath, EntryJarPathStr, INJECT_PATH_MAX);
|
|
||||||
|
|
||||||
toml_datum_t EntryClass = toml_seek(InjectionTree, "EntryClass");
|
GetTomlStringOrDefault(InjectionTree, "EntryClass", "cn.xiaozhou233.juiceloader.entry.Entry", InjectionInfo.EntryClass, INJECT_PATH_MAX);
|
||||||
if (EntryClass.type != TOML_STRING) {
|
GetTomlStringOrDefault(InjectionTree, "EntryMethod", "start", InjectionInfo.EntryMethod, INJECT_PATH_MAX);
|
||||||
log_error("EntryClass is not a string.");
|
|
||||||
return false;
|
snprintf(tmp, sizeof(tmp), "%s\\injection", params->ConfigDir);
|
||||||
}
|
GetTomlStringOrDefault(InjectionTree, "InjectionDir", tmp, InjectionInfo.InjectionDir, INJECT_PATH_MAX);
|
||||||
if (EntryClass.u.s == NULL || strlen(EntryClass.u.s) == 0) {
|
|
||||||
safe_copy(InjectionInfo.EntryClass, "cn.xiaozhou233.juiceloader.entry.Entry", INJECT_PATH_MAX);
|
|
||||||
} else {
|
|
||||||
safe_copy(InjectionInfo.EntryClass, EntryClass.u.s, INJECT_PATH_MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
toml_datum_t EntryMethod = toml_seek(InjectionTree, "EntryMethod");
|
|
||||||
if (EntryMethod.type != TOML_STRING) {
|
|
||||||
log_error("EntryMethod is not a string.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (EntryMethod.u.s == NULL || strlen(EntryMethod.u.s) == 0) {
|
|
||||||
safe_copy(InjectionInfo.EntryMethod, "start", INJECT_PATH_MAX);
|
|
||||||
} else {
|
|
||||||
safe_copy(InjectionInfo.EntryMethod, EntryMethod.u.s, INJECT_PATH_MAX);
|
|
||||||
}
|
|
||||||
// ======= Entry =======
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include "javautils.h"
|
||||||
|
#include <jni.h>
|
||||||
|
#include <jvmti.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char* get_class_name(JNIEnv* env, jclass clazz) {
|
||||||
|
jclass clsClass = (*env)->FindClass(env, "java/lang/Class");
|
||||||
|
jmethodID mid_getName = (*env)->GetMethodID(env, clsClass, "getName", "()Ljava/lang/String;");
|
||||||
|
jstring nameStr = (jstring)(*env)->CallObjectMethod(env, clazz, mid_getName);
|
||||||
|
|
||||||
|
const char* temp = (*env)->GetStringUTFChars(env, nameStr, NULL);
|
||||||
|
char* result = strdup(temp);
|
||||||
|
(*env)->ReleaseStringUTFChars(env, nameStr, temp);
|
||||||
|
(*env)->DeleteLocalRef(env, nameStr);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "./GlobalUtils.h"
|
#include "jvmutils.h"
|
||||||
|
|
||||||
// Get JavaVM
|
// Get JavaVM
|
||||||
jint GetCreatedJVM(JavaVM **out_jvm) {
|
jint GetCreatedJVM(JavaVM **out_jvm) {
|
||||||
@@ -23,6 +23,14 @@ 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
|
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2Ljava_lang_String_2
|
||||||
(JNIEnv *, jobject, jint, jstring, jstring);
|
(JNIEnv *, jobject, jint, jstring, jstring);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: cn_xiaozhou233_juiceagent_injector_InjectorNative
|
||||||
|
* Method: findWindowsByTitle
|
||||||
|
* Signature: (Ljava/lang/String;)[Lcn/xiaozhou233/juiceagent/injector/InjectorNative/WindowInfo;
|
||||||
|
*/
|
||||||
|
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle
|
||||||
|
(JNIEnv *, jclass, jstring);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -232,4 +232,64 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ
|
|||||||
(*env)->ReleaseStringUTFChars(env, configDir, ConfigDir);
|
(*env)->ReleaseStringUTFChars(env, configDir, ConfigDir);
|
||||||
|
|
||||||
return (ret == 0) ? JNI_TRUE : JNI_FALSE;
|
return (ret == 0) ? JNI_TRUE : JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ================ FindWindowsByTitle =================
|
||||||
|
#define MAX_RESULTS 128
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
wchar_t title[256];
|
||||||
|
DWORD pid;
|
||||||
|
} WindowData;
|
||||||
|
|
||||||
|
static wchar_t g_keyword[256];
|
||||||
|
static WindowData g_results[MAX_RESULTS];
|
||||||
|
static int g_count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
|
||||||
|
wchar_t title[256];
|
||||||
|
GetWindowTextW(hwnd, title, 256);
|
||||||
|
if (wcslen(title) == 0) return TRUE;
|
||||||
|
|
||||||
|
if (wcsstr(title, g_keyword)) {
|
||||||
|
DWORD pid = 0;
|
||||||
|
GetWindowThreadProcessId(hwnd, &pid);
|
||||||
|
|
||||||
|
if (g_count < MAX_RESULTS && pid) {
|
||||||
|
wcsncpy(g_results[g_count].title, title, 255);
|
||||||
|
g_results[g_count].pid = pid;
|
||||||
|
g_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle
|
||||||
|
(JNIEnv *env, jclass clazz, jstring keyword) {
|
||||||
|
|
||||||
|
const jchar *input = (*env)->GetStringChars(env, keyword, NULL);
|
||||||
|
wcsncpy(g_keyword, (const wchar_t *)input, 255);
|
||||||
|
g_keyword[255] = L'\0';
|
||||||
|
(*env)->ReleaseStringChars(env, keyword, input);
|
||||||
|
|
||||||
|
g_count = 0;
|
||||||
|
EnumWindows(EnumWindowsProc, 0);
|
||||||
|
|
||||||
|
jclass infoClass = (*env)->FindClass(env, "cn/xiaozhou233/juiceagent/injector/InjectorNative$WindowInfo");
|
||||||
|
if (!infoClass) return NULL;
|
||||||
|
|
||||||
|
jmethodID ctor = (*env)->GetMethodID(env, infoClass, "<init>", "(Ljava/lang/String;I)V");
|
||||||
|
if (!ctor) return NULL;
|
||||||
|
|
||||||
|
jobjectArray array = (*env)->NewObjectArray(env, g_count, infoClass, NULL);
|
||||||
|
|
||||||
|
for (int i = 0; i < g_count; i++) {
|
||||||
|
jstring title = (*env)->NewString(env, (jchar*)g_results[i].title, wcslen(g_results[i].title));
|
||||||
|
jobject obj = (*env)->NewObject(env, infoClass, ctor, title, (jint)g_results[i].pid);
|
||||||
|
(*env)->SetObjectArrayElement(env, array, i, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
/// ===============================================================================
|
||||||
@@ -1 +0,0 @@
|
|||||||
# src/loader/CMakeLists.txt
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#ifndef JUICELOADER_H
|
|
||||||
#define JUICELOADER_H
|
|
||||||
|
|
||||||
#include <jni.h>
|
|
||||||
#include <jvmti.h>
|
|
||||||
|
|
||||||
struct _JuiceLoaderNative {
|
|
||||||
JavaVM *jvm;
|
|
||||||
jvmtiEnv *jvmti;
|
|
||||||
} JuiceLoaderNative;
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
|
||||||
#include <jni.h>
|
|
||||||
/* Header for class cn_xiaozhou233_juiceloader_JuiceLoaderNative */
|
|
||||||
|
|
||||||
#ifndef _Included_cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
#define _Included_cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
* Method: init
|
|
||||||
* Signature: ()Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_init
|
|
||||||
(JNIEnv *, jobject);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
* Method: injectJar
|
|
||||||
* Signature: (Ljava/lang/String;)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_injectJar
|
|
||||||
(JNIEnv *, jobject, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
* Method: redefineClass
|
|
||||||
* Signature: (Ljava/lang/Class;[BI)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_redefineClass__Ljava_lang_Class_2_3BI
|
|
||||||
(JNIEnv *, jobject, jclass, jbyteArray, jint);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
* Method: redefineClass
|
|
||||||
* Signature: (Ljava/lang/String;[BI)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_redefineClass__Ljava_lang_String_2_3BI
|
|
||||||
(JNIEnv *, jobject, jstring, jbyteArray, jint);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceloader_JuiceLoaderNative
|
|
||||||
* Method: getLoadedClasses
|
|
||||||
* Signature: ()[Ljava/lang/Class;
|
|
||||||
*/
|
|
||||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_getLoadedClasses
|
|
||||||
(JNIEnv *, jobject);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef JUICELOADER_UTILS_H
|
|
||||||
#define JUICELOADER_UTILS_H
|
|
||||||
|
|
||||||
#include "jvmti.h"
|
|
||||||
|
|
||||||
char* get_method_fullname(jvmtiEnv*, JNIEnv*, jthread, jmethodID);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,239 +0,0 @@
|
|||||||
#include "windows.h"
|
|
||||||
#include "JuiceLoaderNative.h"
|
|
||||||
#include "jvmti.h"
|
|
||||||
#include "log.h"
|
|
||||||
#include "JuiceLoaderUtils.h"
|
|
||||||
#include "JuiceLoader.h"
|
|
||||||
|
|
||||||
#define LOG_PREFIX "[JuiceLoader]"
|
|
||||||
static void check_jvmti_error(jvmtiEnv *j, jvmtiError err, const char *msg) {
|
|
||||||
if (err != JVMTI_ERROR_NONE) {
|
|
||||||
char *errname = NULL;
|
|
||||||
(*j)->GetErrorName(j, err, &errname);
|
|
||||||
log_error("JVMTI ERROR: %s: %d (%s)", msg, err, (errname==NULL?"Unknown":errname));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Method entry callback */
|
|
||||||
static void JNICALL callbackMethodEntry(jvmtiEnv *j, JNIEnv *env, jthread thread, jmethodID method) {
|
|
||||||
char *fullname = get_method_fullname(j, env, thread, method);
|
|
||||||
if (fullname) {
|
|
||||||
log_info("ENTER: %s", fullname);
|
|
||||||
free(fullname);
|
|
||||||
} else {
|
|
||||||
log_warn("ENTER: <unknown method>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int setup(JNIEnv *env) {
|
|
||||||
log_is_log_filename(false);
|
|
||||||
log_is_log_line(false);
|
|
||||||
log_is_log_time(false);
|
|
||||||
log_set_level(LOG_DEBUG);
|
|
||||||
|
|
||||||
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
|
||||||
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
|
||||||
|
|
||||||
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
|
||||||
|
|
||||||
log_info("%s setup!", LOG_PREFIX);
|
|
||||||
// Get JavaVM
|
|
||||||
if ((*env)->GetJavaVM(env, &JuiceLoaderNative.jvm) != 0 || JuiceLoaderNative.jvm == NULL) {
|
|
||||||
log_error("%s GetJavaVM failed", LOG_PREFIX);
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
log_info("%s JNI_GetCreatedJavaVMs success", LOG_PREFIX);
|
|
||||||
}
|
|
||||||
|
|
||||||
// // Get JNIEnv
|
|
||||||
// if ((*JuiceLoaderNative.jvm)->AttachCurrentThread(JuiceLoaderNative.jvm, (void **)&JuiceLoaderNative.env, NULL) != JNI_OK) {
|
|
||||||
// log_error("%s AttachCurrentThread failed", LOG_PREFIX);
|
|
||||||
// return 1;
|
|
||||||
// } else {
|
|
||||||
// log_info("%s AttachCurrentThread success", LOG_PREFIX);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Debug: JVM version
|
|
||||||
jint version = (*env)->GetVersion(env);
|
|
||||||
log_debug("%s JVM version: 0x%x", LOG_PREFIX, version);
|
|
||||||
|
|
||||||
if ((*JuiceLoaderNative.jvm)->GetEnv(JuiceLoaderNative.jvm, (void**)&JuiceLoaderNative.jvmti, JVMTI_VERSION_1_2) != JNI_OK || JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("%s GetEnv for jvmti failed", LOG_PREFIX);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
log_info("%s Enabled JVMTI at %p", LOG_PREFIX, JuiceLoaderNative.jvmti);
|
|
||||||
jvmtiError err;
|
|
||||||
// jvmtiEventCallbacks callbacks;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("JVMTI is NULL (Before AddCapabilities, After GetEnv), %p", JuiceLoaderNative.jvmti);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
err = (*JuiceLoaderNative.jvmti)->AddCapabilities(JuiceLoaderNative.jvmti, &caps);
|
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddCapabilities");
|
|
||||||
|
|
||||||
// memset(&callbacks, 0, sizeof(callbacks));
|
|
||||||
// callbacks.MethodEntry = &callbackMethodEntry;
|
|
||||||
// err = (*JuiceLoaderNative.jvmti)->SetEventCallbacks(JuiceLoaderNative.jvmti, &callbacks, (jint)sizeof(callbacks));
|
|
||||||
// check_jvmti_error(JuiceLoaderNative.jvmti, err, "SetEventCallbacks");
|
|
||||||
|
|
||||||
// err = (*JuiceLoaderNative.jvmti)->SetEventNotificationMode(JuiceLoaderNative.jvmti, JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, NULL);
|
|
||||||
// check_jvmti_error(JuiceLoaderNative.jvmti, err, "EnableMethodEntry");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
JNI Func: JuiceLoaderNative.init()
|
|
||||||
Desc: Init environment
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_init
|
|
||||||
(JNIEnv *env, jobject obj) {
|
|
||||||
log_info("%s JNI Func Init Invoke!");
|
|
||||||
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
|
||||||
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
|
||||||
if(setup(env) == 0) {
|
|
||||||
return JNI_TRUE;
|
|
||||||
}
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
JNI Func: JuiceLoaderNative.injectJar(path)
|
|
||||||
Desc: add jar to boostrap classloader
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_injectJar
|
|
||||||
(JNIEnv *env, jobject obj, jstring path) {
|
|
||||||
log_info("%s JNI Func InjectJar Invoke!", LOG_PREFIX);
|
|
||||||
jvmtiError err;
|
|
||||||
if(JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *pathStr = (*env)->GetStringUTFChars(env, path, NULL);
|
|
||||||
log_info("%s InjectJar: %s", LOG_PREFIX, pathStr);
|
|
||||||
|
|
||||||
err = (*JuiceLoaderNative.jvmti)->AddToSystemClassLoaderSearch(JuiceLoaderNative.jvmti, pathStr);
|
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddToSystemClassLoaderSearch");
|
|
||||||
return JNI_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
JNI Func: JuiceLoaderNative.redefine
|
|
||||||
Desc: Redefine Class (class, bytecodes, len)
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_redefineClass__Ljava_lang_Class_2_3BI
|
|
||||||
(JNIEnv *env, jobject obj, jclass clazz, jbyteArray bytecodes, jint len) {
|
|
||||||
log_info("%s JNI Func RedefineClass Invoke!", LOG_PREFIX);
|
|
||||||
jvmtiError err;
|
|
||||||
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
log_info("%s Creating class definition...", LOG_PREFIX);
|
|
||||||
jbyte* buf = (*env)->GetByteArrayElements(env, bytecodes, NULL); // get raw bytes
|
|
||||||
|
|
||||||
jvmtiClassDefinition defs[1];
|
|
||||||
defs[0].klass = clazz;
|
|
||||||
defs[0].class_byte_count = len;
|
|
||||||
defs[0].class_bytes = (const unsigned char*) buf;
|
|
||||||
|
|
||||||
log_info("%s Redefining class...", LOG_PREFIX);
|
|
||||||
err = (*JuiceLoaderNative.jvmti)->RedefineClasses(JuiceLoaderNative.jvmti, 1, defs);
|
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "RedefineClasses");
|
|
||||||
|
|
||||||
(*env)->ReleaseByteArrayElements(env, bytecodes, buf, JNI_ABORT); // do not copy back, just release
|
|
||||||
|
|
||||||
log_info("%s RedefineClass Done!", LOG_PREFIX);
|
|
||||||
return JNI_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
JNI Func: JuiceLoaderNative.redifine
|
|
||||||
Desc: Redefine Class (classname, bytecodes, len)
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_redefineClass__Ljava_lang_String_2_3BI
|
|
||||||
(JNIEnv *env, jobject obj, jstring classname, jbyteArray bytecodes, jint len) {
|
|
||||||
log_info("%s RedefineClass Invoke!", LOG_PREFIX);
|
|
||||||
jvmtiError err;
|
|
||||||
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("%s JVM not init! (JVMTI = NULL)", LOG_PREFIX);
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- 获取 class 名称 ---
|
|
||||||
const char* cname = (*env)->GetStringUTFChars(env, classname, NULL);
|
|
||||||
log_debug("%s Finding class: %s", LOG_PREFIX, cname);
|
|
||||||
|
|
||||||
jclass clazz = (*env)->FindClass(env, cname);
|
|
||||||
(*env)->ReleaseStringUTFChars(env, classname, cname); // 正确释放
|
|
||||||
|
|
||||||
if (clazz == NULL) {
|
|
||||||
log_error("%s Class not found!", LOG_PREFIX);
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- 获取字节数组 ---
|
|
||||||
jbyte* buf = (*env)->GetByteArrayElements(env, bytecodes, NULL);
|
|
||||||
|
|
||||||
jvmtiClassDefinition defs[1];
|
|
||||||
defs[0].klass = clazz;
|
|
||||||
defs[0].class_byte_count = len;
|
|
||||||
defs[0].class_bytes = (const unsigned char*) buf;
|
|
||||||
|
|
||||||
log_info("%s Redefining class...", LOG_PREFIX);
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("JVMTI is NULL (Before RedefineClasses, After FindClass), %p", JuiceLoaderNative.jvmti);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
err = (*JuiceLoaderNative.jvmti)->RedefineClasses(JuiceLoaderNative.jvmti, 1, defs);
|
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "RedefineClasses");
|
|
||||||
|
|
||||||
(*env)->ReleaseByteArrayElements(env, bytecodes, buf, JNI_ABORT); // 用之前的 buf
|
|
||||||
|
|
||||||
log_info("%s Done!", LOG_PREFIX);
|
|
||||||
return JNI_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_getLoadedClasses
|
|
||||||
(JNIEnv *env, jobject obj) {
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
|
||||||
log_error("JVMTI is NULL (When GetLoadedClasses[invoked JNI Func]), %p", JuiceLoaderNative.jvmti);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
jvmtiPhase phase; (*JuiceLoaderNative.jvmti)->GetPhase(JuiceLoaderNative.jvmti, &phase); log_info("Phase=%d", phase);
|
|
||||||
jint count = 0;
|
|
||||||
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
|
||||||
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
|
||||||
|
|
||||||
jclass* classes = NULL;
|
|
||||||
|
|
||||||
jvmtiError err = (*JuiceLoaderNative.jvmti)->GetLoadedClasses(JuiceLoaderNative.jvmti, &count, &classes);
|
|
||||||
if (err != JVMTI_ERROR_NONE || count == 0) {
|
|
||||||
return (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "java/lang/Class"), NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
jclass classClass = (*env)->FindClass(env, "java/lang/Class");
|
|
||||||
jobjectArray result = (*env)->NewObjectArray(env, count, classClass, NULL);
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
(*env)->SetObjectArrayElement(env, result, i, classes[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
(*JuiceLoaderNative.jvmti)->Deallocate(JuiceLoaderNative.jvmti, (unsigned char*)classes);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#include "JuiceLoaderUtils.h"
|
|
||||||
#include "windows.h"
|
|
||||||
|
|
||||||
/* Helper: get method full name: ClassName.methodName(signature) */
|
|
||||||
char* get_method_fullname(jvmtiEnv *j, JNIEnv *env, jthread thread, jmethodID method) {
|
|
||||||
char *mname = NULL, *msig = NULL, *mgeneric = NULL;
|
|
||||||
jclass declaring = NULL;
|
|
||||||
char *cls_sig = NULL;
|
|
||||||
char *result = NULL;
|
|
||||||
|
|
||||||
jvmtiError err;
|
|
||||||
|
|
||||||
err = (*j)->GetMethodName(j, method, &mname, &msig, &mgeneric);
|
|
||||||
if (err != JVMTI_ERROR_NONE) goto cleanup;
|
|
||||||
|
|
||||||
err = (*j)->GetMethodDeclaringClass(j, method, &declaring);
|
|
||||||
if (err != JVMTI_ERROR_NONE) goto cleanup;
|
|
||||||
|
|
||||||
err = (*j)->GetClassSignature(j, declaring, &cls_sig, NULL);
|
|
||||||
if (err != JVMTI_ERROR_NONE) goto cleanup;
|
|
||||||
|
|
||||||
/* cls_sig is like "Lcom/example/MyClass;" -> make it human-friendly */
|
|
||||||
size_t len = strlen(cls_sig) + strlen(mname) + strlen(msig) + 4;
|
|
||||||
result = (char*)malloc(len);
|
|
||||||
if (result) {
|
|
||||||
/* remove leading L and trailing ; from class signature if present */
|
|
||||||
char *cls_nice = cls_sig;
|
|
||||||
if (cls_sig[0] == 'L') cls_nice = cls_sig + 1;
|
|
||||||
size_t cls_len = strlen(cls_nice);
|
|
||||||
if (cls_nice[cls_len - 1] == ';') cls_nice[cls_len - 1] = '\0';
|
|
||||||
snprintf(result, len, "%s.%s%s", cls_nice, mname, msig);
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (mname) (*j)->Deallocate(j, (unsigned char*)mname);
|
|
||||||
if (msig) (*j)->Deallocate(j, (unsigned char*)msig);
|
|
||||||
if (mgeneric) (*j)->Deallocate(j, (unsigned char*)mgeneric);
|
|
||||||
if (cls_sig) (*j)->Deallocate(j, (unsigned char*)cls_sig);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
# src/utils/CMakeLists.txt
|
|
||||||
|
|
||||||
add_library(utils STATIC
|
|
||||||
JVMUtils.c)
|
|
||||||
|
|
||||||
target_include_directories(utils PUBLIC
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
|
||||||
${CMAKE_SOURCE_DIR}/includes
|
|
||||||
${CMAKE_SOURCE_DIR}/includes/jvm
|
|
||||||
${CMAKE_SOURCE_DIR}/includes/jvm/win32)
|
|
||||||
|
|
||||||
target_link_libraries(utils PRIVATE
|
|
||||||
jvm)
|
|
||||||
Reference in new issue
Block a user