Feat: add retransform-class example with build/run scripts and docs

This commit is contained in:
xiaozhou233 committed 2026-08-15 13:41:26 +08:00
1 parent 57405e810c
commit b08656daa6
14 files changed
+340 -4

No files matched your search

+4 -4
View File
@@ -3,7 +3,7 @@
**/*.class
# JuiceAgent runtime binaries copied into the example (not committed)
load-via-injector-exe/injector.exe
load-via-injector-exe/libagent.dll
load-via-injector-exe/libinject.dll
load-via-injector-exe/libloader.dll
**/injector.exe
**/libagent.dll
**/libinject.dll
**/libloader.dll
+7
View File
@@ -0,0 +1,7 @@
# Build output directory (intermediate)
out/
# Do not ignore root build artifacts:
!new_target.class
!playload.jar
!target.jar
+86
View File
@@ -0,0 +1,86 @@
============================================
HowToUse
============================================
Prerequisites: Java 8+ (javac/jar for building, jps for injection)
Directory Files:
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: <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
============================================
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.
+58
View File
@@ -0,0 +1,58 @@
@echo off
setlocal
set "API_JAR=playload\JuiceAgent-API-1.4.1+build.1.jar"
set "PAYLOAD_JAR=playload.jar"
set "TARGET_JAR=target.jar"
set "OUT=out"
set "OUT_NT=%OUT%\new_target"
set "OUT_PL=%OUT%\playload"
set "OUT_TG=%OUT%\target"
set "NEW_TARGET_CLASS=new_target.class"
if exist "%OUT%" rmdir /s /q "%OUT%"
if exist "%PAYLOAD_JAR%" del /q "%PAYLOAD_JAR%"
if exist "%TARGET_JAR%" del /q "%TARGET_JAR%"
if exist "%NEW_TARGET_CLASS%" del /q "%NEW_TARGET_CLASS%"
mkdir "%OUT_NT%"
mkdir "%OUT_PL%"
mkdir "%OUT_TG%"
echo === Compiling new_target ===
javac -encoding UTF-8 --release 8 -d "%OUT_NT%" new_target\target.java
if errorlevel 1 goto :error
ren "%OUT_NT%\target.class" new_target.class
if errorlevel 1 goto :error
copy /y "%OUT_NT%\new_target.class" "%NEW_TARGET_CLASS%"
if errorlevel 1 goto :error
echo === Compiling playload ===
javac -encoding UTF-8 --release 8 -cp "%API_JAR%" -d "%OUT_PL%" playload\playload.java
if errorlevel 1 goto :error
echo === Compiling target ===
javac -encoding UTF-8 --release 8 -d "%OUT_TG%" target\target.java
if errorlevel 1 goto :error
echo === Packaging payload jar ===
jar --create --file "%PAYLOAD_JAR%" -C "%OUT_PL%" playload.class
if errorlevel 1 goto :error
echo === Packaging target jar ===
jar --create --file "%TARGET_JAR%" --main-class target -C "%OUT_TG%" .
if errorlevel 1 goto :error
rmdir /s /q "%OUT%"
echo Build succeeded: %PAYLOAD_JAR%, %TARGET_JAR%, %NEW_TARGET_CLASS%
goto :end
:error
echo Build failed.
exit /b 1
:end
endlocal
+21
View File
@@ -0,0 +1,21 @@
[JuiceAgent]
Version = 1
[JuiceAgent.Loader]
# Path to JuiceAgent-API jar, leave empty to use the built-in API
JuiceAgentAPIJarPath = ""
# Path to libagent.dll, default is "./libagent.dll"
JuiceAgentNativeLibraryPath = ""
[JuiceAgent.Modules]
[JuiceAgent.Modules.JarLoader]
Enabled = true
# Directory containing dependency JARs to load into the JVM (put your deps here)
InjectionDir = "./injection"
# Path to the injection JAR to be loaded
JarPath = "./playload.jar"
# Entry class to be executed after loading the JAR
EntryClass = "playload"
# Entry method, must be: public static void run()
EntryMethod = "entry"
Binary file not shown.
@@ -0,0 +1,23 @@
import java.lang.management.ManagementFactory;
public class target {
public static void say() {
System.out.println("Hello!");
//// RETRANSFORM HERE
System.out.println("Retransformed!");
//// RETRANSFORM HERE
}
public static void main(String[] args) {
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
try {
while (true) {
System.out.println("[Target] I am the target JVM (PID: " + pid + ")");
say();
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Binary file not shown.
@@ -0,0 +1,25 @@
import cn.xiaozhou233.juiceagent.api.JuiceAgent;
public class playload {
public static void entry() throws java.io.IOException {
System.out.println("Playload loaded!");
java.io.InputStream in = new java.io.FileInputStream("new_target.class");
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
byte[] bytes = out.toByteArray();
in.close();
JuiceAgent.retransformClassByName(
"target",
bytes,
bytes.length
);
System.out.println("Playload executed!");
}
}
+14
View File
@@ -0,0 +1,14 @@
@echo off
setlocal
java -jar target.jar
if errorlevel 1 goto :error
goto :end
:error
echo Run failed.
exit /b 1
:end
endlocal
Binary file not shown.
@@ -0,0 +1,20 @@
import java.lang.management.ManagementFactory;
public class target {
public static void say() {
System.out.println("Hello!");
}
public static void main(String[] args) {
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
try {
while (true) {
System.out.println("[Target] I am the target JVM (PID: " + pid + ")");
say();
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,82 @@
============================================
如何使用
============================================
前置条件: 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: <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 是最新的。