-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper_gdb.py
88 lines (76 loc) · 2.43 KB
/
helper_gdb.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
#!/usr/bin/python3
#
# This file is part of libmempool.
# Copyright (c) 2017, Aaron Adams <aaron.adams(at)nccgroup(dot)trust>
# Copyright (c) 2017, Cedric Halbronn <cedric.halbronn(at)nccgroup(dot)trust>
import traceback
import os, re, json, pickle, gdb, sys
from functools import wraps
import helper as h
import importlib
importlib.reload(h)
class logger:
def logmsg(s, debug=True):
if not debug:
return
if type(s) == str:
print("[helper_gdb] " + s)
else:
print(s)
def get_info():
res = gdb.execute("maintenance info sections ?", to_string=True)
bin_name = os.path.basename(h.build_bin_name(res))
if not bin_name:
raise("get_info: failed to find bin name")
return bin_name
def get_arch():
res = gdb.execute("maintenance info sections ?", to_string=True)
if "elf32-i386" in res and "elf64-x86-64" in res:
raise("get_arch: could not determine arch (1)")
if "elf32-i386" not in res and "elf64-x86-64" not in res:
raise("get_arch: could not determine arch (2)")
if "elf32-i386" in res:
return "elf32-i386"
elif "elf64-x86-64" in res:
return "elf64-x86-64"
else:
raise("get_arch: failed to find arch")
def get_inferior():
try:
if len(gdb.inferiors()) == 0:
print("No gdb inferior could be found.")
return -1
else:
inferior = gdb.inferiors()[0]
return inferior
except AttributeError:
print("This gdb's python support is too old.")
exit()
def has_inferior(f):
"decorator to make sure we have an inferior to operate on"
@wraps(f)
def with_inferior(*args, **kwargs):
inferior = get_inferior()
if inferior != -1 and inferior != None:
if (inferior.pid != 0) and (inferior.pid is not None):
return f(*args, **kwargs)
else:
print("No debugee could be found. Attach or start a program.")
exit()
else:
exit()
return with_inferior
def retrieve_sizesz():
"Retrieve the SIZE_SZ after binary loading finished, this allows import within .gdbinit"
_machine = get_arch()
if "elf64" in _machine:
SIZE_SZ = 8
elif "elf32" in _machine:
SIZE_SZ = 4
else:
raise Exception("Retrieving the SIZE_SZ failed.")
if SIZE_SZ == 4:
pass
elif SIZE_SZ == 8:
pass
return SIZE_SZ