Fix: harden injector/libinject/loader against UB, format bugs and leaks

This commit is contained in:
xiaozhou233 committed 2026-08-15 12:30:49 +08:00
1 parent ce08b19170
commit 1931072b96
5 files changed
+28 -27

No files matched your search

+1 -1
View File
@@ -90,7 +90,7 @@ public:
return value;
}
catch (const std::exception& e) {
catch (...) {
spdlog::warn("Config get failed: {}, using default value", path);
return default_value;
}
+1 -1
View File
@@ -116,7 +116,7 @@ static int run(int argc, char** argv)
return 3;
}
inject_fn_t inject = (inject_fn_t)proc;
inject_fn_t inject = (inject_fn_t)(void*)proc;
printf("[*] Calling inject(pid=%d, dllPath=\"%s\", currentDir=\"%s\")\n",
pid, dll_path, cwd);
+17 -15
View File
@@ -5,8 +5,7 @@
#include <ReflectiveDLLInjection/LoadLibraryR.h>
#define WIN_X64
#define BREAK_WITH_ERROR( e ) { printf("[-] %s. Error=%l", e, GetLastError()); break; }
#define MAX_PATH 260
#define BREAK_WITH_ERROR(e) { printf("[-] %s. Error=%lu\n", e, GetLastError()); break; }
__declspec(dllexport)
int inject(int pid, char *path, char *params){
@@ -83,21 +82,21 @@ int inject(int pid, char *path, char *params){
}
/* allocate remote memory for InjectParameters */
lpRemoteParam = VirtualAllocEx(hProcess, NULL, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!lpRemoteParam)
BREAK_WITH_ERROR("VirtualAllocEx for remote param failed\n");
printf("remote param addr = %p\n", lpRemoteParam);
/* write params to remote memory */
SIZE_T written = 0;
if (params) {
if (!WriteProcessMemory(hProcess, lpRemoteParam, params, MAX_PATH, &written) || written != MAX_PATH)
BREAK_WITH_ERROR("WriteProcessMemory for remote param failed\n");
/* allocate remote memory for params */
lpRemoteParam = VirtualAllocEx(hProcess, NULL, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!lpRemoteParam)
BREAK_WITH_ERROR("VirtualAllocEx for remote param failed");
printf("remote param addr = %p\n", lpRemoteParam);
SIZE_T param_len = strlen(params) + 1;
if (!WriteProcessMemory(hProcess, lpRemoteParam, params, param_len, &written) || written != param_len)
BREAK_WITH_ERROR("WriteProcessMemory for remote param failed");
} else {
printf("No parameters provided; using null pointer\n");
lpRemoteParam = NULL;
}
/* read back some bytes to verify write */
@@ -124,7 +123,7 @@ int inject(int pid, char *path, char *params){
BREAK_WITH_ERROR("LoadRemoteLibraryR returned NULL");
}
printf("[+] DLL injected: '%s' into PID %d, remote param at %p\n", path, dwProcessId, lpRemoteParam);
printf("[+] DLL injected: '%s' into PID %lu, remote param at %p\n", path, dwProcessId, lpRemoteParam);
/* wait for reflective loader thread to complete */
WaitForSingleObject(hRemoteThread, INFINITE);
@@ -164,6 +163,7 @@ int inject(int pid, char *path, char *params){
*/
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2
(JNIEnv *env, jobject obj, jint pid, jstring path) {
(void)obj;
// Injection Path
const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL);
@@ -181,12 +181,13 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ
*/
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;
const char* InjectionDLL = (*env)->GetStringUTFChars(env, path, NULL);
const char* ConfigDir = (*env)->GetStringUTFChars(env, configDir, NULL);
char params[MAX_PATH] = {0}; /* use a real buffer */
strncpy(params, ConfigDir ? ConfigDir : "", MAX_PATH - 1);
params[MAX_PATH - 1] = '\0';
int ret = inject(pid, (char*)InjectionDLL, params);
@@ -210,6 +211,7 @@ static int g_count = 0;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
(void)lParam;
wchar_t title[256];
GetWindowTextW(hwnd, title, 256);
if (wcslen(title) == 0) return TRUE;
@@ -229,7 +231,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle
(JNIEnv *env, jclass clazz, jstring keyword) {
(void)clazz;
const jchar *input = (*env)->GetStringChars(env, keyword, NULL);
wcsncpy(g_keyword, (const wchar_t *)input, 255);
g_keyword[255] = L'\0';
+8 -8
View File
@@ -52,13 +52,13 @@ static bool invoke_juiceagent_init(JNIEnv* env, const LoaderConfig& config) {
return true;
}
static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) {
static void init(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) {
if (!runtime_dir) {
spdlog::warn("Runtime directory is not set!");
}
// Load configuration
JuiceAgent::Config::Config cfg(runtime_dir);
JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : "");
if (!cfg.is_valid()) {
spdlog::error("Failed to load configuration!");
@@ -91,9 +91,9 @@ static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) {
spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256);
// Load JuiceAgent API to system classloader
jint status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
if (status != JNI_OK) {
spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", status);
jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
if (status != JVMTI_ERROR_NONE) {
spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast<int>(status));
return;
}
@@ -108,16 +108,16 @@ static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) {
void entrypoint(const char* runtime_dir) {
// Attach to the JVM
JuiceAgent::Loader::Jvm jvm;
Jvm jvm;
if (!jvm.attach()) {
spdlog::error("Failed to attach to JVM!");
spdlog::error("Failed to attach to JVM!");
return;
}
// Get jvm and jni env
JNIEnv* env = jvm.get_env();
auto* jvmti = jvm.get_jvmti();
if(!env || !jvmti) {
if (!env || !jvmti) {
spdlog::error("Failed to get jni env or jvmti env!");
return;
}
+1 -2
View File
@@ -56,10 +56,9 @@ private:
attached_ = false;
}
bool retry(const char* what, int attempt) {
void retry(const char* what, int attempt) {
spdlog::debug("Failed to get {}, attempt {}/{}", what, attempt, max_try_);
std::this_thread::sleep_for(std::chrono::milliseconds(retry_delay_ms_));
return false;
}
bool acquire_jvm() {