-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector.cpp
51 lines (42 loc) · 1.35 KB
/
injector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "injector.h"
const char* BKSInjector::getDllPath(const char* dllName)
{
char* buffer = new char[MAX_PATH];
GetFullPathNameA(dllName, MAX_PATH, buffer, NULL);
return buffer;
}
void BKSInjector::inject(DWORD processID, const char* dllPath)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
if (hProcess == NULL)
{
std::cout << "Failed to open process" << std::endl;
return;
}
LPVOID dllPathAddress = VirtualAllocEx(hProcess, NULL, strlen(dllPath) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (dllPathAddress == NULL)
{
std::cout << "Failed to allocate memory in process" << std::endl;
CloseHandle(hProcess);
return;
}
if (!WriteProcessMemory(hProcess, dllPathAddress, dllPath, strlen(dllPath) + 1, NULL))
{
std::cout << "Failed to write memory in process" << std::endl;
VirtualFreeEx(hProcess, dllPathAddress, 0, MEM_RELEASE);
CloseHandle(hProcess);
return;
}
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, dllPathAddress, 0, NULL);
if (hThread == NULL)
{
std::cout << "Failed to create remote thread" << std::endl;
VirtualFreeEx(hProcess, dllPathAddress, 0, MEM_RELEASE);
CloseHandle(hProcess);
return;
}
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
VirtualFreeEx(hProcess, dllPathAddress, 0, MEM_RELEASE);
CloseHandle(hProcess);
}