mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3dc83d83db | ||
|
|
9bb9faacdf | ||
|
|
7cdd3e4b11 |
No files matched your search
+1
-1
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|||||||
# version
|
# version
|
||||||
set(PROJECT_VERSION_MAJOR 1)
|
set(PROJECT_VERSION_MAJOR 1)
|
||||||
set(PROJECT_VERSION_MINOR 3)
|
set(PROJECT_VERSION_MINOR 3)
|
||||||
set(PROJECT_BUILD_NUMBER 35)
|
set(PROJECT_BUILD_NUMBER 36)
|
||||||
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} ======== ")
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,21 @@ typedef struct _JuiceLoaderNative {
|
|||||||
} JuiceLoaderNativeType;
|
} JuiceLoaderNativeType;
|
||||||
extern JuiceLoaderNativeType JuiceLoaderNative;
|
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);
|
jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti);
|
||||||
|
|
||||||
// JNI Functions
|
// JNI Functions
|
||||||
@@ -26,4 +41,8 @@ JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loade
|
|||||||
JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz);
|
JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz);
|
||||||
// public static native byte[] getClassBytesByName(String className);
|
// public static native byte[] getClassBytesByName(String className);
|
||||||
JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass loader_class, jstring 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);
|
||||||
#endif
|
#endif
|
||||||
+112
-4
@@ -10,6 +10,7 @@ Desc: JuiceLoader Native, Provide jni function for JuiceLoader
|
|||||||
#include "javautils.h"
|
#include "javautils.h"
|
||||||
|
|
||||||
JuiceLoaderNativeType JuiceLoaderNative;
|
JuiceLoaderNativeType JuiceLoaderNative;
|
||||||
|
RetransformClassCacheType RetransformClassCache = {NULL, 0, 0};
|
||||||
|
|
||||||
static const char* g_bytecodes_classname = NULL;
|
static const char* g_bytecodes_classname = NULL;
|
||||||
static unsigned char* g_bytecodes = NULL;
|
static unsigned char* g_bytecodes = NULL;
|
||||||
@@ -34,8 +35,7 @@ void JNICALL ClassFileLoadHook(
|
|||||||
jint* new_class_data_len,
|
jint* new_class_data_len,
|
||||||
unsigned char** new_classbytes) {
|
unsigned char** new_classbytes) {
|
||||||
|
|
||||||
//log_trace("CallBack: ClassFileLoadHook [%s]", name);
|
// ---- get class bytecode ---- //
|
||||||
|
|
||||||
if (g_bytecodes_classname != NULL && g_bytecodes == NULL) {
|
if (g_bytecodes_classname != NULL && g_bytecodes == NULL) {
|
||||||
if (strcmp(name, g_bytecodes_classname) == 0) {
|
if (strcmp(name, g_bytecodes_classname) == 0) {
|
||||||
g_bytecodes_len = class_data_len;
|
g_bytecodes_len = class_data_len;
|
||||||
@@ -44,11 +44,49 @@ void JNICALL ClassFileLoadHook(
|
|||||||
|
|
||||||
*new_classbytes = NULL;
|
*new_classbytes = NULL;
|
||||||
*new_class_data_len = 0;
|
*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) {
|
jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
||||||
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||||
|
|
||||||
@@ -86,7 +124,11 @@ jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
|||||||
// public static native byte[] getClassBytes(Class<?> clazz);
|
// public static native byte[] getClassBytes(Class<?> clazz);
|
||||||
// public static native byte[] getClassBytesByName(String className);
|
// public static native byte[] getClassBytesByName(String className);
|
||||||
{"getClassBytes", "(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes},
|
{"getClassBytes", "(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes},
|
||||||
{ "getClassBytesByName", "(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName}
|
{ "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}
|
||||||
};
|
};
|
||||||
jint result = (*env)->RegisterNatives(env, clazz, methods, sizeof(methods) / sizeof(methods[0]));
|
jint result = (*env)->RegisterNatives(env, clazz, methods, sizeof(methods) / sizeof(methods[0]));
|
||||||
if (result != JNI_OK) {
|
if (result != JNI_OK) {
|
||||||
@@ -94,6 +136,11 @@ jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
|||||||
return JNI_ERR;
|
return JNI_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init RetransformClassCache
|
||||||
|
RetransformClassCache.arr = NULL;
|
||||||
|
RetransformClassCache.size = 0;
|
||||||
|
RetransformClassCache.capacity = 0;
|
||||||
|
|
||||||
// Application ability
|
// Application ability
|
||||||
jvmtiCapabilities caps;
|
jvmtiCapabilities caps;
|
||||||
memset(&caps, 0, sizeof(caps));
|
memset(&caps, 0, sizeof(caps));
|
||||||
@@ -342,4 +389,65 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass load
|
|||||||
}
|
}
|
||||||
|
|
||||||
return loader_getClassBytes(env, loader_class, clazz);
|
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);
|
||||||
|
}
|
||||||
@@ -8,7 +8,9 @@ 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;
|
||||||
@@ -86,4 +88,4 @@ bool ReadInjectionInfo(toml_result_t result, InjectParameters* params) {
|
|||||||
}
|
}
|
||||||
// ======= Entry =======
|
// ======= Entry =======
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user