mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
[Update] Injector
This commit is contained in:
1 parent
e3481d007c
commit
56cd994715
3 files changed
+13
-280
No files matched your search
@@ -5,13 +5,17 @@
|
||||
#include <string.h>
|
||||
#include "LoadLibraryR.h"
|
||||
#include "InjectorNative.h"
|
||||
#include "InjectParameters.h"
|
||||
#include "log.c"
|
||||
#include "JuiceAgent.h"
|
||||
|
||||
#define WIN_X64
|
||||
#define log_debug(...) printf("[DEBUG] %s:%d: " __VA_ARGS__, __FILE__, __LINE__)
|
||||
#define log_info(...) printf("[INFO] %s:%d: " __VA_ARGS__, __FILE__, __LINE__)
|
||||
#define log_warn(...) printf("[WARN] %s:%d: " __VA_ARGS__, __FILE__, __LINE__)
|
||||
#define log_error(...) printf("[ERROR] %s:%d: " __VA_ARGS__, __FILE__, __LINE__)
|
||||
|
||||
#define BREAK_WITH_ERROR( e ) { log_error("[-] %s. Error=%l", e, GetLastError()); break; }
|
||||
|
||||
int inject(int pid, char *path, InjectParameters *params){
|
||||
int inject(int pid, char *path, InjectParams *params){
|
||||
HANDLE hFile = NULL;
|
||||
HANDLE hRemoteThread = NULL;
|
||||
HANDLE hProcess = NULL;
|
||||
@@ -24,11 +28,6 @@ int inject(int pid, char *path, InjectParameters *params){
|
||||
TOKEN_PRIVILEGES priv = {0};
|
||||
BOOL bSuccess = FALSE;
|
||||
|
||||
log_set_level(LOG_DEBUG);
|
||||
log_is_log_filename(false);
|
||||
log_is_log_line(false);
|
||||
log_is_log_time(false);
|
||||
|
||||
log_info("[*] libinjector Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||
|
||||
log_debug("Injector process pointer size = %zu bytes", sizeof(void*));
|
||||
@@ -120,8 +119,8 @@ int inject(int pid, char *path, InjectParameters *params){
|
||||
log_error("Unknown DLL machine type; proceed with caution");
|
||||
}
|
||||
|
||||
/* allocate remote memory for InjectParameters */
|
||||
lpRemoteParam = VirtualAllocEx(hProcess, NULL, sizeof(InjectParameters), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
/* allocate remote memory for InjectParams */
|
||||
lpRemoteParam = VirtualAllocEx(hProcess, NULL, sizeof(InjectParams), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if (!lpRemoteParam)
|
||||
BREAK_WITH_ERROR("VirtualAllocEx for remote param failed");
|
||||
|
||||
@@ -130,7 +129,7 @@ int inject(int pid, char *path, InjectParameters *params){
|
||||
/* write params to remote memory */
|
||||
SIZE_T written = 0;
|
||||
if (params) {
|
||||
if (!WriteProcessMemory(hProcess, lpRemoteParam, params, sizeof(InjectParameters), &written) || written != sizeof(InjectParameters))
|
||||
if (!WriteProcessMemory(hProcess, lpRemoteParam, params, sizeof(InjectParams), &written) || written != sizeof(InjectParams))
|
||||
BREAK_WITH_ERROR("WriteProcessMemory for remote param failed");
|
||||
} else {
|
||||
log_warn("No parameters provided; using null pointer");
|
||||
@@ -139,9 +138,9 @@ int inject(int pid, char *path, InjectParameters *params){
|
||||
|
||||
/* read back some bytes to verify write */
|
||||
{
|
||||
InjectParameters verify;
|
||||
InjectParams verify;
|
||||
SIZE_T read = 0;
|
||||
if (ReadProcessMemory(hProcess, lpRemoteParam, &verify, sizeof(InjectParameters), &read)) {
|
||||
if (ReadProcessMemory(hProcess, lpRemoteParam, &verify, sizeof(InjectParams), &read)) {
|
||||
log_debug("[*] Read back remote param, ConfigDir=%.*s", (int)sizeof(verify.ConfigDir), verify.ConfigDir);
|
||||
} else {
|
||||
log_error("[-] ReadProcessMemory failed: %lu", GetLastError());
|
||||
@@ -220,7 +219,7 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNativ
|
||||
// Config Path
|
||||
const char* ConfigDir = (*env)->GetStringUTFChars(env, configDir, NULL);
|
||||
|
||||
InjectParameters params;
|
||||
InjectParams params;
|
||||
memset(¶ms, 0, sizeof(params));
|
||||
|
||||
strncpy(params.ConfigDir, ConfigDir ? ConfigDir : "", sizeof(params.ConfigDir)-1);
|
||||
@@ -1,266 +0,0 @@
|
||||
#include <JuiceAgent.h>
|
||||
#include <Logger.hpp>
|
||||
#include <windows.h>
|
||||
#include <InjectorUtils.h>
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BREAK_WITH_ERROR( e ) { PLOGE.printf("[-] %s. Error=%lu", e, GetLastError()); break; }
|
||||
|
||||
/*
|
||||
* Standard DLL injection:
|
||||
* - write DLL path string into remote process memory
|
||||
* - CreateRemoteThread with LoadLibraryA(remotePath)
|
||||
* - wait, get exit code, clean up
|
||||
*/
|
||||
|
||||
extern "C" __declspec(dllexport)
|
||||
int inject(int pid, const char *path, InjectParams *params) {
|
||||
HANDLE hFile = NULL;
|
||||
HANDLE hRemoteThread = NULL;
|
||||
HANDLE hProcess = NULL;
|
||||
HANDLE hToken = NULL;
|
||||
LPVOID lpBuffer = NULL; // local file buffer (optional, used for PE inspection)
|
||||
LPVOID lpRemoteParam = NULL; // REMOTE memory for InjectParams (not used in standard injection)
|
||||
LPVOID lpRemotePath = NULL; // REMOTE memory for path string (used)
|
||||
DWORD dwLength = 0;
|
||||
DWORD dwBytesRead = 0;
|
||||
DWORD dwProcessId = (DWORD)pid;
|
||||
TOKEN_PRIVILEGES priv = {0};
|
||||
BOOL bSuccess = FALSE;
|
||||
|
||||
InitLogger();
|
||||
|
||||
PLOGI.printf("[*] libinjector Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
|
||||
PLOGD << "[*] InjectParams pointer: " << ¶ms;
|
||||
|
||||
if (!path) {
|
||||
PLOGE << "[!] Invalid path";
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
/* optional: open DLL file to inspect PE info (kept for logging) */
|
||||
hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (hFile == INVALID_HANDLE_VALUE) {
|
||||
PLOGW.printf("[!] Cannot open DLL for inspection: %s, Error=%lu -- will still try standard injection", path, GetLastError());
|
||||
hFile = NULL; // continue without file buffer
|
||||
} else {
|
||||
/* get file size (use GetFileSize) */
|
||||
dwLength = GetFileSize(hFile, NULL);
|
||||
if (dwLength != INVALID_FILE_SIZE && dwLength > 0) {
|
||||
lpBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
|
||||
if (lpBuffer) {
|
||||
if (!ReadFile(hFile, lpBuffer, dwLength, &dwBytesRead, NULL) || dwBytesRead != dwLength) {
|
||||
PLOGW.printf("[!] ReadFile incomplete or failed: %lu", GetLastError());
|
||||
HeapFree(GetProcessHeap(), 0, lpBuffer);
|
||||
lpBuffer = NULL;
|
||||
} else {
|
||||
WORD dllMachine = get_pe_machine(lpBuffer, dwLength);
|
||||
PLOGD.printf("[*] DLL PE machine: %s (0x%04x)", machine_to_str(dllMachine), dllMachine);
|
||||
BOOL hasRL = has_reflective_loader_export(lpBuffer, dwLength);
|
||||
PLOGD.printf("[*] DLL has ReflectiveLoader export: %s", hasRL ? "YES" : "NO");
|
||||
}
|
||||
} else {
|
||||
PLOGW.printf("[!] HeapAlloc for file buffer failed: %lu", GetLastError());
|
||||
}
|
||||
} else {
|
||||
PLOGW.printf("[!] GetFileSize returned invalid or zero size: %lu", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
/* try to enable SeDebugPrivilege (best-effort) */
|
||||
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
|
||||
priv.PrivilegeCount = 1;
|
||||
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
||||
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &priv.Privileges[0].Luid))
|
||||
AdjustTokenPrivileges(hToken, FALSE, &priv, 0, NULL, NULL);
|
||||
CloseHandle(hToken);
|
||||
hToken = NULL;
|
||||
}
|
||||
|
||||
/* open target process */
|
||||
hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, dwProcessId);
|
||||
if (!hProcess) {
|
||||
BREAK_WITH_ERROR("Failed to open target process");
|
||||
}
|
||||
|
||||
/* detect target architecture (logging only) */
|
||||
BOOL targetIsWow64 = FALSE;
|
||||
if (!IsWow64Process(hProcess, &targetIsWow64)) {
|
||||
PLOGE.printf("IsWow64Process failed: %lu", GetLastError());
|
||||
} else {
|
||||
PLOGI.printf("Is target process wow64 = %d (TRUE means 32-bit process on 64-bit OS)", targetIsWow64);
|
||||
}
|
||||
|
||||
#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
|
||||
PLOGI.printf("Injector compiled as: x64");
|
||||
#else
|
||||
PLOGI.printf("Injector compiled as: x86");
|
||||
#endif
|
||||
|
||||
/* allocate remote memory for DLL path string */
|
||||
size_t pathLen = strlen(path) + 1;
|
||||
lpRemotePath = VirtualAllocEx(hProcess, NULL, pathLen, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if (!lpRemotePath) {
|
||||
BREAK_WITH_ERROR("VirtualAllocEx for remote path failed");
|
||||
}
|
||||
|
||||
PLOGD.printf("remote path addr = %p", lpRemotePath);
|
||||
|
||||
/* write DLL path into remote process */
|
||||
SIZE_T written = 0;
|
||||
if (!WriteProcessMemory(hProcess, lpRemotePath, path, pathLen, &written) || written != pathLen) {
|
||||
BREAK_WITH_ERROR("WriteProcessMemory for remote path failed");
|
||||
}
|
||||
|
||||
/* create remote thread calling LoadLibraryA(remotePath) */
|
||||
DWORD dwThreadId = 0;
|
||||
hRemoteThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, lpRemotePath, 0, &dwThreadId);
|
||||
if (!hRemoteThread) {
|
||||
BREAK_WITH_ERROR("CreateRemoteThread failed");
|
||||
}
|
||||
|
||||
PLOGI.printf("[+] DLL injection thread created for '%s' into PID %d (threadId=%lu)", path, dwProcessId, dwThreadId);
|
||||
|
||||
/* wait for thread to finish */
|
||||
WaitForSingleObject(hRemoteThread, INFINITE);
|
||||
|
||||
/* get remote thread exit code (module handle or 0) */
|
||||
DWORD exitCode = 0;
|
||||
if (GetExitCodeThread(hRemoteThread, &exitCode)) {
|
||||
if (exitCode == 0) {
|
||||
PLOGW.printf("[-] Remote LoadLibraryA returned NULL (failed to load).");
|
||||
} else {
|
||||
PLOGI.printf("[+] Remote LoadLibraryA returned module handle: 0x%08lx", exitCode);
|
||||
}
|
||||
} else {
|
||||
PLOGW.printf("[-] GetExitCodeThread failed: %lu", GetLastError());
|
||||
}
|
||||
|
||||
bSuccess = TRUE;
|
||||
|
||||
/* cleanup remote thread handle */
|
||||
CloseHandle(hRemoteThread);
|
||||
hRemoteThread = NULL;
|
||||
|
||||
/* free remote path memory */
|
||||
if (lpRemotePath) {
|
||||
VirtualFreeEx(hProcess, lpRemotePath, 0, MEM_RELEASE);
|
||||
lpRemotePath = NULL;
|
||||
}
|
||||
|
||||
} while (0);
|
||||
|
||||
/* cleanup local resources */
|
||||
if (lpBuffer) {
|
||||
HeapFree(GetProcessHeap(), 0, lpBuffer);
|
||||
lpBuffer = NULL;
|
||||
}
|
||||
if (hFile && hFile != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(hFile);
|
||||
hFile = NULL;
|
||||
}
|
||||
if (hProcess) {
|
||||
CloseHandle(hProcess);
|
||||
hProcess = NULL;
|
||||
}
|
||||
|
||||
return bSuccess ? 0 : -1;
|
||||
}
|
||||
|
||||
/* JNI wrappers (unchanged except they call the above inject) */
|
||||
|
||||
/*
|
||||
* JNI Function: inject(int pid, String path)
|
||||
*/
|
||||
extern "C" __declspec(dllexport)
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_inject__ILjava_lang_String_2
|
||||
(JNIEnv *env, jobject obj, jint pid, jstring path) {
|
||||
const char* InjectionDLL = env->GetStringUTFChars(path, NULL);
|
||||
int ret = inject(pid, InjectionDLL, NULL);
|
||||
env->ReleaseStringUTFChars(path, InjectionDLL);
|
||||
return (ret == 0) ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* JNI Function: inject(int pid, String path, String configDir)
|
||||
*/
|
||||
extern "C" __declspec(dllexport)
|
||||
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){
|
||||
const char* InjectionDLL = env->GetStringUTFChars(path, NULL);
|
||||
const char* ConfigDir = env->GetStringUTFChars(configDir, NULL);
|
||||
|
||||
/* NOTE: Standard LoadLibrary injection cannot directly pass InjectParams.
|
||||
If your DLL needs ConfigDir, it should read it from a known location (e.g. file, registry),
|
||||
or you can implement an exported init function inside the DLL which you call later. */
|
||||
|
||||
int ret = inject(pid, InjectionDLL, NULL);
|
||||
|
||||
env->ReleaseStringUTFChars(path, InjectionDLL);
|
||||
env->ReleaseStringUTFChars(configDir, ConfigDir);
|
||||
|
||||
return (ret == 0) ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
/* Find windows by title implementation (unchanged) */
|
||||
|
||||
#define MAX_RESULTS 128
|
||||
|
||||
typedef struct {
|
||||
wchar_t title[256];
|
||||
DWORD pid;
|
||||
} WindowData;
|
||||
|
||||
static wchar_t g_keyword[256];
|
||||
static WindowData g_results[MAX_RESULTS];
|
||||
static int g_count = 0;
|
||||
|
||||
extern "C" __declspec(dllexport)
|
||||
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
|
||||
wchar_t title[256];
|
||||
GetWindowTextW(hwnd, title, 256);
|
||||
if (wcslen(title) == 0) return TRUE;
|
||||
|
||||
if (wcsstr(title, g_keyword)) {
|
||||
DWORD pid = 0;
|
||||
GetWindowThreadProcessId(hwnd, &pid);
|
||||
|
||||
if (g_count < MAX_RESULTS && pid) {
|
||||
wcsncpy(g_results[g_count].title, title, 255);
|
||||
g_results[g_count].pid = pid;
|
||||
g_count++;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport)
|
||||
JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceagent_injector_InjectorNative_findWindowsByTitle
|
||||
(JNIEnv *env, jclass clazz, jstring keyword) {
|
||||
|
||||
const jchar *input = env->GetStringChars(keyword, NULL);
|
||||
wcsncpy(g_keyword, (const wchar_t *)input, 255);
|
||||
g_keyword[255] = L'\0';
|
||||
env->ReleaseStringChars(keyword, input);
|
||||
|
||||
g_count = 0;
|
||||
EnumWindows(EnumWindowsProc, 0);
|
||||
|
||||
jclass infoClass = env->FindClass("cn/xiaozhou233/juiceagent/injector/InjectorNative$WindowInfo");
|
||||
if (!infoClass) return NULL;
|
||||
|
||||
jmethodID ctor = env->GetMethodID(infoClass, "<init>", "(Ljava/lang/String;I)V");
|
||||
if (!ctor) return NULL;
|
||||
|
||||
jobjectArray array = env->NewObjectArray(g_count, infoClass, NULL);
|
||||
|
||||
for (int i = 0; i < g_count; i++) {
|
||||
jstring title = env->NewString((jchar*)g_results[i].title, wcslen(g_results[i].title));
|
||||
jobject obj = env->NewObject(infoClass, ctor, title, (jint)g_results[i].pid);
|
||||
env->SetObjectArrayElement(array, i, obj);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
File renamed without changes.
Reference in new issue
Block a user