Compare commits

...
6 Commits
Author SHA1 Message Date
xiaozhou233 f317ea3e8d [Update] Update Version to V1.1 Build 27 2025-10-26 14:53:35 +08:00
xiaozhou233 8b2ab3e588 [Update] Full Support Agent_OnLoad Inject Method
[Add] Agent_OnLoad Inject Method
[Todo] Remove Duplicated Code (vm_init/DllMain) and vars
2025-10-26 14:53:02 +08:00
xiaozhou233 c7907fa82a [Update] Version to 1.1 b25 2025-10-26 02:20:01 +08:00
xiaozhou233 5b92db6f5a [Update] README
[Add] Disclaimer
[Todo] Update Tutorial
2025-10-26 02:19:11 +08:00
xiaozhou233 d74322503c [Update] vm_init
[Todo] Remove Duplicated Code
[Todo] Impl vm_init and load agent
2025-10-26 02:02:03 +08:00
xiaozhou233 cd8657ad4f [Update] Agent_OnLoad Inject Method
[Add] Agent_OnLoad
[Add] JVM Options Inject Method Detection
[Todo] Remove Duplicated Code
2025-10-26 01:47:36 +08:00
3 changed files with 151 additions and 5 deletions

No files matched your search

+2 -2
View File
@@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# version
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_BUILD_NUMBER 24)
set(PROJECT_VERSION_MINOR 1)
set(PROJECT_BUILD_NUMBER 27)
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ")
+12
View File
@@ -5,6 +5,10 @@
JuiceAgent — a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when DisableAttachMechanism is enabled.
**NOTICE:** Currently only supports **Windows**, and plans to support **Linux** in the future.
**WARNING:** This project is still in the experimental stage and is not recommended for production use.
**Disclaimer: This project is for learning and research in controlled environments only. The author is not responsible for any illegal or unauthorized use. Users must ensure their actions are legal. (See Full Disclaimer below)**
***免责声明: 本项目仅供受控环境下的学习和研究使用。作者不对任何非法或未授权的使用承担责任。使用者应确保自己的行为合法。(在下方查看完整免责声明)***
## Summary
JuiceAgent is a JVMTI-native injection framework that injects native code into a target JVM to dynamically load external JARs and perform bytecode redefinition/transformations without requiring a javaagent (works when DisableAttachMechanism=true).
## Features
@@ -28,6 +32,8 @@ JuiceAgent is a JVMTI-native injection framework that injects native code into a
- Get Loaded Classes
- JNI Function (Can be invoke in java)
## How To Use
***TODO: Update How to Use***
***Notice: Tutorial for Version 1.0***
### 1. Download Files
- Download `libagent`, `libinjector`, and `libjuiceloader` from the Release to the same directory.
@@ -100,6 +106,12 @@ cmake --build build --
dlls: build/bin/
jars: see [JuiceLoader](https://github.com/xiaozhou233/JuiceLoader)
## Full Disclaimer/ 完整免责声明
Disclaimer: This project is intended for learning and research in controlled environments only. The author is not responsible for any consequences arising from unauthorized use, modification, cracking, or violation of any applicable agreements or laws. Users are responsible for ensuring their actions are legal and compliant.
免责声明: 本项目仅建议在受控环境中用于学习与研究。任何利用本项目实施破解、篡改、未经授权获取源码或其他违反目标协议或法律的行为所导致的一切后果,作者概不负责。使用者应自行确保其行为合法合规,并对相应后果承担全部责任。
## Acknowledgements
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL Inject
+137 -3
View File
@@ -22,6 +22,13 @@ extern HINSTANCE hAppInstance;
InjectionInfoType InjectionInfo;
static void initLogger() {
log_set_level(LOG_TRACE);
log_is_log_time(false);
log_set_name("libagent");
log_info("Logger initialized.");
}
DWORD WINAPI ThreadProc(LPVOID lpParam) {
log_trace("New thread started.");
@@ -147,6 +154,11 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
return 0;
}
/// =========================================================== ///
/// Function: DllMain
/// Description: DLL Injection Entry Point
/// Inject Method: A) ReflectiveDLLInjection B) LoadLibraryA+CreateRemoteThread
/// =========================================================== ///
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
BOOL bReturnValue = TRUE;
switch (dwReason){
@@ -159,14 +171,34 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
hAppInstance = hinstDLL;
// Initialize log
log_set_level(LOG_TRACE);
log_is_log_time(false);
log_set_name("libagent");
initLogger();
log_info("DLL_PROCESS_ATTACH");
log_info("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
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 ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_6) == 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
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
if (localParm == NULL) {
@@ -224,3 +256,105 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
}
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 *agentpath_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("JNIEnv: %p", env);
log_info("jvmtiEnv: %p", jvmti);
log_info("Version: %d", (*env)->GetVersion(env));
// TODO: Remove Duplicated Code
// Check if agentpath_options is null
if (agentpath_options == NULL) {
// Use DLL path as default
log_info("agentpath_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);
agentpath_options = DllDir;
}
}
log_trace("agentpath_options: %s", agentpath_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, agentpath_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) {
agentpath_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
/// =========================================================== ///