diff --git a/examples/inject-via-java-api/HowToUse.txt b/examples/inject-via-java-api/HowToUse.txt new file mode 100644 index 0000000..9a8246e --- /dev/null +++ b/examples/inject-via-java-api/HowToUse.txt @@ -0,0 +1,80 @@ +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 +====== + + 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) +============================ + + [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. \ No newline at end of file diff --git a/examples/inject-via-java-api/build.bat b/examples/inject-via-java-api/build.bat new file mode 100644 index 0000000..d2a88ea --- /dev/null +++ b/examples/inject-via-java-api/build.bat @@ -0,0 +1,23 @@ +@echo off +echo === Building injector jar === +pushd injector +call build.bat +popd +if errorlevel 1 goto :error + +echo. +echo === Building target jar === +pushd target +call build.bat +popd +if errorlevel 1 goto :error + +echo. +echo Build finished: JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar and target.jar are ready. +goto :end + +:error +echo Build failed. +exit /b 1 + +:end \ No newline at end of file diff --git a/examples/inject-via-java-api/config.toml b/examples/inject-via-java-api/config.toml new file mode 100644 index 0000000..1abf9cd --- /dev/null +++ b/examples/inject-via-java-api/config.toml @@ -0,0 +1,14 @@ +[JuiceAgent] +Version = 1 + +[JuiceAgent.Loader] +JuiceAgentNativeLibraryPath = "" + +[JuiceAgent.Modules] + +[JuiceAgent.Modules.JarLoader] +Enabled = true +InjectionDir = "./injection" +JarPath = "./demo.jar" +EntryClass = "Example.Main" +EntryMethod = "run" \ No newline at end of file diff --git a/examples/inject-via-java-api/injector/JavaInjector.java b/examples/inject-via-java-api/injector/JavaInjector.java new file mode 100644 index 0000000..405e2f7 --- /dev/null +++ b/examples/inject-via-java-api/injector/JavaInjector.java @@ -0,0 +1,35 @@ +import cn.xiaozhou233.juiceagent.injector.Injector; +import cn.xiaozhou233.juiceagent.injector.WindowInfo; + +import java.util.Scanner; +import java.io.File; + +public class JavaInjector { + public static void main(String[] args) { + // Load the native library + System.load(new File("./libinject.dll").getAbsolutePath()); + + // Find windows by title + for (WindowInfo window : Injector.findWindowsByTitle("cmd")) { + System.out.println(window.toString()); + } + System.out.println("----------------"); + + Scanner scanner = new Scanner(System.in); + + // Input + System.out.println("PID: "); + String pid = scanner.nextLine(); + System.out.println("Dll: "); + String dll = scanner.nextLine(); + System.out.println("config_path: "); + String config_path = scanner.nextLine(); + + if (config_path == null || config_path.isEmpty()) { + // config_path is empty + Injector.inject(Integer.parseInt(pid), dll); + } else { + Injector.inject(Integer.parseInt(pid), dll, config_path); + } + } +} \ No newline at end of file diff --git a/examples/inject-via-java-api/injector/JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar b/examples/inject-via-java-api/injector/JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..6af1dde Binary files /dev/null and b/examples/inject-via-java-api/injector/JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar differ diff --git a/examples/inject-via-java-api/injector/build.bat b/examples/inject-via-java-api/injector/build.bat new file mode 100644 index 0000000..34c7b5d --- /dev/null +++ b/examples/inject-via-java-api/injector/build.bat @@ -0,0 +1,51 @@ +@echo off +setlocal + +set "OUT=out" +set "JAR=JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar" +set "TMP=tmp_jar" + +if not exist "%JAR%" ( + echo Error: %JAR% not found. Please restore the original jar. + exit /b 1 +) + +if exist "%OUT%" rmdir /s /q "%OUT%" +if exist "%TMP%" rmdir /s /q "%TMP%" + +mkdir "%OUT%" +mkdir "%TMP%" + +javac -encoding UTF-8 --release 8 -cp "%JAR%" -d "%OUT%" JavaInjector.java +if errorlevel 1 goto :error + +rem Extract existing jar (contains Injector/WindowInfo classes) into temp dir +rem -J-XX:-UsePerfData prevents JVM from creating locked hsperfdata files in CWD +pushd "%TMP%" +jar -J-XX:-UsePerfData xf "..\%JAR%" +popd +if errorlevel 1 goto :error + +rem Remove any hsperfdata folder that may have leaked into the jar +for /d %%D in ("%TMP%\hsperfdata_*") do rmdir /s /q "%%D" + +rem Merge compiled JavaInjector.class into temp dir +copy /y "%OUT%\JavaInjector.class" "%TMP%\JavaInjector.class" >nul +if errorlevel 1 goto :error + +rem Repackage with Main-Class +jar -J-XX:-UsePerfData --create --file "%JAR%" --main-class JavaInjector -C "%TMP%" . +if errorlevel 1 goto :error + +rmdir /s /q "%OUT%" +rmdir /s /q "%TMP%" + +echo Build succeeded: %JAR% +goto :end + +:error +echo Build failed. +exit /b 1 + +:end +endlocal diff --git a/examples/inject-via-java-api/run.bat b/examples/inject-via-java-api/run.bat new file mode 100644 index 0000000..029da6c --- /dev/null +++ b/examples/inject-via-java-api/run.bat @@ -0,0 +1,4 @@ +@echo off +cd /d "%~dp0" +java -jar target.jar +pause \ No newline at end of file diff --git a/examples/inject-via-java-api/run_injector.bat b/examples/inject-via-java-api/run_injector.bat new file mode 100644 index 0000000..d005ce6 --- /dev/null +++ b/examples/inject-via-java-api/run_injector.bat @@ -0,0 +1,17 @@ +@echo off +cd /d "%~dp0" + +set "INJECTOR_JAR=injector\JuiceAgent-JavaInjector-1.0-SNAPSHOT.jar" + +echo === Java API Injector === +echo. +echo Make sure the target JVM is running (run.bat). +echo. +echo Usage: This script runs the JavaInjector which will prompt for: +echo - PID: target JVM process ID +echo - DLL: path to libloader.dll +echo - config_path: directory containing config.toml (leave empty to use current dir) +echo. + +java -cp "%INJECTOR_JAR%" JavaInjector +pause \ No newline at end of file diff --git a/examples/inject-via-java-api/target.jar b/examples/inject-via-java-api/target.jar new file mode 100644 index 0000000..81bf9cc Binary files /dev/null and b/examples/inject-via-java-api/target.jar differ diff --git a/examples/inject-via-java-api/target/build.bat b/examples/inject-via-java-api/target/build.bat new file mode 100644 index 0000000..7774c90 --- /dev/null +++ b/examples/inject-via-java-api/target/build.bat @@ -0,0 +1,28 @@ +@echo off +setlocal + +set "SRC=target.java" +set "OUT=out" +set "JAR=..\target.jar" + +if exist "%OUT%" rmdir /s /q "%OUT%" +if exist "%JAR%" del /q "%JAR%" + +mkdir "%OUT%" +javac -encoding UTF-8 --release 8 -d "%OUT%" "%SRC%" +if errorlevel 1 goto :error + +jar --create --file "%JAR%" --main-class target -C "%OUT%" . +if errorlevel 1 goto :error + +rmdir /s /q "%OUT%" + +echo Build succeeded: target.jar +goto :end + +:error +echo Build failed. +exit /b 1 + +:end +endlocal diff --git a/examples/inject-via-java-api/target/target.java b/examples/inject-via-java-api/target/target.java new file mode 100644 index 0000000..1c60ba6 --- /dev/null +++ b/examples/inject-via-java-api/target/target.java @@ -0,0 +1,15 @@ +import java.lang.management.ManagementFactory; + +public class target { + 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 + ")"); + Thread.sleep(1000); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/examples/inject-via-java-api/如何使用.txt b/examples/inject-via-java-api/如何使用.txt new file mode 100644 index 0000000..e1b3066 --- /dev/null +++ b/examples/inject-via-java-api/如何使用.txt @@ -0,0 +1,79 @@ +如何使用 -- 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) +======================= + + [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 + 中的路径相对于该目录是正确的。 \ No newline at end of file diff --git a/includes/JuiceAgent/cn_xiaozhou233_juiceagent_injector_Injector.h b/includes/JuiceAgent/cn_xiaozhou233_juiceagent_injector_Injector.h new file mode 100644 index 0000000..48f2c58 --- /dev/null +++ b/includes/JuiceAgent/cn_xiaozhou233_juiceagent_injector_Injector.h @@ -0,0 +1,37 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class cn_xiaozhou233_juiceagent_injector_Injector */ + +#ifndef _Included_cn_xiaozhou233_juiceagent_injector_Injector +#define _Included_cn_xiaozhou233_juiceagent_injector_Injector +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: cn_xiaozhou233_juiceagent_injector_Injector + * Method: inject + * Signature: (ILjava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_inject__ILjava_lang_String_2 + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: cn_xiaozhou233_juiceagent_injector_Injector + * Method: inject + * Signature: (ILjava/lang/String;Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_inject__ILjava_lang_String_2Ljava_lang_String_2 + (JNIEnv *, jclass, jint, jstring, jstring); + +/* + * Class: cn_xiaozhou233_juiceagent_injector_Injector + * Method: findWindowsByTitle + * Signature: (Ljava/lang/String;)[Lcn/xiaozhou233/juiceagent/injector/WindowInfo; + */ +JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_findWindowsByTitle + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/libinject/libinject.c b/src/libinject/libinject.c index a14df9f..cf45d44 100644 --- a/src/libinject/libinject.c +++ b/src/libinject/libinject.c @@ -5,6 +5,8 @@ #include #include +#include + #define BREAK_WITH_ERROR(e) { printf("[-] %s. Error=%lu\n", e, GetLastError()); break; } __declspec(dllexport) @@ -131,9 +133,8 @@ static jboolean jni_inject(JNIEnv* env, jint pid, jstring path, const char* conf /* * JNI Function: inject(int pid, String path) */ -JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2 - (JNIEnv *env, jobject obj, jint pid, jstring path) { - (void)obj; +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_inject__ILjava_lang_String_2 + (JNIEnv *env, jclass, jint pid, jstring path) { return jni_inject(env, pid, path, NULL); } @@ -141,9 +142,8 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ * JNI Function: inject(int pid, String path, String configDir) * configDir: path of config file (toml) */ -JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2Ljava_lang_String_2 - (JNIEnv *env, jobject obj, jint pid, jstring path, jstring configDir) { - (void)obj; +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_inject__ILjava_lang_String_2Ljava_lang_String_2 + (JNIEnv *env, jclass, jint pid, jstring path, jstring configDir) { const char* config_dir = (*env)->GetStringUTFChars(env, configDir, NULL); jboolean result = jni_inject(env, pid, path, config_dir); (*env)->ReleaseStringUTFChars(env, configDir, config_dir); @@ -182,7 +182,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { return TRUE; } -JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle +JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_Injector_findWindowsByTitle (JNIEnv *env, jclass clazz, jstring keyword) { (void)clazz; const jchar *input = (*env)->GetStringChars(env, keyword, NULL); @@ -193,7 +193,7 @@ JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorN g_count = 0; EnumWindows(EnumWindowsProc, 0); - jclass infoClass = (*env)->FindClass(env, "cn/xiaozhou233/juiceagent/injector/InjectorNative$WindowInfo"); + jclass infoClass = (*env)->FindClass(env, "cn/xiaozhou233/juiceagent/injector/WindowInfo"); if (!infoClass) return NULL; jmethodID ctor = (*env)->GetMethodID(env, infoClass, "", "(Ljava/lang/String;I)V");