diff --git a/examples/Examples.md b/examples/Examples.md new file mode 100644 index 0000000..142ec8d --- /dev/null +++ b/examples/Examples.md @@ -0,0 +1,25 @@ +# Examples + +A collection of JuiceAgent examples demonstrating different ways to inject +JuiceAgent into a running JVM process. + +## 1. inject-via-java-api + +Inject JuiceAgent into a running JVM using the Java API (JNI), without needing `injector.exe`. + +- [HowToUse.md](./inject-via-java-api/HowToUse.md) +- [如何使用.md](./inject-via-java-api/如何使用.md) + +## 2. load-via-injector-exe + +Inject a JAR into a running JVM using `injector.exe`. + +- [HowToUse.md](./load-via-injector-exe/HowToUse.md) +- [如何使用.md](./load-via-injector-exe/如何使用.md) + +## 3. retransform-class + +Retransform an already-loaded class at runtime after injection, without restarting the JVM. + +- [HowToUse.md](./retransform-class/HowToUse.md) +- [如何使用.md](./retransform-class/如何使用.md) diff --git a/examples/Examples.zh.md b/examples/Examples.zh.md new file mode 100644 index 0000000..c6542e6 --- /dev/null +++ b/examples/Examples.zh.md @@ -0,0 +1,24 @@ +# Examples + +JuiceAgent 示例集合,演示将 JuiceAgent 注入到正在运行的 JVM 进程的不同方式。 + +## 1. inject-via-java-api + +使用 Java API(JNI)将 JuiceAgent 注入正在运行的 JVM,无需使用 `injector.exe`。 + +- [HowToUse.md](./inject-via-java-api/HowToUse.md) +- [如何使用.md](./inject-via-java-api/如何使用.md) + +## 2. load-via-injector-exe + +使用 `injector.exe` 将 JAR 注入正在运行的 JVM。 + +- [HowToUse.md](./load-via-injector-exe/HowToUse.md) +- [如何使用.md](./load-via-injector-exe/如何使用.md) + +## 3. retransform-class + +注入后在运行时热替换(retransform)已加载的类,无需重启 JVM。 + +- [HowToUse.md](./retransform-class/HowToUse.md) +- [如何使用.md](./retransform-class/如何使用.md) diff --git a/examples/inject-via-java-api/HowToUse.md b/examples/inject-via-java-api/HowToUse.md new file mode 100644 index 0000000..824eb16 --- /dev/null +++ b/examples/inject-via-java-api/HowToUse.md @@ -0,0 +1,97 @@ +# How to Use — inject-via-java-api (JuiceAgent) + +This example demonstrates injecting JuiceAgent into a running JVM process +using the Java API (JNI), without needing `injector.exe`. + +## Prerequisites + +- Java 8+ installed and available in `PATH` +- JuiceAgent native DLLs (`libinject.dll`, `libloader.dll`, `libagent.dll`) + placed in the same directory as this example +- A `config.toml` file configured for your use case + +## Quick Start + +### Step 1: Build + +Run `build.bat` to compile both the injector and the target jars: + +``` +> build.bat +``` + +### Step 2: Run the Target JVM + +Open a terminal and start the target JVM: + +``` +> run.bat +``` + +The target JVM will start and print its PID periodically. Keep it running. + +### Step 3: Run the Injector + +Open another terminal and run the injector: + +``` +> run_injector.bat +``` + +You will be prompted to enter: + +- `PID` : the target JVM process ID (shown in the target terminal) +- `DLL` : path to `libinject.dll` (e.g. `.\libinject.dll`) +- `config_path` : directory containing `config.toml` (leave empty for current dir) + +### Step 4: Verify + +If the injection is successful, the target JVM will load the JAR specified +in `config.toml` and execute its entry method. + +## Files + +| File | Description | +| --- | --- | +| `build.bat` | Build all jars (injector + target) | +| `run.bat` | Start the target JVM | +| `run_injector.bat` | Run the JavaInjector to inject into the target JVM | +| `config.toml` | JuiceAgent configuration file | +| `injector/` | JavaInjector source code and build script | +| `target/` | Target JVM source code and build script | + +## Configuration (config.toml) + +```toml +[JuiceAgent] +Version = 1 + +[JuiceAgent.Loader] +JuiceAgentNativeLibraryPath = "" + +[JuiceAgent.Modules] + +[JuiceAgent.Modules.JarLoader] +Enabled = true +InjectionDir = "./injection" +JarPath = "./demo.jar" +EntryClass = "Example.Main" +EntryMethod = "run" +``` + +- `JarPath`: the JAR to load into the target JVM +- `EntryClass`: the fully qualified class name with the entry method +- `EntryMethod`: must be a `public static void` method with no parameters +- `InjectionDir`: directory containing dependency JARs loaded before the main JAR + +## Troubleshooting + +- `Error: JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar not found`: + Run `build.bat` first to compile the injector. + +- `java.lang.UnsatisfiedLinkError`: + Ensure `libinject.dll` is in the current directory or provide the full path. + +- Injection has no effect: + Verify that `config.toml` is in the directory you specified as `config_path`, + and that the paths inside `config.toml` are correct relative to that directory. diff --git a/examples/inject-via-java-api/如何使用.md b/examples/inject-via-java-api/如何使用.md new file mode 100644 index 0000000..87c3f75 --- /dev/null +++ b/examples/inject-via-java-api/如何使用.md @@ -0,0 +1,96 @@ +# 如何使用 — inject-via-java-api (JuiceAgent) + +本示例演示如何使用 Java API(JNI)将 JuiceAgent 注入到正在运行的 JVM 进程中, +无需使用 `injector.exe`。 + +## 环境要求 + +- Java 8+ 已安装并在 `PATH` 中可用 +- JuiceAgent 原生 DLL(`libinject.dll`、`libloader.dll`、`libagent.dll`) + 已放置在本示例所在目录 +- 已根据实际需求配置好 `config.toml` + +## 快速开始 + +### 步骤 1:构建 + +运行 `build.bat` 编译 injector 和 target 两个 jar: + +``` +> build.bat +``` + +### 步骤 2:启动目标 JVM + +打开一个终端,启动目标 JVM: + +``` +> run.bat +``` + +目标 JVM 将启动并周期性打印其 PID。保持该窗口运行。 + +### 步骤 3:运行注入器 + +打开另一个终端,运行注入器: + +``` +> run_injector.bat +``` + +程序会提示你输入以下信息: + +- `PID` — 目标 JVM 进程 ID(在目标 JVM 终端中显示) +- `DLL` — `libinject.dll` 的路径(例如 `.\libinject.dll`) +- `config_path` — `config.toml` 所在目录(留空则使用当前目录) + +### 步骤 4:验证 + +注入成功后,目标 JVM 将加载 `config.toml` 中指定的 JAR 并执行其入口方法。 + +## 文件说明 + +| 文件 | 说明 | +| --- | --- | +| `build.bat` | 构建所有 jar(injector + target) | +| `run.bat` | 启动目标 JVM | +| `run_injector.bat` | 运行 JavaInjector,将 agent 注入目标 JVM | +| `config.toml` | JuiceAgent 配置文件 | +| `injector/` | JavaInjector 源码及构建脚本 | +| `target/` | 目标 JVM 源码及构建脚本 | + +## 配置说明(config.toml) + +```toml +[JuiceAgent] +Version = 1 + +[JuiceAgent.Loader] +JuiceAgentNativeLibraryPath = "" + +[JuiceAgent.Modules] + +[JuiceAgent.Modules.JarLoader] +Enabled = true +InjectionDir = "./injection" +JarPath = "./demo.jar" +EntryClass = "Example.Main" +EntryMethod = "run" +``` + +- `JarPath`:要加载到目标 JVM 的 JAR 文件 +- `EntryClass`:包含入口方法的完整类名 +- `EntryMethod`:必须是 `public static void` 且无参数的方法 +- `InjectionDir`:依赖 JAR 所在的目录,这些 JAR 会在主 JAR 之前加载 + +## 常见问题 + +- `Error: JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar not found`: + 请先运行 `build.bat` 编译 injector。 + +- `java.lang.UnsatisfiedLinkError`: + 请确保 `libinject.dll` 在当前目录下,或提供完整路径。 + +- 注入后无效果: + 请确认 `config.toml` 在你指定的 `config_path` 目录中,且 `config.toml` + 中的路径相对于该目录是正确的。 diff --git a/examples/load-via-injector-exe/HowToUse.md b/examples/load-via-injector-exe/HowToUse.md new file mode 100644 index 0000000..1d728ce --- /dev/null +++ b/examples/load-via-injector-exe/HowToUse.md @@ -0,0 +1,80 @@ +# HowToUse — load-via-injector-exe + +**Prerequisites:** Java 8+ (`javac`/`jar` for building, `jps` for injection) + +## Directory Files + +| File | Description | +| --- | --- | +| `injector.exe` `libagent.dll` `libinject.dll` `libloader.dll` | JuiceAgent runtime | +| `config.toml` | Injection config | +| `demo.jar` | JAR to inject (built by `build.bat`) | +| `target.jar` | Target JVM program (built by `build.bat`) | +| `injection/` | Injection source | +| `target/` | Target source | +| `build.bat` | Rebuild `demo.jar` / `target.jar` | +| `run.bat` | Run target JVM (`target.jar`) | + +## Usage Steps + +### 1. Build (optional) + +`demo.jar` and `target.jar` are already included by default. +If you modified `injection/Main.java` or `target/target.java`, run: + +``` +build.bat +``` + +to regenerate `demo.jar` and `target.jar` in this directory. + +### 2. Run the target JVM + +Double-click `run.bat` (or run: `java -jar target.jar`). A black window +appears and prints: + +``` +[Target] I am the target JVM (PID: ) +``` + +... (once per second) + +### 3. Inject + +Double-click `injector.exe`, type the target PID and press Enter: + +``` +6644 target.jar +10504 jdk.jcmd/sun.tools.jps.Jps +Input PID: 6644 +``` + +### 4. Verify + +Go back to the target window. Injection succeeded if you see: + +``` +[Demo] Injection succeeded! Hello from JuiceAgent. +[Demo] Current thread: Thread-0 +``` + +## Config / config.toml + +| Key | Description | +| --- | --- | +| `JarPath` | JAR path to inject (default: `./demo.jar`) | +| `EntryClass` | Entry class (default: `Example.Main`) | +| `EntryMethod` | Entry method, must be `public static void run()` | +| `InjectionDir` | Dependency JAR directory (only `*.jar` is loaded) | + +## Troubleshooting + +- `java/javac/jar is not recognized`: + Install a JDK and add its `bin` directory to `PATH`. +- `injector.exe` reports injection failure: + Make sure the target JVM is running and the PID is correct. +- No `[Demo]` output appears: + The target window prints many log lines which may affect reading. + Check the `[JuiceAgent JarLoader]` lines to locate the cause. + If it still fails, copy the full log and submit an Issue, or paste + it directly to an AI. diff --git a/examples/load-via-injector-exe/如何使用.md b/examples/load-via-injector-exe/如何使用.md new file mode 100644 index 0000000..cf3d6b1 --- /dev/null +++ b/examples/load-via-injector-exe/如何使用.md @@ -0,0 +1,78 @@ +# 使用说明 — load-via-injector-exe + +**前提条件:** 已安装 Java 8+(需要 `javac`/`jar` 构建,`jps` 用于注入) + +## 目录文件说明 + +| 文件 | 说明 | +| --- | --- | +| `injector.exe` `libagent.dll` `libinject.dll` `libloader.dll` | JuiceAgent 运行时 | +| `config.toml` | 注入配置 | +| `demo.jar` | 待注入的 JAR(`build.bat` 生成) | +| `target.jar` | 目标 JVM 程序(`build.bat` 生成) | +| `injection/` | 注入代码源码 | +| `target/` | 目标程序源码 | +| `build.bat` | 重新生成 `demo.jar` / `target.jar` | +| `run.bat` | 运行目标 JVM(`target.jar`) | + +## 使用步骤 + +### 1. 构建(可选) + +默认已包含 `demo.jar` 和 `target.jar`。 +若修改了 `injection/Main.java` 或 `target/target.java`,请运行: + +``` +build.bat +``` + +生成 `demo.jar` 和 `target.jar` 到本目录。 + +### 2. 运行目标 JVM + +双击 `run.bat`(或执行 `java -jar target.jar`),出现黑色窗口并输出: + +``` +[Target] I am the target JVM (PID: ) +``` + +...(每秒输出一次) + +### 3. 注入 + +双击 `injector.exe`,出现黑色窗口,输入目标 PID 并回车: + +``` +6644 target.jar +10504 jdk.jcmd/sun.tools.jps.Jps +Input PID: 6644 +``` + +### 4. 验证 + +回到目标窗口,看到以下输出即注入成功: + +``` +[Demo] Injection succeeded! Hello from JuiceAgent. +[Demo] Current thread: Thread-0 +``` + +## 配置说明 / config.toml + +| 配置项 | 说明 | +| --- | --- | +| `JarPath` | 待注入的 JAR 路径(默认 `./demo.jar`) | +| `EntryClass` | 入口类(默认 `Example.Main`) | +| `EntryMethod` | 入口方法,需为 `public static void run()` | +| `InjectionDir` | 依赖 JAR 目录(仅加载 `*.jar`) | + +## 常见问题 + +- `java/javac/jar 不是内部或外部命令`: + 安装 JDK 并将其 `bin` 目录加入 `PATH`。 +- `injector.exe` 提示注入失败: + 确认目标 JVM 正在运行,且输入的 PID 正确。 +- 没有出现 `[Demo]` 输出: + 目标窗口会有大量日志,可能影响阅读;请查看其中的 + `[JuiceAgent JarLoader]` 日志定位原因。 + 如仍无法解决,请复制完整日志提交 Issue,或直接丢给 AI。 diff --git a/examples/retransform-class/HowToUse.md b/examples/retransform-class/HowToUse.md new file mode 100644 index 0000000..07a65b1 --- /dev/null +++ b/examples/retransform-class/HowToUse.md @@ -0,0 +1,109 @@ +# HowToUse — retransform-class + +**Prerequisites:** Java 8+ (`javac`/`jar` for building, `jps` for injection) + +## Directory Files + +| File | Description | +| --- | --- | +| `injector.exe` `libagent.dll` `libinject.dll` `libloader.dll` | JuiceAgent runtime | +| `config.toml` | Injection config | +| `build.bat` | Build script | +| `run.bat` | Run target JVM | +| `playload.jar` | JAR to inject (built by `build.bat`) | +| `target.jar` | Target JVM program (built by `build.bat`) | +| `new_target.class` | Retransformed bytecode (built by `build.bat`) | +| `playload/` | Payload source | +| `target/` | Target source | +| `new_target/` | Modified copy of target source | + +## What This Example Does + +The target class `target` has a method `say()` that prints: + +``` +Hello! +``` + +`new_target/target.java` is a modified copy that also prints: + +``` +Retransformed! +``` + +This example injects `playload.jar` into a running JVM. The payload reads +`new_target.class` from its own JAR and calls +`JuiceAgent.retransformClassByName("target", ...)` to swap the already-loaded +`target` class on the fly. After that, `say()` starts printing the new message +without restarting the JVM. + +## Usage Steps + +### 1. Build (optional) + +`playload.jar` and `target.jar` are already included by default. +If you modified `playload/`, `target/` or `new_target/`, run: + +``` +build.bat +``` + +to regenerate `playload.jar`, `target.jar` and `new_target.class`. + +### 2. Run the target JVM + +Double-click `run.bat` (or run: `java -jar target.jar`). A black window +appears and prints: + +``` +[Target] I am the target JVM (PID: ) +Hello! +``` + +... (once per second) + +### 3. Inject + +Double-click `injector.exe`, type the target PID and press Enter: + +``` +6644 target.jar +10504 jdk.jcmd/sun.tools.jps.Jps +Input PID: 6644 +``` + +### 4. Verify + +Go back to the target window. Injection succeeded if you see: + +``` +Playload loaded! +Playload executed! +``` + +and the output of `say()` changes from `Hello!` to: + +``` +Hello! +Retransformed! +``` + +## Config / config.toml + +| Key | Description | +| --- | --- | +| `JarPath` | JAR path to inject (default: `./playload.jar`) | +| `EntryClass` | Entry class (default: `playload`) | +| `EntryMethod` | Entry method, must be `public static void entry()` | + +## Troubleshooting + +- `java/javac/jar is not recognized`: + Install a JDK and add its `bin` directory to `PATH`. +- `injector.exe` reports injection failure: + Make sure the target JVM is running and the PID is correct. +- No `Playload loaded!` output appears: + Check `libagent.log` and `libloader.log` in this directory for the cause. +- `say()` output does not change: + Make sure `build.bat` was run after editing `new_target/`, and that + `new_target.class` inside `playload.jar` is up to date. diff --git a/examples/retransform-class/如何使用.md b/examples/retransform-class/如何使用.md new file mode 100644 index 0000000..4ed9a70 --- /dev/null +++ b/examples/retransform-class/如何使用.md @@ -0,0 +1,104 @@ +# 如何使用 — retransform-class + +**前置条件:** Java 8+(构建需要 `javac`/`jar`,注入需要 `jps`) + +## 目录文件 + +| 文件 | 说明 | +| --- | --- | +| `injector.exe` `libagent.dll` `libinject.dll` `libloader.dll` | JuiceAgent 运行库 | +| `config.toml` | 注入配置 | +| `build.bat` | 构建脚本 | +| `run.bat` | 运行目标 JVM | +| `playload.jar` | 注入用的 JAR(由 `build.bat` 生成) | +| `target.jar` | 目标 JVM 程序(由 `build.bat` 生成) | +| `new_target.class` | 重变换后的字节码(由 `build.bat` 生成) | +| `playload/` | payload 源码 | +| `target/` | 目标类源码 | +| `new_target/` | target 的修改版源码 | + +## 本示例的作用 + +目标类 `target` 的 `say()` 方法原本打印: + +``` +Hello! +``` + +`new_target/target.java` 是它的修改版,额外打印: + +``` +Retransformed! +``` + +本示例把 `playload.jar` 注入到运行中的 JVM。payload 从自己的 JAR 中读取 +`new_target.class`,调用 `JuiceAgent.retransformClassByName("target", ...)` +在运行时动态替换已加载的 `target` 类。之后 `say()` 无需重启 JVM 就会输出新内容。 + +## 使用步骤 + +### 1. 构建(可选) + +`playload.jar` 和 `target.jar` 默认已包含。 +如果修改了 `playload/`、`target/` 或 `new_target/`,运行: + +``` +build.bat +``` + +重新生成 `playload.jar`、`target.jar` 和 `new_target.class`。 + +### 2. 运行目标 JVM + +双击 `run.bat`(或执行 `java -jar target.jar`)。黑窗口出现并每秒打印: + +``` +[Target] I am the target JVM (PID: ) +Hello! +``` + +### 3. 注入 + +双击 `injector.exe`,输入目标 PID 后回车: + +``` +6644 target.jar +10504 jdk.jcmd/sun.tools.jps.Jps +Input PID: 6644 +``` + +### 4. 验证 + +回到目标窗口,注入成功应看到: + +``` +Playload loaded! +Playload executed! +``` + +并且 `say()` 的输出从 `Hello!` 变为: + +``` +Hello! +Retransformed! +``` + +## 配置 / config.toml + +| 配置项 | 说明 | +| --- | --- | +| `JarPath` | 注入的 JAR 路径(默认 `./playload.jar`) | +| `EntryClass` | 入口类(默认 `playload`) | +| `EntryMethod` | 入口方法,必须为 `public static void entry()` | + +## 常见问题 + +- `java/javac/jar is not recognized`: + 安装 JDK 并将 `bin` 目录加入 `PATH`。 +- `injector.exe` 提示注入失败: + 确认目标 JVM 正在运行且 PID 正确。 +- 没有出现 `Playload loaded!` 输出: + 查看本目录下的 `libagent.log` 和 `libloader.log` 定位原因。 +- `say()` 输出没有变化: + 确认修改 `new_target/` 后重新运行过 `build.bat`,且 `playload.jar` 内的 + `new_target.class` 是最新的。