-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjitrop.cpp
638 lines (483 loc) · 16.6 KB
/
jitrop.cpp
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <capstone/capstone.h>
#include "jitrop.h"
#include "gadget.h"
#include "lookup.h"
#include "constants.h"
using namespace std;
/* Reads data from the target process, and places it on the `dest_buffer`
* using either `ptrace` or `pread` on `/proc/pid/mem`.
* The target process is not passed, but read from the static peekbuf.
* `sm_attach()` MUST be called before this function. */
size_t readmemory(int pid, uint8_t *dest_buffer, const char *target_address, size_t size)
{
size_t nread = 0;
/* Read the memory with `ptrace()`: the API specifies that `ptrace()` returns a `long`, which
* is the size of a word for the current architecture, so this section will deal in `long`s */
assert(size % sizeof(long) == 0);
errno = 0;
for (nread = 0; nread < size; nread += sizeof(long)) {
const char *ptrace_address = target_address + nread;
long ptraced_long = ptrace(PTRACE_PEEKDATA, pid, ptrace_address, NULL);
/* check if ptrace() succeeded */
if (ptraced_long == -1L && errno != 0) {
/* it's possible i'm trying to read partially oob */
if (errno == EIO || errno == EFAULT) {
int j;
/* read backwards until we get a good read, then shift out the right value */
for (j = 1, errno = 0; j < sizeof(long); j++, errno = 0) {
/* try for a shifted ptrace - 'continue' (i.e. try an increased shift) if it fails */
ptraced_long = ptrace(PTRACE_PEEKDATA, pid, ptrace_address - j, NULL);
if ((ptraced_long == -1L) && (errno == EIO || errno == EFAULT))
continue;
/* store it with the appropriate offset */
uint8_t* new_memory_ptr = (uint8_t*)(&ptraced_long) + j;
memcpy(dest_buffer + nread, new_memory_ptr, sizeof(long) - j);
nread += sizeof(long) - j;
/* interrupt the partial gathering process */
break;
}
}
/* interrupt the gathering process */
break;
}
/* otherwise, ptrace() worked - store the data */
memcpy(dest_buffer + nread, &ptraced_long, sizeof(long));
}
return nread;
}
/*
#define CODE "\x55\x48\x8b\x05\xb8\x13\x00\x00"
int disas_raw_code(unsigned char *custom_code, size_t code_size, unsigned long start_addr){
csh handle;
cs_insn *insn;
size_t count;
printf("%ld\n", sizeof(custom_code));
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
return -1;
count = cs_disasm(handle, custom_code, code_size-1, start_addr, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
insn[j].op_str);
}
cs_free(insn, count);
} else
printf("ERROR: Failed to disassemble given code!\n");
cs_close(&handle);
return 0;
}
*/
unsigned long page_no(unsigned long addr){
return addr & 0xFFFFFFFFFFFFF000;
}
unsigned long get_got_slot(unsigned long code_ptr, int pid) {
unsigned char *data = NULL;
size_t read_size = 16;
if ((data = (unsigned char *)malloc(read_size * sizeof(char))) == NULL) {
if (IS_LOG) printf("sorry, there was a memory allocation error for plt data.\n");
return 0;
}
size_t nread = readmemory(pid, data, (const char *) code_ptr, read_size);
if (nread <= 0) {
if (IS_LOG) printf("get_got_slot::Memory read error, no data to disassemble.\n");
return 0;
}
csh handle;
cs_insn *insn;
size_t count;
string first_inst = "";
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
if (IS_LOG) printf("got slot::Capstone cs_open() error!\n");
return 0;
}
unsigned long library_ptr = 0x0;
char libptr_raw[16];
count = cs_disasm(handle, data, nread, code_ptr, 0, &insn);
if (count == 3) {
if (strcmp(insn[0].mnemonic, "jmp") == 0 &&
strcmp(insn[1].mnemonic, "push") == 0 &&
strcmp(insn[2].mnemonic, "jmp") == 0) {
first_inst = insn[0].op_str;
//cout << insn[0].address << " ";
//cout << first_inst.substr(first_inst.find("0x"), first_inst.find("]") - first_inst.find("0x")) << endl;
unsigned long got_slot = insn[0].address +
strtoul(first_inst.substr(first_inst.find("0x"), first_inst.find("]") - first_inst.find("0x")).c_str(),
NULL, 16) + 0x6;
//cout << got_slot << endl;
nread = readmemory(pid, data, (const char *) got_slot, 8);
//cout << nread << endl;
sprintf(libptr_raw, "0x%x%x%x%x%x%x%x%x", data[7], data[6], data[5], data[4], data[3], data[2], data[1], data[0]);
//printf("%s\n", libptr_raw);
library_ptr = strtoul(libptr_raw, NULL, 0);
//printf("%lx\n", library_ptr);
}
cs_free(insn, count);
} //else {
//printf("ERROR: Failed to disassemble given code!\n");
//}
cs_close(&handle);
free(data);
return library_ptr;
}
bool branch_inst(cs_insn insn){
if (strcmp(insn.mnemonic, "call") == 0 ||
strcmp(insn.mnemonic, "jmp") == 0 ||
strcmp(insn.mnemonic, "je") == 0 ||
strcmp(insn.mnemonic, "jl") == 0 ||
strcmp(insn.mnemonic, "jle") == 0 ||
strcmp(insn.mnemonic, "jb") == 0 ||
strcmp(insn.mnemonic, "jbe") == 0 ||
strcmp(insn.mnemonic, "jg") == 0 ||
strcmp(insn.mnemonic, "jge") == 0 ||
strcmp(insn.mnemonic, "ja") == 0 ||
strcmp(insn.mnemonic, "jae") == 0 ||
strcmp(insn.mnemonic, "js") == 0 ||
strcmp(insn.mnemonic, "jo") == 0 ||
strcmp(insn.mnemonic, "jnp") == 0 ||
strcmp(insn.mnemonic, "jns") == 0 )
return true;
return false;
}
bool is_complete(int *tc_set, int tc_priority_mv) {
int gsize = get_gadget_size(tc_priority_mv); // defined in lookup.cpp
for (int i = 0; i < gsize; i++)
if(!tc_set[i])
return false;
return true;
}
int sum_tc(int *tc_set, int tc_priority_mv) {
int gsize = get_gadget_size(tc_priority_mv); // defined in lookup.cpp
int total = 0;
for (int i = 0; i < gsize; i++){
//cout << "TCSET: " << i << " " << tc_set[i] << endl;
total += tc_set[i]? 1: 0;
}
return total;
}
void rerand_recursive_codepage_harvest(
int pid,
unsigned long codeptr,
set<unsigned long> *visited,
int *tc_set,
set<string> *gadget_set,
set<string> *regset,
int which,
double start_time,
bool exec_only)
{
bool complete = is_complete(tc_set, which);
jitrop_timing category = (jitrop_timing) which;
if (category == JITROP_TIME_MOVTC || category == JITROP_TIME_MOVTC_COUNT) {
if (complete && regset->size() >= 4) return;
} else {
if (complete) return;
}
unsigned long pageno = page_no(codeptr);
set<unsigned long>::iterator it = visited->find(pageno);
if (*it == pageno) return;
visited->insert(pageno);
unsigned char *data = NULL;
size_t read_size = 4096;
if ((data = (unsigned char *) malloc(read_size * sizeof(char))) == NULL) {
if (IS_LOG) printf("sorry, there was a memory allocation error.\n");
return;
}
size_t nread = readmemory(pid, data, (const char *) pageno, read_size);
//printf("Code pointer = %lx %ld\n", pageno, nread);
if (nread <= 0) {
if (IS_LOG) printf("Memory read error, no data to disassemble.\n");
return;
}
csh handle;
cs_insn *insn;
size_t count;
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
if (IS_LOG) printf("Capstone cs_open() error!\n");
return;
}
printf("%lx ", pageno);
chrono::milliseconds gstart_time = chrono::duration_cast< chrono::milliseconds >(
chrono::system_clock::now().time_since_epoch());
vector<string> result = create_gadgets(data, nread, pageno, 5, 3);
chrono::milliseconds gend_time = chrono::duration_cast< chrono::milliseconds >(
chrono::system_clock::now().time_since_epoch());
//cout << (gend_time.count() - gstart_time.count()) << " " << endl;
switch(category) {
case JITROP_TIME_TC: get_min_tc_set(result, tc_set); break;
case JITROP_TIME_PRIORITY: get_priority_set(result, tc_set); break;
case JITROP_TIME_MOVTC: get_mov_tc_set(result, tc_set, regset); break;
case JITROP_TIME_MOVTC_COUNT: get_mov_tc_count(result, tc_set, gadget_set, regset); break;
case JITROP_TIME_PAYLOAD1: get_payload_set_one(result, tc_set); break;
default: get_min_tc_set(result, tc_set); break;
}
chrono::milliseconds cur_time = chrono::duration_cast< chrono::milliseconds >(
chrono::system_clock::now().time_since_epoch());
cout << (gend_time.count() - gstart_time.count()) << " "
<< (cur_time.count() - gend_time.count()) << " "
<< (cur_time.count()- start_time) << " "
<< sum_tc(tc_set, which) << endl;
result.clear();
count = cs_disasm(handle, data, nread, pageno, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
//printf("0x%" PRIx64 ":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
if (branch_inst(insn[j])) {
if (insn[j].op_str[0] == '0' && insn[j].op_str[1] == 'x'){
//printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
unsigned long old_codeptr = strtoul(insn[j].op_str, NULL, 16);
if (!exec_only) {
if (strcmp(insn[j].mnemonic, "call") == 0) {
unsigned long new_codeptr = get_got_slot(old_codeptr, pid);
//printf("%lx\n", new_codeptr);
if (new_codeptr > 0) {
rerand_recursive_codepage_harvest(
pid,
new_codeptr,
visited,
tc_set,
gadget_set,
regset,
which,
start_time,
exec_only );
}
}
}
rerand_recursive_codepage_harvest(
pid,
old_codeptr,
visited,
tc_set,
gadget_set,
regset,
which,
start_time,
exec_only );
}
}
}
cs_free(insn, count);
}
cs_close(&handle);
free(data);
}
void run_rerand_timing(int pid, unsigned long addr, int which) {
set<unsigned long> visited;
int gsize = get_gadget_size(which);
int tc_set[gsize] = {0, };
set<string> regset;
set<string> gadget_set;
chrono::milliseconds start_time = chrono::duration_cast< chrono::milliseconds >(
chrono::system_clock::now().time_since_epoch());
rerand_recursive_codepage_harvest(
pid,
addr,
&visited,
tc_set,
&gadget_set,
®set,
which,
start_time.count(),
false );
chrono::milliseconds cur_time = chrono::duration_cast< chrono::milliseconds >(
chrono::system_clock::now().time_since_epoch());
//cout.precision(3);
cout << (cur_time.count() - start_time.count()) << " "
<< sum_tc(tc_set, which) << endl;
jitrop_timing category = (jitrop_timing) which;
if (category == JITROP_TIME_MOVTC_COUNT) {
for (int i = 0; i < gsize; i++) {
cout << tc_set[i] << " ";
}
cout << endl;
}
}
int scan_codepages (
int pid,
unsigned long codeptr,
set<unsigned long> *visited,
bool exec_only,
int which,
int *limit,
int total_cp)
{
if (codeptr == 0x0) return 0;
if (total_cp > 0 && *limit > total_cp) return 0;
unsigned long pageno = page_no(codeptr);
set<unsigned long>::iterator it = visited->find(pageno);
if (*it == pageno)
return 0;
visited->insert(pageno);
printf("0x%lx\n", pageno);
run_rerand_timing(pid, pageno, which);
*limit += 1;
unsigned char *data = NULL;
size_t read_size = 4096;
if ((data = (unsigned char *)malloc(read_size * sizeof(char))) == NULL) {
if (IS_LOG) printf("sorry, there was a memory allocation error.\n");
return -1;
}
size_t nread = readmemory(pid, data, (const char *) pageno, read_size);
if (nread <= 0) {
if (IS_LOG) printf("Memory read error, no data to disassemble.\n");
return -1;
}
csh handle;
cs_insn *insn;
size_t count;
//printf("%ld\n", sizeof(custom_code));
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
if (IS_LOG) printf("Capstone cs_open() error!\n");
return -1;
}
count = cs_disasm(handle, data, nread, pageno, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
if (branch_inst(insn[j])) {
if (insn[j].op_str[0] == '0' && insn[j].op_str[1] == 'x') {
//printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
// insn[j].op_str);
unsigned long new_codeptr = strtoul(insn[j].op_str, NULL, 16);
//printf("%ld %lx\n", new_codeptr, new_codeptr);
scan_codepages(pid, new_codeptr, visited, exec_only, which, limit, total_cp);
}
}
}
cs_free(insn, count);
} //else {
//printf("ERROR: Failed to disassemble given code!\n");
//}
cs_close(&handle);
free(data);
return 0;
}
void init_rerand_timing(int pid, unsigned long addr, int which, int cpages) {
set<unsigned long> visited_for_scan;
int limit = 0;
scan_codepages(pid, addr, &visited_for_scan, true, which, &limit, cpages);
//printf("%s\n", "Here");
//run_rerand_timing(pid, addr, which);
}
vector<string> tc_recursive_codepage_harvest(
int pid,
unsigned long codeptr,
set<unsigned long> *visited,
bool exec_only)
{
vector<string> to_return, new_result;
unsigned long pageno = page_no(codeptr);
set<unsigned long>::iterator it = visited->find(pageno);
if (*it == pageno)
return to_return;
visited->insert(pageno);
unsigned char *data = NULL;
size_t read_size = 4096;
if ((data = (unsigned char *)malloc(read_size * sizeof(char))) == NULL) {
if (IS_LOG) printf("sorry, there was a memory allocation error.\n");
return to_return; // to_return is empty at this point
}
size_t nread = readmemory(pid, data, (const char *) pageno, read_size);
if (nread <= 0) {
if (IS_LOG) printf("Memory read error, no data to disassemble.\n");
return to_return; // to_return is empty at this point
}
csh handle;
cs_insn *insn;
size_t count;
//printf("%ld\n", sizeof(custom_code));
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) {
if (IS_LOG) printf("Capstone cs_open() error!\n");
return to_return; // to_return is empty at this point
}
printf("0x%lx\n", pageno);
vector<string> result = create_gadgets(data, nread, pageno, 5, 3);
count = cs_disasm(handle, data, nread, pageno, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
//printf("0x%" PRIx64 ":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
// insn[j].op_str);
if (branch_inst(insn[j]))
{
if (insn[j].op_str[0] == '0' && insn[j].op_str[1] == 'x'){
//printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
unsigned long old_codeptr = strtoul(insn[j].op_str, NULL, 16);
if (!exec_only) {
if (strcmp(insn[j].mnemonic, "call") == 0) {
unsigned long new_codeptr = get_got_slot(old_codeptr, pid);
//printf("%lx\n", new_codeptr);
if (new_codeptr > 0) {
new_result = tc_recursive_codepage_harvest(
pid,
new_codeptr,
visited,
exec_only );
copy(new_result.begin(), new_result.end(), back_inserter(to_return));
copy(result.begin(), result.end(), back_inserter(to_return));
new_result.clear();
result.clear();
}
}
}
new_result = tc_recursive_codepage_harvest(
pid,
old_codeptr,
visited,
exec_only );
copy(new_result.begin(), new_result.end(), back_inserter(to_return));
copy(result.begin(), result.end(), back_inserter(to_return));
new_result.clear();
result.clear();
}
}
}
cs_free(insn, count);
} //else {
//printf("ERROR: Failed to disassemble given code!\n");
//}
cs_close(&handle);
free(data);
return to_return;
}
void find_tc_gadgets(int pid, unsigned long addr, bool exec_only) {
set<unsigned long> visited;
int tc_set[TC_GADGETS] = {0, };
vector<string> result = tc_recursive_codepage_harvest(pid, addr, &visited, exec_only);
lookup_gadgets(result);
}
void find_priority_gadgets(int pid, unsigned long addr, bool exec_only) {
set<unsigned long> visited;
int tc_set[TC_GADGETS] = {0, };
vector<string> result = tc_recursive_codepage_harvest(pid, addr, &visited, exec_only);
lookup_priority_gadgets(result);
}
void find_movtc_gadgets(int pid, unsigned long addr, bool exec_only) {
set<unsigned long> visited;
int tc_set[TC_GADGETS] = {0, };
set<string> regset;
set<string> gadget_set;
vector<string> result = tc_recursive_codepage_harvest(pid, addr, &visited, exec_only);
lookup_movtc_gadgets(result, &gadget_set, ®set);
}