-
Notifications
You must be signed in to change notification settings - Fork 36
/
unicorn-wrapper.js
518 lines (480 loc) · 21.7 KB
/
unicorn-wrapper.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/**
* (c) 2016-2017 Unicorn.JS
* Wrapper made by Alexandro Sanchez Bach.
*/
// Emscripten demodularize
var MUnicorn = new MUnicorn();
// Number conversion modes
ELF_INT_NUMBER = 1
ELF_INT_STRING = 2
ELF_INT_OBJECT = 3
var uc = {
// Static
version: function() {
major_ptr = MUnicorn._malloc(4);
minor_ptr = MUnicorn._malloc(4);
var ret = MUnicorn.ccall('uc_version', 'number',
['pointer', 'pointer'], [major_ptr, minor_ptr]);
major = MUnicorn.getValue(major_ptr, 'i32');
minor = MUnicorn.getValue(minor_ptr, 'i32');
MUnicorn._free(major_ptr);
MUnicorn._free(minor_ptr);
return ret;
},
arch_supported: function(arch) {
var ret = MUnicorn.ccall('uc_arch_supported', 'number', ['number'], [arch]);
return ret;
},
strerror: function(code) {
var ret = MUnicorn.ccall('uc_strerror', 'string', ['number'], [code]);
return ret;
},
/**
* Unicorn object
*/
Unicorn: function (arch, mode) {
this.arch = arch;
this.mode = mode;
this.handle_ptr = MUnicorn._malloc(4);
// Methods
this.reg_write = function (regid, bytes) {
// Allocate bytes buffer and copy data
var buffer_len = bytes.length;
var buffer_ptr = MUnicorn._malloc(buffer_len);
MUnicorn.writeArrayToMemory(bytes, buffer_ptr);
// Register write
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_reg_write', 'number',
['pointer', 'number', 'pointer'],
[handle, regid, buffer_ptr]
);
// Free memory and handle return code
MUnicorn._free(buffer_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_reg_write failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.reg_read = function (regid, size) {
// Allocate space for the output value
var buffer_ptr = MUnicorn._malloc(size);
for (var i = 0; i < size; i++) {
MUnicorn.setValue(buffer_ptr + i, 0, 'i8');
}
// Register read
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_reg_read', 'number',
['pointer', 'number', 'pointer'],
[handle, regid, buffer_ptr]
);
// Get register value, free memory and handle return code
var value = new Uint8Array(size);
for (var i = 0; i < size; i++) {
value[i] = MUnicorn.getValue(buffer_ptr + i, 'i8');
}
MUnicorn._free(buffer_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_reg_read failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
return value;
}
this.mem_write = function (address, bytes) {
// Allocate bytes buffer and copy data
var buffer_len = bytes.length;
var buffer_ptr = MUnicorn._malloc(buffer_len);
MUnicorn.writeArrayToMemory(bytes, buffer_ptr);
// Convert address types
var [addr_lo, addr_hi] = this.__address(address);
// Write to memory
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_mem_write', 'number',
['pointer', 'number', 'number', 'pointer', 'number'],
[handle, addr_lo, addr_hi, buffer_ptr, buffer_len]
);
// Free memory and handle return code
MUnicorn._free(buffer_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_write failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.mem_read = function (address, size) {
// Allocate space for the output value
var buffer_ptr = MUnicorn._malloc(size);
for (var i = 0; i < size; i++) {
MUnicorn.setValue(buffer_ptr + i, 0, 'i8');
}
// Convert address types
var [addr_lo, addr_hi] = this.__address(address);
// Read from memory
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_mem_read', 'number',
['pointer', 'number', 'number', 'pointer', 'number'],
[handle, addr_lo, addr_hi, buffer_ptr, size]
);
// Get register value, free memory and handle return code
var buffer = new Uint8Array(size);
for (var i = 0; i < size; i++) {
buffer[i] = MUnicorn.getValue(buffer_ptr + i, 'i8');
}
MUnicorn._free(buffer_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_read failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
return buffer;
}
this.mem_map = function (address, size, perms) {
// Convert address types
var [addr_lo, addr_hi] = this.__address(address);
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_mem_map', 'number',
['pointer', 'number', 'number', 'number', 'number'],
[handle, addr_lo, addr_hi, size, perms]
);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_map failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.mem_protect = function (address, size, perms) {
// Convert address types
var [addr_lo, addr_hi] = this.__address(address);
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_mem_protect', 'number',
['pointer', 'number', 'number', 'number', 'number'],
[handle, addr_lo, addr_hi, size, perms]
);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_protect failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.mem_regions = function () {
console.error("Unicorn.js: Method mem_regions unimplemented");
}
this.mem_unmap = function (address, size) {
// Convert address types
var [addr_lo, addr_hi] = this.__address(address);
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_mem_unmap', 'number',
['pointer', 'number', 'number', 'number'],
[handle, addr_lo, addr_hi, size]
);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_unmap failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.hook_add = function (type, user_callback, user_data, begin, end, extra) {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
// Default arguments
if (typeof user_data === 'undefined') {
user_data = {}
}
if (typeof begin === 'undefined' &&
typeof end === 'undefined') {
begin = 1;
end = 0;
}
// Wrap callback
var extra_types = ['number'];
var extra_values = [extra];
switch (type) {
case uc.HOOK_INSN:
extra_types = ['number'];
extra_values = [extra];
var callback = (function (handle, user_data) {
return function (_, _2) {
user_callback(handle, user_data);
}
})(this, user_data);
var callback_ptr = MUnicorn.addFunction(callback, 'vii');
break;
// uc_cb_hookintr_t
case uc.HOOK_INTR:
var callback = (function (handle, user_data) {
return function (_, intno, _2) {
user_callback(handle, intno, user_data);
}
})(this, user_data);
var callback_ptr = MUnicorn.addFunction(callback, 'viii');
break;
// uc_cb_hookcode_t
case uc.HOOK_CODE:
case uc.HOOK_BLOCK:
var callback = (function (handle, user_data) {
return function (_, addr_lo, addr_hi, size, _2) {
user_callback(handle, addr_lo, addr_hi, size, user_data);
}
})(this, user_data);
var callback_ptr = MUnicorn.addFunction(callback, 'viiii');
break;
default:
// uc_cb_hookmem_t
if ((type & uc.HOOK_MEM_READ) ||
(type & uc.HOOK_MEM_WRITE) ||
(type & uc.HOOK_MEM_FETCH) ||
(type & uc.HOOK_MEM_READ_AFTER)) {
var callback = (function (handle, user_data) {
return function (_, type, addr_lo, addr_hi, size, value_lo, value_hi, _2) {
user_callback(handle, type, addr_lo, addr_hi, size, value_lo, value_hi, user_data);
}
})(this, user_data);
var callback_ptr = MUnicorn.addFunction(callback, 'viiiiiiii');
}
// uc_cb_eventmem_t
if ((type & uc.HOOK_MEM_READ_UNMAPPED) ||
(type & uc.HOOK_MEM_WRITE_UNMAPPED) ||
(type & uc.HOOK_MEM_FETCH_UNMAPPED) ||
(type & uc.HOOK_MEM_READ_PROT) ||
(type & uc.HOOK_MEM_WRITE_PROT) ||
(type & uc.HOOK_MEM_FETCH_PROT)) {
var callback = (function (handle, user_data) {
return function (_, type, addr_lo, addr_hi, size, value_lo, value_hi, _2) {
return user_callback(handle, type, addr_lo, addr_hi, size, value_lo, value_hi, user_data);
}
})(this, user_data);
var callback_ptr = MUnicorn.addFunction(callback, 'iiiiiiiii');
}
}
if (typeof callback === 'undefined') {
throw 'Unicorn.js: Unimplemented hook type'
}
// Set hook
var hook_ptr = MUnicorn._malloc(4);
var ret = MUnicorn.ccall('uc_hook_add', 'number',
['pointer', 'pointer', 'number', 'pointer', 'pointer',
'number', 'number', 'number', 'number', 'number'],
[handle, hook_ptr, type, callback_ptr, 0,
begin, 0, end, 0, extra]
);
if (ret != uc.ERR_OK) {
MUnicorn.removeFunction(callback_ptr);
MUnicorn._free(hook_ptr);
var error = 'Unicorn.js: Function uc_mem_unmap failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
var hook = {
handle: MUnicorn.getValue(hook_ptr, '*'),
callback: callback_ptr
};
MUnicorn._free(hook_ptr);
return hook
}
this.hook_del = function (hook) {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_hook_del', 'number',
['pointer', 'pointer'],
[handle, hook.handle]
);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_mem_unmap failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
MUnicorn.removeFunction(hook.callback);
}
this.emu_start = function (begin, until, timeout, count) {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_emu_start', 'number',
['pointer', 'number', 'number', 'number', 'number', 'number', 'number', 'number'],
[handle, begin, 0, until, 0, timeout, 0, count]
);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_emu_start failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.emu_stop = function (begin, until, timeout, count) {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_emu_stop', 'number', ['pointer'], [handle]);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_emu_stop failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.context_alloc = function () {
console.error('Unicorn.js: Contexts not implemented');
}
this.context_free = function () {
console.error('Unicorn.js: Contexts not implemented');
}
this.context_save = function () {
console.error('Unicorn.js: Contexts not implemented');
}
this.context_restore = function () {
console.error('Unicorn.js: Contexts not implemented');
}
this.errno = function() {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_errno', 'number', ['pointer'], [handle]);
return ret;
}
this.close = function() {
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_close', 'number', ['pointer'], [handle]);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_close failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
// Helpers
this.__integer = function (value, width) {
if (typeof value === "number") {
value = [value];
}
switch (this.get_integer_type()) {
case ELF_INT_NUMBER:
return value[0];
case ELF_INT_STRING:
return value
.map(x => x.toString(16).toUpperCase())
.map(x => '0'.repeat(width/4 - x.length) + x)
.reverse().join('');
case ELF_INT_OBJECT:
switch (width) {
case 8: return new ElfUInt8(value);
case 16: return new ElfUInt16(value);
case 32: return new ElfUInt32(value);
case 64: return new ElfUInt64(value);
default: throw 'Unexpected width';
}
default:
var error = 'Unimplemented integer type';
throw error;
}
}
this.__address = function (address) {
var address_obj = new ElfUInt64(address);
return [address_obj.chunks[0] + (address_obj.chunks[1]<<16), address_obj.chunks[2] + (address_obj.chunks[3]<<16)];
}
this._sizeof = function (type) {
switch (type) {
case 'i8': return 1;
case 'i16': return 2;
case 'i32': return 4;
case 'i64': return 8;
case 'float': return 4;
case 'double': return 8;
default: return 0;
}
}
this.reg_write_type = function (regid, type, value) {
// Allocate space for the output value
var value_size = this._sizeof(type);
var value_ptr = MUnicorn._malloc(value_size);
// Convert integer types
var value_obj = new (ElfUInt(value_size*8))(value);
for (var i = 0; i < value_size/2; i++) {
MUnicorn.setValue(value_ptr + i*2, value_obj.chunks[i], 'i16');
}
// Register write
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_reg_write', 'number',
['pointer', 'number', 'pointer'],
[handle, regid, value_ptr]
);
// Free memory and handle return code
MUnicorn._free(value_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_reg_write failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
this.reg_write_i8 = function (regid, value) { this.reg_write_type(regid, 'i8', value); }
this.reg_write_i16 = function (regid, value) { this.reg_write_type(regid, 'i16', value); }
this.reg_write_i32 = function (regid, value) { this.reg_write_type(regid, 'i32', value); }
this.reg_write_i64 = function (regid, value) { this.reg_write_type(regid, 'i64', value); }
this.reg_write_float = function (regid, value) { this.reg_write_type(regid, 'float', value); }
this.reg_write_double = function (regid, value) { this.reg_write_type(regid, 'double', value); }
this.reg_read_type = function (regid, type) {
// Allocate space for the output value
var value_size = this._sizeof(type);
var value_ptr = MUnicorn._malloc(value_size);
MUnicorn.setValue(value_ptr, 0, type);
// Register read
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_reg_read', 'number',
['pointer', 'number', 'pointer'],
[handle, regid, value_ptr]
);
// Get register value, free memory and handle return code
var value = MUnicorn.getValue(value_ptr, type);
if (type === 'i64') {
value = [value, MUnicorn.getValue(value_ptr+4, 'i32')]
}
// Convert integer types
if (type.includes('i')) {
value = this.__integer(value, value_size*8);
}
MUnicorn._free(value_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_reg_read failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
return value;
}
this.reg_read_i8 = function (regid) { return this.reg_read_type(regid, 'i8'); }
this.reg_read_i16 = function (regid) { return this.reg_read_type(regid, 'i16'); }
this.reg_read_i32 = function (regid) { return this.reg_read_type(regid, 'i32'); }
this.reg_read_i64 = function (regid) { return this.reg_read_type(regid, 'i64'); }
this.reg_read_float = function (regid) { return this.reg_read_type(regid, 'float'); }
this.reg_read_double = function (regid) { return this.reg_read_type(regid, 'double'); }
this.query_type = function (query_type, result_type) {
// Allocate space for the output value
var result_size = this._sizeof(result_type);
var result_ptr = MUnicorn._malloc(result_size);
MUnicorn.setValue(value_ptr, 0, result_type);
// Make query
var handle = MUnicorn.getValue(this.handle_ptr, '*');
var ret = MUnicorn.ccall('uc_query', 'number',
['pointer', 'number', 'pointer'],
[handle, query_type, result_ptr]
);
// Get result value, free memory and handle return code
var result = MUnicorn.getValue(result_ptr, result_type);
if (type === 'i64') {
result = [result, MUnicorn.getValue(result_ptr+4, 'i32')]
}
// Convert integer types
if (type.includes('i')) {
result = this.__integer(result, result_size*8);
}
MUnicorn._free(result_ptr);
if (ret != uc.ERR_OK) {
var error = 'Unicorn.js: Function uc_query failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
return result;
}
this.query_i8 = function (type) { return this.query_type(type, 'i8'); }
this.query_i16 = function (type) { return this.query_type(type, 'i16'); }
this.query_i32 = function (type) { return this.query_type(type, 'i32'); }
this.query_i64 = function (type) { return this.query_type(type, 'i64'); }
this.query_float = function (type) { return this.query_type(type, 'float'); }
this.query_double = function (type) { return this.query_type(type, 'double'); }
// Configuration
this.get_integer_type = function () {
// Using ELF_INT_NUMBER as default for 32 bit backward compatibility
if (this.integer_type == null) {
return ELF_INT_NUMBER;
}
return this.integer_type;
}
this.set_integer_type = function (type) {
// Please Use ELF_INT_STRING/ELF_INT_OBJECT for 64 bit support
this.integer_type = type;
}
// Constructor
var ret = MUnicorn.ccall('uc_open', 'number',
['number', 'number', 'pointer'],
[this.arch, this.mode, this.handle_ptr]
);
if (ret != uc.ERR_OK) {
MUnicorn.setValue(this.handle_ptr, 0, '*');
var error = 'Unicorn.js: Function uc_open failed with code ' + ret + ':\n' + uc.strerror(ret);
throw error;
}
}
};