From 0e1280ef7f1ade2646d6a474b126dac38d241180 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Mon, 19 Oct 2026 13:19:50 +0800 Subject: [PATCH] Docs: document Linux injection support - readme: add the Linux platform overview, components and build requirements - readme: describe ptrace + remote dlopen and the $HOME/.juiceagent config default - readme-zh: mirror the same changes in Chinese --- README.md | 84 +++++++++++++++++++++++++++++++++++++++++++++++----- README_ZH.md | 62 +++++++++++++++++++++++++++++++++----- 2 files changed, 132 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 68dfabd..8e89bab 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ JuiceAgent is an advanced JVMTI-based injection framework for runtime JAR loading, bytecode transformation, and runtime JVM instrumentation. It injects into a running JVM process even when `DisableAttachMechanism=true`, without requiring `-javaagent` startup arguments or the attach mechanism. +Two platforms are supported: + +- **Windows x64** — reflective DLL injection (`injector.exe` + `libinject.dll`). +- **Linux x64** — `ptrace` attach plus a remote `dlopen` call that makes the target JVM load `libloader.so` itself. + **Warning**: This project is still experimental and is not recommended for production use. Use it at your own risk. > **Tips**: Some antivirus software may flag JuiceAgent as malware. See [Antivirus False Positive Notice](docs/Antivirus-False-Positive.md) for details. @@ -32,7 +37,7 @@ Welcome to join our QQ/Discord group for more information and support: ![C++](https://img.shields.io/badge/C++-20-blue) ![JNI](https://img.shields.io/badge/JNI-Native-green) ![CMake](https://img.shields.io/badge/CMake-Build-red) -![Platform](https://img.shields.io/badge/Platform-Windows-lightgrey) +![Platform](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux-lightgrey) ![JVMTI](https://img.shields.io/badge/JVMTI-Agent-success) ![Injection](https://img.shields.io/badge/Runtime-Injection-blueviolet) @@ -47,6 +52,7 @@ Welcome to join our QQ/Discord group for more information and support: - [Features](#features) - [Project Structure](#project-structure) - [Quick Start](#quick-start) + - [Linux Support](#linux-support) - [JuiceAgent API](#juiceagent-api) - [Examples](#examples) - [How To Build](#how-to-build) @@ -76,6 +82,14 @@ With JuiceAgent, developers can: The `JuiceAgent-API` jar is embedded in the native binaries, so no separate download is required. +On Linux the pipeline is the same, only the injection step changes. The injector +attaches to the target process with `ptrace`, saves its registers, and performs a +remote `dlopen` so the JVM loads `libloader.so` in-process (no `LD_PRELOAD`, no +`-javaagent`). `libloader.so` then starts from an ELF constructor, resolves its +runtime directory (`JuiceAgent_ConfigPath`, or `$HOME/.juiceagent` by default), +and drives the agent exactly like the Windows loader. See +[Linux Support](#linux-support). + ## Features - Load external JARs into the Bootstrap or System ClassLoader @@ -90,10 +104,10 @@ The `JuiceAgent-API` jar is embedded in the native binaries, so no separate down ## Project Structure -- `src/libloader` — `libloader.dll`. Injected into the target JVM. Attaches to the JVM, parses `config.toml`, injects the embedded `JuiceAgent-API`, and bootstraps the agent. -- `src/libagent` — `libagent.dll`. The JVMTI agent. Registers callbacks (e.g. `ClassFileLoadHook`) and implements the native side of `JuiceAgent-API`. -- `src/injector` — `injector.exe`. Command-line driver that loads `libinject.dll` and injects `libloader.dll` into the target process. -- `src/libinject` — `libinject.dll`. Reflective DLL injector. Exposes a native `inject` function and JNI bindings (`cn.xiaozhou233.juiceagent.injector.InjectorNative`). +- `src/libloader` — `libloader.dll` (Windows) / `libloader.so` (Linux). Injected into the target JVM. Attaches to the JVM, parses `config.toml`, injects the embedded `JuiceAgent-API`, and bootstraps the agent. +- `src/libagent` — `libagent.dll` (Windows) / `libagent.so` (Linux). The JVMTI agent. Registers callbacks (e.g. `ClassFileLoadHook`) and implements the native side of `JuiceAgent-API`. +- `src/injector` — `injector.exe` (Windows) / `injector` (Linux). Command-line driver that loads the loader into the target process. +- `src/libinject` — `libinject.dll` (Windows only). Reflective DLL injector. Exposes a native `inject` function and JNI bindings (`cn.xiaozhou233.juiceagent.injector.InjectorNative`). ## Quick Start @@ -181,6 +195,60 @@ See [Inject](docs/Inject.md) The target JVM will load the specified JAR and execute the specified entry class and method. +## Linux Support + +JuiceAgent also works on Linux x64. Instead of reflective DLL injection, the +Linux injector uses `ptrace` to attach to the target process and then performs a +**remote `dlopen`**: it saves the thread state, makes the target call +`dlopen("libloader.so", RTLD_NOW)`, restores the state and detaches. The JVM +therefore loads the loader itself — no `LD_PRELOAD` and no `-javaagent` — and the +rest of the pipeline is identical to Windows. + +### Components + +- `libloader.so` — loaded into the target JVM, starts from an ELF constructor. +- `libagent.so` — the JVMTI agent. This is the default + `JuiceAgentNativeLibraryPath` on Linux (instead of `libagent.dll`). +- `injector` — attaches to the target PID and performs the remote `dlopen`. + +### Configuration location + +On Linux the loader does **not** look in the current working directory. It +resolves the runtime directory in this order: + +1. `JuiceAgent_ConfigPath` — if the environment variable is set and non-empty, it + is used as-is. +2. `$HOME/.juiceagent` — the default; created automatically if it does not exist. + +`config.toml` is read from that directory. The schema is identical to Windows +and relative paths (`JarPath`, `InjectionDir`, ...) are resolved against it, so a +typical layout is: + +```text +~/.juiceagent/ +├── config.toml +├── MyCustomJar.jar +└── injection/ + └── Dependencies.jar +``` + +To point the loader at another directory: + +```bash +export JuiceAgent_ConfigPath=/opt/juiceagent +``` + +### Requirements + +- Run the injector as the same user as the target JVM (or as `root`), and make + sure `ptrace` is permitted. If injection fails with `EPERM`, check + `/proc/sys/kernel/yama/ptrace_scope`. +- The target JVM and the loader must both be x64. + +> **Note**: The Linux injector binary is not open source yet. The `injector` +> target in this repository is a placeholder that only prints a notice; the +> loader and the agent are fully open source and build from this tree. + ## JuiceAgent API The following native functions are exposed to Java through the `cn.xiaozhou233.juiceagent.api.JuiceAgent` class (see the [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) project for the Java API definition): @@ -210,7 +278,7 @@ The following native functions are exposed to Java through the `cn.xiaozhou233.j ### Requirements - Java 8+ -- MinGW +- MinGW-w64 (Windows) or GCC/Clang (Linux) - CMake 3.15+ - Ninja @@ -234,7 +302,9 @@ cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release cmake --build build ``` -The generated binaries will be placed in `build/bin`. +The generated binaries will be placed in `build/bin`. On Linux this produces +`libloader.so`, `libagent.so` and `injector`; on Windows the `.dll` / `.exe` +equivalents. ## Full Disclaimer diff --git a/README_ZH.md b/README_ZH.md index a8128c2..706faba 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -6,6 +6,11 @@ JuiceAgent 是一个基于 JVMTI 的高级注入框架,用于运行时 JAR 加载、字节码变换和运行时 JVM 插桩。它可以在 `DisableAttachMechanism=true` 的情况下注入到正在运行的 JVM 进程中,无需 `-javaagent` 启动参数,也无需使用 attach 机制。 +目前支持两个平台: + +- **Windows x64**——反射式 DLL 注入(`injector.exe` + `libinject.dll`)。 +- **Linux x64**——通过 `ptrace` 附加目标进程,再用一次远程 `dlopen` 让目标 JVM 自己加载 `libloader.so`。 + **警告**:本项目仍处于实验阶段,不建议用于生产环境。使用风险自负。 > **提示**:部分杀毒软件可能将 JuiceAgent 误报为恶意软件。详情见[杀毒软件误报说明](docs/Antivirus-False-Positive.md)。 @@ -29,7 +34,7 @@ JuiceAgent 是一个基于 JVMTI 的高级注入框架,用于运行时 JAR 加 ![C++](https://img.shields.io/badge/C++-20-blue) ![JNI](https://img.shields.io/badge/JNI-Native-green) ![CMake](https://img.shields.io/badge/CMake-Build-red) -![Platform](https://img.shields.io/badge/Platform-Windows-lightgrey) +![Platform](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux-lightgrey) ![JVMTI](https://img.shields.io/badge/JVMTI-Agent-success) ![Injection](https://img.shields.io/badge/Runtime-Injection-blueviolet) @@ -44,6 +49,7 @@ JuiceAgent 是一个基于 JVMTI 的高级注入框架,用于运行时 JAR 加 - [特性](#特性) - [项目结构](#项目结构) - [快速开始](#快速开始) + - [Linux 支持](#linux-支持) - [JuiceAgent API](#juiceagent-api) - [示例](#示例) - [如何构建](#如何构建) @@ -73,6 +79,8 @@ JuiceAgent 是 [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) `JuiceAgent-API` jar 内嵌于原生二进制中,无需单独下载。 +Linux 上的流程与此相同,只有注入这一步不同。注入器通过 `ptrace` 附加到目标进程,保存线程状态,发起一次远程 `dlopen` 让 JVM 在自身进程内加载 `libloader.so`(无需 `LD_PRELOAD`,也无需 `-javaagent`)。随后 `libloader.so` 从 ELF constructor 启动,解析运行时目录(`JuiceAgent_ConfigPath`,默认 `$HOME/.juiceagent`),之后与 Windows 加载器完全一致地驱动 agent。详见 [Linux 支持](#linux-支持)。 + ## 特性 - 将外部 JAR 加载到 Bootstrap 或 System ClassLoader @@ -87,10 +95,10 @@ JuiceAgent 是 [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) ## 项目结构 -- `src/libloader` — `libloader.dll`。注入到目标 JVM,附加 JVM、解析 `config.toml`、注入内嵌的 `JuiceAgent-API` 并引导 agent。 -- `src/libagent` — `libagent.dll`。JVMTI agent。注册回调(例如 `ClassFileLoadHook`)并实现 `JuiceAgent-API` 的原生侧。 -- `src/injector` — `injector.exe`。命令行驱动,加载 `libinject.dll` 并将 `libloader.dll` 注入到目标进程。 -- `src/libinject` — `libinject.dll`。反射式 DLL 注入器,导出原生 `inject` 函数并提供 JNI 绑定(`cn.xiaozhou233.juiceagent.injector.InjectorNative`)。 +- `src/libloader` — `libloader.dll`(Windows)/ `libloader.so`(Linux)。注入到目标 JVM,附加 JVM、解析 `config.toml`、注入内嵌的 `JuiceAgent-API` 并引导 agent。 +- `src/libagent` — `libagent.dll`(Windows)/ `libagent.so`(Linux)。JVMTI agent。注册回调(例如 `ClassFileLoadHook`)并实现 `JuiceAgent-API` 的原生侧。 +- `src/injector` — `injector.exe`(Windows)/ `injector`(Linux)。命令行驱动,将加载器注入到目标进程。 +- `src/libinject` — `libinject.dll`(仅 Windows)。反射式 DLL 注入器,导出原生 `inject` 函数并提供 JNI 绑定(`cn.xiaozhou233.juiceagent.injector.InjectorNative`)。 ## 快速开始 @@ -178,6 +186,46 @@ Input PID: 目标 JVM 将加载指定的 JAR 并执行指定的入口类和入口方法。 +## Linux 支持 + +JuiceAgent 同样支持 Linux x64。与反射式 DLL 注入不同,Linux 注入器使用 `ptrace` 附加目标进程,然后执行一次**远程 `dlopen`**:保存线程状态,让目标进程调用 `dlopen("libloader.so", RTLD_NOW)`,恢复状态并 detach。这样由 JVM 自己加载加载器——无需 `LD_PRELOAD`,也无需 `-javaagent`——其后的流程与 Windows 完全一致。 + +### 组件 + +- `libloader.so` —— 加载进目标 JVM,从 ELF constructor 启动。 +- `libagent.so` —— JVMTI agent。在 Linux 上它是 `JuiceAgentNativeLibraryPath` 的默认值(而不是 `libagent.dll`)。 +- `injector` —— 附加到目标 PID 并执行远程 `dlopen`。 + +### 配置位置 + +Linux 上加载器**不会**在当前工作目录中查找配置,而是按以下顺序确定运行时目录: + +1. `JuiceAgent_ConfigPath` —— 环境变量存在且非空时直接使用。 +2. `$HOME/.juiceagent` —— 默认值,目录不存在时会自动创建。 + +`config.toml` 从该目录读取,配置结构(schema)与 Windows 完全一致,相对路径(`JarPath`、`InjectionDir` 等)也以该目录为基准解析。典型目录结构: + +```text +~/.juiceagent/ +├── config.toml +├── MyCustomJar.jar +└── injection/ + └── Dependencies.jar +``` + +如需指定其他目录: + +```bash +export JuiceAgent_ConfigPath=/opt/juiceagent +``` + +### 环境要求 + +- 以与目标 JVM 相同的用户(或 `root`)运行注入器,并确保允许 `ptrace`。如果注入失败并返回 `EPERM`,请检查 `/proc/sys/kernel/yama/ptrace_scope`。 +- 目标 JVM 与加载器都必须是 x64。 + +> **注意**:Linux 注入器二进制目前尚未开源。本仓库中的 `injector` 目标只是一个占位程序,仅打印提示信息;加载器与 agent 完全开源,可直接从本仓库构建。 + ## JuiceAgent API 以下原生函数通过 `cn.xiaozhou233.juiceagent.api.JuiceAgent` 类暴露给 Java(Java API 定义见 [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) 项目): @@ -207,7 +255,7 @@ Input PID: ### 环境要求 - Java 8+ -- MinGW +- MinGW-w64(Windows)或 GCC/Clang(Linux) - CMake 3.15+ - Ninja @@ -231,7 +279,7 @@ cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release cmake --build build ``` -生成的二进制文件将放在 `build/bin` 中。 +生成的二进制文件将放在 `build/bin` 中。Linux 下会生成 `libloader.so`、`libagent.so` 和 `injector`;Windows 下为对应的 `.dll` / `.exe`。 ## 完整免责声明