From 87a1701f22fea5637f6a0c8af184d8a7eaf01bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=91=A8XiaoZhou?= Date: Thu, 6 Nov 2025 01:03:49 +0800 Subject: [PATCH] [Update] Remove other load method --- src/agent/libagent.c | 113 ------------------------------------------- 1 file changed, 113 deletions(-) diff --git a/src/agent/libagent.c b/src/agent/libagent.c index 5fdd50b..9aceb0a 100644 --- a/src/agent/libagent.c +++ b/src/agent/libagent.c @@ -239,117 +239,4 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){ break; } 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:.dll= -/// Example: java -agentpath:./libagent.dll=./config -jar myapp.jar -/// =========================================================== /// - -// WARN: This inject method maybe not work on some JVMs. -// 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; } \ No newline at end of file