Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Injector.h
- #include "stdafx.h"
- enum InjectMethod
- {
- LOADLIBRARY,
- MANUAL_MAP
- };
- class Injector
- {
- protected:
- std::string DLLPath;
- std::string ProcessName;
- HANDLE hProcess;
- DWORD dwPID;
- InjectMethod Method;
- typedef HMODULE (WINAPI* pLoadLibraryA)(LPCSTR);
- typedef FARPROC (WINAPI* pGetProcAddress)(HMODULE,LPCSTR);
- typedef BOOL (WINAPI* pDLLMain)(HMODULE,DWORD,PVOID);
- bool AttachProcess(char* ProcessName)
- {
- HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- PROCESSENTRY32 procEntry;
- procEntry.dwSize = sizeof(procEntry);
- const WCHAR* procNameChar;
- int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
- procNameChar = new WCHAR[nChars];
- MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
- do
- if (!wcscmp(procEntry.szExeFile, procNameChar))
- {
- dwPID = procEntry.th32ProcessID;
- CloseHandle(hPID);
- hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
- if (hProcess == INVALID_HANDLE_VALUE) return false;
- return true;
- }
- while (Process32Next(hPID, &procEntry));
- CloseHandle(hPID);
- return false;
- }
- typedef struct _MANUAL_INJECT
- {
- PVOID ImageBase;
- PIMAGE_NT_HEADERS NtHeaders;
- PIMAGE_BASE_RELOCATION BaseRelocation;
- PIMAGE_IMPORT_DESCRIPTOR ImportDirectory;
- pLoadLibraryA fnLoadLibraryA;
- pGetProcAddress fnGetProcAddress;
- } MANUAL_INJECT,*PMANUAL_INJECT;
- public:
- Injector( std::string DllPath, std::string ProcessName, InjectMethod Method)
- {
- this->DLLPath = DllPath;
- this->ProcessName = ProcessName;
- this->Method = Method;
- }
- ~Injector()
- {
- CloseHandle(hProcess);
- }
- bool Inject()
- {
- switch (Method)
- {
- case LOADLIBRARY:
- {
- /// LoadLibrary method
- AttachProcess(ProcessName.c_str());
- HANDLE DllHandle = GetModuleHandleA("Kernel32");
- if (DllHandle == INVALID_HANDLE_VALUE) return false;
- FARPROC LoadLibraryAddr = GetProcAddress(DllHandle, "LoadLibraryA");
- if (LoadLibraryAddr == NULL) return false;
- LPVOID BaseAddr = VirtualAllocEx( hProcess, NULL, 256, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE );
- if (BaseAddr == NULL) return false;
- bool success = WriteProcessMemory( hProcess, BaseAddr, DLLPath.c_str(), sizeof(DLLPath.c_str())+1, NULL ) == TRUE;
- if (!success) return false;
- HANDLE Thread = CreateRemoteThread( hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, NULL, NULL);
- if (Thread==NULL) return false;
- return true;
- }
- break;
- case MANUAL_MAP:
- {
- /// Manual Map
- }
- break;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement