forked from cursey/kanan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaults.js
426 lines (329 loc) · 10.1 KB
/
Defaults.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// This file is prepended to every script before it is ran by kanan, making
// everything within this file available to all scripts.
// Sends a msg back to kanan's window with the name of the script prepended.
function msg(str) {
if (scriptName != undefined) {
send(scriptName + ": " + str);
}
else {
send(str);
}
}
// Same as above but only outputs a msg when in debug mode.
function dmsg(str) {
if (debug) {
msg(str);
}
}
// Fixes the signature so single ?'s are converted to ??'s.
function fixSig(sig) {
var oldLen = sig.length;
var newLen = 0;
while ((newLen = (sig = sig.replace(' ? ', ' ?? ')).length) != oldLen) {
oldLen = sig.length;
}
return sig;
}
// Scans for patterns in specific modules code section.
function scan(name, sig) {
if (sig == undefined) {
sig = name;
name = 'client.exe';
}
sig = fixSig(sig);
var ranges = Module.enumerateRangesSync(name, 'r-x');
var address = NULL;
for (var i = 0; i < ranges.length; ++i) {
var range = ranges[i];
var results = Memory.scanSync(range.base, range.size, sig);
if (results.length > 0) {
if (results.length > 1) {
dmsg("More than 1 result for: " + sig);
}
address = results[0].address;
break;
}
}
dmsg(address);
if (address.isNull()) {
dmsg("No results for: " + sig);
}
else if (debug) {
// Send the results of the scan back to kanan.py
send({script: scriptName, signature: sig, address: address});
}
return address;
}
// Just adds an offset to the base address of a module.
function moduleOffset(moduleName, offset) {
var baseAddress = Module.findBaseAddress(moduleName);
if (baseAddress.isNull()) {
return NULL;
}
return baseAddress.add(offset);
}
// Writes a string to allocated memory. Make sure theres enough room at the
// address for str.length + 1 (for the trailing zero).
function writeStr(address, str) {
for (var i = 0; i < str.length; ++i) {
Memory.writeU8(address.add(i), str.charCodeAt(i));
}
Memory.writeU8(address.add(str.length), 0);
}
// Writes a wide str (utf16) to allocated memory. Make sure theres at least
// str.length * 2 + 2 (for the trailing zero).
function writeWideStr(address, str) {
for (var i = 0; i < str.length; ++i) {
Memory.writeU16(address.add(i * 2), str.charCodeAt(i));
}
Memory.writeU16(address.add(str.length * 2), 0);
}
// NativeFunctions used by the following functions.
var LoadLibraryA = new NativeFunction(Module.findExportByName('Kernel32.dll', 'LoadLibraryA'),
'pointer', ['pointer'], 'stdcall');
var GetProcAddress = new NativeFunction(Module.findExportByName('Kernel32.dll', 'GetProcAddress'),
'pointer', ['pointer', 'pointer'], 'stdcall');
var VirtualAlloc = new NativeFunction(Module.findExportByName('Kernel32.dll', 'VirtualAlloc'),
'pointer', ['pointer', 'ulong', 'uint32', 'uint32'], 'stdcall');
var VirtualFree = new NativeFunction(Module.findExportByName('Kernel32.dll', 'VirtualFree'),
'int', ['pointer', 'ulong', 'uint32'], 'stdcall');
var VirtualProtect = new NativeFunction(Module.findExportByName('Kernel32.dll', 'VirtualProtect'),
'int', ['pointer', 'ulong', 'uint32', 'pointer'], 'stdcall');
// Constants used by the following functions.
var MEM_COMMIT = 0x00001000;
var MEM_RESERVE = 0x00002000;
var MEM_DECOMMIT = 0x4000;
var PAGE_EXECUTE_READWRITE = 0x40;
// Allocates some memory.
function allocateMemory(len) {
return VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
}
// Frees memory allocated with allocateMemory.
function freeMemory(address, len) {
return VirtualFree(address, len, MEM_DECOMMIT);
}
// Protects an area of memory and returns the old protection (0 on failure).
function protect(address, len, protect) {
var oldProtectPtr = allocateMemory(4);
var result = VirtualProtect(address, len, protect, oldProtectPtr);
var oldProtect = Memory.readU32(oldProtectPtr);
freeMemory(oldProtectPtr, 4);
if (result == 0) {
msg("Failed to protect " + address);
return 0;
}
return oldProtect;
}
// Unprotects (sets read, write and executable) an area of memory and returns
// the old protection (0 on failure).
function unprotect(address, len) {
return protect(address, len, PAGE_EXECUTE_READWRITE);
}
// Helper that just allocates memory for a str and writes the str to that
// mem.
function allocateStr(str) {
var mem = allocateMemory(str.length + 1);
writeStr(mem, str);
return mem;
}
// Like above but for wide (utf 16) strings.
function allocateWideStr(str) {
var mem = allocateMemory(str.length * 2 + 2);
writeWideStr(mem, str);
return mem;
}
// Frees an allocated str from allocateStr.
function freeStr(str) {
// We can pass 0 to freeMemory because str must have been allocated with
// allocateStr (see docs on VirtualFree where the address is the address
// returned from VirtualAlloc).
freeMemory(str, 0);
}
// Alias for above.
function freeWideStr(str) {
freeStr(str);
}
// Loads the dll located at filepath. Returns the base address of the loaded
// dll or NULL.
function loadDll(filepath) {
var str = allocateStr(filepath);
var result = LoadLibraryA(str);
freeStr(str);
return result;
}
// Gets the address of an exported function.
function getProcAddress(moduleName, funcName) {
// Search the currently loaded modules for the function.
var addr = Module.findExportByName(moduleName, funcName);
if (!addr.isNull()) {
return addr;
}
// Otherwise, fallback to the win32 api way of doing things. If the module
// isn't already loaded it will be.
var str = allocateStr(funcName);
var result = GetProcAddress(loadDll(moduleName), str);
freeStr(str);
return result;
}
// Wrapper for NativeFunction that uses the above getProcAddress.
function native(moduleName, funcName, returnType, paramTypes, callType) {
return new NativeFunction(getProcAddress(moduleName, funcName), returnType, paramTypes, callType);
}
// Validates a patch address.
// TODO: Make this more robust.
function isValidPatchAddress(addr) {
return (!addr.isNull() && addr.toInt32() > 1000);
}
// Patches an array of bytes or a single byte.
function patch(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch.");
return;
}
if (testing) {
return;
}
if (!Array.isArray(c)) {
c = [c];
}
var p = unprotect(addr, c.length);
for (var i = 0; i < c.length; ++i) {
if (c[i] >= 0 && c[i] <= 0xFF) {
Memory.writeU8(addr.add(i), c[i]);
}
}
protect(addr, c.length, p);
}
// Patches a byte (8 bits). This is the same as calling the above patch()
// function with a single byte as the argument.
function patchByte(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch byte.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 1);
Memory.writeU8(addr, c);
protect(addr, 1, p);
}
// Patches a word (16 bits, shorts).
function patchWord(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch word.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 2);
Memory.writeU16(addr, c);
protect(addr, 2, p);
}
// Patches a dword (32 bits, ints, longs, addresses).
function patchDword(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch dword.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 4);
Memory.writeU32(addr, c);
protect(addr, 4, p);
}
// Patches a qword (64 bits, long longs, 64-bit addresses).
function patchQword(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch qword.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 8);
Memory.writeU64(addr, c);
protect(addr, 8, p);
}
// Patches a float (32 bits, no doubles).
function patchFloat(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch float.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 4);
Memory.writeFloat(addr, c);
protect(addr, 4, p);
}
// Patches a double (64 bits).
function patchDouble(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch double.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 8);
Memory.writeDouble(addr, c);
protect(addr, 8, p);
}
// Patches a pointer.
function patchPointer(addr, c) {
if (!isValidPatchAddress(addr)) {
msg("Failed to patch pointer.");
return;
}
if (testing) {
return;
}
var p = unprotect(addr, 4);
Memory.writePointer(addr, c);
protect(addr, 4, p);
}
// Same as above but called address instead.
var patchAddress = patchPointer;
// Aliases for the above functions.
var writeByte = patchByte;
var writeWord = patchWord;
var writeDword = patchDword;
var writeQword = patchQword;
var writeFloat = patchFloat;
var writeDouble = patchDouble;
var writePointer = patchPointer;
var writeAddress = patchAddress;
// Copies bytes.
function copy(dst, src, len) {
if (!isValidPatchAddress(dst) || !isValidPatchAddress(src)) {
msg("Failed to copy.");
return;
}
if (testing) {
return;
}
var dstp = unprotect(dst, len);
var srcp = unprotect(src, len);
Memory.copy(dst, src, len);
protect(src, len, srcp);
protect(dst, len, dstp);
}
// Inserts a 5-byte jmp at the address to the destination.
// NOTE: Make sure there is room for the jmp!!!!
function insertJmp(address, destination) {
if (!isValidPatchAddress(address)) {
msg("Failed to insert jmp.");
return;
}
if (testing) {
return;
}
var p = unprotect(address, 5);
Memory.writeU8(address, 0xE9);
Memory.writeS32(address.add(1), destination.toInt32() - address.toInt32() - 5);
protect(address, 5, p);
}