inject-library.c 4.4 KB

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