-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtrace.c
841 lines (772 loc) · 26.3 KB
/
trace.c
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#include "binflow.h"
#define M_OFFSETOF(STRUCT, ELEMENT) \
(unsigned long) &((STRUCT *)NULL)->ELEMENT;
struct options opts;
int get_ptrace_regs_portable(handle_t *h, x86_regs_t *regs, struct user_regs_struct *pt_reg)
{
if (ptrace(PTRACE_GETREGS, h->pid, NULL, pt_reg) < 0) {
perror("PTRACE_GETREGS");
return -1;
}
#ifdef __x86_64__
regs->esp = pt_reg->rsp;
regs->eip = pt_reg->rip;
regs->eax = pt_reg->rax;
regs->ebx = pt_reg->rbx;
regs->ecx = pt_reg->rcx;
regs->edx = pt_reg->rdx;
regs->esi = pt_reg->rsi;
regs->edi = pt_reg->rdi;
#else
regs->esp = pt_reg->esp;
regs->eip = pt_reg->eip;
regs->eax = pt_reg->eax;
regs->ebx = pt_reg->ebx;
regs->ecx = pt_reg->ecx;
regs->edx = pt_reg->edx;
regs->esi = pt_reg->esi;
regs->edi = pt_reg->edi;
#endif
return 0;
}
int set_ptrace_eip_portable(handle_t *h, struct user_regs_struct *pt_regs, unsigned long eip)
{
#ifdef __x86_64__
pt_regs->rip = eip;
#else
pt_regs->eip = eip;
#endif
if (ptrace(PTRACE_SETREGS, h->pid, NULL, pt_regs) < 0) {
perror("PTRACE_GETREGS");
return -1;
}
return 0;
}
int pid_write(int pid, void *dest, const void *src, size_t len)
{
size_t rem = len % sizeof(void *);
size_t quot = len / sizeof(void *);
unsigned char *s = (unsigned char *) src;
unsigned char *d = (unsigned char *) dest;
while (quot-- != 0) {
if ( ptrace(PTRACE_POKEDATA, pid, d, *(void **)s) == -1 )
goto out_error;
s += sizeof(void *);
d += sizeof(void *);
}
if (rem != 0) {
long w;
unsigned char *wp = (unsigned char *)&w;
w = ptrace(PTRACE_PEEKDATA, pid, d, NULL);
if (w == -1 && errno != 0) {
d -= sizeof(void *) - rem;
w = ptrace(PTRACE_PEEKDATA, pid, d, NULL);
if (w == -1 && errno != 0)
goto out_error;
wp += sizeof(void *) - rem;
}
while (rem-- != 0)
wp[rem] = s[rem];
if (ptrace(PTRACE_POKEDATA, pid, (void *)d, (void *)w) == -1)
goto out_error;
}
return 0;
out_error:
fprintf(stderr, "pid_write() failed, pid: %d: %s\n", pid, strerror(errno));
return -1;
}
int pid_read(int pid, void *dst, const void *src, size_t len)
{
int sz = len / sizeof(void *);
unsigned char *s = (unsigned char *)src;
unsigned char *d = (unsigned char *)dst;
long word;
while (sz-- != 0) {
word = ptrace(PTRACE_PEEKTEXT, pid, s, NULL);
if (word == -1 && errno)
return -1;
*(long *)d = word;
s += sizeof(long);
d += sizeof(long);
}
return 0;
}
static char * get_pointer_range_str(handle_t *h, unsigned long addr)
{
int j;
char *s;
for (j = 0; j < 4; j++) {
if (addr >= h->addrspace[j].svaddr && addr <= h->addrspace[j].evaddr)
switch(j) {
case TEXT_SPACE:
if (opts.strings) {
s = getstr(addr, h->pid);
if (s != NULL)
return s;
else
return xfmtstrdup("(text_t *)%lx", addr);
}
return xfmtstrdup("(text_t *)");
case HEAP_SPACE:
if (opts.strings) {
s = getstr(addr, h->pid);
if (s != NULL)
return s;
else
return xfmtstrdup("(heap_t *)%lx", addr);
}
return xfmtstrdup("(heap_t *)");
case DATA_SPACE:
if (opts.strings) {
s = getstr(addr, h->pid);
if (s != NULL)
return s;
else
return xfmtstrdup("(data_t *)%lx", addr);
}
return xfmtstrdup("(data_t *)");
case STACK_SPACE:
if (opts.strings) {
s = getstr(addr, h->pid);
if (s != NULL)
return s;
else
return xfmtstrdup("(stack_t *)%lx", addr);
}
return xfmtstrdup("(stack_t *)");
}
}
return NULL;
}
/*
* Do not let the unsafe strcpy/strcat
* in this crazy function scare you. Everything
* is pre-allocated and nothing will ever exceed
* the sizes to create an overflow condition.
*/
char * build_arg_string(handle_t *h, branch_site_t *branch)
{
char *str = (char *)heapAlloc(1024);
char *tmp = (char *)alloca(512);
char *args[6];
char *ptr_range, *p;
struct user_regs_struct regs;
int is_str = 0;
ptrace(PTRACE_GETREGS, h->pid, NULL, ®s);
switch(branch->branch.argc) {
case 0:
strcpy(str, "(");
break;
case 1:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
if ((p = strchr(str, ',')) != NULL)
*p = '\0'; // , is added by getstr()
free(args[0]);
break;
case 2:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
sprintf(tmp, "0x%llx", regs.rsi);
ptr_range = get_pointer_range_str(h, regs.rsi);
if (ptr_range == NULL)
args[1] = xstrdup(tmp);
else {
if (opts.strings)
args[1] = xfmtstrdup("%s", ptr_range);
else
args[1] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
if ((p = strchr(args[1], ',')) != NULL)
*p = '\0';
strcat(str, args[1]);
free(args[0]);
free(args[1]);
break;
case 3:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
sprintf(tmp, "0x%llx", regs.rsi);
ptr_range = get_pointer_range_str(h, regs.rsi);
if (ptr_range == NULL)
args[1] = xstrdup(tmp);
else {
if (opts.strings)
args[1] = xfmtstrdup("%s", ptr_range);
else
args[1] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[1]);
sprintf(tmp, "0x%llx", regs.rdx);
ptr_range = get_pointer_range_str(h, regs.rdx);
if (ptr_range == NULL)
args[2] = xstrdup(tmp);
else {
if (opts.strings)
args[2] = xfmtstrdup("%s", ptr_range);
else
args[2] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
if ((p = strchr(args[2], ',')) != NULL)
*p = '\0';
strcat(str, args[2]);
free(args[0]);
free(args[1]);
free(args[2]);
break;
case 4:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
sprintf(tmp, "0x%llx", regs.rsi);
ptr_range = get_pointer_range_str(h, regs.rsi);
if (ptr_range == NULL)
args[1] = xstrdup(tmp);
else {
if (opts.strings)
args[1] = xfmtstrdup("%s", ptr_range);
else
args[1] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[1]);
sprintf(tmp, "0x%llx", regs.rdx);
ptr_range = get_pointer_range_str(h, regs.rdx);
if (ptr_range == NULL)
args[2] = xstrdup(tmp);
else {
if (opts.strings)
args[2] = xfmtstrdup("%s", ptr_range);
else
args[2] = xfmtstrdup("%s%s", ptr_range, tmp);
}
free(ptr_range);
strcat(str, ", ");
strcat(str, args[2]);
sprintf(tmp, "0x%llx", regs.rdx);
ptr_range = get_pointer_range_str(h, regs.rcx);
if (ptr_range == NULL)
args[3] = xstrdup(tmp);
else {
if (opts.strings)
args[3] = xfmtstrdup("%s", ptr_range);
else
args[3] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
if ((p = strchr(args[3], ',')) != NULL)
*p = '\0';
strcat(str, args[3]);
free(args[0]);
free(args[1]);
free(args[2]);
free(args[3]);
//sprintf(str, "(0x%llx, 0x%llx, 0x%llx, 0x%llx)", regs.rdi, regs.rsi, regs.rdx, regs.rcx);
break;
case 5:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
sprintf(tmp, "0x%llx", regs.rsi);
ptr_range = get_pointer_range_str(h, regs.rsi);
if (ptr_range == NULL)
args[1] = xstrdup(tmp);
else {
if (opts.strings)
args[1] = xfmtstrdup("%s", ptr_range);
else
args[1] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[1]);
sprintf(tmp, "0x%llx", regs.rdx);
ptr_range = get_pointer_range_str(h, regs.rdx);
if (ptr_range == NULL)
args[2] = xstrdup(tmp);
else {
if (opts.strings)
args[2] = xfmtstrdup("%s", ptr_range);
else
args[2] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[2]);
sprintf(tmp, "0x%llx", regs.rcx);
ptr_range = get_pointer_range_str(h, regs.rcx);
if (ptr_range == NULL)
args[3] = xstrdup(tmp);
else {
if (opts.strings)
args[3] = xfmtstrdup("%s", ptr_range);
else
args[3] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[3]);
sprintf(tmp, "0x%llx", regs.r9);
ptr_range = get_pointer_range_str(h, regs.r9);
if (ptr_range == NULL)
args[4] = xstrdup(tmp);
else {
if (opts.strings)
args[4] = xfmtstrdup("%s", ptr_range);
else
args[4] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
if ((p = strchr(args[4], ',')) != NULL)
*p = '\0';
strcat(str, args[4]);
free(args[0]);
free(args[1]);
free(args[2]);
free(args[3]);
free(args[4]);
break;
case 6:
sprintf(tmp, "0x%llx", regs.rdi);
ptr_range = get_pointer_range_str(h, regs.rdi);
if (ptr_range == NULL)
args[0] = xstrdup(tmp);
else {
if (opts.strings)
args[0] = xfmtstrdup("%s", ptr_range);
else
args[0] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcpy(str, "(");
strcat(str, args[0]);
sprintf(tmp, "0x%llx", regs.rsi);
ptr_range = get_pointer_range_str(h, regs.rsi);
if (ptr_range == NULL)
args[1] = xstrdup(tmp);
else {
if (opts.strings)
args[1] = xfmtstrdup("%s", ptr_range);
else
args[1] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[1]);
sprintf(tmp, "0x%llx", regs.rdx);
ptr_range = get_pointer_range_str(h, regs.rdx);
if (ptr_range == NULL)
args[2] = xstrdup(tmp);
else {
if (opts.strings)
args[2] = xfmtstrdup("%s", ptr_range);
else
args[2] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[2]);
sprintf(tmp, "0x%llx", regs.rcx);
ptr_range = get_pointer_range_str(h, regs.rcx);
if (ptr_range == NULL)
args[3] = xstrdup(tmp);
else {
if (opts.strings)
args[3] = xfmtstrdup("%s", ptr_range);
else
args[3] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[3]);
sprintf(tmp, "0x%llx", regs.r9);
ptr_range = get_pointer_range_str(h, regs.r9);
if (ptr_range == NULL)
args[4] = xstrdup(tmp);
else {
if (opts.strings)
args[4] = xfmtstrdup("%s", ptr_range);
else
args[4] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
strcat(str, args[4]);
sprintf(tmp, "0x%llx", regs.r8);
ptr_range = get_pointer_range_str(h, regs.r8);
if (ptr_range == NULL)
args[5] = xstrdup(tmp);
else {
if (opts.strings)
args[5] = xfmtstrdup("%s", ptr_range);
else
args[5] = xfmtstrdup("%s%s", ptr_range, tmp);
}
xfree(ptr_range);
strcat(str, ", ");
if ((p = strchr(args[5], ',')) != NULL)
*p = '\0';
strcat(str, args[5]);
free(args[0]);
free(args[1]);
free(args[2]);
free(args[3]);
free(args[4]);
free(args[5]);
break;
}
strcat(str, ")");
return str;
}
char *getstr(unsigned long addr, int pid)
{
int i, j, c;
uint8_t buf[sizeof(long)];
char *string = (char *)heapAlloc(256);
unsigned long vaddr;
string[0] = '"';
for (c = 1, i = 0; i < 256; i += sizeof(long)) {
vaddr = addr + i;
if (pid_read(pid, buf, (void *)vaddr, sizeof(long)) == -1) {
fprintf(stderr, "pid_read() failed: %s <0x%lx>\n", strerror(errno), vaddr);
exit(-1);
}
for (j = 0; j < sizeof(long); j++) {
if (buf[j] == '\n') {
string[c++] = '\\';
string[c++] = 'n';
continue;
}
if (buf[j] == '\t') {
string[c++] = '\\';
string[c++] = 't';
continue;
}
if (buf[j] != '\0' && isascii(buf[j]))
string[c++] = buf[j];
else {
if (j == 1)
return NULL;
goto out;
}
}
}
out:
string[c++] = '"';
//string[c++] = ',';
string[c] = '\0';
return string;
}
void set_breakpoint (int pid, unsigned long bp, unsigned long *backup)
{
unsigned long bpinstr;
*backup = ptrace(PTRACE_PEEKDATA, pid, bp, 0);
bpinstr = (*backup & ~0xff) | 0xcc;
pid_write(pid, (void *)bp, (void *)&bpinstr, sizeof(long));
}
void remove_breakpoint_and_step(int pid, unsigned long bp, unsigned long backup)
{
int status, i;
FILE *fd;
unsigned int eip_offset, ip;
unsigned long addr = bp; // addr = breakpoint addr
struct user_regs_struct pt_reg;
siginfo_t siginfo;
char dbuf[16];
#ifdef __x86_64__
eip_offset = M_OFFSETOF(struct user_regs_struct, rip);
#else
eip_offset = m_OFFSETOF(struct user_regs_struct, eip);
#endif
pid_write(pid, (void *)addr, (void *)&backup, sizeof(long));
//pid_read(pid, dbuf, addr, sizeof(long));
/* now we set the process' instruction pointer back to the start of the original instruction */
long ret = ptrace(PTRACE_POKEUSER, pid, eip_offset, addr);
if (ret < 0) {
perror("PTRACE_POKEUSER");
exit(-1);
}
if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) < 0) {
perror("PTRACE_SINGLESTEP");
exit(-1);
}
wait(&status);
//waitpid(pid, &status, WUNTRACED|WCONTINUED);
}
unsigned long get_instruction_pointer(int pid)
{
unsigned int eip_offset; // offset of rip in struct user_regs_struct
unsigned long eip;
#ifdef __x86_64__
eip_offset = M_OFFSETOF(struct user_regs_struct, rip);
#else
eip_offset = m_OFFSETOF(struct user_regs_struct, eip);
#endif
eip = ptrace(PTRACE_PEEKUSER, pid, eip_offset);
return eip;
}
unsigned long get_top_of_stack(handle_t *h)
{
unsigned int esp_offset;
unsigned long esp;
int pid = h->pid;
struct user_regs_struct pt_reg;
unsigned long retval;
int i;
ptrace(PTRACE_GETREGS, h->pid, NULL, &pt_reg);
pid_read(pid, (void *)&retval, (void *)pt_reg.rsp, 8);
return retval; //ptrace(PTRACE_PEEKDATA, pid, (void *)pt_reg.rsp + 500, NULL);
}
long lookup_breakpoint_origval(handle_t *h, unsigned long addr)
{
unsigned int i;
for (i = 0; i < h->branch_count; i++) {
if (h->branch_site[i].branch.location == addr)
return h->branch_site[i].branch.orig_word;
}
return 0;
}
branch_site_t * lookup_breakpoint_branch(handle_t *h, unsigned long addr)
{
unsigned int i;
for (i = 0; i < h->branch_count; i++) {
if (h->branch_site[i].branch.location == addr)
return &h->branch_site[i];
}
return NULL;
}
int instrument_process(handle_t *h)
{
size_t i;
for (i = 0; i < h->branch_count; i++) {
switch(h->branch_site[i].branch_type) {
case IMMEDIATE_CALL:
if (opts.debug)
printf("[+] Setting breakpoint on immediate 'call %lx' instruction: %lx\n",
h->branch_site[i].branch.target_vaddr, h->branch_site[i].branch.location);
set_breakpoint(h->pid, h->branch_site[i].branch.location, &h->branch_site[i].branch.orig_word);
break;
case INDIRECT_CALL:
break;
case RET_TARGET:
if (opts.debug)
printf("[+] Setting breakpoint on ret target at %lx\n", h->branch_site[i].branch.location);
set_breakpoint(h->pid, h->branch_site[i].branch.location, &h->branch_site[i].branch.orig_word);
break;
case IMMEDIATE_JMP:
if (opts.debug)
printf("[+] Setting breakpoint on immediate jmp instruction: %lx\n", h->branch_site[i].branch.location);
set_breakpoint(h->pid, h->branch_site[i].branch.location, &h->branch_site[i].branch.orig_word);
break;
case INDIRECT_JMP:
break;
}
}
}
int process_breakpoint_location(handle_t *h, unsigned long bpaddr)
{
char *shdr1, *shdr2, *argstr, *f1, *f2;
unsigned long vaddr;
branch_site_t *branch_site = lookup_breakpoint_branch(h, bpaddr);
if (branch_site == NULL) {
fprintf(stderr, "[!] failed to locate branch data for breakpoint at %lx\n", bpaddr);
return -1;
}
switch(branch_site->branch_type) {
case IMMEDIATE_CALL:
switch(branch_site->branch.calltype) {
case LOCAL_CALL:
/*
* This may not be a call but a ret_target which is indexed in as a call
*/
argstr = build_arg_string(h, branch_site);
printf("%sLOCAL_call@0x%lx%s: %s%s\n", GREEN, bpaddr, WHITE, branch_site->branch.function, argstr);
free (argstr);
break;
case PLT_CALL:
argstr = build_arg_string(h, branch_site);
printf("%sPLT_call@0x%lx:%s %s%s\n", GREEN, bpaddr, WHITE, branch_site->branch.function, argstr);
free(argstr);
break;
}
break;
case IMMEDIATE_JMP:
if (!opts.cflow)
break;
shdr1 = get_section_by_range(h, bpaddr);
if (shdr1 == NULL)
shdr1 = (char *)_strdupa("<unknown section>");
shdr2 = get_section_by_range(h, branch_site->branch.target_vaddr);
if (shdr2 == NULL)
shdr2 = (char *)_strdupa("<unknown_section>");
if (!strcmp(shdr1, ".text") && !strcmp(shdr2, ".text")) {
f1 = get_fn_by_range(h, bpaddr);
f2 = get_fn_by_range(h, branch_site->branch.target_vaddr);
if (f1 == NULL || f2 == NULL) {
f1 = xstrdup(shdr1); // replace func names with shdr names if they can't be found
f2 = xstrdup(shdr2);
}
printf("%s(CONTROL FLOW CHANGE [%s%s%s]):%s Jump from %s %lx to %s %lx\n",
CYAN, BLUE, branch_site->branch.mnemonic, CYAN, WHITE,
f1, bpaddr, f2, branch_site->branch.target_vaddr);
free(f1);
free(f2);
} else
/* If we are in any section other than .text then we print the
* src and dst section name instead of the function name.
*/
printf("%s(CONTROL FLOW CHANGE [%s%s%s]):%s Jump from %s %lx into %s %lx\n",
CYAN, BLUE, branch_site->branch.mnemonic, CYAN, WHITE,
shdr1, bpaddr, shdr2, branch_site->branch.target_vaddr);
break;
}
return 0;
}
int examine_process(handle_t *h)
{
unsigned int i, j;
h->addrspace = (struct address_space *)heapAlloc(sizeof(struct address_space) * MAX_ADDR_SPACE);
struct user_regs_struct pt_regs;
x86_regs_t x86_regs;
int status;
unsigned int eip_offset;
unsigned long orig_word;
unsigned long bpaddr;
unsigned long null;
siginfo_t siginfo;
char buf[16];
#ifdef __x86_64__
eip_offset = M_OFFSETOF(struct user_regs_struct, rip);
#else
eip_offset = m_OFFSETOF(struct user_regs_struct, eip);
#endif
get_address_space((struct address_space *)h->addrspace, h->pid, h->path);
if (opts.verbose || opts.debug)
printf("[+] Tracing process %d\n", h->pid);
if (opts.elfdata) {
printf("[+] Printing Symbol Information:\n\n");
for (i = 0; i < h->lsc; i++) {
if (h->lsyms[i].name == NULL)
printf("UNKNOWN: 0x%lx\n", h->lsyms[i].value);
else
printf("%s 0x%lx\n", h->lsyms[i].name, h->lsyms[i].value);
}
for (i = 0; i < h->dsc; i++) {
if (h->dsyms[i].name == NULL)
printf("UNKNOWN: 0x%lx\n", h->dsyms[i].value);
else
printf("%s 0x%lx\n", h->dsyms[i].name, h->dsyms[i].value);
}
printf("\n[+] Printing shared library dependencies:\n\n");
parse_dynamic_dt_needed(h);
for (i = 0; i < h->lnc; i++) {
printf("[%d]\t%s\n", i + 1, h->libnames[i]);
}
}
int newpid;
int special;
do_trace:
special = 0;
if (ptrace(PTRACE_CONT, h->pid, NULL, NULL) < 0) {
perror("PTRACE_CONT");
return -1;
}
wait(&status);
if (WIFEXITED(status))
goto done;
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
if ((status >> 8) == (SIGTRAP | PTRACE_EVENT_FORK << 8)) {
special++;
ptrace(PTRACE_GETEVENTMSG, h->pid, NULL, (void *)&newpid);
printf("New process forked: %d\n", newpid);
} else
if ((status >> 8) == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
special++;
ptrace(PTRACE_GETEVENTMSG, h->pid, NULL, (void *)&newpid);
printf("New process cloned: %d\n", newpid);
} else
if ((status >> 8) == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
special++;
ptrace(PTRACE_GETEVENTMSG, h->pid, NULL, (void *)&newpid);
printf("Dying pid: %d\n", newpid);
}
// continue back tracing
if (special)
goto do_trace;
char dbuf[16];
/* Regular SIGTRAP? */
bpaddr = get_instruction_pointer(h->pid); // will be at bpaddr + 1
orig_word = lookup_breakpoint_origval(h, bpaddr - 1);
if (orig_word == 0)
goto do_trace;
process_breakpoint_location(h, bpaddr - 1);
remove_breakpoint_and_step(h->pid, bpaddr - 1, orig_word);
set_breakpoint(h->pid, bpaddr - 1, &null);
goto do_trace;
}
goto do_trace;
done:
return 0;
}