-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
362 lines (289 loc) · 9.71 KB
/
index.js
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const fs = require('fs');
const memoryjs = require('./build/Release/memoryjs');
const Debugger = require('./src/debugger');
const constants = require('./src/consts');
const { STRUCTRON_TYPE_STRING } = require('./src/utils');
/* TODO:
* - remove callbacks from all functions and implement promise support using Napi
* - validate argument types in JS space instead of C++
* - refactor read/write memory functions to use buffers instead?
* - remove `closeProcess` in favour of `closeHandle`
*/
function openProcess(processIdentifier, callback) {
if (arguments.length === 1) {
return memoryjs.openProcess(processIdentifier);
}
return memoryjs.openProcess(processIdentifier, callback);
}
function closeProcess(handle) {
return memoryjs.closeHandle(handle);
}
function getProcesses(callback) {
if (arguments.length === 0) {
return memoryjs.getProcesses();
}
return memoryjs.getProcesses(callback);
}
function findModule(moduleName, processId, callback) {
if (arguments.length === 2) {
return memoryjs.findModule(moduleName, processId);
}
return memoryjs.findModule(moduleName, processId, callback);
}
function getModules(processId, callback) {
if (arguments.length === 1) {
return memoryjs.getModules(processId);
}
return memoryjs.getModules(processId, callback);
}
function readMemory(handle, address, dataType, callback) {
if (dataType.toLowerCase().endsWith('_be')) {
return readMemoryBE(handle, address, dataType, callback);
}
if (arguments.length === 3) {
return memoryjs.readMemory(handle, address, dataType.toLowerCase());
}
return memoryjs.readMemory(handle, address, dataType.toLowerCase(), callback);
}
function readMemoryBE(handle, address, dataType, callback) {
let value = null;
switch (dataType) {
case constants.INT64_BE:
value = readBuffer(handle, address, 8).readBigInt64BE();
break;
case constants.UINT64_BE:
value = readBuffer(handle, address, 8).readBigUInt64BE();
break;
case constants.INT32_BE:
case constants.INT_BE:
case constants.LONG_BE:
value = readBuffer(handle, address, 4).readInt32BE();
break;
case constants.UINT32_BE:
case constants.UINT_BE:
case constants.ULONG_BE:
value = readBuffer(handle, address, 4).readUInt32BE();
break;
case constants.INT16_BE:
case constants.SHORT_BE:
value = readBuffer(handle, address, 2).readInt16BE();
break;
case constants.UINT16_BE:
case constants.USHORT_BE:
value = readBuffer(handle, address, 2).readUInt16BE();
break;
case constants.FLOAT_BE:
value = readBuffer(handle, address, 4).readFloatBE();
break;
case constants.DOUBLE_BE:
value = readBuffer(handle, address, 8).readDoubleBE();
break;
}
if (typeof callback !== 'function') {
if (value === null) {
throw new Error('Invalid data type argument!');
}
return value;
}
callback(value === null ? 'Invalid data type argument!' : '', value);
}
function readBuffer(handle, address, size, callback) {
if (arguments.length === 3) {
return memoryjs.readBuffer(handle, address, size);
}
return memoryjs.readBuffer(handle, address, size, callback);
}
function writeMemory(handle, address, value, dataType) {
let dataValue = value;
if (dataType === constants.STR || dataType === constants.STRING) {
dataValue += '\0'; // add terminator
}
const bigintTypes = [constants.INT64, constants.INT64_BE, constants.UINT64, constants.UINT64_BE];
if (bigintTypes.indexOf(dataType) != -1 && typeof value !== 'bigint') {
throw new Error(`${dataType.toUpperCase()} expects type BigInt`);
}
if (dataType.endsWith('_be')) {
return writeMemoryBE(handle, address, dataValue, dataType);
}
return memoryjs.writeMemory(handle, address, dataValue, dataType.toLowerCase());
}
function writeMemoryBE(handle, address, value, dataType) {
let buffer = null;
switch (dataType) {
case constants.INT64_BE:
if (typeof value !== 'bigint') {
throw new Error('INT64_BE expects type BigInt');
}
buffer = Buffer.alloc(8);
buffer.writeBigInt64BE(value);
break;
case constants.UINT64_BE:
if (typeof value !== 'bigint') {
throw new Error('UINT64_BE expects type BigInt');
}
buffer = Buffer.alloc(8);
buffer.writeBigUInt64BE(value);
break;
case constants.INT32_BE:
case constants.INT_BE:
case constants.LONG_BE:
buffer = Buffer.alloc(4);
buffer.writeInt32BE(value);
break;
case constants.UINT32_BE:
case constants.UINT_BE:
case constants.ULONG_BE:
buffer = Buffer.alloc(4);
buffer.writeUInt32BE(value);
break;
case constants.INT16_BE:
case constants.SHORT_BE:
buffer = Buffer.alloc(2);
buffer.writeInt16BE(value);
break;
case constants.UINT16_BE:
case constants.USHORT_BE:
buffer = Buffer.alloc(2);
buffer.writeUInt16BE(value);
break;
case constants.FLOAT_BE:
buffer = Buffer.alloc(4);
buffer.writeFloatBE(value);
break;
case constants.DOUBLE_BE:
buffer = Buffer.alloc(8);
buffer.writeDoubleBE(value);
break;
}
if (buffer == null) {
throw new Error('Invalid data type argument!');
}
writeBuffer(handle, address, buffer);
}
function writeBuffer(handle, address, buffer) {
return memoryjs.writeBuffer(handle, address, buffer);
}
function findPattern() {
const findPattern = ['number', 'string', 'number', 'number'].toString();
const findPatternByModule = ['number', 'string', 'string', 'number', 'number'].toString();
const findPatternByAddress = ['number', 'number', 'string', 'number', 'number'].toString();
const args = Array.from(arguments).map(arg => typeof arg);
if (args.slice(0, 4).toString() === findPattern) {
if (args.length === 4 || (args.length === 5 && args[4] === 'function')) {
return memoryjs.findPattern(...arguments);
}
}
if (args.slice(0, 5).toString() === findPatternByModule) {
if (args.length === 5 || (args.length === 6 && args[5] === 'function')) {
return memoryjs.findPatternByModule(...arguments);
}
}
if (args.slice(0, 5).toString() === findPatternByAddress) {
if (args.length === 5 || (args.length === 6 && args[5] === 'function')) {
return memoryjs.findPatternByAddress(...arguments);
}
}
throw new Error('invalid arguments!');
}
function callFunction(handle, args, returnType, address, callback) {
if (arguments.length === 4) {
return memoryjs.callFunction(handle, args, returnType, address);
}
return memoryjs.callFunction(handle, args, returnType, address, callback);
}
function virtualAllocEx(handle, address, size, allocationType, protection, callback) {
if (arguments.length === 5) {
return memoryjs.virtualAllocEx(handle, address, size, allocationType, protection);
}
return memoryjs.virtualAllocEx(handle, address, size, allocationType, protection, callback);
}
function virtualProtectEx(handle, address, size, protection, callback) {
if (arguments.length === 4) {
return memoryjs.virtualProtectEx(handle, address, size, protection);
}
return memoryjs.virtualProtectEx(handle, address, size, protection, callback);
}
function getRegions(handle, getOffsets, callback) {
if (arguments.length === 1) {
return memoryjs.getRegions(handle);
}
return memoryjs.getRegions(handle, callback);
}
function virtualQueryEx(handle, address, callback) {
if (arguments.length === 2) {
return memoryjs.virtualQueryEx(handle, address);
}
return memoryjs.virtualQueryEx(handle, address, callback);
}
function injectDll(handle, dllPath, callback) {
if (!dllPath.endsWith('.dll')) {
throw new Error("Given path is invalid: file is not of type 'dll'.");
}
if (!fs.existsSync(dllPath)) {
throw new Error('Given path is invaild: file does not exist.');
}
if (arguments.length === 2) {
return memoryjs.injectDll(handle, dllPath);
}
return memoryjs.injectDll(handle, dllPath, callback);
}
function unloadDll(handle, module, callback) {
if (arguments.length === 2) {
return memoryjs.unloadDll(handle, module);
}
return memoryjs.unloadDll(handle, module, callback);
}
function openFileMapping(fileName) {
if (arguments.length !== 1 || typeof fileName !== 'string') {
throw new Error('invalid arguments!');
}
return memoryjs.openFileMapping(fileName);
}
function mapViewOfFile(processHandle, fileHandle, offset, viewSize, pageProtection) {
const validArgs = [
['number', 'number'],
['number', 'number', 'number', 'number', 'number'],
['number', 'number', 'bigint', 'bigint', 'number']
];
const receivedArgs = Array.from(arguments).map(arg => typeof arg);
if (!validArgs.some(args => args.join(",") == receivedArgs.join(","))) {
throw new Error('invalid arguments!');
}
if (arguments.length == 2) {
return memoryjs.mapViewOfFile(processHandle, fileHandle, 0, 0, constants.PAGE_READONLY);
}
return memoryjs.mapViewOfFile(processHandle, fileHandle, offset, viewSize, pageProtection);
}
const library = {
openProcess,
closeProcess,
getProcesses,
findModule,
getModules,
readMemory,
readBuffer,
writeMemory,
writeBuffer,
findPattern,
callFunction,
virtualAllocEx,
virtualProtectEx,
getRegions,
virtualQueryEx,
injectDll,
unloadDll,
openFileMapping,
mapViewOfFile,
attachDebugger: memoryjs.attachDebugger,
detachDebugger: memoryjs.detachDebugger,
awaitDebugEvent: memoryjs.awaitDebugEvent,
handleDebugEvent: memoryjs.handleDebugEvent,
setHardwareBreakpoint: memoryjs.setHardwareBreakpoint,
removeHardwareBreakpoint: memoryjs.removeHardwareBreakpoint,
Debugger: new Debugger(memoryjs),
};
module.exports = {
...constants,
...library,
STRUCTRON_TYPE_STRING: STRUCTRON_TYPE_STRING(library),
};