From 6f104145b410a953450dc6b708b95c21f9e5b525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=91=A8XiaoZhou?= Date: Sun, 26 Oct 2025 00:38:06 +0800 Subject: [PATCH] [Refactor] utils and library entry --- CMakeLists.txt | 2 +- src/agent/CMakeLists.txt | 2 +- src/agent/include/datautils.h | 22 +++++++ src/agent/libagent.c | 109 +++------------------------------- src/agent/utils/datautils.c | 104 ++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 103 deletions(-) create mode 100644 src/agent/include/datautils.h create mode 100644 src/agent/utils/datautils.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d333d7b..b286b3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # version set(PROJECT_VERSION_MAJOR 1) set(PROJECT_VERSION_MINOR 0) -set(PROJECT_BUILD_NUMBER 23) +set(PROJECT_BUILD_NUMBER 24) set(PROJECT_VERSION_STRING "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}-b${PROJECT_BUILD_NUMBER}") message(STATUS " ======== Build version: ${PROJECT_VERSION_STRING} ======== ") diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index 189b1e0..9ae5880 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -1,4 +1,4 @@ -# src/core/CMakeLists.txt +# src/agent/CMakeLists.txt target_include_directories(agent PRIVATE ${CMAKE_SOURCE_DIR}/includes/tomlc17) diff --git a/src/agent/include/datautils.h b/src/agent/include/datautils.h new file mode 100644 index 0000000..b41244d --- /dev/null +++ b/src/agent/include/datautils.h @@ -0,0 +1,22 @@ +#ifndef DATAUTILS_H +#define DATAUTILS_H + +#include +#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 \ No newline at end of file diff --git a/src/agent/libagent.c b/src/agent/libagent.c index 147ab61..bc99f4a 100644 --- a/src/agent/libagent.c +++ b/src/agent/libagent.c @@ -1,3 +1,9 @@ +/* +Author: xiaozhou233 (xiaozhou233@vip.qq.com) +File: libagent.c +Desc: Entry point for the agent +*/ + #include "ReflectiveLoader.h" #include "windows.h" #include "unistd.h" @@ -9,111 +15,12 @@ #include "GlobalUtils.h" #include "shlwapi.h" #include "tomlc17.h" +#include "datautils.h" #define WIN_X64 extern HINSTANCE hAppInstance; -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]; -} 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("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; -} +InjectionInfoType InjectionInfo; DWORD WINAPI ThreadProc(LPVOID lpParam) { log_trace("New thread started."); diff --git a/src/agent/utils/datautils.c b/src/agent/utils/datautils.c new file mode 100644 index 0000000..16653d9 --- /dev/null +++ b/src/agent/utils/datautils.c @@ -0,0 +1,104 @@ +/* +Author: xiaozhou233 (xiaozhou233@vip.qq.com) +File: datautils.c +Desc: data utils +*/ + + +#include "datautils.h" +#include +#include +#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; +}