inject-library.c 4.2 KB

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