Advertisement
Guest User

CreateRemoteThread Injection

a guest
Sep 5th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. std::string injectDLL(std::string dllPath, std::string windowName){
  2.     if (dllPath.empty() || windowName.empty())
  3.         return "[*] Input Box Empty";
  4.  
  5.     HWND window = FindWindowA(NULL, windowName.c_str());
  6.     if (window == NULL)
  7.         return "[*] Failed To Find Window";
  8.  
  9.     DWORD pid = 0;
  10.     GetWindowThreadProcessId(window, &pid);
  11.     if (pid == NULL)
  12.         return "[*] Failed To Get The PID";
  13.  
  14.     HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  15.     if (handle == NULL)
  16.         return "[*] Failed To Open The Process";
  17.  
  18.     LPVOID allocedMem = VirtualAllocEx(handle, NULL, dllPath.length(), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  19.     if (allocedMem == NULL)
  20.         return "[*] Failed To Allocate Memory";
  21.  
  22.     bool wroteFine = WriteProcessMemory(handle, allocedMem, dllPath.c_str(), dllPath.length(), NULL);
  23.     if (wroteFine == NULL)
  24.         return "[*] Failed To Write To Memory";
  25.  
  26.     HMODULE k32dll = GetModuleHandleA("kernel32");
  27.     if (k32dll == NULL)
  28.         return "[*] Failed To Grab A Handle To Kernel32";
  29.  
  30.     FARPROC loadLibraryFunct = GetProcAddress(k32dll, "LoadLibraryA");
  31.     if (loadLibraryFunct == NULL)
  32.         return "[*] Failed To Get The LoadLibraryA Address";
  33.  
  34.     if (!CreateRemoteThread(handle, NULL, NULL, (LPTHREAD_START_ROUTINE)loadLibraryFunct, allocedMem, NULL, NULL)){
  35.         VirtualFreeEx(handle, allocedMem, dllPath.length(), MEM_RELEASE);
  36.         CloseHandle(handle);
  37.         return "[*] Injection Failed";
  38.     }
  39.  
  40.     else{
  41.         VirtualFreeEx(handle, allocedMem, dllPath.length(), MEM_RELEASE);
  42.         CloseHandle(handle);
  43.         return "[*] Injection Successful";
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement