[Update] injector and backspace :(

This commit is contained in:
xiaozhou233 committed 2025-11-10 23:18:09 +08:00
1 parent ffd42c5e30
commit 4c184cbb95
4 files changed
+151 -107

No files matched your search

+6 -1
View File
@@ -1,3 +1,8 @@
# src/executable/CMakeLists.txt
add_executable(injector_executable injector.cpp)
add_executable(injector_executable injector.c)
target_include_directories(injector_executable PRIVATE
${CMAKE_SOURCE_DIR}/includes
${CMAKE_SOURCE_DIR}/includes/JuiceAgent
)
+117
View File
@@ -0,0 +1,117 @@
/* injector_client.c
Console injector client in C.
Usage:
injector_client.exe <pid> <dll_path_to_inject> [injector_lib_path]
If pid not provided, program runs "jps -l" and prompts for PID.
*/
/* Comments in code are in English as requested. */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PATH_LEN 4096
/* Execute "jps -l" to help find JVM PIDs. */
static void ExecuteJps(void) {
/* Use _popen to run jps -l and print output */
FILE *pipe = _popen("jps -l", "r");
if (!pipe) {
printf("Error: failed to run jps -l (is JDK's bin on PATH?).\n");
return;
}
printf("=== jps -l output ===\n");
char buf[256];
while (fgets(buf, sizeof(buf), pipe) != NULL) {
fputs(buf, stdout);
}
printf("=====================\n");
_pclose(pipe);
}
int main(int argc, char **argv) {
int pid = 0;
char dll_path[MAX_PATH_LEN] = {0};
char injector_lib_path[MAX_PATH_LEN] = {0};
char cwd[MAX_PATH_LEN] = {0};
/* If user provided pid and dll path as arguments, use them. */
if (argc >= 3) {
pid = atoi(argv[1]);
strncpy(dll_path, argv[2], MAX_PATH_LEN - 1);
if (argc >= 4) {
strncpy(injector_lib_path, argv[3], MAX_PATH_LEN - 1);
}
} else {
/* Show jps output to help user find JVM PID */
ExecuteJps();
printf("Input PID: ");
if (scanf("%d", &pid) != 1 || pid <= 0) {
printf("Invalid PID input.\n");
return 1;
}
/* Default dll_path = currentdir\\libagent.dll */
if (!GetCurrentDirectoryA(MAX_PATH_LEN, cwd)) {
printf("Warning: GetCurrentDirectory failed, using '.'\n");
strncpy(cwd, ".", MAX_PATH_LEN - 1);
}
snprintf(dll_path, MAX_PATH_LEN, "%s\\libagent.dll", cwd);
printf("Using DLL path: %s\n", dll_path);
}
if (pid <= 0) {
printf("Invalid or missing PID.\n");
return 1;
}
/* Load injector library */
HMODULE hLib = NULL;
if (injector_lib_path[0] != '\0') {
hLib = LoadLibraryA(injector_lib_path);
if (!hLib) {
printf("[-] Failed to load injector library: %s (Error=%lu)\n", injector_lib_path, GetLastError());
return 2;
}
} else {
/* Try to load by name from current dir / PATH */
hLib = LoadLibraryA("libinjector.dll");
if (!hLib) {
printf("[-] Failed to load libinjector.dll by name (Error=%lu).\n", GetLastError());
printf(" Provide full path as the 3rd argument if needed.\n");
return 2;
}
}
printf("[+] Loaded injector library.\n");
/* Define function pointer type (cdecl) - match the DLL export */
typedef int (__cdecl *inject_fn_t)(int, const char*, void*);
/* GetProcAddress for 'inject' */
FARPROC proc = GetProcAddress(hLib, "inject");
if (!proc) {
/* Try common alternative name */
proc = GetProcAddress(hLib, "Inject");
}
if (!proc) {
printf("[-] GetProcAddress failed for 'inject' (Error=%lu). Make sure your injector exports C symbol 'inject'.\n", GetLastError());
FreeLibrary(hLib);
return 3;
}
inject_fn_t inject = (inject_fn_t)proc;
printf("[*] Calling inject(pid=%d, dllPath=\"%s\", NULL)\n", pid, dll_path);
int ret = inject(pid, dll_path, NULL);
if (ret == 0) {
printf("[+] inject returned success (0)\n");
} else {
printf("[-] inject returned failure (%d)\n", ret);
}
FreeLibrary(hLib);
return (ret == 0) ? 0 : 4;
}
-79
View File
@@ -1,79 +0,0 @@
// injector_client.cpp
// Console injector client - uses main() for maximum compatibility.
// Usage:
// injector_client.exe <pid> <dll_path_to_inject> [injector_lib_path]
//
// Notes:
// - injector library must export a C symbol: extern "C" __declspec(dllexport) int inject(int, const char*, void*);
// - This client calls inject(pid, dllPath, NULL).
#include <windows.h>
#include <iostream>
#include <string>
int main(int argc, char** argv) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <pid> <dll_path_to_inject> [injector_lib_path]\n";
return 1;
}
// parse pid
int pid = atoi(argv[1]);
if (pid <= 0) {
std::cerr << "Invalid PID: " << argv[1] << "\n";
return 1;
}
// dll path (UTF-8 or ANSI depending on environment)
const char* dllPath = argv[2];
std::string dllPathStr = dllPath;
// load injector library
HMODULE hLib = NULL;
if (argc >= 4) {
const char* libPath = argv[3];
hLib = LoadLibraryA(libPath);
if (!hLib) {
std::cerr << "[-] Failed to load injector library: " << libPath << ", Error=" << GetLastError() << "\n";
return 2;
}
} else {
// try to load from current dir / PATH by name
hLib = LoadLibraryA("libinjector.dll");
if (!hLib) {
std::cerr << "[-] Failed to load libinjector.dll by name. Error=" << GetLastError() << "\n";
std::cerr << " Provide full path as the 3rd argument if needed.\n";
return 2;
}
}
std::cout << "[+] Loaded injector library\n";
// function pointer type: adjust calling convention if your DLL uses different one.
typedef int (__cdecl *inject_fn_t)(int, const char*, void*);
FARPROC p = GetProcAddress(hLib, "inject");
if (!p) {
// try some common alternatives
p = GetProcAddress(hLib, "Inject");
}
if (!p) {
std::cerr << "[-] GetProcAddress failed for 'inject'. Make sure your library exports 'inject' as C symbol.\n";
FreeLibrary(hLib);
return 3;
}
inject_fn_t inject = reinterpret_cast<inject_fn_t>(p);
std::cout << "[*] Calling inject(pid, dllPath, NULL)\n";
int result = inject(pid, dllPathStr.c_str(), NULL);
if (result == 0) {
std::cout << "[+] inject returned success (0)\n";
} else {
std::cout << "[-] inject returned failure (" << result << ")\n";
}
FreeLibrary(hLib);
return (result == 0) ? 0 : 4;
}
+28 -27
View File
@@ -10,6 +10,7 @@
#define WIN_X64
#define BREAK_WITH_ERROR( e ) { printf("[-] %s. Error=%l", e, GetLastError()); break; }
__declspec(dllexport)
int inject(int pid, char *path, InjectParams *params){
HANDLE hFile = NULL;
HANDLE hRemoteThread = NULL;
@@ -24,13 +25,13 @@ int inject(int pid, char *path, InjectParams *params){
BOOL bSuccess = FALSE;
printf("[*] libinjector Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
printf("[*] libinjector Version %d.%d Build %d\n", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER);
printf("Injector process pointer size = %zu bytes", sizeof(void*));
printf("Injector process pointer size = %zu bytes\n", sizeof(void*));
do {
if (!params) {
printf("[!] No parameters provided");
printf("[!] No parameters provided\n");
}
/* open DLL file */
@@ -40,20 +41,20 @@ int inject(int pid, char *path, InjectParams *params){
/* Debug helper: print the path being opened and its absolute path */
char fullPath[MAX_PATH] = {0};
if (GetFullPathNameA(path, MAX_PATH, fullPath, NULL) != 0) {
printf("Injector GetFullPathNameA success: %s", fullPath);
printf("Injector GetFullPathNameA success: %s\n", fullPath);
} else {
printf("[*] Injector GetFullPathNameA failed: %lu", GetLastError());
printf("[*] Injector GetFullPathNameA failed: %lu\n", GetLastError());
}
/* Before CreateFileA, print which file we are about to open */
printf("[*] About to open DLL: %s", path);
printf("[*] About to open DLL: %s\n", path);
/* after ReadFile (or after loading into lpBuffer), verify exports again on the buffer we actually read */
/* get file size */
dwLength = GetFileSize(hFile, NULL);
if (dwLength == INVALID_FILE_SIZE || dwLength == 0)
BREAK_WITH_ERROR("Failed to get DLL file size");
BREAK_WITH_ERROR("Failed to get DLL file size\n");
/* alloc local buffer and read file */
lpBuffer = HeapAlloc(GetProcessHeap(), 0, dwLength);
@@ -65,11 +66,11 @@ int inject(int pid, char *path, InjectParams *params){
/* print machine type of DLL buffer */
WORD dllMachine = get_pe_machine(lpBuffer, dwLength);
printf("[*] DLL PE machine: %s (0x%04x)", machine_to_str(dllMachine), dllMachine);
printf("[*] DLL PE machine: %s (0x%04x)\n", machine_to_str(dllMachine), dllMachine);
/* check if DLL has ReflectiveLoader export */
BOOL hasRL = has_reflective_loader_export(lpBuffer, dwLength);
printf("[*] DLL has ReflectiveLoader export: %s", hasRL ? "YES" : "NO");
printf("[*] DLL has ReflectiveLoader export: %s\n", hasRL ? "YES" : "NO");
/* enable SeDebugPrivilege (best-effort) */
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
@@ -89,46 +90,46 @@ int inject(int pid, char *path, InjectParams *params){
/* detect target architecture */
BOOL targetIsWow64 = FALSE;
if (!IsWow64Process(hProcess, &targetIsWow64)) {
printf("IsWow64Process failed");
printf("IsWow64Process failed\n");
} else {
printf("Is target process wow64 = %d (TRUE means 32-bit process on 64-bit OS)", targetIsWow64);
printf("Is target process wow64 = %d (TRUE means 32-bit process on 64-bit OS)\n", targetIsWow64);
}
/* compare architectures */
#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
printf("Injector compiled as: x64");
printf("Injector compiled as: x64\n");
#else
printf("Injector compiled as: x86");
printf("Injector compiled as: x86\n");
#endif
if (dllMachine == IMAGE_FILE_MACHINE_AMD64) {
printf("[*] DLL is x64");
printf("[*] DLL is x64\n");
#if !defined(_M_X64) && !defined(__x86_64__)
printf("[-] MISMATCH: injector is 32-bit but DLL is x64. This will fail.");
#endif
} else if (dllMachine == IMAGE_FILE_MACHINE_I386) {
printf("[*] DLL is x86");
printf("[*] DLL is x86\n");
#if defined(_M_X64) || defined(__x86_64__)
printf("[-] MISMATCH: injector is 64-bit but DLL is x86. This will fail.");
printf("[-] MISMATCH: injector is 64-bit but DLL is x86. This will fail.\n");
#endif
} else {
printf("Unknown DLL machine type; proceed with caution");
printf("Unknown DLL machine type; proceed with caution\n");
}
/* allocate remote memory for InjectParameters */
lpRemoteParam = VirtualAllocEx(hProcess, NULL, sizeof(InjectParams), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!lpRemoteParam)
BREAK_WITH_ERROR("VirtualAllocEx for remote param failed");
BREAK_WITH_ERROR("VirtualAllocEx for remote param failed\n");
printf("remote param addr = %p", lpRemoteParam);
printf("remote param addr = %p\n", lpRemoteParam);
/* write params to remote memory */
SIZE_T written = 0;
if (params) {
if (!WriteProcessMemory(hProcess, lpRemoteParam, params, sizeof(InjectParams), &written) || written != sizeof(InjectParams))
BREAK_WITH_ERROR("WriteProcessMemory for remote param failed");
BREAK_WITH_ERROR("WriteProcessMemory for remote param failed\n");
} else {
printf("No parameters provided; using null pointer");
printf("No parameters provided; using null pointer\n");
lpRemoteParam = NULL;
}
@@ -137,9 +138,9 @@ int inject(int pid, char *path, InjectParams *params){
InjectParams verify;
SIZE_T read = 0;
if (ReadProcessMemory(hProcess, lpRemoteParam, &verify, sizeof(InjectParams), &read)) {
printf("[*] Read back remote param, ConfigDir=%.*s", (int)sizeof(verify.ConfigDir), verify.ConfigDir);
printf("[*] Read back remote param, ConfigDir=%.*s\n", (int)sizeof(verify.ConfigDir), verify.ConfigDir);
} else {
printf("[-] ReadProcessMemory failed: %lu", GetLastError());
printf("[-] ReadProcessMemory failed: %lu\n", GetLastError());
}
}
@@ -147,20 +148,20 @@ int inject(int pid, char *path, InjectParams *params){
hRemoteThread = LoadRemoteLibraryR(hProcess, lpBuffer, dwLength, lpRemoteParam);
if (!hRemoteThread) {
/* library didn't set last error; print hint info we gathered */
printf("[-] LoadRemoteLibraryR returned NULL. GetLastError=%lu", GetLastError());
printf("[-] Diagnostic summary: dllMachine=0x%04x, hasReflectiveLoader=%d, targetIsWow64=%d",
printf("[-] LoadRemoteLibraryR returned NULL. GetLastError=%lu\n", GetLastError());
printf("[-] Diagnostic summary: dllMachine=0x%04x, hasReflectiveLoader=%d, targetIsWow64=%d\n",
dllMachine, hasRL ? 1 : 0, targetIsWow64 ? 1 : 0);
BREAK_WITH_ERROR("LoadRemoteLibraryR returned NULL");
}
printf("[+] DLL injected: '%s' into PID %d, remote param at %p", path, dwProcessId, lpRemoteParam);
printf("[+] DLL injected: '%s' into PID %d, remote param at %p\n", path, dwProcessId, lpRemoteParam);
/* wait for reflective loader thread to complete */
WaitForSingleObject(hRemoteThread, INFINITE);
/* get remote thread exit code */
DWORD exitCode = 0;
if (GetExitCodeThread(hRemoteThread, &exitCode))
printf("[+] Remote thread exit code: %lu", exitCode);
printf("[+] Remote thread exit code: %lu\n", exitCode);
bSuccess = TRUE;