-
Notifications
You must be signed in to change notification settings - Fork 7
/
exgdbcmdmethods.py
1692 lines (1548 loc) · 53.7 KB
/
exgdbcmdmethods.py
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from enert import *
dashboard_is_enabled = False
peda_is_enabled = False
if "dashboard" in globals():
dashboard_is_enabled = True
elif "PEDA" in globals():
peda_is_enabled = True
r_asmStr = re.compile(r"0x[0-9a-f]*\x1b\[0m\s\((.*)\)")
r_asmStr_2 = re.compile(r"<.*>:\s*(.*)")
r_asmStr_3 = re.compile(r"^([^\s]*)\s*(.*)")
r_args = re.compile(r"(.*),(.*)")
regs = sum(REGISTERS.values(), [])
asm_ops = {};
asm_ops["mov"] = "mov_reg_imm:mov_reg_reg:mov_reg_ptr:mov_ptr_imm:mov_ptr_reg:mov_ptr_imm"
asm_ops["mov_reg_imm"] = "Write[arg1]:arg2"
asm_ops["mov_reg_reg"] = "Read[arg2]:arg2_data,Write[arg1]:arg2_data"
asm_ops["mov_reg_ptr"] = "Read[arg2_reg]:arg2_reg_data,Read[arg2_data]:main_data,Write[arg1]:main_data"
asm_ops["mov_ptr_imm"] = "Read[arg1_reg]:arg1_reg_data,Write[arg1_data]:arg2"
asm_ops["mov_ptr_reg"] = "Read[arg2]:arg2_data,Read[arg1_reg]:arg1_reg_data,Write[arg1_data]:arg2_data"
asm_ops["mov_ptr_ptr"] = "Read[arg2_reg]:arg2_reg_data,Read[arg2_data]:main_data,Read[arg1_reg]:arg1_reg_data,Write[arg1_data]:main_data"
asm_ops["lea"] = "Read[arg2_reg]:arg2_reg_data,Write[arg1]:arg2_data"
asm_ops["cmp"] = "Read[arg1]:arg1_data"
asm_ops["add"] = "add_reg_reg:add_reg_imm:add_reg_ptr:add_ptr_imm:add_ptr_reg"
asm_ops["add_reg_reg"] = "Read[arg1]:arg1_data,Read[arg2]:arg2_data,Write[arg1]:new_data"
asm_ops["add_reg_imm"] = "Read[arg1]:arg1_data,Write[arg1]:new_data"
asm_ops["add_reg_ptr"] = "Read[arg1]:arg1_data,Read[arg2_reg]:arg2_reg_data,Read[arg2_data]:org_data,Write[arg1]:new_data"
asm_ops["add_ptr_imm"] = ""
asm_ops["add_ptr_reg"] = ""
asm_ops["sub"] = "sub_reg_reg:sub_reg_imm:sub_reg_ptr:sub_ptr_imm:sub_ptr_reg"
asm_ops["sub_reg_reg"] = "Read[arg1]:arg1_data,Read[arg2]:arg2_data,Write[arg1]:new_data"
asm_ops["sub_reg_imm"] = "Read[arg1]:arg1_data,Write[arg1]:new_data"
asm_ops["sub_reg_ptr"] = "Read[arg1]:arg1_data,Read[arg2_reg]:arg2_reg_data,Read[arg2_data]:org_data,Write[arg1]:new_data"
asm_ops["sub_ptr_imm"] = ""
asm_ops["sub_ptr_reg"] = ""
asm_ops["nop"] = ""
(arch, bits) = peda.getarch()
if bits == 64:
asm_ops["push"] = "Read[rsp]:sp_addr,Write[rsp]:new_sp_addr,Read[rsp]:new_sp_addr,Write[new_sp_addr]:arg1_data"
if bits == 32:
asm_ops["push"] = "Read[esp]:sp_addr,Write[esp]:new_sp_addr,Read[esp]:new_sp_addr,Write[new_sp_addr]:arg1_data"
if bits == 64:
asm_ops["ret"] = "Read[rsp]:sp_addr,Read[sp_addr]:sp_data,Write[rip]:sp_data,Read[rsp]:sp_addr,Write[rsp]new_sp_addr"
else:
asm_ops["ret"] = "Read[esp]:sp_addr,Read[sp_addr]:sp_data,Write[rip]:sp_data,Read[esp]:sp_addr,Write[esp]new_sp_addr"
if bits == 64:
asm_ops["leave"] = "Read[rbp]:bp_addr,Write[rsp]:bp_addr,Read[rsp]:sp_addr,Read[sp_addr]:sp_data,Write[rbp]:sp_data,Read[rsp]:sp_addr,Write[rsp]new_sp_addr"
else:
asm_ops["leave"] = "Read[ebp]:bp_addr,Write[esp]:bp_addr,Read[esp]:sp_addr,Read[sp_addr]:sp_data,Write[ebp]:sp_data,Read[esp]:sp_addr,Write[esp]:new_sp_addr"
#r_comment = re.compile(r"\s*#.*$")
r_comment_before = re.compile(r"\[.*$")
r_comment = re.compile(r".*#\s")
r_comment2 = re.compile(r"\s.*$")
peda_context = "reg,code,stack"
dashboard_context = "asm,bp,expr,hist,mem,reg_dashboard,src,bt,thr,var"
peda_dashboard_context = "reg,stack,asm,bp,expr,hist,mem,src,bt,thr,var"
all_context = peda_context + "," + dashboard_context
context_opt_list = {"peda": peda_context,
"dashboard": dashboard_context,
"peda_dashboard": peda_dashboard_context,
"all": all_context}
class BpRetHandler(gdb.FinishBreakpoint):
def __init__(self, id_str, stop=False, fn=None, source=None, debug=False):
gdb.FinishBreakpoint.__init__(self, gdb.newest_frame(), internal=True)
self.id = id_str
self.stop_ = stop
self.fn = fn
self.source = source
self.debug = debug
def stop(self):
if self.debug:
print("[+]return detected: " + self.id)
if self.fn != None:
self.fn()
if self.source != None:
gdb.execute("source " + self.source)
if self.stop_:
return True
else:
return False
class BpHandler(gdb.Breakpoint):
def __init__(self, id_str, stop=False, ret=False, stop_ret=False, fn=None, ret_fn=None, debug=False, source=None, source_ret=None):
gdb.Breakpoint.__init__(self, id_str, type=gdb.BP_BREAKPOINT, internal=False)
self.id = id_str
self.ret = ret
self.stop_ = stop
self.stop_ret = stop_ret
self.fn = fn
self.ret_fn = ret_fn
self.debug = debug
self.source = source
self.source_ret = source_ret
def stop(self):
if self.debug:
print("[+]enter detected: " + self.id)
if self.fn != None:
self.fn()
if self.source != None:
gdb.execute("source " + self.source)
if self.ret or self.source_ret:
BpRetHandler(self.id, stop=self.stop_ret, fn=self.ret_fn, source=self.source_ret, debug=self.debug)
if self.stop_:
return True
else:
return False
class TraceHandler(gdb.Breakpoint):
def __init__(self, id_str, fpath, pid, silent=False):
gdb.Breakpoint.__init__(self, id_str, type=gdb.BP_BREAKPOINT, internal=False)
self.id = id_str
self.fpath = fpath
self.silent = silent
self.pid = pid
def stop(self):
try:
c.memtrace(self.fpath, pid=self.pid)
except Exception as e:
traceback.print_exc()
if not self.silent:
gdb.execute("shell cat " + self.fpath + " | tail -1")
return False
class ExgdbCmdMethods(object):
def _is_running(self):
"""
Customized _is_running function from https://github.com/longld/peda
This always return 1
"""
return 1
setattr(ExgdbCmd, "_is_running", _is_running)
def start(self, *arg):
"""
Start debugging
Usage:
start [buf filename]
"""
(script_name, ) = utils.normalize_argv(arg, 1)
if script_name == None:
gdb.execute("start")
else:
gdb.execute("start < " + script_name)
def st(self):
"""
Start debugging with ./gdbrc.py
Usage:
st
"""
os.system('rm -rf /tmp/gdb.pause')
gdb.execute('source ./gdbrc.py')
def ctn(self):
"""
Continue
Usage:
MYNAME
"""
gdb.execute("continue")
c = ctn
def brk(self, *arg):
"""
Set break point
Usage:
MYNAME symbol
"""
(sym, ) = utils.normalize_argv(arg, 1)
e.set_breakpoint(sym)
b = brk
def next(self, *arg):
"""
Next n times
Usage:
MYNAME [n]
"""
(n, ) = utils.normalize_argv(arg, 1)
if n == None:
n = 1
gdb.execute("next " + str(n))
n = next
def nexti(self, *arg):
"""
Next n times
Usage:
MYNAME [n]
"""
(n, ) = utils.normalize_argv(arg, 1)
if n == None:
n = 1
gdb.execute("nexti " + str(n))
ni = nexti
def step(self, *arg):
"""
Nexti n times
Usage:
MYNAME [n]
"""
(n, ) = utils.normalize_argv(arg, 1)
if n == None:
n = 1
gdb.execute("step " + str(n))
s = step
def stepi(self, *arg):
"""
Nexti n times
Usage:
MYNAME [n]
"""
(n, ) = utils.normalize_argv(arg, 1)
if n == None:
n = 1
gdb.execute("stepi " + str(n))
si = stepi
def nextnow(self, *arg):
"""
Show n instructions next now
Usage:
MYNAME n
"""
arch = e.getarch()
(expr, ) = utils.normalize_argv(arg,1)
if expr == None:
expr = 10
expr = str(expr)
n = gdb.parse_and_eval(expr)
if arch[1] == 64:
ip = "$rip"
else:
ip = "$eip"
e.execute('pdisas %s /%s' % (ip, n))
nnow = nextnow
def prevnow(self, *arg):
"""
Show n instructions prev now
Usage:
MYNAME n
"""
arch = e.getarch()
(expr, ) = utils.normalize_argv(arg,1)
if expr == None:
expr = 10
expr = str(expr)
n = gdb.parse_and_eval(expr)
n = utils.to_int(n)
if arch[1] == 64:
ip = e.getreg("rip")
else:
ip = e.getreg("eip")
if n == 1:
e.execute('pdisas %s /%s' % (ip, n))
else:
addr = e.prev_inst(ip, n)[1][0]
e.execute('pdisas %s /%s' % (addr, n))
pnow = prevnow
def nextpd(self, *arg):
"""
Show n instructions next given addr
Usage:
MYNAME addr n
"""
arch = e.getarch()
(addr, expr) = utils.normalize_argv(arg,2)
if expr == None:
expr = 10
expr = str(expr)
if expr[0] == "/":
expr = expr[1:]
n = gdb.parse_and_eval(expr)
n = utils.to_int(n)
if arch[1] == 64:
ip = e.getreg("rip")
else:
ip = e.getreg("eip")
e.execute('pdisas %s /%s' % (addr, n))
npd = nextpd
def prevpd(self, *arg):
"""
Show n instructions prev given addr
Usage:
MYNAME addr n
"""
arch = e.getarch()
(addr, expr) = utils.normalize_argv(arg,2)
if expr == None:
expr = 10
expr = str(expr)
if expr[0] == "/":
expr = expr[1:]
n = gdb.parse_and_eval(expr)
n = utils.to_int(n)
if arch[1] == 64:
ip = e.getreg("rip")
else:
ip = e.getreg("eip")
if n == 1:
e.execute('pdisas %s /%s' % (ip, n))
else:
addr = e.prev_inst(ip, n)[1][0]
e.execute('pdisas %s /%s' % (addr, n))
ppd = prevpd
def grep(self, *arg):
"""
Grep command-output
Usage:
MYNAME command regexp
"""
try:
(cmd, regex) = utils.normalize_argv(arg, 2)
cmd = str(cmd)
regex = str(regex)
output = gdb.execute(cmd, to_string=True)
regexed = re.findall(regex, output)
for line in regexed:
print(line)
except Exception as e:
utils.msg("Exception in grep(%s, %s): %s" % (repr(cmd), repr(regex), e), "red")
traceback.print_exc()
return False
def allstack(self):
"""
Show all stack
Usage:
MYNAME
"""
arch = e.getarch()
if arch[1] == 64:
sp = e.getreg("rsp")
bp = e.getreg("rbp")
else:
sp = e.getreg("esp")
bp = e.getreg("ebp")
arg = bp - sp
intsize = e.intsize()
arg = arg/intsize
arg += 1
arg = int(arg)
e.execute("stack %s" % arg)
return
def lpout(self):
"""
Execute nexti until loop-end
Usage:
MYNAME
"""
arch = getarch()
if arch[1] == 64:
peda.execute("nexti $rcx")
else:
peda.execute("nexti $ecx")
return
def nuntil(self, *arg):
"""
Execute nexti until regex
Usage:
MYNAME regex callonlyflag=False
"""
(regex, callonlyflag) = utils.normalize_argv(arg, 2)
regex = str(regex)
r = re.compile(regex)
arch = e.getarch()
ctx = config.Option.get("context")
config.Option.set("context", "code")
if callonlyflag == True or callonlyflag == "True":
cmd = c.nextcall
else:
cmd = c.nexti
c.nexti()
while True:
(addr, code) = e.current_inst(e.getreg("pc"))
regexed_code = r.findall(code)
if len(regexed_code) > 0:
config.Option.set("context", ctx)
gdb.execute("context")
break
else:
cmd()
def tracepoint(self, *arg, fn=None, ret_fn=None, source=None, source_ret=None, stop=False, stop_ret=False, ret=False, debug=False):
"""
tracepoint
"""
(addr, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) = utils.normalize_argv(arg, 9)
for argN in [arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9]:
if argN == None:
continue
if argN == "stop=True":
stop = True
elif argN == "stop_ret=True":
stop_ret = True
elif argN == "ret=True":
ret = True
elif argN == "debug=True":
debug = True
elif argN[:7] == "source=":
fpath = argN[7:]
source = os.path.abspath(fpath)
elif argN[:11] == "source_ret=":
fpath = argN[11:]
source_ret = os.path.abspath(fpath)
BpHandler(addr, stop=stop, ret=ret, stop_ret=stop_ret, fn=fn, ret_fn=ret_fn, debug=debug, source=source, source_ret=source_ret)
return
def rtracepoint(self, *arg, silent=False, pid=None):
"""
regex break
Usage:
MYNAME regex(intel)
"""
(regex, fpath, start_addr, end_addr, arg5, arg6) = utils.normalize_argv(arg, 6)
def is_silent_option(arg):
if silent == False and type(arg) == str and len(arg) > 10 and arg[:7] == "silent=":
return True
else:
return False
def is_pid_option(arg):
if pid == None and type(arg) == str and len(arg) > 7 and arg[:4] == "pid=":
return True
else:
return False
if is_silent_option(arg5):
silent = arg5[7:11]
elif is_pid_option(arg5):
pid = arg5[4:8]
if is_silent_option(arg6):
silent = arg6[7:11]
elif is_pid_option(arg6):
pid = arg6[4:8]
regex = str(regex)
addrs = e.get_addrs_by_regex(regex, start_addr=start_addr, end_addr=end_addr)
if addrs == []:
print("Not found")
return -1
bp_nrs = []
addr = addrs[0]
addr = hex(addr)
def function():
c.memtrace(fpath, pid=pid)
if not silent:
gdb.execute("shell cat " + fpath + " | tail -1")
c.tracepoint("*" + addr, fn=function)
bp_info_list = e.get_breakpoints()
last_binfo = bp_info_list[-1]
bp_nr = int(last_binfo[0])
bp_nrs.append(bp_nr)
for addr in addrs[1:]:
addr = hex(addr)
c.tracepoint("*" + addr, fn=function)
bp_nr += 1
bp_nrs.append(bp_nr)
return bp_nrs
def rbreak(self, *arg, silent=False):
"""
regex break
Usage:
MYNAME regex(intel)
"""
(regex, ) = utils.normalize_argv(arg, 1)
regex = str(regex)
lang = e.get_lang()
if lang == "c":
addrs_or_linenrs = e.get_addrs_by_regex(regex)
else:
addrs_or_linenrs = e.get_linenrs_by_regex(regex)
if addrs_or_linenrs == []:
print("Not found")
return -1
bp_nrs = []
addr_or_linenr = addrs_or_linenrs[0]
if lang == "c":
gdb.execute("break *" + hex(addr_or_linenr), to_string=silent)
else:
gdb.execute("break " + addr_or_linenr)
bp_info_list = e.get_breakpoints()
last_binfo = bp_info_list[-1]
bp_nr = int(last_binfo[0])
bp_nrs.append(bp_nr)
for addr_or_linenr in addrs_or_linenrs[1:]:
gdb.execute("break " + addr_or_linenr)
bp_nr += 1
bp_nrs.append(bp_nr)
return bp_nrs
def alldelete(self, *arg):
"""
delete which can handle list argument too
"""
(addr, ) = utils.normalize_argv(arg, 1)
if addr == None:
return
if type(addr) == str:
gdb.execute("delete " + addr)
elif type(addr) == int:
gdb.execute("delete " + str(addr))
elif type(addr) == list:
addrs = addr
for addr in addrs:
c.alldelete(addr)
def radvance(self, *arg):
"""
regex advance
Usage:
MYNAME regex
"""
(regex, ) = utils.normalize_argv(arg, 1)
regex = str(regex)
bp_nrs = c.rbreak(regex, silent=True)
if bp_nrs == -1:
return
gdb.execute("continue")
c.alldelete(bp_nrs)
rad = radvance
def suntil(self, *arg):
"""
Execute stepi until regex
Usage:
MYNAME regex depth=1 callonlyflag=False
"""
(regex, depth, callonlyflag) = utils.normalize_argv(arg, 3)
regex = str(regex)
depth = utils.to_int(depth)
r = re.compile(regex)
r_call = re.compile("call")
r_ret = re.compile("ret")
if depth == None:
depth = 1
now_depth = 0
if callonlyflag == True or callonlyflag == "True":
cmd = c.nextcall
else:
cmd = c.nexti
next_when_call = c.nexti
step_when_call = c.stepi
arch = e.getarch()
ctx = config.Option.get("context")
config.Option.set("context", "code")
c.stepi()
while True:
(addr, code) = e.current_inst(e.getreg("pc"))
regexed_code = r.findall(code)
if len(regexed_code) > 0:
config.Option.set("context", ctx)
gdb.execute("context")
break
else:
call_code = r_call.findall(code)
ret_code = r_ret.findall(code)
if len(call_code) > 0:
if now_depth < depth:
c.stepi()
now_depth = now_depth + 1
continue
elif len(ret_code) > 0:
if now_depth <= depth:
now_depth = now_depth - 1
c.next()
continue
cmd()
def nextcalluntil(self, *arg):
"""
Execute nextcall until regex
Usage:
MYNAME regex
"""
(regex, ) = utils.normalize_argv(arg, 1)
regex = str(regex)
c.nuntil(regex, True)
def stepcalluntil(self, *arg):
"""
Execute stepcall until regex
Usage:
MYNAME regex depth=1
"""
(regex, depth) = utils.normalize_argv(arg, 2)
regex = str(regex)
depth = utils.to_int(depth)
if depth == None:
depth = 1
c.suntil(regex, depth, True)
def nuntilxor(self):
"""
Execute nexti until jmp-cmds
Usage:
MYNAME
"""
c.nuntil("xor")
def suntilxor(self, *arg):
"""
Execute nexti until jmp-cmds
Usage:
MYNAME depth=1
"""
(depth, ) = utils.normalize_argv(arg, 1)
depth = utils.to_int(depth)
if depth == None:
depth = 1
c.suntil("xor", depth)
def infonow(self):
"""
Show detail information of now instruction
Usage:
MYNAME
"""
(addr, code) = e.current_inst(e.getreg("pc"))
# " ANY_REG" or ",ANY_REG"
for reg in REGISTERS[8]:
reg_A = " " + reg
reg_B = "," + reg
if reg_A in code or reg_B in code:
reg = reg.replace(" ", "")
prefix = green("%s" % reg.upper(), "bold") + ": "
infox_text = e.get_infox_text(gdb.parse_and_eval("$%s" % reg))
print(prefix + infox_text)
for reg in REGISTERS[16]:
regexed_code = re.findall("[ ,]%s" % reg, code)
reg_A = " " + reg
reg_B = "," + reg
if (reg_A in code or reg_B in code) and len(regexed_code) > 0:
reg = reg.replace(" ", "")
prefix = green("%s" % reg.upper(), "bold") + ": "
infox_text = c.get_infox_text(gdb.parse_and_eval("$%s" % reg))
print(prefix + infox_text)
for i in (32, 64):
for reg in REGISTERS[i]:
reg_A = " " + reg
reg_B = "," + reg
if reg_A in code or reg_B in code:
prefix = green("%s" % reg.upper(), "bold") + ": "
if reg == "rip":
now_code_str = gdb.execute("pdisass $rip /1", to_string=True)
print(prefix + now_code_str[6:])
else:
infox_text = e.get_infox_text(gdb.parse_and_eval("$%s" % reg))
print(prefix + infox_text)
# addrs that 0xNNNNNN and more except "[ANY_REG+0xNNNNNN]" from code strings
addrs = re.findall("[^+](0x[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]+)", code)
if len(addrs) > 0:
for addr in addrs:
#print(green("%s" % addr, "bold"), end=": ")
c.infox(addr)
# "[ANY_EXPR]"
addrs = re.findall(r"\[[^ ]*\]", code)
if len(addrs) > 0:
for addr in addrs:
displayed_addr = addr
for reg in REGISTERS[8]:
if reg in addr:
#c.infox("register", reg)
addr = addr.replace(reg, "$" + reg)
displayed_addr = displayed_addr.replace(reg, reg.upper())
for reg in REGISTERS[16]:
regexed_code = re.findall("[ ,]%s" % reg, code)
if reg in addr and len(regexed_code) > 0:
#c.infox("register", reg)
addr = addr.replace(reg, "$" + reg)
displayed_addr = displayed_addr.replace(reg, reg.upper())
for i in (32, 64):
for reg in REGISTERS[i]:
if reg in addr:
#c.infox("register", reg)
addr = addr.replace(reg, "$" + reg)
displayed_addr = displayed_addr.replace(reg, reg.upper())
addr = addr.replace("[", "")
addr = addr.replace("]", "")
addr = str(gdb.parse_and_eval(addr))
# "0x12341234 <'hogefunction'>" -> "0x12341234"
addr = re.findall("0x[0-9a-f]+", addr)[0]
prefix = green("%s" % displayed_addr, "bold") + ": "
infox_text = e.get_infox_text(addr)
print(prefix + infox_text)
# display "Return value" routine
data = gdb.execute("prevnow 2", to_string=True)
if len(data) == 0:
return
before_inst = data.split("\n")[0]
before_inst = before_inst.split()[3]
if before_inst == "call":
arch = e.getarch()
if arch[1] == 64:
reg = "rax"
else:
reg = "eax"
prefix = green("RETURN-VALUE", "bold") + ": "
infox_text = e.get_infox_text(gdb.parse_and_eval("$%s" % reg))
print(prefix + infox_text)
inow = infonow
def infox(self, *arg, regname=None, color=None):
"""
Customized xinfo command from https://github.com/longld/peda
Usage:
MYNAME address
MYNAME register [reg1 reg2]
"""
(address, regname, color) = utils.normalize_argv(arg, 3)
if address == None:
print("error: missing arguments")
return
if regname != None and regname[:6] == "color=":
regname = None
color = regname[6:]
if color != None and color[:6] == "color=":
color = color[6:]
text = e.get_infox_text(address, regname=regname, color=color)
utils.msg(text)
return
def contextmode(self, *arg):
"""
Set context options
Usage:
MYNAME options
"""
if len(arg) == 0:
config.Option.set("context", "None")
return
if len(arg) == 1:
opt = arg[0]
else:
lang = arg[0]
opt = arg[1]
if opt in ["peda", "dashboard", "peda_dashboard", "all"]:
opt = context_opt_list[opt]
context_opt_list[lang] = opt
(opt, ) = utils.normalize_argv(arg, 1)
config.Option.set("context", opt)
def clearmode(self, *arg):
"""
Set clear options
Usage:
MYNAME options
"""
(opt, ) = utils.normalize_argv(arg, 1)
if opt == None:
return
config.Option.set("clearscr", opt)
peda_patch = PEDACmd.patch
def hexpatch(self, *arg):
"""
patch hex-str to addr
Usage:
MYNAME <addr> <value> [size]
"""
addr = None
value = None
size = None
argc = len(arg)
if argc >= 1:
addr = arg[0]
if type(addr) != int:
addr = int(addr, 0)
if argc >= 2:
value = arg[1]
if type(value) != str:
print("error: value must be str type")
return -1
leng = len(value)
if leng < 2:
print("error: value must be hex")
return -1
if argc >= 3:
size = arg[2]
if type(size) != int:
size = int(size, 0)
if size != None:
for i in range(size):
gdb.execute("peda_patch " + str(addr+i) + " 0", to_string=True)
chr_list = list(value)
chr_nr = len(chr_list)
i = 0
idx = 0
while chr_nr > idx:
n = chr_list[idx] + chr_list[idx+1]
n = int(n, 16)
gdb.execute("peda_patch " + str(addr+i) + " " + hex(n), to_string=True)
idx += 2
i += 1
return 0
def strpatch(self, *arg, null=False):
"""
patch str to addr
Usage:
MYNAME <addr> <value> [size]
"""
addr = None
value = None
size = None
def is_null_option(arg):
if type(arg) == str and len(arg) > 8 and arg[:5] == "null=":
return True
else:
return False
def get_null(arg):
if arg[5:10] == "True":
null = True
else:
null = False
return null
argc = len(arg)
if argc >= 1:
if is_null_option(arg[0]):
null = get_null(arg[0])
else:
addr = arg[0]
if type(addr) != int:
addr = int(addr, 0)
if argc >= 2:
if is_null_option(arg[1]):
null = get_null(arg[1])
else:
value = arg[1]
value = str(value)
if argc >= 3:
if is_null_option(arg[2]):
null = get_null(arg[2])
else:
size = arg[2]
if type(size) != int:
size = int(size, 0)
if argc >= 4:
if is_null_option(arg[3]):
null = get_null(arg[3])
if size != None:
for i in range(size):
gdb.execute("peda_patch " + str(addr+i) + " 0", to_string=True)
chr_list = list(value)
str_size = len(chr_list)
for (i, x) in enumerate(chr_list):
gdb.execute("peda_patch " + str(addr+i) + " " + hex(ord(x)), to_string=True)
if null == True:
gdb.execute("peda_patch " + str(addr+str_size) + " 0", to_string=True)
return 0
def wordpatch(self, *arg):
"""
patch word-data to addr
Usage:
MYNAME <addr> <value>
"""
addr = None
value = None
size = None
argc = len(arg)
if argc >= 1:
addr = arg[0]
if type(addr) != int:
addr = int(addr, 0)
if argc >= 2:
value = arg[1]
value = str(value)
if argc >= 3:
size = arg[2]
if type(size) != int:
size = int(size, 0)
if size != None:
for i in range(size):
gdb.execute("peda_patch " + str(addr+i) + " 0", to_string=True)
(arch, bits) = e.getarch()
capsize = int(bits / 8)
for i in range(capsize):
gdb.execute("peda_patch " + str(addr+i) + " 0", to_string=True)
gdb.execute("peda_patch " + str(addr) + " " + hex(int(value, 0)), to_string=True)
return 0
def context_infonow(self):
"""
context_infonow
"""
print(red("======================================inow======================================", "bold"))
try:
c.infonow()
except:
pass
print(red("================================================================================", "bold"))
def context_memtrace(self):
"""
context_memtrace
"""
print(red("======================================memt======================================", "bold"))
try:
c.memtrace()
except:
pass
print(red("================================================================================", "bold"))
def context_memt_inow(self):
"""
context_memt_inow
"""
print(red("====================================memtinow===================================", "bold"))
try:
c.memtrace()
except:
pass
try:
c.infonow()
except:
pass
print(red("================================================================================", "bold"))
def context_dashboard_module(self, module_name):
"""
context_dashboard_module <module_name>
"""
try:
d = {}
for module in dashboard.modules:
if module.name == module_name:
d[module.name] = module.enabled
module.enabled = True
else:
d[module.name] = module.enabled
module.enabled = False
clear_screen_bak = Dashboard.clear_screen
Dashboard.clear_screen = exutils.return_emptystr
dashboard.redisplay()
Dashboard.clear_screen = clear_screen_bak
for module in dashboard.modules:
module.enabled = d[module.name]
except:
pass
def context_asm(self):
"""
context_asm
"""
c.context_dashboard_module("assembly")
def context_bp(self):
"""
context_bp