-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinjector.cpp
More file actions
281 lines (225 loc) · 7.44 KB
/
Copy pathinjector.cpp
File metadata and controls
281 lines (225 loc) · 7.44 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define UNICODE // Using unicode - so windows functions take wchars
#include <TlHelp32.h>
#include <wchar.h>
#include <nan.h>
using namespace v8;
///////////////// Prototypes
static HANDLE getProcess(const char* processName);
static HANDLE getProcessPID(DWORD pid);
static int injectHandle(HANDLE process, const char* dllFile);
int injectInternal(const char* processName, const char* dllFile);
bool isProcessRunningInternal(const char* processName);
int injectInternalPID(DWORD pid, const char* dllFile);
bool isProcessRunningInternalPID(DWORD pid);
///////////////// Functions
// Get a handle to a running process based on name
static HANDLE getProcess(const char* processName) {
wchar_t wProcessName[MAX_PATH];
mbstate_t mbstate;
mbsrtowcs_s(NULL, wProcessName, &processName, MAX_PATH, &mbstate);
// Take a snapshot of processes currently running
HANDLE runningProcesses = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (runningProcesses == INVALID_HANDLE_VALUE) {
return NULL;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
// Find the desired process
BOOL res = Process32First(runningProcesses, &pe);
while (res) {
if (wcscmp(pe.szExeFile, wProcessName) == 0) {
// Found the process
CloseHandle(runningProcesses);
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
if (process == NULL) {
// Process failed to open
return NULL;
}
// Return a handle to this process
return process;
}
res = Process32Next(runningProcesses, &pe);
}
// Couldn't find the process
CloseHandle(runningProcesses);
return NULL;
}
static HANDLE getProcessPID(DWORD pid) {
// Take a snapshot of processes currently running
HANDLE runningProcesses = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (runningProcesses == INVALID_HANDLE_VALUE) {
return NULL;
}
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
// Find the desired process
BOOL res = Process32First(runningProcesses, &pe);
while (res) {
if (pe.th32ProcessID == pid) {
// Found the process
CloseHandle(runningProcesses);
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
if (process == NULL) {
// Process failed to open
return NULL;
}
// Return a handle to this process
return process;
}
res = Process32Next(runningProcesses, &pe);
}
// Couldn't find the process
CloseHandle(runningProcesses);
return NULL;
}
// Inject a DLL file into the given process
static int injectHandle(HANDLE process, const char* dllFile) {
if (process == NULL) {
// Process is not open
return 1;
}
// Get full DLL path
char dllPath[MAX_PATH];
DWORD r = GetFullPathNameA(dllFile, MAX_PATH, dllPath, NULL);
if (r == 0) {
// Getting path name failed
CloseHandle(process);
return 2;
} else if (r > MAX_PATH) {
// Buffer too small for path name
CloseHandle(process);
return 3;
}
// Get the LoadLibraryA method from the kernel32 dll
LPVOID LoadLib = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
// Allocate memory in the processs for the DLL path, and then write it there
LPVOID remotePathSpace = VirtualAllocEx(process, NULL, strlen(dllPath) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!remotePathSpace) {
CloseHandle(process);
// Failed to allocate memory
return 4;
}
if (!WriteProcessMemory(process, remotePathSpace, dllPath, strlen(dllPath) + 1, NULL)) {
// Failed to write memory
CloseHandle(process);
return 5;
}
// Load the DLL with CreateRemoteThread + LoadLibraryA
HANDLE remoteThread = CreateRemoteThread(process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLib, (LPVOID)remotePathSpace, NULL, NULL);
if (remoteThread == NULL) {
// Failed to create remote thread to load the DLL
CloseHandle(process);
return 6;
}
// Close the handle to the process
CloseHandle(process);
return 0;
}
// Returns true iff a process with the given name is running
bool isProcessRunningInternal(const char* processName) {
HANDLE process = getProcess(processName);
if (process == NULL) {
return false;
}
CloseHandle(process);
return true;
}
// Returns true iff a process with the given pid is running
bool isProcessRunningInternalPID(DWORD pid) {
HANDLE process = getProcessPID(pid);
if (process == NULL) {
return false;
}
CloseHandle(process);
return true;
}
// Inject a DLL file into the process with the given name
int injectInternal(const char* processName, const char* dllFile) {
return injectHandle(getProcess(processName), dllFile);
}
// Inject a DLL file into the process with the given pid
int injectInternalPID(DWORD pid, const char* dllFile) {
return injectHandle(getProcessPID(pid), dllFile);
}
///////////////// NAN methods
NAN_METHOD(inject) {
if (info.Length() != 2) {
return;
}
if (!info[0]->IsString() || !info[1]->IsString()) {
return;
}
String::Utf8Value arg0(info[0]->ToString());
String::Utf8Value arg1(info[1]->ToString());
if (!(*arg0) || !(*arg1)) {
return;
}
const char* processName = *arg0;
const char* dllName = *arg1;
int val = injectInternal(processName, dllName);
Local<Int32> res = Nan::New(val);
info.GetReturnValue().Set(res);
}
NAN_METHOD(injectPID) {
if (info.Length() != 2) {
Local<Int32> res = Nan::New<Int32>(8);
info.GetReturnValue().Set(res);
return;
}
if (!info[0]->IsNumber() || !info[1]->IsString()) {
Local<Int32> res = Nan::New(9);
info.GetReturnValue().Set(res);
return;
}
DWORD arg0(info[0]->Uint32Value());
String::Utf8Value arg1(info[1]->ToString());
if (!(*arg1)) {
Local<Int32> res = Nan::New(10);
info.GetReturnValue().Set(res);
return;
}
const char* dllName = *arg1;
int val = injectInternalPID(arg0, dllName);
Local<Int32> res = Nan::New(val);
info.GetReturnValue().Set(res);
}
NAN_METHOD(isProcessRunning) {
if (info.Length() != 1) {
return;
}
if (!info[0]->IsString()) {
return;
}
String::Utf8Value arg(info[0]->ToString());
if (!(*arg)) {
return;
}
const char* processName = *arg;
bool val = isProcessRunningInternal(processName);
Local<Boolean> res = Nan::New(val);
info.GetReturnValue().Set(res);
}
NAN_METHOD(isProcessRunningPID) {
if (info.Length() != 1) {
return;
}
if (!info[0]->IsUint32()) {
return;
}
DWORD arg(info[0]->Uint32Value());
bool val = isProcessRunningInternalPID(arg);
Local<Boolean> res = Nan::New(val);
info.GetReturnValue().Set(res);
}
///////////////// NAN setup
NAN_MODULE_INIT(InitModule) {
NAN_EXPORT(target, inject);
NAN_EXPORT(target, injectPID);
NAN_EXPORT(target, isProcessRunning);
NAN_EXPORT(target, isProcessRunningPID);
}
// Create the module called "addon" and initialize it with `Initialize` function (created with NAN_MODULE_INIT macro)
NODE_MODULE(injector, InitModule);