mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96fee27476 | ||
|
|
09eca1b802 | ||
|
|
0bb717b935 | ||
|
|
88e8835f8a | ||
|
|
f317ea3e8d | ||
|
|
8b2ab3e588 |
No files matched your search
+1
-1
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
# version
|
||||
set(PROJECT_VERSION_MAJOR 1)
|
||||
set(PROJECT_VERSION_MINOR 1)
|
||||
set(PROJECT_BUILD_NUMBER 25)
|
||||
set(PROJECT_BUILD_NUMBER 29)
|
||||
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
|
||||
message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ")
|
||||
|
||||
|
||||
+64
-6
@@ -147,7 +147,8 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
||||
log_info("invoked. ");
|
||||
log_info("done. cleaning up...");
|
||||
|
||||
(*jvm)->DetachCurrentThread(jvm);
|
||||
// :(
|
||||
// (*jvm)->DetachCurrentThread(jvm);
|
||||
|
||||
free(param);
|
||||
log_info("exit.");
|
||||
@@ -192,7 +193,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
||||
// 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 ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_6) == JNI_EDETACHED) {
|
||||
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");
|
||||
@@ -264,18 +265,62 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
||||
/// Function: vm_init
|
||||
/// Description: Called when the JVM is fully initialized. This is a callback function.
|
||||
/// =========================================================== ///
|
||||
static char *agentpath_options = NULL;
|
||||
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", agentpath_options);
|
||||
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: Impl vm_init and load agent
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// =========================================================== ///
|
||||
@@ -294,7 +339,7 @@ JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
|
||||
|
||||
// save options
|
||||
if (options != NULL) {
|
||||
agentpath_options = strdup(options);
|
||||
agent_options = strdup(options);
|
||||
}
|
||||
|
||||
jvmtiEnv *jvmti;
|
||||
@@ -310,4 +355,17 @@ JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
struct _JuiceLoaderNative {
|
||||
JavaVM *jvm;
|
||||
jvmtiEnv *jvmti;
|
||||
JNIEnv *env;
|
||||
} JuiceLoaderNative;
|
||||
|
||||
|
||||
|
||||
@@ -32,38 +32,37 @@ int setup(JNIEnv *env) {
|
||||
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 (JNI_GetCreatedJavaVMs(&JuiceLoaderNative.jvm, 1, NULL) != JNI_OK || !JuiceLoaderNative.jvm) {
|
||||
log_error("%s JNI_GetCreatedJavaVMs failed", 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);
|
||||
}
|
||||
// // 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 = (*JuiceLoaderNative.env)->GetVersion(JuiceLoaderNative.env);
|
||||
jint version = (*env)->GetVersion(env);
|
||||
log_debug("%s JVM version: 0x%x", LOG_PREFIX, version);
|
||||
|
||||
jint res = (*JuiceLoaderNative.jvm)->GetEnv(JuiceLoaderNative.jvm, (void**)&JuiceLoaderNative.jvmti, JVMTI_VERSION_1_2);
|
||||
if (res != JNI_OK || JuiceLoaderNative.jvmti == NULL) {
|
||||
log_error("%s GetEnv failed", LOG_PREFIX);
|
||||
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;
|
||||
} else {
|
||||
log_info("%s GetEnv success", LOG_PREFIX);
|
||||
}}
|
||||
log_info("%s Enabled JVMTI.");
|
||||
|
||||
}
|
||||
log_info("%s Enabled JVMTI at %p", LOG_PREFIX, JuiceLoaderNative.jvmti);
|
||||
jvmtiError err;
|
||||
// jvmtiEventCallbacks callbacks;
|
||||
|
||||
@@ -75,6 +74,10 @@ int setup(JNIEnv *env) {
|
||||
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");
|
||||
|
||||
@@ -96,6 +99,8 @@ int setup(JNIEnv *env) {
|
||||
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;
|
||||
}
|
||||
@@ -111,7 +116,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_inj
|
||||
log_info("%s JNI Func InjectJar Invoke!", LOG_PREFIX);
|
||||
jvmtiError err;
|
||||
if(JuiceLoaderNative.jvmti == NULL) {
|
||||
log_error("%s JVM not init!", LOG_PREFIX);
|
||||
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
@@ -133,7 +138,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
||||
jvmtiError err;
|
||||
|
||||
if (JuiceLoaderNative.jvmti == NULL) {
|
||||
log_error("%s JVM not init!", LOG_PREFIX);
|
||||
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
@@ -166,7 +171,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
||||
jvmtiError err;
|
||||
|
||||
if (JuiceLoaderNative.jvmti == NULL) {
|
||||
log_error("%s JVM not init!", LOG_PREFIX);
|
||||
log_error("%s JVM not init! (JVMTI = NULL)", LOG_PREFIX);
|
||||
return JNI_FALSE;
|
||||
}
|
||||
|
||||
@@ -191,6 +196,10 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
||||
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");
|
||||
|
||||
@@ -202,7 +211,15 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
||||
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user