inject-library.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <windows.h>
  2. #include <stdbool.h>
  3. #include <util/windows/obfuscate.h>
  4. #include "inject-library.h"
  5. typedef HANDLE(WINAPI *create_remote_thread_t)(HANDLE, LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID,
  6. DWORD, LPDWORD);
  7. typedef BOOL(WINAPI *write_process_memory_t)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T *);
  8. typedef LPVOID(WINAPI *virtual_alloc_ex_t)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
  9. typedef BOOL(WINAPI *virtual_free_ex_t)(HANDLE, LPVOID, SIZE_T, DWORD);
  10. int inject_library_obf(HANDLE process, const wchar_t *dll, const char *create_remote_thread_obf, uint64_t obf1,
  11. const char *write_process_memory_obf, uint64_t obf2, const char *virtual_alloc_ex_obf,
  12. uint64_t obf3, const char *virtual_free_ex_obf, uint64_t obf4, const char *load_library_w_obf,
  13. uint64_t obf5)
  14. {
  15. int ret = INJECT_ERROR_UNLIKELY_FAIL;
  16. DWORD last_error = 0;
  17. bool success = false;
  18. size_t written_size;
  19. DWORD thread_id;
  20. HANDLE thread = NULL;
  21. size_t size;
  22. void *mem;
  23. /* -------------------------------- */
  24. HMODULE kernel32 = GetModuleHandleW(L"KERNEL32");
  25. create_remote_thread_t create_remote_thread;
  26. write_process_memory_t write_process_memory;
  27. virtual_alloc_ex_t virtual_alloc_ex;
  28. virtual_free_ex_t virtual_free_ex;
  29. FARPROC load_library_w;
  30. create_remote_thread = (create_remote_thread_t)ms_get_obfuscated_func(kernel32, create_remote_thread_obf, obf1);
  31. write_process_memory = (write_process_memory_t)ms_get_obfuscated_func(kernel32, write_process_memory_obf, obf2);
  32. virtual_alloc_ex = (virtual_alloc_ex_t)ms_get_obfuscated_func(kernel32, virtual_alloc_ex_obf, obf3);
  33. virtual_free_ex = (virtual_free_ex_t)ms_get_obfuscated_func(kernel32, virtual_free_ex_obf, obf4);
  34. load_library_w = (FARPROC)ms_get_obfuscated_func(kernel32, load_library_w_obf, obf5);
  35. /* -------------------------------- */
  36. size = (wcslen(dll) + 1) * sizeof(wchar_t);
  37. mem = virtual_alloc_ex(process, NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  38. if (!mem) {
  39. goto fail;
  40. }
  41. success = write_process_memory(process, mem, dll, size, &written_size);
  42. if (!success) {
  43. goto fail;
  44. }
  45. thread = create_remote_thread(process, NULL, 0, (LPTHREAD_START_ROUTINE)load_library_w, mem, 0, &thread_id);
  46. if (!thread) {
  47. goto fail;
  48. }
  49. if (WaitForSingleObject(thread, 4000) == WAIT_OBJECT_0) {
  50. DWORD code;
  51. GetExitCodeThread(thread, &code);
  52. ret = (code != 0) ? 0 : INJECT_ERROR_INJECT_FAILED;
  53. SetLastError(0);
  54. }
  55. fail:
  56. if (ret == INJECT_ERROR_UNLIKELY_FAIL) {
  57. last_error = GetLastError();
  58. }
  59. if (thread) {
  60. CloseHandle(thread);
  61. }
  62. if (mem) {
  63. virtual_free_ex(process, mem, 0, MEM_RELEASE);
  64. }
  65. if (last_error != 0) {
  66. SetLastError(last_error);
  67. }
  68. return ret;
  69. }
  70. /* ------------------------------------------------------------------------- */
  71. typedef HHOOK(WINAPI *set_windows_hook_ex_t)(int, HOOKPROC, HINSTANCE, DWORD);
  72. #define RETRY_INTERVAL_MS 500
  73. #define TOTAL_RETRY_TIME_MS 4000
  74. #define RETRY_COUNT (TOTAL_RETRY_TIME_MS / RETRY_INTERVAL_MS)
  75. int inject_library_safe_obf(DWORD thread_id, const wchar_t *dll, const char *set_windows_hook_ex_obf, uint64_t obf1)
  76. {
  77. HMODULE user32 = GetModuleHandleW(L"USER32");
  78. set_windows_hook_ex_t set_windows_hook_ex;
  79. HMODULE lib = LoadLibraryW(dll);
  80. HOOKPROC proc;
  81. HHOOK hook;
  82. size_t i;
  83. if (!lib || !user32) {
  84. return INJECT_ERROR_UNLIKELY_FAIL;
  85. }
  86. #ifdef _WIN64
  87. proc = (HOOKPROC)GetProcAddress(lib, "dummy_debug_proc");
  88. #else
  89. proc = (HOOKPROC)GetProcAddress(lib, "_dummy_debug_proc@12");
  90. #endif
  91. if (!proc) {
  92. return INJECT_ERROR_UNLIKELY_FAIL;
  93. }
  94. set_windows_hook_ex = (set_windows_hook_ex_t)ms_get_obfuscated_func(user32, set_windows_hook_ex_obf, obf1);
  95. hook = set_windows_hook_ex(WH_GETMESSAGE, proc, lib, thread_id);
  96. if (!hook) {
  97. return GetLastError();
  98. }
  99. /* SetWindowsHookEx does not inject the library in to the target
  100. * process unless the event associated with it has occurred, so
  101. * repeatedly send the hook message to start the hook at small
  102. * intervals to signal to SetWindowsHookEx to process the message and
  103. * therefore inject the library in to the target process. Repeating
  104. * this is mostly just a precaution. */
  105. for (i = 0; i < RETRY_COUNT; i++) {
  106. Sleep(RETRY_INTERVAL_MS);
  107. PostThreadMessage(thread_id, WM_USER + 432, 0, (LPARAM)hook);
  108. }
  109. return 0;
  110. }