From e4501f1cf1edb0827c6a76c4d56639941806f2ae Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 16 Sep 2025 18:53:04 +0800 Subject: [PATCH] Update - [+] Inject Native --- CMakeLists.txt | 13 +++- includes/JuiceLoader/InjectorNative.h | 21 ++++++ src/libinjector.c | 93 +++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 includes/JuiceLoader/InjectorNative.h create mode 100644 src/libinjector.c diff --git a/CMakeLists.txt b/CMakeLists.txt index df33d02..527af1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,4 +32,15 @@ target_include_directories(juiceloader PRIVATE ${INCLUDES}/jvm ${INCLUDES}/jvm/win32 ${INCLUDES}/JuiceLoader) -target_link_libraries(juiceloader PRIVATE jvm) \ No newline at end of file +target_link_libraries(juiceloader PRIVATE jvm) + +# libinjector +link_directories("$ENV{JAVA_HOME}/lib") +add_library(libinjector SHARED ${SRC}/libinjector.c) +target_include_directories(libinjector PRIVATE + ${INCLUDES}/ReflectiveDLLInjection + ${INCLUDES}/jvm + ${INCLUDES}/jvm/win32 + ${INCLUDES}/JuiceLoader) +target_link_libraries(libinjector PRIVATE jvm) +target_link_libraries(libinjector PRIVATE ReflectiveDLLInjection) \ No newline at end of file diff --git a/includes/JuiceLoader/InjectorNative.h b/includes/JuiceLoader/InjectorNative.h new file mode 100644 index 0000000..1405a65 --- /dev/null +++ b/includes/JuiceLoader/InjectorNative.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class cn_xiaozhou233_orangex_injector_InjectorNative */ + +#ifndef _Included_cn_xiaozhou233_orangex_injector_InjectorNative +#define _Included_cn_xiaozhou233_orangex_injector_InjectorNative +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: cn_xiaozhou233_orangex_injector_InjectorNative + * Method: inject + * Signature: (ILjava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_orangex_injector_InjectorNative_inject + (JNIEnv *, jobject, jint, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/libinjector.c b/src/libinjector.c new file mode 100644 index 0000000..947aef9 --- /dev/null +++ b/src/libinjector.c @@ -0,0 +1,93 @@ +#include "windows.h" +#include +#include +#include "LoadLibraryR.h" +#include "InjectorNative.h" + +#define WIN_X64 +#define BREAK_WITH_ERROR( e ) { printf( "[-] %s. Error=%d", e, GetLastError() ); break; } + +int inject(int pid, char *path){ + HANDLE hFile = NULL; + HANDLE hModule = NULL; + HANDLE hProcess = NULL; + HANDLE hToken = NULL; + LPVOID lpBuffer = NULL; + DWORD dwLength = 0; + DWORD dwBytesRead = 0; + DWORD dwProcessId = 0; + TOKEN_PRIVILEGES priv = {0}; + char * cpDllFile = NULL; + + cpDllFile = path; + dwProcessId = pid; + + do { + hFile = CreateFileA( cpDllFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); + if( hFile == INVALID_HANDLE_VALUE ) + BREAK_WITH_ERROR( "Failed to open the DLL file" ); + + dwLength = GetFileSize( hFile, NULL ); + if( dwLength == INVALID_FILE_SIZE || dwLength == 0 ) + BREAK_WITH_ERROR( "Failed to get the DLL file size" ); + + lpBuffer = HeapAlloc( GetProcessHeap(), 0, dwLength ); + if( !lpBuffer ) + BREAK_WITH_ERROR( "Failed to get the DLL file size" ); + + if( ReadFile( hFile, lpBuffer, dwLength, &dwBytesRead, NULL ) == FALSE ) + BREAK_WITH_ERROR( "Failed to alloc a buffer!" ); + + 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 ); + } + + 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 the target process" ); + + hModule = LoadRemoteLibraryR( hProcess, lpBuffer, dwLength, NULL ); + if( !hModule ) + BREAK_WITH_ERROR( "Failed to inject the DLL" ); + + printf( "[+] Injected the '%s' DLL into process %d.", cpDllFile, dwProcessId ); + + WaitForSingleObject( hModule, -1 ); + + } while( 0 ); + + if( lpBuffer ) + HeapFree( GetProcessHeap(), 0, lpBuffer ); + + if( hProcess ) + CloseHandle( hProcess ); + + return 0; +} + +int inject_pid(int pid){ + const char* dir = getenv("JuiceAgent_Dir"); + if(!dir) return -1; + + char path[MAX_PATH]; + sprintf(path, "%s\\libagent.dll", dir); + return inject(pid, path); +} + +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_orangex_injector_InjectorNative_inject + (JNIEnv *env, jobject obj, jint pid, jstring path){ + const char* nativePath = (*env)->GetStringUTFChars(env, path, 0); + int ret = inject(pid, (char*)nativePath); + (*env)->ReleaseStringUTFChars(env, path, nativePath); + + if(ret == 0) + return JNI_TRUE; + return JNI_FALSE; +}