Advertisement
Wolfrost

Injector.h

Mar 3rd, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Injector.h
  2.  
  3. #include "stdafx.h"
  4.  
  5. enum InjectMethod
  6. {
  7.     LOADLIBRARY,
  8.     MANUAL_MAP
  9. };
  10.  
  11. class Injector
  12. {
  13. protected:
  14.  
  15.     std::string DLLPath;
  16.     std::string ProcessName;
  17.     HANDLE hProcess;
  18.     DWORD dwPID;
  19.     InjectMethod Method;
  20.  
  21.     typedef HMODULE (WINAPI* pLoadLibraryA)(LPCSTR);
  22.     typedef FARPROC (WINAPI* pGetProcAddress)(HMODULE,LPCSTR);
  23.     typedef BOOL (WINAPI* pDLLMain)(HMODULE,DWORD,PVOID);
  24.  
  25.     bool AttachProcess(char* ProcessName)
  26.     {
  27.         HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  28.         PROCESSENTRY32 procEntry;
  29.         procEntry.dwSize = sizeof(procEntry);
  30.  
  31.         const WCHAR* procNameChar;
  32.         int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
  33.         procNameChar = new WCHAR[nChars];
  34.         MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
  35.  
  36.         do
  37.             if (!wcscmp(procEntry.szExeFile, procNameChar))
  38.             {
  39.                 dwPID = procEntry.th32ProcessID;
  40.                 CloseHandle(hPID);
  41.                 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
  42.                 if (hProcess == INVALID_HANDLE_VALUE) return false;
  43.                 return true;
  44.             }
  45.         while (Process32Next(hPID, &procEntry));
  46.  
  47.         CloseHandle(hPID);
  48.         return false;
  49.     }
  50.  
  51.     typedef struct _MANUAL_INJECT
  52.     {
  53.         PVOID ImageBase;
  54.         PIMAGE_NT_HEADERS NtHeaders;
  55.         PIMAGE_BASE_RELOCATION BaseRelocation;
  56.         PIMAGE_IMPORT_DESCRIPTOR ImportDirectory;
  57.         pLoadLibraryA fnLoadLibraryA;
  58.         pGetProcAddress fnGetProcAddress;
  59.     } MANUAL_INJECT,*PMANUAL_INJECT;
  60.  
  61. public:
  62.  
  63.     Injector( std::string DllPath, std::string ProcessName, InjectMethod Method)
  64.     {
  65.         this->DLLPath = DllPath;
  66.         this->ProcessName = ProcessName;
  67.         this->Method = Method;
  68.     }
  69.     ~Injector()
  70.     {
  71.         CloseHandle(hProcess);
  72.     }
  73.  
  74.     bool Inject()
  75.     {
  76.         switch (Method)
  77.         {
  78.         case LOADLIBRARY:
  79.             {
  80.                 /// LoadLibrary method
  81.                 AttachProcess(ProcessName.c_str());
  82.                 HANDLE DllHandle = GetModuleHandleA("Kernel32");
  83.                 if (DllHandle == INVALID_HANDLE_VALUE) return false;
  84.                 FARPROC LoadLibraryAddr = GetProcAddress(DllHandle, "LoadLibraryA");
  85.                 if (LoadLibraryAddr == NULL) return false;
  86.                 LPVOID BaseAddr = VirtualAllocEx( hProcess, NULL, 256, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE );
  87.                 if (BaseAddr == NULL) return false;
  88.                 bool success = WriteProcessMemory( hProcess, BaseAddr, DLLPath.c_str(), sizeof(DLLPath.c_str())+1, NULL ) == TRUE;
  89.                 if (!success) return false;
  90.                 HANDLE Thread = CreateRemoteThread( hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, NULL, NULL);
  91.                 if (Thread==NULL) return false;
  92.                 return true;
  93.             }
  94.         break;
  95.         case MANUAL_MAP:
  96.             {
  97.                 /// Manual Map
  98.  
  99.             }
  100.         break;
  101.         }
  102.     }
  103.  
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement