-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLLInject.cpp
More file actions
80 lines (65 loc) · 2.29 KB
/
DLLInject.cpp
File metadata and controls
80 lines (65 loc) · 2.29 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "DLLInject.hpp"
#include <tlhelp32.h>
InjectResult injectDLL(
const std::string&& process_name,
const std::string&& dll_name,
const unsigned int poll_interval,
const unsigned int timeout)
{
// Find PID
DWORD process_id = 0;
unsigned int timer = 0;
while(process_id == 0)
{
if(timeout > 0 && timer > timeout)
return InjectResult::TIMEOUT;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(snapshot == INVALID_HANDLE_VALUE)
return InjectResult::UNKOWN;
PROCESSENTRY32 process_entry{};
process_entry.dwSize = sizeof(PROCESSENTRY32);
if(Process32First(snapshot, &process_entry))
{
do
{
std::string process = process_entry.szExeFile;
if(process == process_name)
{
process_id = process_entry.th32ProcessID;
break;
}
} while(Process32Next(snapshot, &process_entry));
}
CloseHandle(snapshot);
Sleep(poll_interval);
timer += poll_interval;
}
// Open target process
HANDLE process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id);
if(process_handle == nullptr)
return InjectResult::COULD_NOT_OPEN_PROCESS;
// Allocate space for the DLL name and a zero byte.
void* dll_address =
VirtualAllocEx(process_handle, nullptr, dll_name.size() + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(dll_address == nullptr)
return InjectResult::COULD_NOT_ALLOCATE_MEMORY;
// Write the DLL name into the allocated memory.
if(WriteProcessMemory(process_handle, dll_address, dll_name.data(), dll_name.size() + 1, nullptr) !=
TRUE)
return InjectResult::COULD_NOT_WRITE_PROCESS_MEMORY;
// Load the DLL into the target process.
HANDLE remote_thread = CreateRemoteThread(
process_handle,
nullptr,
0,
(LPTHREAD_START_ROUTINE)LoadLibraryA,
dll_address,
0,
nullptr);
if(remote_thread != nullptr && remote_thread != INVALID_HANDLE_VALUE)
CloseHandle(remote_thread);
// Finalize
if(process_handle)
CloseHandle(process_handle);
return InjectResult::SUCCESS;
}