inject-helper.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wchar.h>
  4. #include <windows.h>
  5. #include <shellapi.h>
  6. #include <stdbool.h>
  7. #ifdef OBS_LEGACY
  8. #include "../../../libobs/util/windows/obfuscate.h"
  9. #include "../inject-library.h"
  10. #else
  11. #include <util/windows/obfuscate.h>
  12. #include <inject-library.h>
  13. #endif
  14. #if defined(_MSC_VER) && !defined(inline)
  15. #define inline __inline
  16. #endif
  17. static void load_debug_privilege(void)
  18. {
  19. const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
  20. TOKEN_PRIVILEGES tp;
  21. HANDLE token;
  22. LUID val;
  23. if (!OpenProcessToken(GetCurrentProcess(), flags, &token)) {
  24. return;
  25. }
  26. if (!!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &val)) {
  27. tp.PrivilegeCount = 1;
  28. tp.Privileges[0].Luid = val;
  29. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  30. AdjustTokenPrivileges(token, false, &tp, sizeof(tp), NULL, NULL);
  31. }
  32. CloseHandle(token);
  33. }
  34. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle, DWORD process_id)
  35. {
  36. HANDLE(WINAPI * open_process_proc)(DWORD, BOOL, DWORD);
  37. open_process_proc = ms_get_obfuscated_func(GetModuleHandleW(L"KERNEL32"), "HxjcQrmkb|~", 0xc82efdf78201df87);
  38. return open_process_proc(desired_access, inherit_handle, process_id);
  39. }
  40. static inline int inject_library(HANDLE process, const wchar_t *dll)
  41. {
  42. return inject_library_obf(process, dll, "E}mo|d[cefubWk~bgk", 0x7c3371986918e8f6, "Rqbr`T{cnor{Bnlgwz",
  43. 0x81bf81adc9456b35, "]`~wrl`KeghiCt", 0xadc6a7b9acd73c9b, "Zh}{}agHzfd@{",
  44. 0x57135138eb08ff1c, "DnafGhj}l~sX", 0x350bfacdf81b2018);
  45. }
  46. static inline int inject_library_safe(DWORD thread_id, const wchar_t *dll)
  47. {
  48. return inject_library_safe_obf(thread_id, dll, "[bs^fbkmwuKfmfOvI", 0xEAD293602FCF9778ULL);
  49. }
  50. static inline int inject_library_full(DWORD process_id, const wchar_t *dll)
  51. {
  52. HANDLE process = open_process(PROCESS_ALL_ACCESS, false, process_id);
  53. int ret;
  54. if (process) {
  55. ret = inject_library(process, dll);
  56. CloseHandle(process);
  57. } else {
  58. ret = INJECT_ERROR_OPEN_PROCESS_FAIL;
  59. }
  60. return ret;
  61. }
  62. static int inject_helper(wchar_t *argv[], const wchar_t *dll)
  63. {
  64. DWORD id;
  65. DWORD use_safe_inject;
  66. use_safe_inject = wcstol(argv[2], NULL, 10);
  67. id = wcstol(argv[3], NULL, 10);
  68. if (id == 0) {
  69. return INJECT_ERROR_INVALID_PARAMS;
  70. }
  71. return use_safe_inject ? inject_library_safe(id, dll) : inject_library_full(id, dll);
  72. }
  73. int main(void)
  74. {
  75. wchar_t dll_path[MAX_PATH];
  76. LPWSTR pCommandLineW;
  77. int argc;
  78. LPWSTR *argv;
  79. int ret = INJECT_ERROR_INVALID_PARAMS;
  80. SetErrorMode(SEM_FAILCRITICALERRORS);
  81. load_debug_privilege();
  82. pCommandLineW = GetCommandLineW();
  83. argv = CommandLineToArgvW(pCommandLineW, &argc);
  84. if (argv) {
  85. if (argc == 4) {
  86. if (GetModuleFileNameW(NULL, dll_path, MAX_PATH))
  87. ret = inject_helper(argv, argv[1]);
  88. }
  89. LocalFree(argv);
  90. }
  91. return ret;
  92. }