inject-helper.c 2.7 KB

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