From a0e79dcea09ac52078f2afa48d095acdbac5ed2f Mon Sep 17 00:00:00 2001 From: mkrsym1 Date: Sun, 25 Jun 2023 12:32:19 +0300 Subject: [PATCH] Major injector refactoring --- build.sh | 4 +- injector/include/{gpa.inc => gpa.asm} | 0 injector/include/inject.h | 11 +++++ injector/launcher_payload/meson.build | 21 --------- injector/meson.build | 45 ++++++++++++++----- injector/{launcher_payload => }/src/dll.c | 8 ++-- injector/src/{injector.c => exe.c} | 8 ++-- .../src/payload.asm => src/game_p.asm} | 2 +- .../{include/injshared.h => src/inject.c} | 12 ++--- injector/src/{payload.asm => launcher_p.asm} | 2 +- meson.build | 2 +- 11 files changed, 60 insertions(+), 55 deletions(-) rename injector/include/{gpa.inc => gpa.asm} (100%) create mode 100644 injector/include/inject.h delete mode 100644 injector/launcher_payload/meson.build rename injector/{launcher_payload => }/src/dll.c (90%) rename injector/src/{injector.c => exe.c} (92%) rename injector/{launcher_payload/src/payload.asm => src/game_p.asm} (99%) rename injector/{include/injshared.h => src/inject.c} (90%) rename injector/src/{payload.asm => launcher_p.asm} (97%) diff --git a/build.sh b/build.sh index 260d93b..4b27e02 100644 --- a/build.sh +++ b/build.sh @@ -13,12 +13,12 @@ rm -f jadeite.zip rm -rf out sh setup.sh --buildtype=release -ninja -C build +meson compile -C build mkdir out cp ./build/injector/jadeite.exe ./out -cp ./build/injector/launcher_payload/launcher_payload.dll ./out +cp ./build/injector/launcher_payload.dll ./out cp ./build/game_payload/game_payload.dll ./out cp ./LICENSE.txt ./out diff --git a/injector/include/gpa.inc b/injector/include/gpa.asm similarity index 100% rename from injector/include/gpa.inc rename to injector/include/gpa.asm diff --git a/injector/include/inject.h b/injector/include/inject.h new file mode 100644 index 0000000..cccb072 --- /dev/null +++ b/injector/include/inject.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +#define EPFX "__JADEITE_" + +#define ENV_EXE_PATH EPFX"TARGET_EXE_PATH" +#define ENV_DLL_PATH EPFX"INJECT_DLL_PATH" +#define ENV_PROC_CMD EPFX"PROCESS_COMMAND" + +void inject(HANDLE process, const void *payload, size_t payloadSize, const char *dllPath); diff --git a/injector/launcher_payload/meson.build b/injector/launcher_payload/meson.build deleted file mode 100644 index 35fb410..0000000 --- a/injector/launcher_payload/meson.build +++ /dev/null @@ -1,21 +0,0 @@ -# Assemble the payload that will be injected into the game -l_payload_bin = asm_gen.process( - 'src/payload.asm', - extra_args: [ str_include_dir ] -) - -# Embed it into the library -l_res_files = custom_target( - 'lpayload.[oh]', - output: [ 'lpayload.o', 'lpayload.h' ], - input: [ l_payload_bin ], - command: [ gen_res, './injector/launcher_payload', '@OUTPUT0@', '@OUTPUT1@', '@INPUT@' ] -) - -shared_library( - 'launcher_payload', - 'src/dll.c', - l_res_files, - include_directories: include_dir, - name_prefix: '' -) diff --git a/injector/meson.build b/injector/meson.build index 7a755fb..8335fc3 100644 --- a/injector/meson.build +++ b/injector/meson.build @@ -1,27 +1,48 @@ include_dir = include_directories('include') str_include_dir = join_paths(meson.current_source_dir(), 'include') -# Assemble the payload that will be injected into the launcher -inj_payload_bin = asm_gen.process( - 'src/payload.asm', - extra_args: [ str_include_dir ] +# Assemble the payloads +launcher_payload_bin = asm_gen.process( + 'src/launcher_p.asm', + extra_args: [ '-i', str_include_dir ] ) -# Embed it into the library -inj_res_files = custom_target( - 'ipayload.[oh]', - output: [ 'ipayload.o', 'ipayload.h' ], - input: [ inj_payload_bin ], +game_payload_bin = asm_gen.process( + 'src/game_p.asm', + extra_args: [ '-i', str_include_dir ] +) + +# Embed them into .o files +exe_res_files = custom_target( + 'launcher_p.[oh]', + output: [ 'launcher_p.o', 'launcher_p.h' ], + input: [ launcher_payload_bin ], + command: [ gen_res, './injector', '@OUTPUT0@', '@OUTPUT1@', '@INPUT@' ] +) + +dll_res_files = custom_target( + 'game_p.[oh]', + output: [ 'game_p.o', 'game_p.h' ], + input: [ game_payload_bin ], command: [ gen_res, './injector', '@OUTPUT0@', '@OUTPUT1@', '@INPUT@' ] ) # Main injector exe executable( 'jadeite', - 'src/injector.c', - inj_res_files, + 'src/exe.c', + 'src/inject.c', + exe_res_files, include_directories: include_dir, name_prefix: '' ) -subdir('launcher_payload') +# Dll that will be injected into the launcher +shared_library( + 'launcher_payload', + 'src/dll.c', + 'src/inject.c', + dll_res_files, + include_directories: include_dir, + name_prefix: '' +) diff --git a/injector/launcher_payload/src/dll.c b/injector/src/dll.c similarity index 90% rename from injector/launcher_payload/src/dll.c rename to injector/src/dll.c index 7475ecf..55fbc40 100644 --- a/injector/launcher_payload/src/dll.c +++ b/injector/src/dll.c @@ -1,8 +1,8 @@ #include -#include +#include -#include +#include BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { // Only listen for attach @@ -52,8 +52,8 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) { } // Inject - void *payloadStart = &_binary_lpayload_o_p_payload_bin_start; - size_t payloadSize = (size_t)&_binary_lpayload_o_p_payload_bin_size; + void *payloadStart = &_binary_game_p_o_p_game_p_bin_start; + size_t payloadSize = (size_t)&_binary_game_p_o_p_game_p_bin_size; inject(pi.hProcess, payloadStart, payloadSize, injectDll); // Optional: wait for user input before resuming (useful for debugging) diff --git a/injector/src/injector.c b/injector/src/exe.c similarity index 92% rename from injector/src/injector.c rename to injector/src/exe.c index 22be744..f660866 100644 --- a/injector/src/injector.c +++ b/injector/src/exe.c @@ -1,8 +1,8 @@ #include -#include +#include -#include +#include const char LAUNCHER_INJECT_DLL[] = "launcher_payload.dll"; const char GAME_INJECT_DLL[] = "game_payload.dll"; @@ -108,8 +108,8 @@ int main(int argc, char **argv) { printf("Started launcher process (%ld)\n", pi.dwProcessId); // Inject - void *payloadStart = &_binary_ipayload_o_p_payload_bin_start; - size_t payloadSize = (size_t)&_binary_ipayload_o_p_payload_bin_size; // yes this is valid + void *payloadStart = &_binary_launcher_p_o_p_launcher_p_bin_start; + size_t payloadSize = (size_t)&_binary_launcher_p_o_p_launcher_p_bin_size; // yes this is valid inject(pi.hProcess, payloadStart, payloadSize, launcherPayloadPath); // Resume the process diff --git a/injector/launcher_payload/src/payload.asm b/injector/src/game_p.asm similarity index 99% rename from injector/launcher_payload/src/payload.asm rename to injector/src/game_p.asm index 16d7a87..0a6dbea 100644 --- a/injector/launcher_payload/src/payload.asm +++ b/injector/src/game_p.asm @@ -63,7 +63,7 @@ main: ; Replacement entry point ret -%include "gpa.inc" +%include "gpa.asm" ; Strings diff --git a/injector/include/injshared.h b/injector/src/inject.c similarity index 90% rename from injector/include/injshared.h rename to injector/src/inject.c index 77baad3..ecf2256 100644 --- a/injector/include/injshared.h +++ b/injector/src/inject.c @@ -1,10 +1,4 @@ -#include - -#define EPFX "__JADEITE_" - -const char ENV_EXE_PATH[] = EPFX"TARGET_EXE_PATH"; -const char ENV_DLL_PATH[] = EPFX"INJECT_DLL_PATH"; -const char ENV_PROC_CMD[] = EPFX"PROCESS_COMMAND"; +#include static inline void write_protected_process_memory(HANDLE process, void *address, const void *buf, size_t size) { DWORD oldProtect; @@ -16,8 +10,8 @@ static inline void write_protected_process_memory(HANDLE process, void *address, VirtualProtectEx(process, address, size, oldProtect, &oldProtect); } -static inline void inject(HANDLE process, const void *payload, size_t payloadSize, const char *dllPath) { - size_t _; +void inject(HANDLE process, const void *payload, size_t payloadSize, const char *dllPath) { + size_t _; // Contrary to the docs, {Write,Read}ProcessMemory likes to crash if the last arg is NULL // Inject the loader into the module size_t dllPathLen = strlen(dllPath) + 1; diff --git a/injector/src/payload.asm b/injector/src/launcher_p.asm similarity index 97% rename from injector/src/payload.asm rename to injector/src/launcher_p.asm index 434b765..89aa55c 100644 --- a/injector/src/payload.asm +++ b/injector/src/launcher_p.asm @@ -28,7 +28,7 @@ main: ; Replacement entry point ret -%include "gpa.inc" +%include "gpa.asm" ; Strings diff --git a/meson.build b/meson.build index 75d80ff..faa2a48 100644 --- a/meson.build +++ b/meson.build @@ -8,7 +8,7 @@ asm_gen = generator( nasm, output: '@BASENAME@.bin', arguments: [ - '-i', '@EXTRA_ARGS@', + '@EXTRA_ARGS@', '-f', 'bin', '@INPUT@', '-o', '@OUTPUT@'