forked from longld/peda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pd.py
executable file
·690 lines (544 loc) · 19.5 KB
/
pd.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
# PD - Best debug; Fork of PEDA
# TODO: syscalls
import inspect
import struct
import sys
import os
__gdbModule = None
__lldbModule = None
def unpack(fmt, data):
size = struct.calcsize(fmt)
rfmt = fmt
outp = struct.unpack(rfmt, data)
if len(outp) == 1:
return outp[0]
return outp
def is_all_ascii(data):
for i in range(8):
ind_byte = (data >> (8 * i)) & 0xff
if ind_byte < 0x20 or ind_byte > 0x7e:
return False
return True
def magic_str_to_int(strnum):
if len(strnum) > 2:
if strnum[0:2] == '0x':
return int(strnum, 16)
elif strnum[0:2] == '0b':
return int(strnum, 2)
# Only accept _explicit_ octal style
elif strnum[0:2] == '0o':
return int(strnum, 8)
# Either it's just a two digit number, or it's decimal
return int(strnum, 10)
# --- try import dbg lib ---
try:
import gdb
__gdbModule = gdb
except ImportError:
pass
try:
import lldb
__lldbModule = lldb
except ImportError:
pass
# --- end try import dbg lib ---
def _throw_unimpl():
raise Exception('Method unimplemented: ' + repr(inspect.stack()))
# --- color helpers ---
_START_ATTR = '\x1b['
_RESET_ATTRS = '\x1b[0m'
def red(x):
return _START_ATTR + '31m' + x + _RESET_ATTRS
def green(x):
return _START_ATTR + '32m' + x + _RESET_ATTRS
def blue(x):
return _START_ATTR + '34m' + x + _RESET_ATTRS
def bold(x):
return _START_ATTR + '1m' + x + _RESET_ATTRS
# --- end color helpers ---
(OS_MAC, OS_LINUX, OS_UNK) = range(3)
(ARCH_X86, ARCH_X64, ARCH_UNK) = range(3)
class ArchInfo():
def __init__(self, host_arch, host_os, ptr_size, regs, pc, stack_pointer, flags_reg, flags):
self.pointer_size = ptr_size
self.gp_regs = regs
self.stack_pointer = stack_pointer
self.gp_flags = flags_reg
self.flags = flags
self.arch = host_arch
self.os = host_os
self.pc_reg = pc
class MemoryMap():
def __init__(self, begin, end, is_readable, is_writeable, is_executable, name):
self.begin = begin
self.end = end
self.is_readable = is_readable
self.is_writeable = is_writeable
self.is_executable = is_executable
self.permissions_string = ''
if self.is_readable:
self.permissions_string += 'r'
else:
self.permissions_string += '-'
if self.is_writeable:
self.permissions_string += 'w'
else:
self.permissions_string += '-'
if self.is_executable:
self.permissions_string += 'x'
else:
self.permissions_string += '-'
self.name = name
def pretty_print(self):
_name = self.name
if _name == '':
_name = '(anon)'
region_description = '%016x-%016x %s (%s)' % (self.begin, self.end, _name, self.permissions_string)
if self.is_executable:
print(red(region_description))
elif self.is_writeable:
print(blue(region_description))
else:
print(region_description)
# Debugger
# - get_pid
# - set_asm_syntax
# - set_prompt
# - clear_screen
# - get_gp_registers
# - get_gpr_val
# - get_memory
# - get_virtual_maps
# - get_permissions_for_addr
# - get_map_region_for_ptr
# - is_mapped
# - get_heap_base
# - get_current_disasm
# - shell
class Debugger:
current_arch = None
def set_prompt(self, astr):
_throw_unimpl()
def get_pid(self):
_throw_unimpl()
def set_syntax(self, syntax):
_throw_unimpl()
def clear_screen(self):
sys.stdout.write("\x1b[2J\x1b[H")
def get_gp_registers(self):
_throw_unimpl()
def get_gpr_val(self, reg):
_throw_unimpl()
def get_memory(self, addr):
_throw_unimpl()
def get_arch(self):
_throw_unimpl()
def get_virtual_maps(self):
# should i really make this a binary heap?
# maybe premature optimization
_throw_unimpl()
def get_heap_base(self):
vmaps = self.get_virtual_maps()
for mp in vmaps:
if mp.name == "[heap]":
return mp
return None
def get_current_disasm(self):
return "disasm"
def parse_linux_vmmap(self, memstr):
full_maps = []
maps = memstr.split('\n')
for lm in maps:
if len(lm) == 0:
continue
terms = lm.split(' ')
beg = int(terms[0].split('-')[0], 16)
end = int(terms[0].split('-')[1], 16)
# this is gross
full_maps.append(MemoryMap(beg, end, terms[1][0] == 'r', terms[1][1] == 'w', terms[1][2] == 'x', terms[-1]))
return full_maps
def get_permissions_for_addr(self, addr):
_throw_unimpl()
def is_mapped(self, addr):
_throw_unimpl()
def print_gp_registers(self, regs):
print(red('[-------registers------]'))
for r in self.current_arch.gp_regs:
if r in regs:
v = regs[r]
print(green(r.upper().ljust(3, ' ')) + ': ' + '0x{:x}'.format(v))
flags = self.current_arch.gp_flags
if flags in regs:
curr_flags = regs[flags]
flags_line = red(flags.upper()) + ': ' + '0x{:x}'.format(curr_flags)
flags_line += ' ('
fls = []
for fl in self.current_arch.flags:
if (1 << fl) & curr_flags:
fls.append(bold(red(self.current_arch.flags[fl].upper())))
else:
fls.append(green(self.current_arch.flags[fl].lower()))
flags_line += ' '.join(fls)
flags_line += ')'
print(flags_line)
def telescope(self, addr, num):
memmaps = self.get_virtual_maps()
for i in range(num):
mem_val = int(unpack("<Q", (self.get_memory(addr + 8*i, 8))))
# sline is a text run
sline = '%04d| ' % (8 * i) + blue(hex(addr + 8 * i)) + ' -> ' + hex(mem_val)
iterval = mem_val
for i in range(10):
mp = self.get_map_region_for_ptr(iterval, maps=memmaps)
if not mp is None and mp.is_readable:
iterval = int(unpack("<Q", self.get_memory(iterval, 8)))
sline += ' -> ' + hex(iterval)
# Determine if iterval is ascii.. if so, fetch the rest
else:
break
print(sline)
def shell(self, cmd):
_throw_unimpl()
def print_disasm(self):
print(red('[---------code---------]'))
disasm = self.get_current_disasm()
if not disasm is None:
print(disasm)
def get_map_region_for_ptr(self, ptr, maps=None):
if maps is None:
maps = self.get_virtual_maps()
if maps is None:
return None
for mp in maps:
if ptr > mp.begin and ptr < mp.end:
return mp
return None
def print_stack(self):
print(red('[---------stack--------]'))
sp = self.get_gpr_val(self.current_arch.stack_pointer)
if sp is None:
return
self.telescope(sp, 8) # Default telescope depth
class GDBDBG(Debugger):
def register_hooks(self):
gdb.events.stop.connect(stop_hook)
def shell(self, cmd):
# will need a hack to capture this..
return self._executeCommand('shell ' + cmd)
def get_current_frame(self):
return gdb.selected_frame()
def get_gp_registers(self):
regs = gdb.execute('info registers', to_string=True)
rlines = regs.split('\n')
regs = dict()
for line in rlines:
rv = line.split()
if len(rv) == 0:
continue
rname = rv[0]
rval = rv[1]
regs[rname] = int(rval, 16)
if regs['eflags']:
regs['rflags'] = regs['eflags']
return regs
def get_virtual_maps(self):
#if current_arch.os == OS_LINUX:
# _throw_unimpl()
#elif current_arch.os == OS_MAC:
# _throw_unimpl()
return []
def get_gpr_val(self, reg):
return self.get_gp_registers()[reg]
def get_arch(self):
return ARCH_X64, 8
def set_syntax(self, syntax):
self._executeCommand("set disassembly-flavor " + syntax)
def stop_hook(a, b):
print('bbbbb')
def initialize_ui(self):
self._executeCommand('set height 0')
def add_aliases(self):
pass
def _executeCommand(self, st):
return gdb.execute(st)
# for some reason, calling set height 0 with to_string=True
# doesnt do anything...
def _executeCommandWithResponse(self, st):
return gdb.execute(st, to_string=True)
def set_prompt(self, pstr):
self._executeCommand('set prompt ' + red(pstr))
pass
def get_memory(self, addr, size):
inf = gdb.inferiors()[0]
mem = inf.read_memory(addr, size)
return mem.tobytes()
class LLDBDBG(Debugger):
debugger = None
def __init__(self):
self.debugger = lldb.debugger
self.debugger.SetAsync(True)
def register_hooks(self):
self._executeCommand('target stop-hook add -o context')
def get_arch(self):
triple = lldb.debugger.GetSelectedTarget().GetTriple()
triple = triple.split('-')
arch = ARCH_UNK
ptr_size = 4
if 'x86_64' in triple:
arch = ARCH_X64
ptr_size = 8
if 'x86' in triple:
arch = ARCH_X86
ptr_size = 4
return arch, ptr_size
def get_memory(self, addr, size):
err = lldb.SBError()
process = self.get_current_process()
mem = process.ReadMemory(addr, size, err)
return mem
def initialize_ui(self):
self._executeCommand('settings set stop-disassembly-display never')
def set_syntax(self, syntax):
self._executeCommand('settings set target.x86-disassembly-flavor ' + syntax)
def get_gpr_val(self, reg):
regs = self.get_gp_registers()
if reg in regs:
return regs[reg]
return None
def shell(self, cmd):
return os.system(cmd)
def get_current_disasm(self):
cframe = self.get_current_frame()
if cframe is None:
return
vv = cframe.Disassemble()
if vv is None:
return
vv = vv.split('\n')[:-1]
found_c = -1
disasm = []
for l in vv:
if len(disasm) > 7:
disasm = disasm[1:]
disasm.append(l)
if l[0:2] == '->':
found_c = 0
if found_c >= 0:
found_c += 1
if found_c == 5:
break
return '\n'.join(disasm)
def get_pid(self):
return self.get_current_process().id
def get_current_process(self):
return lldb.debugger.GetSelectedTarget().GetProcess()
def get_current_frame(self):
return self.get_current_process().GetSelectedThread().GetSelectedFrame()
def get_gp_registers(self):
frame = self.get_current_frame()
#print(frame.GetRegisters())
gprs = None
for rset in frame.GetRegisters():
if str(rset.GetName()).startswith('General'):
gprs = rset
break
#gprs = frame.GetRegisters().GetFirstValueByName('General Purpose Registers')
rvals = dict()
if not gprs is None and gprs.IsValid():
for reg in gprs:
rvals[reg.GetName()] = int(reg.GetValue(), 16)
return rvals
def initialize_ui(self):
self._executeCommand("settings set stop-disassembly-display never")
pass
def start(self, cmd, result, m, b, c=None):
self._executeCommand('process launch --stop-at-entry')
def get_all_memory_sections(self):
# currently missing stack and heap.. great i know
# lldb-devs mentions how its not possible, but vmmap does it :(
process = self.get_current_process()
region_list = process.GetMemoryRegions()
maps = []
for i in range(region_list.GetSize()):
region = lldb.SBMemoryRegionInfo()
region_list.GetMemoryRegionAtIndex(i, region)
begin_address = region.GetRegionBase()
end_address = region.GetRegionEnd() - 1
name = region.GetName()
if name is None:
name = ''
perm_str = ''
maps.append(MemoryMap(begin_address, end_address, region.IsReadable(), region.IsWritable(), region.IsExecutable(), name))
return maps
#target = lldb.debugger.GetSelectedTarget()
#mods_count = target.GetNumModules()
#for i in range(mods_count):
# mod = target.GetModuleAtIndex(i)
# mod_sections = mod.GetNumSections()
# for j in range(mod_sections):
# section = mod.GetSectionAtIndex(j)
# base = int(section.GetLoadAddress(target))
# if base == lldb.LLDB_INVALID_ADDRESS:
# base = int(section.addr)
#
# end = base + int(section.GetByteSize())
# perms = section.GetPermissions()
# perm_str = ''
# if perms & 0x2:
# perm_str = 'r'
# else:
# perm_str = '-'
# if perms & 0x1:
# perm_str += 'w'
# else:
# perm_str += '-'
# if perms & 0x4:
# perm_str += 'x'
# else:
# perm_str += '-'
# name = section.GetName() + " @ " + str(mod.platform_file)
#
# maps.append(MemoryMap(base, end, perm_str, name))
#return maps
def get_virtual_maps(self):
proc = self.get_current_process()
pid = proc.GetProcessID()
if self.current_arch.os == OS_LINUX:
maps = open('/proc/' + str(pid) + '/maps', 'rb').read()
return self.parse_linux_vmmap(maps)
elif self.current_arch.os == OS_MAC:
return self.get_all_memory_sections()
return []
def do_vmmap(self, cmd, args, m, b, c=None):
vmaps = self.get_virtual_maps()
if vmaps is None:
print("Couldn't get virtual memory maps... :(")
return
if len(args) == 0:
# No arguments, print all regions
# print all regions
for mp in vmaps:
mp.pretty_print()
return
address = None
# For the world where someone has something mapped below 99...
# I think __PAGEZERO and MMAP_MIN_ADDR prevent this
# But only for non-root?
address = magic_str_to_int(args)
for mp in vmaps:
if address >= mp.begin and address < mp.end:
mp.pretty_print()
break
def do_telescope(self, cmd, args, m, b, c=None):
# How does this work without args again?
# I think it's just the current stack walk?
numc = 8
address = None
if len(args) > 0:
# Attempt to split it first
arglist = args.split(' ')
if len(arglist) > 1:
address = magic_str_to_int(arglist[0])
numc = magic_str_to_int(arglist[1])
else:
address = magic_str_to_int(args)
else:
address = self.get_gpr_val(dbg.current_arch.stack_pointer)
self.telescope(address, numc)
def add_aliases(self):
self._executeCommand('command script add --function pd.context context')
self._executeCommand('command script add --function pd.context ctx')
self._executeCommand('command script add --function pd.context ct')
self._executeCommand('command script add --function pd.dbg.start start')
self._executeCommand('command script add --function pd.dbg.do_vmmap vmmap')
self._executeCommand('command script add --function pd.dbg.do_vmmap vmm')
self._executeCommand('command script add --function pd.dbg.do_telescope telescope')
self._executeCommand('command script add --function pd.dbg.do_telescope tele')
# searchmem, tls, deactive, checksec, aslr, print disassembly, stepuntil
# xrefs, heap stuff
# libc, heap, ld (print base)
# heapinfo, magic, one_gadget, canary
# findmainarea, fpu (floating point registers/stack)
def _executeCommand(self, inp):
self.debugger.HandleCommand(inp)
def set_prompt(self, pstr):
self._executeCommand("settings set prompt '" + pstr + "'")
def _executeCommandWithRet(self, s):
ret = lldb.SBCommandReturnObject()
err = self.debugger.GetCommandInterpreter().HandleCommand(s, ret)
return ret, err
def _executeCommand(self, st):
self.debugger.HandleCommand(st)
def set_prompt(self, pstr):
self._executeCommand("settings set prompt '" + pstr + "'")
pass
# --- setup debugger instance ---
dbg = None
if __gdbModule is not None:
dbg = GDBDBG()
if __lldbModule is not None:
dbg = LLDBDBG()
if dbg is None:
print('What debugger are you using?')
raise Exception('Cannot determine debugger')
# --- Handle UI ---
def context(*args):
if dbg.current_arch is None:
dbg.current_arch = determine_arch()
regs = dbg.get_gp_registers()
dbg.clear_screen()
dbg.print_gp_registers(regs)
dbg.print_disasm()
dbg.print_stack()
def stop_hook(*args):
context(args)
# --- hopefully figured out debugger ---
# --- run initialization ---
# maybe this should be a better data type.. that way i can look up 'ah' -> 'eax' easily
# also handling flags..
arch_gpr_map = {
ARCH_X86: { 'gpr': ['eax', 'ebx', 'ecx', 'edx', 'esi', 'edi', 'ebp', 'esp', 'eip', 'eflags'],
'pc': 'eip',
'sp': 'esp',
'flags_reg': 'eflags',
'flags': { 1: 'CF', 2: 'PF', 4: 'AF', 6: 'ZF', 7: 'SF', 8: 'TF', 9: 'IF', 10: 'DF', 11: 'OF' }
},
ARCH_X64: { 'gpr': ['rax', 'rbx', 'rcx', 'rdx', 'rsi', 'rdi', 'rbp', 'rsp', 'rip', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15'],
'pc': 'rip',
'sp': 'rsp',
'flags_reg': 'rflags',
'flags': { 1: 'CF', 2: 'PF', 4: 'AF', 6: 'ZF', 7: 'SF', 8: 'TF', 9: 'IF', 10: 'DF', 11: 'OF' }
}
}
def determine_arch():
archs, size = dbg.get_arch()
arch_gpregs = []
flags_gpreg = None
stack_pointer = None
flags = None
archs = ARCH_X64
if archs in arch_gpr_map:
arch_gpregs = arch_gpr_map[archs]['gpr']
flags_gpreg = arch_gpr_map[archs]['flags_reg']
stack_pointer = arch_gpr_map[archs]['sp']
flags = arch_gpr_map[archs]['flags']
pc = arch_gpr_map[archs]['pc']
else:
print('Unsupported arch?')
return
hostos = None
uname = os.uname()[0]
if uname == "Linux":
hostos = OS_LINUX
elif uname == "Darwin":
hostos = OS_MAC
if hostos is None:
hostos = OS_UNK
print("Unknown arch.. beware")
hostos = OS_MAC
return ArchInfo(archs, hostos, size, arch_gpregs, pc, stack_pointer, flags_gpreg, flags)
dbg.set_prompt("(pd) ")
dbg.add_aliases()
dbg.initialize_ui()
dbg.set_syntax('intel') # you want this
dbg.register_hooks()