Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Injector.h"
- HANDLE CInjector::GetProcessByName(const std::string& strProcessName)
- {
- PROCESSENTRY32 ProcessInfo;
- ProcessInfo.dwSize = sizeof(PROCESSENTRY32);
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (hSnapshot == INVALID_HANDLE_VALUE) return NULL;
- Process32First(hSnapshot, &ProcessInfo);
- if (strProcessName.find(ProcessInfo.szExeFile) != std::string::npos)
- {
- CloseHandle(hSnapshot);
- if (!ProcessInfo.th32ProcessID) return NULL;
- return OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessInfo.th32ProcessID);
- }
- while (Process32Next(hSnapshot, &ProcessInfo))
- {
- if (strProcessName.find(ProcessInfo.szExeFile) != std::string::npos)
- {
- CloseHandle(hSnapshot);
- if (!ProcessInfo.th32ProcessID) return NULL;
- //return OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION, PROCESS_VM_READ, ProcessInfo.th32ProcessID);
- return OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessInfo.th32ProcessID);
- }
- }
- CloseHandle(hSnapshot);
- return NULL;
- }
- bool CInjector::SetPrivilege(const std::string& strNamePrivilege, bool bEnableTF)
- {
- HANDLE hToken;
- LUID SeValue;
- TOKEN_PRIVILEGES TokenPrivileges;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- return false;
- if (!LookupPrivilegeValue(NULL, strNamePrivilege.c_str(), &SeValue))
- {
- CloseHandle(hToken);
- return false;
- }
- TokenPrivileges.PrivilegeCount = 1;
- TokenPrivileges.Privileges[0].Luid = SeValue;
- TokenPrivileges.Privileges[0].Attributes = bEnableTF ? SE_PRIVILEGE_ENABLED : 0;
- AdjustTokenPrivileges(hToken, FALSE, &TokenPrivileges, sizeof(TokenPrivileges), NULL, NULL);
- CloseHandle(hToken);
- return true;
- }
- bool CInjector::Inject(const std::string& strModulePath, const std::string& strProcessName)
- {
- LPVOID lpRemoteString, lpLoadLibrary;
- HANDLE hThread, hProcess;
- DWORD dwOutput;
- if (!SetPrivilege("SeDebugPrivilege", true)) return false;
- hProcess = GetProcessByName(strProcessName);
- if (!hProcess) return false;
- lpLoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
- if (!lpLoadLibrary) return false;
- lpRemoteString = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(strModulePath.c_str()), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
- if (!lpRemoteString) return false;
- if (!WriteProcessMemory(hProcess, (LPVOID)lpRemoteString, strModulePath.c_str(), strlen(strModulePath.c_str()), NULL))
- return false;
- hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)lpLoadLibrary, (LPVOID)lpRemoteString, NULL, NULL);
- if (!hThread) return false;
- else
- {
- while (GetExitCodeThread(hThread, &dwOutput))
- if (dwOutput != STILL_ACTIVE) break;
- }
- CloseHandle(hThread);
- CloseHandle(hProcess);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement