-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDLL_Loader.cpp
More file actions
211 lines (180 loc) · 5.77 KB
/
DLL_Loader.cpp
File metadata and controls
211 lines (180 loc) · 5.77 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* =========================================================
* DLL_Loader.h
* *********************************************************
*
* Class: CDLL_Loader
*
* Author: Dan Revah
*
* Date: 25/11/2011 (DD/MM/YYYY)
*
* (-!-) User-mode root (DLL Hooking)
* ======================================================= */
#include "DLL_Loader.h"
#include <iostream>
/* ===========================================================
* CDLL_Loader::CDLL_Loader()
*
* (*) Default constructor
*/
CDLL_Loader::CDLL_Loader():
ProcessID(0), DllFilePath(0), init(false), injected(false)
{
}
/* ===========================================================
* CDLL_Loader::CDLL_Loader(DWORD Pid, char* DllPath)
*
* (*) Initializing constructor
* Parameters:
* Pid - Process ID to inject
* DllPath - The Dll path that you use to inject
*/
CDLL_Loader::CDLL_Loader(DWORD Pid, char* DllPath):
ProcessID(Pid), DllFilePath(DllPath), init(true), injected(false)
{
}
/* ====================================
* CDLL_Loader::~CDLL_Loader()
*
* (*) Destructor
*/
CDLL_Loader::~CDLL_Loader()
{
//EjectDll();
ProcessID = 0;
DllFilePath = 0;
init = false;
injected = false;
}
/* ===========================================================
* DLL_Results CDLL_Loader::InitDLL(DWORD Pid, char* DllPath)
*
* Parameters:
* Pid - Process ID to inject
* DllPath - The Dll path that you use to inject
*/
DLL_Results CDLL_Loader::InitDLL(DWORD Pid, char* DllPath)
{
if (isInit())
return INIT_OK;
ProcessID = Pid;
DllFilePath = DllPath;
init = true;
injected = false;
return INIT_OK;
}
/* ===========================================================
* DWORD CDLL_Loader::GetProcessIdByName(char* process_name)
*
* Return Value
* If the function succeeds, it returns the Process ID
* If the function fails, it will return NULL
*/
DWORD CDLL_Loader::GetProcessIdByName(char* process_name)
{
HANDLE hProcess;
PROCESSENTRY32 ProcessEntry = {0};
ProcessEntry.dwSize = sizeof(PROCESSENTRY32);
hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcess == INVALID_HANDLE_VALUE)
return NULL;
if (Process32First(hProcess, &ProcessEntry) != FALSE)
{
do {
if (strcmp(process_name, String_Convertion::WStringToString(ProcessEntry.szExeFile).c_str() ) == 0)
{
return ProcessEntry.th32ProcessID;
}
} while (Process32Next(hProcess, &ProcessEntry) != FALSE);
}
CloseHandle(hProcess);
return NULL;
}
/* ===========================================================
* DLL_Results CDLL_Loader::InjectDll()
*
* Injecting the dll into the process
* Return Values:
* If the function succeeds, it returns the value DLLINJECT_OK
* If the function fails:
* NOT_INIT - Not initliazed
* DLL_ALREADY_INJECTED - The dll was already injected
* PROCESS_ERROR_OPEN - Unable to open process ID
* PROCESS_ERRORR_VALLOC - Unable to virtual alloc
* PROCESS_ERROR_WRITE - Unable to write process memory
* PROCESS_ERROR_CREATE_RTHREAD - Unable to create remote thread
*/
DLL_Results CDLL_Loader::InjectDll()
{
LPVOID VirtualMem;
HANDLE hProcess, hRemoteThread;
HMODULE hModule;
if (!isInit())
return NOT_INIT;
if (isInjected())
return DLL_ALREADY_INJECTED;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
std::cout << "Last Error: " << GetLastError() << std::endl;
if (hProcess == NULL)
return PROCESS_ERROR_OPEN;
VirtualMem = VirtualAllocEx (hProcess, NULL, strlen(DllFilePath), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (VirtualMem == NULL)
return PROCESS_ERRORR_VALLOC;
if (WriteProcessMemory(hProcess, (LPVOID)VirtualMem, DllFilePath, strlen(DllFilePath), NULL) == 0)
{
VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE|MEM_COMMIT);
CloseHandle(hProcess);
return PROCESS_ERROR_WRITE;
}
hModule = GetModuleHandle(L"kernel32.dll");
hRemoteThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "LoadLibraryA"),
(LPVOID)VirtualMem, 0, NULL);
if (hRemoteThread == NULL)
{
FreeLibrary(hModule);
VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
CloseHandle(hProcess);
return PROCESS_ERROR_CREATE_RTHREAD;
}
WaitForSingleObject(hRemoteThread, INFINITE);
GetExitCodeThread(hRemoteThread, &ThreadTeminationStatus);
FreeLibrary(hModule);
VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
CloseHandle(hRemoteThread);
CloseHandle(hProcess);
injected = true;
return DLLINJECT_OK;
}
/* ===========================================================
* DLL_Results CDLL_Loader::EjectDll()
*
* Ejecting the dll from the process
* Return Values:
* If the function succeeds, it returns the value DLLEJECT_OK
* If the function fails:
* PROCESS_ERROR_OPEN - Unable to open process ID
*/
DLL_Results CDLL_Loader::EjectDll()
{
HANDLE hProcess, hRemoteThread;
HMODULE hModule;
if (isInjected())
return DLLEJECT_OK;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
if (hProcess == NULL)
return PROCESS_ERROR_OPEN;
hModule = GetModuleHandle(L"kernel32.dll");
hRemoteThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "FreeLibrary"),
(LPVOID)ThreadTeminationStatus, 0, NULL);
if (hRemoteThread != NULL)
{
WaitForSingleObject(hRemoteThread, INFINITE);
GetExitCodeThread(hRemoteThread, &ThreadTeminationStatus);
}
CloseHandle(hRemoteThread);
CloseHandle(hProcess);
injected = false;
return DLLEJECT_OK;
}