mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96fee27476 | ||
|
|
09eca1b802 | ||
|
|
0bb717b935 | ||
|
|
88e8835f8a | ||
|
|
f317ea3e8d | ||
|
|
8b2ab3e588 | ||
|
|
c7907fa82a | ||
|
|
5b92db6f5a | ||
|
|
d74322503c | ||
|
|
cd8657ad4f | ||
|
|
6f104145b4 | ||
|
|
5eff534dbd | ||
|
|
0dd7e4610b | ||
|
|
e5d824671d | ||
|
|
13f4e02e3b | ||
|
|
ab168c2f06 | ||
|
|
f7c25e91cf | ||
|
|
6ae4299b01 |
No files matched your search
+13
-3
@@ -4,6 +4,19 @@ project(JuiceAgent LANGUAGES C CXX)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
# version
|
||||||
|
set(PROJECT_VERSION_MAJOR 1)
|
||||||
|
set(PROJECT_VERSION_MINOR 1)
|
||||||
|
set(PROJECT_BUILD_NUMBER 29)
|
||||||
|
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
|
||||||
|
message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ")
|
||||||
|
|
||||||
|
add_compile_definitions(
|
||||||
|
PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
|
||||||
|
PROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
|
||||||
|
PROJECT_BUILD_NUMBER=${PROJECT_BUILD_NUMBER}
|
||||||
|
PROJECT_VERSION_STRING=\"${PROJECT_VERSION_STRING}\")
|
||||||
|
|
||||||
# output directories
|
# output directories
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
@@ -19,9 +32,6 @@ add_subdirectory(${INCLUDES}/tomlc17)
|
|||||||
add_subdirectory(src/utils)
|
add_subdirectory(src/utils)
|
||||||
|
|
||||||
include(cmake/JuiceModule.cmake)
|
include(cmake/JuiceModule.cmake)
|
||||||
include(cmake/BuildVersion.cmake)
|
|
||||||
|
|
||||||
build_version()
|
|
||||||
|
|
||||||
# modules
|
# modules
|
||||||
add_juice_module(agent)
|
add_juice_module(agent)
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@
|
|||||||
{
|
{
|
||||||
"name": "ninja-debug",
|
"name": "ninja-debug",
|
||||||
"generator": "Ninja",
|
"generator": "Ninja",
|
||||||
"binaryDir": "${sourceDir}/debug",
|
"binaryDir": "${sourceDir}/build",
|
||||||
"cacheVariables": {
|
"cacheVariables": {
|
||||||
"CMAKE_BUILD_TYPE": "Debug"
|
"CMAKE_BUILD_TYPE": "Debug"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
JuiceAgent — a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when DisableAttachMechanism is enabled.
|
JuiceAgent — a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when DisableAttachMechanism is enabled.
|
||||||
|
|
||||||
**NOTICE:** Currently only supports **Windows**, and plans to support **Linux** in the future.
|
**NOTICE:** Currently only supports **Windows**, and plans to support **Linux** in the future.
|
||||||
|
|
||||||
|
**WARNING:** This project is still in the experimental stage and is not recommended for production use.
|
||||||
|
**Disclaimer: This project is for learning and research in controlled environments only. The author is not responsible for any illegal or unauthorized use. Users must ensure their actions are legal. (See Full Disclaimer below)**
|
||||||
|
***免责声明: 本项目仅供受控环境下的学习和研究使用。作者不对任何非法或未授权的使用承担责任。使用者应确保自己的行为合法。(在下方查看完整免责声明)***
|
||||||
## Summary
|
## 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/transformations without requiring a javaagent (works when DisableAttachMechanism=true).
|
JuiceAgent is a JVMTI-native injection framework that injects native code into a target JVM to dynamically load external JARs and perform bytecode redefinition/transformations without requiring a javaagent (works when DisableAttachMechanism=true).
|
||||||
## Features
|
## Features
|
||||||
@@ -28,6 +32,8 @@ JuiceAgent is a JVMTI-native injection framework that injects native code into a
|
|||||||
- Get Loaded Classes
|
- Get Loaded Classes
|
||||||
- JNI Function (Can be invoke in java)
|
- JNI Function (Can be invoke in java)
|
||||||
## How To Use
|
## How To Use
|
||||||
|
***TODO: Update How to Use***
|
||||||
|
***Notice: Tutorial for Version 1.0***
|
||||||
|
|
||||||
### 1. Download Files
|
### 1. Download Files
|
||||||
- Download `libagent`, `libinjector`, and `libjuiceloader` from the Release to the same directory.
|
- Download `libagent`, `libinjector`, and `libjuiceloader` from the Release to the same directory.
|
||||||
@@ -100,6 +106,12 @@ cmake --build build --
|
|||||||
dlls: build/bin/
|
dlls: build/bin/
|
||||||
|
|
||||||
jars: see [JuiceLoader](https://github.com/xiaozhou233/JuiceLoader)
|
jars: see [JuiceLoader](https://github.com/xiaozhou233/JuiceLoader)
|
||||||
|
|
||||||
|
## Full Disclaimer/ 完整免责声明
|
||||||
|
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 their actions are legal and compliant.
|
||||||
|
|
||||||
|
免责声明: 本项目仅建议在受控环境中用于学习与研究。任何利用本项目实施破解、篡改、未经授权获取源码或其他违反目标协议或法律的行为所导致的一切后果,作者概不负责。使用者应自行确保其行为合法合规,并对相应后果承担全部责任。
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
|
|
||||||
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL Inject
|
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL Inject
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
# BuildVersion.cmake
|
|
||||||
# function to get and set the build version
|
|
||||||
|
|
||||||
function(build_version)
|
|
||||||
set(VERSION_FILE "${CMAKE_SOURCE_DIR}/version.txt")
|
|
||||||
|
|
||||||
file(READ "${VERSION_FILE}" VERSION_CONTENT)
|
|
||||||
|
|
||||||
# VERSION_MAJOR
|
|
||||||
string(REGEX MATCH "VERSION_MAJOR=([0-9]+)" _ ${VERSION_CONTENT})
|
|
||||||
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
|
|
||||||
# VERSION_MINOR
|
|
||||||
string(REGEX MATCH "VERSION_MINOR=([0-9]+)" _ ${VERSION_CONTENT})
|
|
||||||
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1})
|
|
||||||
# BUILD_NUMBER
|
|
||||||
string(REGEX MATCH "BUILD_NUMBER=([0-9]+)" _ ${VERSION_CONTENT})
|
|
||||||
set(PROJECT_BUILD_NUMBER ${CMAKE_MATCH_1})
|
|
||||||
if(RELEASE_BUILD)
|
|
||||||
math(EXPR PROJECT_BUILD_NUMBER "${PROJECT_BUILD_NUMBER} + 1")
|
|
||||||
file(WRITE "${VERSION_FILE}"
|
|
||||||
"VERSION_MAJOR=${PROJECT_VERSION_MAJOR}\n"
|
|
||||||
"VERSION_MINOR=${PROJECT_VERSION_MINOR}\n"
|
|
||||||
"BUILD_NUMBER=${PROJECT_BUILD_NUMBER}\n"
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}")
|
|
||||||
|
|
||||||
message(STATUS "Build version: ${PROJECT_VERSION_STRING}")
|
|
||||||
|
|
||||||
add_compile_definitions(
|
|
||||||
PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
|
|
||||||
PROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
|
|
||||||
PROJECT_BUILD_NUMBER=${PROJECT_BUILD_NUMBER}
|
|
||||||
PROJECT_VERSION_STRING=\"${PROJECT_VERSION_STRING}\")
|
|
||||||
endfunction()
|
|
||||||
@@ -5,3 +5,5 @@ add_library(log.c STATIC
|
|||||||
target_include_directories(log.c PUBLIC
|
target_include_directories(log.c PUBLIC
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(log.c PRIVATE LOG_USE_COLOR)
|
||||||
+14
-5
@@ -27,6 +27,7 @@
|
|||||||
* - log level switch
|
* - log level switch
|
||||||
* - log filename switch
|
* - log filename switch
|
||||||
* - log line number switch
|
* - log line number switch
|
||||||
|
* - log name prefix
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@@ -50,11 +51,14 @@ static struct {
|
|||||||
bool is_log_level;
|
bool is_log_level;
|
||||||
bool is_log_filename;
|
bool is_log_filename;
|
||||||
bool is_log_line;
|
bool is_log_line;
|
||||||
|
|
||||||
|
const char *logname;
|
||||||
} L = {
|
} L = {
|
||||||
.is_log_time = true,
|
.is_log_time = true,
|
||||||
.is_log_level = true,
|
.is_log_level = true,
|
||||||
.is_log_filename = true,
|
.is_log_filename = false,
|
||||||
.is_log_line = true
|
.is_log_line = false,
|
||||||
|
.logname = NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -112,6 +116,8 @@ static void stdout_callback(log_Event *ev) {
|
|||||||
fprintf(ev->udata, ": ");
|
fprintf(ev->udata, ": ");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (L.logname) fprintf(ev->udata, "[%s] ", L.logname);
|
||||||
|
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
fflush(ev->udata);
|
fflush(ev->udata);
|
||||||
@@ -121,9 +127,9 @@ static void stdout_callback(log_Event *ev) {
|
|||||||
static void file_callback(log_Event *ev) {
|
static void file_callback(log_Event *ev) {
|
||||||
char buf[64];
|
char buf[64];
|
||||||
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
||||||
fprintf(
|
fprintf(ev->udata, "%s %-5s ", buf, level_strings[ev->level]);
|
||||||
ev->udata, "%s %-5s %s:%d: ",
|
fprintf(ev->udata, "%s:%d: ", ev->file, ev->line);
|
||||||
buf, level_strings[ev->level], ev->file, ev->line);
|
if (L.logname) fprintf(ev->udata, "[%s] ", L.logname);
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
fflush(ev->udata);
|
fflush(ev->udata);
|
||||||
@@ -230,3 +236,6 @@ void log_is_log_filename(bool enable) {
|
|||||||
void log_is_log_line(bool enable) {
|
void log_is_log_line(bool enable) {
|
||||||
L.is_log_line = enable;
|
L.is_log_line = enable;
|
||||||
}
|
}
|
||||||
|
void log_set_name(const char *name) {
|
||||||
|
L.logname = name;
|
||||||
|
}
|
||||||
@@ -54,6 +54,7 @@ void log_is_log_time(bool enable);
|
|||||||
void log_is_log_level(bool enable);
|
void log_is_log_level(bool enable);
|
||||||
void log_is_log_filename(bool enable);
|
void log_is_log_filename(bool enable);
|
||||||
void log_is_log_line(bool enable);
|
void log_is_log_line(bool enable);
|
||||||
|
void log_set_name(const char *name);
|
||||||
|
|
||||||
void log_log(int level, const char *file, int line, const char *fmt, ...);
|
void log_log(int level, const char *file, int line, const char *fmt, ...);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# src/core/CMakeLists.txt
|
# src/agent/CMakeLists.txt
|
||||||
|
|
||||||
target_include_directories(agent PRIVATE ${CMAKE_SOURCE_DIR}/includes/tomlc17)
|
target_include_directories(agent PRIVATE ${CMAKE_SOURCE_DIR}/includes/tomlc17)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef DATAUTILS_H
|
||||||
|
#define DATAUTILS_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "InjectParameters.h"
|
||||||
|
#include "tomlc17.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
typedef struct _InjectionInfo {
|
||||||
|
char BootstrapAPIPath[INJECT_PATH_MAX];
|
||||||
|
char JuiceLoaderJarPath[INJECT_PATH_MAX];
|
||||||
|
char JuiceLoaderLibPath[INJECT_PATH_MAX];
|
||||||
|
char EntryJarPath[INJECT_PATH_MAX];
|
||||||
|
char EntryClass[INJECT_PATH_MAX];
|
||||||
|
char EntryMethod[INJECT_PATH_MAX];
|
||||||
|
} InjectionInfoType;
|
||||||
|
extern InjectionInfoType InjectionInfo;
|
||||||
|
|
||||||
|
void safe_copy(char *dest, const char *src, size_t dest_size);
|
||||||
|
bool ReadInjectionInfo(toml_result_t result, InjectParameters* params);
|
||||||
|
|
||||||
|
#endif // DATAUTILS_H
|
||||||
+194
-145
@@ -1,3 +1,9 @@
|
|||||||
|
/*
|
||||||
|
Author: xiaozhou233 (xiaozhou233@vip.qq.com)
|
||||||
|
File: libagent.c
|
||||||
|
Desc: Entry point for the agent
|
||||||
|
*/
|
||||||
|
|
||||||
#include "ReflectiveLoader.h"
|
#include "ReflectiveLoader.h"
|
||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
#include "unistd.h"
|
#include "unistd.h"
|
||||||
@@ -9,127 +15,33 @@
|
|||||||
#include "GlobalUtils.h"
|
#include "GlobalUtils.h"
|
||||||
#include "shlwapi.h"
|
#include "shlwapi.h"
|
||||||
#include "tomlc17.h"
|
#include "tomlc17.h"
|
||||||
|
#include "datautils.h"
|
||||||
|
|
||||||
#define WIN_X64
|
#define WIN_X64
|
||||||
extern HINSTANCE hAppInstance;
|
extern HINSTANCE hAppInstance;
|
||||||
|
|
||||||
#define LOG_PREFIX "[JuiceAgent]"
|
InjectionInfoType InjectionInfo;
|
||||||
|
|
||||||
struct _InjectionInfo {
|
static void initLogger() {
|
||||||
char BootstrapAPIPath[INJECT_PATH_MAX];
|
log_set_level(LOG_TRACE);
|
||||||
char JuiceLoaderJarPath[INJECT_PATH_MAX];
|
log_is_log_time(false);
|
||||||
char JuiceLoaderLibPath[INJECT_PATH_MAX];
|
log_set_name("libagent");
|
||||||
char EntryJarPath[INJECT_PATH_MAX];
|
log_info("Logger initialized.");
|
||||||
char EntryClass[INJECT_PATH_MAX];
|
|
||||||
char EntryMethod[INJECT_PATH_MAX];
|
|
||||||
} InjectionInfo;
|
|
||||||
|
|
||||||
void safe_copy(char *dest, const char *src, size_t dest_size) {
|
|
||||||
if (!dest || dest_size == 0) return;
|
|
||||||
strncpy(dest, src, dest_size - 1);
|
|
||||||
dest[dest_size - 1] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ReadInjectionInfo(toml_result_t result, InjectParameters* params) {
|
|
||||||
toml_datum_t InjectionTree = toml_seek(result.toptab, "Injection");
|
|
||||||
|
|
||||||
// ======= BootstrapAPIPath =======
|
|
||||||
toml_datum_t BootstrapAPIPath = toml_seek(InjectionTree, "BootstrapAPIPath");
|
|
||||||
char BootstrapAPIPathStr[INJECT_PATH_MAX];
|
|
||||||
if (BootstrapAPIPath.type != TOML_STRING) {
|
|
||||||
log_error("%s BootstrapAPIPath is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (BootstrapAPIPath.u.s == NULL || strlen(BootstrapAPIPath.u.s) == 0) {
|
|
||||||
sprintf(BootstrapAPIPathStr, "%s\\bootstrap-api.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(BootstrapAPIPathStr, "%s", BootstrapAPIPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.BootstrapAPIPath, BootstrapAPIPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= BootstrapAPIPath =======
|
|
||||||
|
|
||||||
// ======= JuiceLoaderJarPath =======
|
|
||||||
toml_datum_t JuiceLoaderJarPath = toml_seek(InjectionTree, "JuiceLoaderJarPath");
|
|
||||||
char JuiceLoaderJarPathStr[INJECT_PATH_MAX];
|
|
||||||
if (JuiceLoaderJarPath.type != TOML_STRING) {
|
|
||||||
log_error("%s JuiceLoaderJarPath is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JuiceLoaderJarPath.u.s == NULL || strlen(JuiceLoaderJarPath.u.s) == 0) {
|
|
||||||
sprintf(JuiceLoaderJarPathStr, "%s\\JuiceLoader.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(JuiceLoaderJarPathStr, "%s", JuiceLoaderJarPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.JuiceLoaderJarPath, JuiceLoaderJarPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= JuiceLoaderJarPath =======
|
|
||||||
|
|
||||||
// ======= JuiceLoaderLibPath =======
|
|
||||||
toml_datum_t JuiceLoaderLibPath = toml_seek(InjectionTree, "JuiceLoaderLibPath");
|
|
||||||
char JuiceLoaderLibPathStr[INJECT_PATH_MAX];
|
|
||||||
if (JuiceLoaderLibPath.type != TOML_STRING) {
|
|
||||||
log_error("%s JuiceLoaderLibPath is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (JuiceLoaderLibPath.u.s == NULL || strlen(JuiceLoaderLibPath.u.s) == 0) {
|
|
||||||
sprintf(JuiceLoaderLibPathStr, "%s\\libjuiceloader.dll", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(JuiceLoaderLibPathStr, "%s", JuiceLoaderLibPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.JuiceLoaderLibPath, JuiceLoaderLibPathStr, INJECT_PATH_MAX);
|
|
||||||
// ======= JuiceLoaderLibPath =======
|
|
||||||
|
|
||||||
// ======= Entry =======
|
|
||||||
toml_datum_t EntryJarPath = toml_seek(InjectionTree, "EntryJarPath");
|
|
||||||
char EntryJarPathStr[INJECT_PATH_MAX];
|
|
||||||
if (EntryJarPath.type != TOML_STRING) {
|
|
||||||
log_error("%s EntryJarPath is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (EntryJarPath.u.s == NULL || strlen(EntryJarPath.u.s) == 0) {
|
|
||||||
sprintf(EntryJarPathStr, "%s\\entry.jar", params->ConfigDir);
|
|
||||||
} else {
|
|
||||||
sprintf(EntryJarPathStr, "%s", EntryJarPath.u.s);
|
|
||||||
}
|
|
||||||
safe_copy(InjectionInfo.EntryJarPath, EntryJarPathStr, INJECT_PATH_MAX);
|
|
||||||
|
|
||||||
toml_datum_t EntryClass = toml_seek(InjectionTree, "EntryClass");
|
|
||||||
if (EntryClass.type != TOML_STRING) {
|
|
||||||
log_error("%s EntryClass is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (EntryClass.u.s == NULL || strlen(EntryClass.u.s) == 0) {
|
|
||||||
safe_copy(InjectionInfo.EntryClass, "cn.xiaozhou233.juiceloader.entry.Entry", INJECT_PATH_MAX);
|
|
||||||
} else {
|
|
||||||
safe_copy(InjectionInfo.EntryClass, EntryClass.u.s, INJECT_PATH_MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
toml_datum_t EntryMethod = toml_seek(InjectionTree, "EntryMethod");
|
|
||||||
if (EntryMethod.type != TOML_STRING) {
|
|
||||||
log_error("%s EntryMethod is not a string.", LOG_PREFIX);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (EntryMethod.u.s == NULL || strlen(EntryMethod.u.s) == 0) {
|
|
||||||
safe_copy(InjectionInfo.EntryMethod, "start", INJECT_PATH_MAX);
|
|
||||||
} else {
|
|
||||||
safe_copy(InjectionInfo.EntryMethod, EntryMethod.u.s, INJECT_PATH_MAX);
|
|
||||||
}
|
|
||||||
// ======= Entry =======
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
||||||
log_trace("%s New thread started.", LOG_PREFIX);
|
log_trace("New thread started.");
|
||||||
|
|
||||||
memset(&InjectionInfo, 0, sizeof(InjectionInfo));
|
memset(&InjectionInfo, 0, sizeof(InjectionInfo));
|
||||||
|
|
||||||
/// ======== Check Environment ======== ///
|
/// ======== Check Environment ======== ///
|
||||||
InjectParameters *param = (InjectParameters*)lpParam;
|
InjectParameters *param = (InjectParameters*)lpParam;
|
||||||
if (!param) {
|
if (!param) {
|
||||||
log_error("%s ThreadProc got NULL param", LOG_PREFIX);
|
log_error("ThreadProc got NULL param");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (!param->ConfigDir || param->ConfigDir[0] == '\0') {
|
if (!param->ConfigDir || param->ConfigDir[0] == '\0') {
|
||||||
log_error("%s ThreadProc got NULL or empty ConfigPath", LOG_PREFIX);
|
log_error("ThreadProc got NULL or empty ConfigPath");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,27 +49,27 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
snprintf(ConfigPath, sizeof(ConfigPath), "%s\\AgentConfig.toml", param->ConfigDir);
|
snprintf(ConfigPath, sizeof(ConfigPath), "%s\\AgentConfig.toml", param->ConfigDir);
|
||||||
toml_result_t toml_result = toml_parse_file_ex(ConfigPath);
|
toml_result_t toml_result = toml_parse_file_ex(ConfigPath);
|
||||||
if (!toml_result.ok) {
|
if (!toml_result.ok) {
|
||||||
log_error("%s ThreadProc failed to parse config file: %s", LOG_PREFIX, toml_result.errmsg);
|
log_error("ThreadProc failed to parse config file: %s", toml_result.errmsg);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (!ReadInjectionInfo(toml_result, param)) {
|
if (!ReadInjectionInfo(toml_result, param)) {
|
||||||
log_error("%s Failed to read injection info", LOG_PREFIX);
|
log_error("Failed to read injection info");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
toml_free(toml_result);
|
toml_free(toml_result);
|
||||||
|
|
||||||
log_trace("%s Checking bootstrap-api.jar", LOG_PREFIX);
|
log_trace("Checking bootstrap-api.jar");
|
||||||
if (GetFileAttributesA(InjectionInfo.BootstrapAPIPath) == INVALID_FILE_ATTRIBUTES) {
|
if (GetFileAttributesA(InjectionInfo.BootstrapAPIPath) == INVALID_FILE_ATTRIBUTES) {
|
||||||
log_error("%s bootstrap-api.jar not found: %s", LOG_PREFIX, InjectionInfo.BootstrapAPIPath);
|
log_error("bootstrap-api.jar not found: %s", InjectionInfo.BootstrapAPIPath);
|
||||||
// MessageBoxA(NULL, "bootstrap-api.jar not found", "Error", MB_OK);
|
// MessageBoxA(NULL, "bootstrap-api.jar not found", "Error", MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check JuiceLoader.jar
|
// Check JuiceLoader.jar
|
||||||
|
|
||||||
log_trace("%s Checking JuiceLoader.jar", LOG_PREFIX);
|
log_trace("Checking JuiceLoader.jar");
|
||||||
if (GetFileAttributesA(InjectionInfo.JuiceLoaderJarPath) == INVALID_FILE_ATTRIBUTES) {
|
if (GetFileAttributesA(InjectionInfo.JuiceLoaderJarPath) == INVALID_FILE_ATTRIBUTES) {
|
||||||
log_error("%s JuiceLoader.jar not found: %s", LOG_PREFIX, InjectionInfo.JuiceLoaderJarPath);
|
log_error("JuiceLoader.jar not found: %s", InjectionInfo.JuiceLoaderJarPath);
|
||||||
// MessageBoxA(NULL, "JuiceLoader.jar not found", "Error", MB_OK);
|
// MessageBoxA(NULL, "JuiceLoader.jar not found", "Error", MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -171,54 +83,54 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
|
|
||||||
result = GetCreatedJVM(&jvm);
|
result = GetCreatedJVM(&jvm);
|
||||||
if (result != JNI_OK) {
|
if (result != JNI_OK) {
|
||||||
log_error("%s Failed to get JVM (%d)", LOG_PREFIX, result);
|
log_error("Failed to get JVM (%d)", result);
|
||||||
MessageBoxA(NULL, "Failed to get JVM", "Error", MB_OK);
|
MessageBoxA(NULL, "Failed to get JVM", "Error", MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
log_info("%s Got JVM", LOG_PREFIX);
|
log_info("Got JVM");
|
||||||
|
|
||||||
|
|
||||||
result = GetJNIEnv(jvm, &env);
|
result = GetJNIEnv(jvm, &env);
|
||||||
if (result != JNI_OK) {
|
if (result != JNI_OK) {
|
||||||
log_error("%s Failed to get JNIEnv (%d)", LOG_PREFIX, result);
|
log_error("Failed to get JNIEnv (%d)", result);
|
||||||
// MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
|
// MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
log_info("%s Got JNIEnv", LOG_PREFIX);
|
log_info("Got JNIEnv");
|
||||||
|
|
||||||
// Debug: JVM version
|
// Debug: JVM version
|
||||||
jint version = (*env)->GetVersion(env);
|
jint version = (*env)->GetVersion(env);
|
||||||
log_trace("%s JVM version: 0x%x", LOG_PREFIX, version);
|
log_trace("JVM version: 0x%x", version);
|
||||||
|
|
||||||
// Init and enable jvmti
|
// Init and enable jvmti
|
||||||
result = GetJVMTI(jvm, &jvmti);
|
result = GetJVMTI(jvm, &jvmti);
|
||||||
if (result != JNI_OK) {
|
if (result != JNI_OK) {
|
||||||
log_error("%s Failed to get JVMTI (%d)", LOG_PREFIX, result);
|
log_error("Failed to get JVMTI (%d)", result);
|
||||||
// MessageBoxA(NULL, "Failed to get JVMTI", "Error", MB_OK);
|
// MessageBoxA(NULL, "Failed to get JVMTI", "Error", MB_OK);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
log_info("%s Enabled jvmti", LOG_PREFIX);
|
log_info("Enabled jvmti");
|
||||||
|
|
||||||
// Inject jars
|
// Inject jars
|
||||||
log_info("%s injecting bootstrap-api jar...", LOG_PREFIX);
|
log_info("injecting bootstrap-api jar...");
|
||||||
(*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, InjectionInfo.BootstrapAPIPath);
|
(*jvmti)->AddToSystemClassLoaderSearch(jvmti, InjectionInfo.BootstrapAPIPath);
|
||||||
log_info("%s injected bootstrap-api jar", LOG_PREFIX);
|
log_info("injected bootstrap-api jar");
|
||||||
|
|
||||||
log_info("%s injecting loader jar...", LOG_PREFIX);
|
log_info("injecting loader jar...");
|
||||||
(*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, InjectionInfo.JuiceLoaderJarPath);
|
(*jvmti)->AddToSystemClassLoaderSearch(jvmti, InjectionInfo.JuiceLoaderJarPath);
|
||||||
log_info("%s injected loader jar", LOG_PREFIX);
|
log_info("injected loader jar");
|
||||||
|
|
||||||
// Invoke loader.init(JuiceLoaderLibPath, EntryJarPath)
|
// Invoke loader.init(JuiceLoaderLibPath, EntryJarPath)
|
||||||
log_info("%s invoking loader init()...", LOG_PREFIX);
|
log_info("invoking loader init()...");
|
||||||
log_info("\n============ Loader Info ============\n");
|
log_info("============ Loader Info ============\n");
|
||||||
jclass cls = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoader");
|
jclass cls = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoader");
|
||||||
if (cls == NULL) {
|
if (cls == NULL) {
|
||||||
log_error("%s FindClass JuiceLoader failed", LOG_PREFIX);
|
log_error("FindClass JuiceLoader failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "init", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
|
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "init", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
|
||||||
if (mid == NULL) {
|
if (mid == NULL) {
|
||||||
log_error("%s GetStaticMethodID init failed", LOG_PREFIX);
|
log_error("GetStaticMethodID init failed");
|
||||||
if ((*env)->ExceptionCheck(env)) {
|
if ((*env)->ExceptionCheck(env)) {
|
||||||
(*env)->ExceptionDescribe(env);
|
(*env)->ExceptionDescribe(env);
|
||||||
(*env)->ExceptionClear(env);
|
(*env)->ExceptionClear(env);
|
||||||
@@ -231,17 +143,23 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
|||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryJarPath),
|
(*env)->NewStringUTF(env, InjectionInfo.EntryJarPath),
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryClass),
|
(*env)->NewStringUTF(env, InjectionInfo.EntryClass),
|
||||||
(*env)->NewStringUTF(env, InjectionInfo.EntryMethod));
|
(*env)->NewStringUTF(env, InjectionInfo.EntryMethod));
|
||||||
log_info("\n============ Loader Info =============\n");
|
log_info("============ Loader Info =============\n");
|
||||||
log_info("%s invoked. ", LOG_PREFIX);
|
log_info("invoked. ");
|
||||||
log_info("%s done. cleaning up...", LOG_PREFIX);
|
log_info("done. cleaning up...");
|
||||||
|
|
||||||
(*jvm)->DetachCurrentThread(jvm);
|
// :(
|
||||||
|
// (*jvm)->DetachCurrentThread(jvm);
|
||||||
|
|
||||||
free(param);
|
free(param);
|
||||||
log_info("%s exit.", LOG_PREFIX);
|
log_info("exit.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// =========================================================== ///
|
||||||
|
/// Function: DllMain
|
||||||
|
/// Description: DLL Injection Entry Point
|
||||||
|
/// Inject Method: A) ReflectiveDLLInjection B) LoadLibraryA+CreateRemoteThread
|
||||||
|
/// =========================================================== ///
|
||||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
||||||
BOOL bReturnValue = TRUE;
|
BOOL bReturnValue = TRUE;
|
||||||
switch (dwReason){
|
switch (dwReason){
|
||||||
@@ -254,19 +172,38 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
hAppInstance = hinstDLL;
|
hAppInstance = hinstDLL;
|
||||||
|
|
||||||
// Initialize log
|
// Initialize log
|
||||||
log_set_level(LOG_TRACE);
|
initLogger();
|
||||||
log_is_log_filename(false);
|
log_info("DLL_PROCESS_ATTACH");
|
||||||
log_is_log_line(false);
|
|
||||||
log_is_log_time(false);
|
|
||||||
log_info("%s DLL_PROCESS_ATTACH", LOG_PREFIX);
|
|
||||||
log_info("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
log_info("[*] JuiceAgent Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||||
|
|
||||||
DisableThreadLibraryCalls(hinstDLL);
|
DisableThreadLibraryCalls(hinstDLL);
|
||||||
|
|
||||||
|
// JVM Options Inject Method Detection
|
||||||
|
// TODO: Remove Duplicated Code: GetCreatedJVM/GetEnv and var jvm/env (Duplicated at: ThreadProc)
|
||||||
|
JavaVM *jvm = NULL;
|
||||||
|
JNIEnv *env = NULL;
|
||||||
|
jint result = GetCreatedJVM(&jvm);
|
||||||
|
// Check if JVM is created
|
||||||
|
if (result != JNI_OK || jvm == NULL) {
|
||||||
|
log_error("Failed to get JVM (%d) [JVM: %p]", result, jvm);
|
||||||
|
MessageBoxA(NULL, "Failed to get JVM [GetCreatedJVM Faliled or JVM is NULL]\nCheck console for more info.", "Error", MB_OK);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
log_info("DllMain GetCreatedJVM OK. Checking if JVM is inited...");
|
||||||
|
// Check if JVM is inited
|
||||||
|
// JNI_EDETACHED: jvm not initialized, means Agent loaded before JVM inited (jvm options: -agentpath)
|
||||||
|
// Will exit DllMain and jvm will invoke Agent_OnLoad
|
||||||
|
if (GetJNIEnv(jvm, &env) == JNI_EDETACHED) {
|
||||||
|
log_warn("Inject Method JVM Options Detected! Exiting...");
|
||||||
|
log_trace("DllMain(DLL_PROCESS_ATTACH) Exiting...");
|
||||||
|
log_info("Agent Will be injected by the Inject Methood JVM Options.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Allocate memory for local param
|
// Allocate memory for local param
|
||||||
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
||||||
if (localParm == NULL) {
|
if (localParm == NULL) {
|
||||||
log_error("%s malloc failed for InjectParameters", LOG_PREFIX);
|
log_error("malloc failed for InjectParameters");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
memset(localParm, 0, sizeof(InjectParameters));
|
memset(localParm, 0, sizeof(InjectParameters));
|
||||||
@@ -278,7 +215,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
|
|
||||||
safe_copy(localParm->ConfigDir, remoteParm->ConfigDir, sizeof(localParm->ConfigDir));
|
safe_copy(localParm->ConfigDir, remoteParm->ConfigDir, sizeof(localParm->ConfigDir));
|
||||||
} else {
|
} else {
|
||||||
log_info("%s RemoteParm is NULL!", LOG_PREFIX);
|
log_info("RemoteParm is NULL!");
|
||||||
|
|
||||||
char DllDir[INJECT_PATH_MAX] = {0};
|
char DllDir[INJECT_PATH_MAX] = {0};
|
||||||
|
|
||||||
@@ -287,32 +224,32 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
DWORD err = GetLastError();
|
DWORD err = GetLastError();
|
||||||
if (err == ERROR_MOD_NOT_FOUND) {
|
if (err == ERROR_MOD_NOT_FOUND) {
|
||||||
// Reflective detected, lpReserved must be set.
|
// Reflective detected, lpReserved must be set.
|
||||||
log_fatal("%s Reflective load detected! Please set ConfigDir manually! Exit.", LOG_PREFIX);
|
log_fatal("Reflective load detected! Please set ConfigDir manually! Exit.");
|
||||||
} else {
|
} else {
|
||||||
log_error("%s Normal Inject: GetModuleFileNameA failed! Error: %lu", LOG_PREFIX, err);
|
log_error("Normal Inject: GetModuleFileNameA failed! Error: %lu", err);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// Normal Inject(e.g. CreateRemoteThread+LoadLibraryA), lpReserved can be NULL.
|
// Normal Inject(e.g. CreateRemoteThread+LoadLibraryA), lpReserved can be NULL.
|
||||||
PathRemoveFileSpecA(DllDir);
|
PathRemoveFileSpecA(DllDir);
|
||||||
log_info("%s GetModuleFileNameA: %s", LOG_PREFIX, DllDir);
|
log_info("GetModuleFileNameA: %s", DllDir);
|
||||||
safe_copy(localParm->ConfigDir, DllDir, INJECT_PATH_MAX);
|
safe_copy(localParm->ConfigDir, DllDir, INJECT_PATH_MAX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log_info("%s ConfigDir: %s", LOG_PREFIX, localParm->ConfigDir);
|
log_info("ConfigDir: %s", localParm->ConfigDir);
|
||||||
|
|
||||||
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL);
|
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL);
|
||||||
if (hThread) {
|
if (hThread) {
|
||||||
CloseHandle(hThread);
|
CloseHandle(hThread);
|
||||||
} else {
|
} else {
|
||||||
log_error("%s CreateThread failed %lu", LOG_PREFIX, GetLastError());
|
log_error("CreateThread failed %lu", GetLastError());
|
||||||
free(localParm);
|
free(localParm);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case DLL_PROCESS_DETACH:
|
case DLL_PROCESS_DETACH:
|
||||||
log_info("%s DLL_PROCESS_DETACH", LOG_PREFIX);
|
log_info("DLL_PROCESS_DETACH");
|
||||||
break;
|
break;
|
||||||
case DLL_THREAD_ATTACH:
|
case DLL_THREAD_ATTACH:
|
||||||
case DLL_THREAD_DETACH:
|
case DLL_THREAD_DETACH:
|
||||||
@@ -320,3 +257,115 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
|
|||||||
}
|
}
|
||||||
return bReturnValue;
|
return bReturnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// =========================================================== ///
|
||||||
|
/// Inject Method: JVM Options -agentpath
|
||||||
|
|
||||||
|
/// =========================================================== ///
|
||||||
|
/// Function: vm_init
|
||||||
|
/// Description: Called when the JVM is fully initialized. This is a callback function.
|
||||||
|
/// =========================================================== ///
|
||||||
|
static char *agent_options = NULL;
|
||||||
|
static void JNICALL vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) {
|
||||||
|
log_info("vminit: VM fully initialized!");
|
||||||
|
log_info("Use options: %s", agent_options);
|
||||||
|
|
||||||
|
log_info("JNIEnv: %p", env);
|
||||||
|
log_info("jvmtiEnv: %p", jvmti);
|
||||||
|
|
||||||
|
log_info("Version: %d", (*env)->GetVersion(env));
|
||||||
|
|
||||||
|
// TODO: Remove Duplicated Code
|
||||||
|
|
||||||
|
// Check if agent_options is null
|
||||||
|
if (agent_options == NULL) {
|
||||||
|
// Use DLL path as default
|
||||||
|
log_info("agent_options is null, use DLL path as default");
|
||||||
|
char DllDir[INJECT_PATH_MAX] = {0};
|
||||||
|
|
||||||
|
if (GetModuleFileNameA(hAppInstance, DllDir, INJECT_PATH_MAX) == 0) {
|
||||||
|
// Failed to get module file name
|
||||||
|
DWORD err = GetLastError();
|
||||||
|
if (err == ERROR_MOD_NOT_FOUND) {
|
||||||
|
// Reflective detected, lpReserved must be set.
|
||||||
|
log_fatal("Reflective load detected! Please set ConfigDir manually! Exit.");
|
||||||
|
} else {
|
||||||
|
log_error("Normal Inject: GetModuleFileNameA failed! Error: %lu", err);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
// Normal Inject(e.g. CreateRemoteThread+LoadLibraryA), lpReserved can be NULL.
|
||||||
|
PathRemoveFileSpecA(DllDir);
|
||||||
|
log_info("GetModuleFileNameA: %s", DllDir);
|
||||||
|
agent_options = DllDir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log_trace("agent_options: %s", agent_options);
|
||||||
|
|
||||||
|
// Allocate memory for InjectParameters
|
||||||
|
InjectParameters *localParm = (InjectParameters*)malloc(sizeof(InjectParameters));
|
||||||
|
if (localParm == NULL) {
|
||||||
|
log_error("malloc failed for InjectParameters");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memset(localParm, 0, sizeof(InjectParameters));
|
||||||
|
|
||||||
|
// safe copy
|
||||||
|
safe_copy(localParm->ConfigDir, agent_options, INJECT_PATH_MAX);
|
||||||
|
|
||||||
|
// Start new thread
|
||||||
|
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, localParm, 0, NULL);
|
||||||
|
if (hThread) {
|
||||||
|
CloseHandle(hThread);
|
||||||
|
} else {
|
||||||
|
log_error("CreateThread failed %lu", GetLastError());
|
||||||
|
free(localParm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// =========================================================== ///
|
||||||
|
/// Function: Agent_OnLoad
|
||||||
|
/// Description: Java Agent OnLoad
|
||||||
|
/// Inject Method: JVM Options A) -agentpath:<path-to-agent>.dll=<param-config-dir>
|
||||||
|
/// Example: java -agentpath:./libagent.dll=./config -jar myapp.jar
|
||||||
|
/// =========================================================== ///
|
||||||
|
|
||||||
|
// Notice: This function is called by the JVM when the agent is loaded.
|
||||||
|
// Warning: JVM not initialized yet when this function is called.
|
||||||
|
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
|
||||||
|
initLogger();
|
||||||
|
log_info("Agent_OnLoad");
|
||||||
|
log_info("Agent_OnLoad: options: %s", options);
|
||||||
|
|
||||||
|
// save options
|
||||||
|
if (options != NULL) {
|
||||||
|
agent_options = strdup(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
jvmtiEnv *jvmti;
|
||||||
|
(*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1_2);
|
||||||
|
|
||||||
|
// callbacks
|
||||||
|
jvmtiEventCallbacks callbacks = {0};
|
||||||
|
callbacks.VMInit = &vm_init;
|
||||||
|
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
|
||||||
|
|
||||||
|
// enable events
|
||||||
|
(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
|
||||||
|
|
||||||
|
log_info("Agent_OnLoad: Done, waiting for JVM Init event.");
|
||||||
|
return JNI_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Inject Method: JVM Options -agentpath
|
||||||
|
/// =========================================================== ///
|
||||||
|
|
||||||
|
// Notice: This function is useless but required by the JVM (in Windows).
|
||||||
|
JNIEXPORT jint JNICALL Agent_OnAttach (JavaVM* vm, char *options, void *reserved) {
|
||||||
|
log_info("Agent_OnAttach");
|
||||||
|
#ifdef WIN_X64
|
||||||
|
log_warn("Agent_OnAttach: Windows will invoke DllMain, Exiting Agent_OnAttach.");
|
||||||
|
log_info("Agent_OnAttach will exit in Windows, Loading will be done in DllMain.\n");
|
||||||
|
#endif
|
||||||
|
return JNI_OK;
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
Author: xiaozhou233 (xiaozhou233@vip.qq.com)
|
||||||
|
File: datautils.c
|
||||||
|
Desc: data utils
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "datautils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "tomlc17.h"
|
||||||
|
|
||||||
|
void safe_copy(char *dest, const char *src, size_t dest_size) {
|
||||||
|
if (!dest || dest_size == 0) return;
|
||||||
|
strncpy(dest, src, dest_size - 1);
|
||||||
|
dest[dest_size - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ReadInjectionInfo(toml_result_t result, InjectParameters* params) {
|
||||||
|
toml_datum_t InjectionTree = toml_seek(result.toptab, "Injection");
|
||||||
|
|
||||||
|
// ======= BootstrapAPIPath =======
|
||||||
|
toml_datum_t BootstrapAPIPath = toml_seek(InjectionTree, "BootstrapAPIPath");
|
||||||
|
char BootstrapAPIPathStr[INJECT_PATH_MAX];
|
||||||
|
if (BootstrapAPIPath.type != TOML_STRING) {
|
||||||
|
log_error("BootstrapAPIPath is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (BootstrapAPIPath.u.s == NULL || strlen(BootstrapAPIPath.u.s) == 0) {
|
||||||
|
sprintf(BootstrapAPIPathStr, "%s\\bootstrap-api.jar", params->ConfigDir);
|
||||||
|
} else {
|
||||||
|
sprintf(BootstrapAPIPathStr, "%s", BootstrapAPIPath.u.s);
|
||||||
|
}
|
||||||
|
safe_copy(InjectionInfo.BootstrapAPIPath, BootstrapAPIPathStr, INJECT_PATH_MAX);
|
||||||
|
// ======= BootstrapAPIPath =======
|
||||||
|
|
||||||
|
// ======= JuiceLoaderJarPath =======
|
||||||
|
toml_datum_t JuiceLoaderJarPath = toml_seek(InjectionTree, "JuiceLoaderJarPath");
|
||||||
|
char JuiceLoaderJarPathStr[INJECT_PATH_MAX];
|
||||||
|
if (JuiceLoaderJarPath.type != TOML_STRING) {
|
||||||
|
log_error("JuiceLoaderJarPath is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (JuiceLoaderJarPath.u.s == NULL || strlen(JuiceLoaderJarPath.u.s) == 0) {
|
||||||
|
sprintf(JuiceLoaderJarPathStr, "%s\\JuiceLoader.jar", params->ConfigDir);
|
||||||
|
} else {
|
||||||
|
sprintf(JuiceLoaderJarPathStr, "%s", JuiceLoaderJarPath.u.s);
|
||||||
|
}
|
||||||
|
safe_copy(InjectionInfo.JuiceLoaderJarPath, JuiceLoaderJarPathStr, INJECT_PATH_MAX);
|
||||||
|
// ======= JuiceLoaderJarPath =======
|
||||||
|
|
||||||
|
// ======= JuiceLoaderLibPath =======
|
||||||
|
toml_datum_t JuiceLoaderLibPath = toml_seek(InjectionTree, "JuiceLoaderLibPath");
|
||||||
|
char JuiceLoaderLibPathStr[INJECT_PATH_MAX];
|
||||||
|
if (JuiceLoaderLibPath.type != TOML_STRING) {
|
||||||
|
log_error("JuiceLoaderLibPath is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (JuiceLoaderLibPath.u.s == NULL || strlen(JuiceLoaderLibPath.u.s) == 0) {
|
||||||
|
sprintf(JuiceLoaderLibPathStr, "%s\\libjuiceloader.dll", params->ConfigDir);
|
||||||
|
} else {
|
||||||
|
sprintf(JuiceLoaderLibPathStr, "%s", JuiceLoaderLibPath.u.s);
|
||||||
|
}
|
||||||
|
safe_copy(InjectionInfo.JuiceLoaderLibPath, JuiceLoaderLibPathStr, INJECT_PATH_MAX);
|
||||||
|
// ======= JuiceLoaderLibPath =======
|
||||||
|
|
||||||
|
// ======= Entry =======
|
||||||
|
toml_datum_t EntryJarPath = toml_seek(InjectionTree, "EntryJarPath");
|
||||||
|
char EntryJarPathStr[INJECT_PATH_MAX];
|
||||||
|
if (EntryJarPath.type != TOML_STRING) {
|
||||||
|
log_error("EntryJarPath is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (EntryJarPath.u.s == NULL || strlen(EntryJarPath.u.s) == 0) {
|
||||||
|
sprintf(EntryJarPathStr, "%s\\entry.jar", params->ConfigDir);
|
||||||
|
} else {
|
||||||
|
sprintf(EntryJarPathStr, "%s", EntryJarPath.u.s);
|
||||||
|
}
|
||||||
|
safe_copy(InjectionInfo.EntryJarPath, EntryJarPathStr, INJECT_PATH_MAX);
|
||||||
|
|
||||||
|
toml_datum_t EntryClass = toml_seek(InjectionTree, "EntryClass");
|
||||||
|
if (EntryClass.type != TOML_STRING) {
|
||||||
|
log_error("EntryClass is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (EntryClass.u.s == NULL || strlen(EntryClass.u.s) == 0) {
|
||||||
|
safe_copy(InjectionInfo.EntryClass, "cn.xiaozhou233.juiceloader.entry.Entry", INJECT_PATH_MAX);
|
||||||
|
} else {
|
||||||
|
safe_copy(InjectionInfo.EntryClass, EntryClass.u.s, INJECT_PATH_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_datum_t EntryMethod = toml_seek(InjectionTree, "EntryMethod");
|
||||||
|
if (EntryMethod.type != TOML_STRING) {
|
||||||
|
log_error("EntryMethod is not a string.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (EntryMethod.u.s == NULL || strlen(EntryMethod.u.s) == 0) {
|
||||||
|
safe_copy(InjectionInfo.EntryMethod, "start", INJECT_PATH_MAX);
|
||||||
|
} else {
|
||||||
|
safe_copy(InjectionInfo.EntryMethod, EntryMethod.u.s, INJECT_PATH_MAX);
|
||||||
|
}
|
||||||
|
// ======= Entry =======
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
struct _JuiceLoaderNative {
|
struct _JuiceLoaderNative {
|
||||||
JavaVM *jvm;
|
JavaVM *jvm;
|
||||||
jvmtiEnv *jvmti;
|
jvmtiEnv *jvmti;
|
||||||
JNIEnv *env;
|
|
||||||
} JuiceLoaderNative;
|
} JuiceLoaderNative;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -32,38 +32,37 @@ int setup(JNIEnv *env) {
|
|||||||
log_is_log_time(false);
|
log_is_log_time(false);
|
||||||
log_set_level(LOG_DEBUG);
|
log_set_level(LOG_DEBUG);
|
||||||
|
|
||||||
|
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
||||||
|
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
||||||
|
|
||||||
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
log_info("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||||
|
|
||||||
log_info("%s setup!", LOG_PREFIX);
|
log_info("%s setup!", LOG_PREFIX);
|
||||||
{// Get JavaVM
|
// Get JavaVM
|
||||||
if (JNI_GetCreatedJavaVMs(&JuiceLoaderNative.jvm, 1, NULL) != JNI_OK || !JuiceLoaderNative.jvm) {
|
if ((*env)->GetJavaVM(env, &JuiceLoaderNative.jvm) != 0 || JuiceLoaderNative.jvm == NULL) {
|
||||||
log_error("%s JNI_GetCreatedJavaVMs failed", LOG_PREFIX);
|
log_error("%s GetJavaVM failed", LOG_PREFIX);
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
log_info("%s JNI_GetCreatedJavaVMs success", LOG_PREFIX);
|
log_info("%s JNI_GetCreatedJavaVMs success", LOG_PREFIX);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get JNIEnv
|
// // Get JNIEnv
|
||||||
if ((*JuiceLoaderNative.jvm)->AttachCurrentThread(JuiceLoaderNative.jvm, (void **)&JuiceLoaderNative.env, NULL) != JNI_OK) {
|
// if ((*JuiceLoaderNative.jvm)->AttachCurrentThread(JuiceLoaderNative.jvm, (void **)&JuiceLoaderNative.env, NULL) != JNI_OK) {
|
||||||
log_error("%s AttachCurrentThread failed", LOG_PREFIX);
|
// log_error("%s AttachCurrentThread failed", LOG_PREFIX);
|
||||||
return 1;
|
// return 1;
|
||||||
} else {
|
// } else {
|
||||||
log_info("%s AttachCurrentThread success", LOG_PREFIX);
|
// log_info("%s AttachCurrentThread success", LOG_PREFIX);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Debug: JVM version
|
// Debug: JVM version
|
||||||
jint version = (*JuiceLoaderNative.env)->GetVersion(JuiceLoaderNative.env);
|
jint version = (*env)->GetVersion(env);
|
||||||
log_debug("%s JVM version: 0x%x", LOG_PREFIX, version);
|
log_debug("%s JVM version: 0x%x", LOG_PREFIX, version);
|
||||||
|
|
||||||
jint res = (*JuiceLoaderNative.jvm)->GetEnv(JuiceLoaderNative.jvm, (void**)&JuiceLoaderNative.jvmti, JVMTI_VERSION_1_2);
|
if ((*JuiceLoaderNative.jvm)->GetEnv(JuiceLoaderNative.jvm, (void**)&JuiceLoaderNative.jvmti, JVMTI_VERSION_1_2) != JNI_OK || JuiceLoaderNative.jvmti == NULL) {
|
||||||
if (res != JNI_OK || JuiceLoaderNative.jvmti == NULL) {
|
log_error("%s GetEnv for jvmti failed", LOG_PREFIX);
|
||||||
log_error("%s GetEnv failed", LOG_PREFIX);
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
log_info("%s GetEnv success", LOG_PREFIX);
|
log_info("%s Enabled JVMTI at %p", LOG_PREFIX, JuiceLoaderNative.jvmti);
|
||||||
}}
|
|
||||||
log_info("%s Enabled JVMTI.");
|
|
||||||
|
|
||||||
jvmtiError err;
|
jvmtiError err;
|
||||||
// jvmtiEventCallbacks callbacks;
|
// jvmtiEventCallbacks callbacks;
|
||||||
|
|
||||||
@@ -75,6 +74,10 @@ int setup(JNIEnv *env) {
|
|||||||
caps.can_retransform_classes = 1;
|
caps.can_retransform_classes = 1;
|
||||||
caps.can_retransform_any_class = 1;
|
caps.can_retransform_any_class = 1;
|
||||||
|
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JVMTI is NULL (Before AddCapabilities, After GetEnv), %p", JuiceLoaderNative.jvmti);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
err = (*JuiceLoaderNative.jvmti)->AddCapabilities(JuiceLoaderNative.jvmti, &caps);
|
err = (*JuiceLoaderNative.jvmti)->AddCapabilities(JuiceLoaderNative.jvmti, &caps);
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddCapabilities");
|
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddCapabilities");
|
||||||
|
|
||||||
@@ -96,6 +99,8 @@ int setup(JNIEnv *env) {
|
|||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_init
|
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_init
|
||||||
(JNIEnv *env, jobject obj) {
|
(JNIEnv *env, jobject obj) {
|
||||||
log_info("%s JNI Func Init Invoke!");
|
log_info("%s JNI Func Init Invoke!");
|
||||||
|
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
||||||
|
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
||||||
if(setup(env) == 0) {
|
if(setup(env) == 0) {
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
@@ -111,15 +116,15 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_inj
|
|||||||
log_info("%s JNI Func InjectJar Invoke!", LOG_PREFIX);
|
log_info("%s JNI Func InjectJar Invoke!", LOG_PREFIX);
|
||||||
jvmtiError err;
|
jvmtiError err;
|
||||||
if(JuiceLoaderNative.jvmti == NULL) {
|
if(JuiceLoaderNative.jvmti == NULL) {
|
||||||
log_error("%s JVM not init!", LOG_PREFIX);
|
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *pathStr = (*env)->GetStringUTFChars(env, path, NULL);
|
const char *pathStr = (*env)->GetStringUTFChars(env, path, NULL);
|
||||||
log_info("%s InjectJar: %s", LOG_PREFIX, pathStr);
|
log_info("%s InjectJar: %s", LOG_PREFIX, pathStr);
|
||||||
|
|
||||||
err = (*JuiceLoaderNative.jvmti)->AddToBootstrapClassLoaderSearch(JuiceLoaderNative.jvmti, pathStr);
|
err = (*JuiceLoaderNative.jvmti)->AddToSystemClassLoaderSearch(JuiceLoaderNative.jvmti, pathStr);
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddToBootstrapClassLoaderSearch");
|
check_jvmti_error(JuiceLoaderNative.jvmti, err, "AddToSystemClassLoaderSearch");
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +138,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
|||||||
jvmtiError err;
|
jvmtiError err;
|
||||||
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
log_error("%s JVM not init!", LOG_PREFIX);
|
log_error("%s JVM not init! (JVMTI == NULL)", LOG_PREFIX);
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +171,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
|||||||
jvmtiError err;
|
jvmtiError err;
|
||||||
|
|
||||||
if (JuiceLoaderNative.jvmti == NULL) {
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
log_error("%s JVM not init!", LOG_PREFIX);
|
log_error("%s JVM not init! (JVMTI = NULL)", LOG_PREFIX);
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,6 +196,10 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
|||||||
defs[0].class_bytes = (const unsigned char*) buf;
|
defs[0].class_bytes = (const unsigned char*) buf;
|
||||||
|
|
||||||
log_info("%s Redefining class...", LOG_PREFIX);
|
log_info("%s Redefining class...", LOG_PREFIX);
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JVMTI is NULL (Before RedefineClasses, After FindClass), %p", JuiceLoaderNative.jvmti);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
err = (*JuiceLoaderNative.jvmti)->RedefineClasses(JuiceLoaderNative.jvmti, 1, defs);
|
err = (*JuiceLoaderNative.jvmti)->RedefineClasses(JuiceLoaderNative.jvmti, 1, defs);
|
||||||
check_jvmti_error(JuiceLoaderNative.jvmti, err, "RedefineClasses");
|
check_jvmti_error(JuiceLoaderNative.jvmti, err, "RedefineClasses");
|
||||||
|
|
||||||
@@ -202,7 +211,15 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_red
|
|||||||
|
|
||||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_getLoadedClasses
|
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoaderNative_getLoadedClasses
|
||||||
(JNIEnv *env, jobject obj) {
|
(JNIEnv *env, jobject obj) {
|
||||||
|
if (JuiceLoaderNative.jvmti == NULL) {
|
||||||
|
log_error("JVMTI is NULL (When GetLoadedClasses[invoked JNI Func]), %p", JuiceLoaderNative.jvmti);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
jvmtiPhase phase; (*JuiceLoaderNative.jvmti)->GetPhase(JuiceLoaderNative.jvmti, &phase); log_info("Phase=%d", phase);
|
||||||
jint count = 0;
|
jint count = 0;
|
||||||
|
log_debug("[JuiceLoader] JuiceLoaderNative addr = %p", &JuiceLoaderNative);
|
||||||
|
log_debug("[JuiceLoader] JVMTI ptr = %p", JuiceLoaderNative.jvmti);
|
||||||
|
|
||||||
jclass* classes = NULL;
|
jclass* classes = NULL;
|
||||||
|
|
||||||
jvmtiError err = (*JuiceLoaderNative.jvmti)->GetLoadedClasses(JuiceLoaderNative.jvmti, &count, &classes);
|
jvmtiError err = (*JuiceLoaderNative.jvmti)->GetLoadedClasses(JuiceLoaderNative.jvmti, &count, &classes);
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
VERSION_MAJOR=1
|
|
||||||
VERSION_MINOR=0
|
|
||||||
BUILD_NUMBER=19
|
|
||||||
Reference in new issue
Block a user