4.6 KiB
JuiceAgent
JuiceAgent is a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when DisableAttachMechanism is enabled.
Notice: Currently, only Windows is supported. Linux support may be added in the future.
Notice: The current repository version is Version 3.3 Build 1.
Warning: This project is still experimental and is not recommended for production use. Use it at your own risk.
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 or retransformation without requiring a javaagent. It can still work when DisableAttachMechanism=true.
Features
This project is the native implementation of JuiceAgent-API. For the API definition, see JuiceAgent.java.
addToBootstrapClassLoaderSearchaddToSystemClassLoaderSearchaddToClassLoaderdefineClassredefineClass(ByName)retransformClass(ByName)getLoadedClassesgetClassByNamegetClassBytes(ByName)
How To Use
1. Download Files
Download the following files from Releases and place them in YourDir:
libagent.dlllibinject.dlllibloader.dllinjector.exe
Download JuiceAgent-API-X.X.X+build.X.jar from JuiceAgent-API Releases and place it in YourDir.
2. Write the Config File
Create a file named config.toml in YourDir.
[JuiceAgent]
Version = 1
[JuiceAgent.Loader]
JuiceAgentAPIJarPath = ""
JuiceAgentNativeLibraryPath = ""
[JuiceAgent.Modules]
[JuiceAgent.Modules.JarLoader]
Enabled = true
InjectionDir = ""
JarPath = ""
EntryClass = ""
EntryMethod = ""
3. Run the Injector
Method A: Use injector.exe
Run injector.exe from YourDir.
<jps output>
Input PID:
Enter the PID of the target JVM process and press Enter.
Method B: Use JNI to call inject
Create cn/xiaozhou233/juiceagent/injector/InjectorNative.java in your project:
package cn.xiaozhou233.juiceagent.injector;
public class InjectorNative {
/*
* @param pid target process id
* @param path injection DLL path
*/
public native boolean inject(int pid, String path);
/*
* @param pid target process id
* @param path injection DLL path
* @param configDir directory containing config.toml
*/
public native boolean inject(int pid, String path, String configDir);
}
Example:
import cn.xiaozhou233.juiceagent.injector.InjectorNative;
System.load("<path-to-libinject>");
InjectorNative injectorNative = new InjectorNative();
injectorNative.inject(<pid>, "<path-to-libloader>", "<path-to-config-directory>");
4. Done
If the injection succeeds, the target process will load Entry.jar (or the file specified by EntryJarPath) and invoke EntryClass.EntryMethod (default: com.example.Entry.start).
How To Build
Requirements
- Java 8+
- MinGW
- CMake 3.15+
- Ninja
Build with CMake Presets
git clone https://github.com/xiaozhou233/JuiceAgent.git
cd JuiceAgent
cmake --preset ninja-release
cmake --build build
Manual Build
git clone https://github.com/xiaozhou233/JuiceAgent.git
cd JuiceAgent
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
The generated binaries will be placed in build/bin.
JuiceAgent-API.jar can be built from or downloaded from JuiceAgent-API.
Full 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 that their actions are legal and compliant.
Acknowledgements
- ReflectiveDLLInjection - DLL injection implementation
- plog - Portable, simple, and extensible C++ logging library
- tinytoml - Header-only C++11 TOML parser
- eventpp - C++ library for event dispatchers and callback lists