-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunctions.cc
347 lines (287 loc) · 7.22 KB
/
functions.cc
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "functions.h"
#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>
using namespace v8;
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 if 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 if 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;
}
// Returns PID if a process with the given name is running, else -1
int getPIDByNameInternal(const char* processName) {
HANDLE process = getProcess(processName);
if (process == NULL) {
return -1;
}
int PID = GetProcessId(process);
CloseHandle(process);
return PID;
}
// 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_METHOD(inject)
{
if (info.Length() != 2)
{
Local<Int32> res = Nan::New<Int32>(8);
info.GetReturnValue().Set(res);
return;
}
if (!info[0]->IsString() || !info[1]->IsString())
{
Local<Int32> res = Nan::New(9);
info.GetReturnValue().Set(res);
return;
}
Local<Value> value0;
Local<Value> value1;
(info[0]->ToString(Nan::GetCurrentContext())).ToLocal(&value0);
(info[1]->ToString(Nan::GetCurrentContext())).ToLocal(&value1);
String::Utf8Value arg0(Isolate::GetCurrent(), value0);
String::Utf8Value arg1(Isolate::GetCurrent(), value1);
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]->IsUint32() || !info[1]->IsString())
{
Local<Int32> res = Nan::New(9);
info.GetReturnValue().Set(res);
return;
}
uint32_t value0 = info[0]->IsUndefined() ? 0 : Nan::To<uint32_t>(info[0]).FromJust();
DWORD arg0(value0);
Local<Value> value1;
(info[1]->ToString(Nan::GetCurrentContext())).ToLocal(&value1);
String::Utf8Value arg1(Isolate::GetCurrent(), value1);
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;
}
Local<Value> value;
(info[0]->ToString(Nan::GetCurrentContext())).ToLocal(&value);
String::Utf8Value arg(Isolate::GetCurrent(), value);
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;
}
uint32_t value = info[0]->IsUndefined() ? 0 : Nan::To<uint32_t>(info[0]).FromJust();
DWORD arg(value);
bool val = isProcessRunningInternalPID(arg);
Local<Boolean> res = Nan::New(val);
info.GetReturnValue().Set(res);
}
NAN_METHOD(getPIDByName)
{
if (info.Length() != 1) {
return;
}
if (!info[0]->IsString()) {
return;
}
Local<Value> value;
(info[0]->ToString(Nan::GetCurrentContext())).ToLocal(&value);
String::Utf8Value arg(Isolate::GetCurrent(), value);
if (!(*arg)) {
return;
}
const char* processName = *arg;
int val = getPIDByNameInternal(processName);
Local<Int32> res = Nan::New(val);
info.GetReturnValue().Set(res);
}