-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygdb.py
282 lines (226 loc) · 6.06 KB
/
pygdb.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
import os
import re
import gdb
def gdb_exec(command):
return gdb.execute(command, to_string=True)
def gdb_pid(procname):
filelines = os.popen("ps -x|grep " + procname, "r")
for line in filelines:
if not line:
continue
line, count = re.subn('^\s+', '', line)
results = re.split("\s+", line)
if procname == results[4]:
return results[0]
return None
HEXSTR = "0123456789abcdef"
def ishex(data):
data = data.lower()
if '0x' == data[:2]:
data = data[2:]
for x in data:
if x not in HEXSTR:
return None
return '*0x' + data
REGSTR = (
"al", "ah", "ax", "eax", "rax",
"cl", "ch", "cx", "ecx", "rcx",
"dl", "dh", "dx", "edx", "rdx",
"bl", "bh", "bx", "ebx", "rbx",
"si", "esi", "rsi",
"di", "edi", "rdi",
"bp", "ebp", "rbp",
"sp", "esp", "rsp",
"ip", "eip", "rip",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
)
def isreg(data):
data = data.lower()
if data[:2] not in REGSTR and data[:3] not in REGSTR:
return None
return '$' + data
class Mod(gdb.Command):
"""print modules infomation."""
def __init__(self):
gdb.Command.__init__(self, "mod", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
procinfo = gdb_exec("info proc")
lines = procinfo.split("\n")
pid = ""
for line in lines:
if("process " == line[:8]):
pid = line[8:]
break
os.system("cat /proc/%s/maps" % pid)
Mod()
class Oep(gdb.Command):
"""Prints the entry point address of the target and stores it in the variable entry_point."""
def __init__(self):
gdb.Command.__init__(self, "oep", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
fileinfo = gdb_exec("info files")
lines = fileinfo.split("\n")
for line in lines:
divstr = line.split("Entry point:")
if(2 == len(divstr)):
if "noprint" != arg:
print(line)
EntryPoint = divstr[1]
gdb_exec("set $entry_point_address = " + EntryPoint)
break
Oep()
class Atn(gdb.Command):
"""
Attach process by name.
Example:
atn babyuse
"""
def __init__(self):
gdb.Command.__init__(self, "atn", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
pid = gdb_pid(arg)
if not pid:
print('cannot found "' + arg + '" to attach.')
return
print("attach to \"%s\" (pid=%s)." % (arg, pid))
gdb.execute("attach " + pid)
Atn()
class Db(gdb.Command):
"""
Display 8 lines of a hex dump of address starting at ADDR.
Example:
db eax
db 0xB7FFF918
db main
"""
def __init__(self):
gdb.Command.__init__(self, "db", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = isreg(arg)
if not addr:
addr = arg
gdb.execute("_db " + addr)
Db()
class Dd(gdb.Command):
"""
Display 8 lines of a hex dump of address starting at ADDR.
Example:
dd eax
dd 0xB7FFF918
dd main
"""
def __init__(self):
gdb.Command.__init__(self, "dd", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = isreg(arg)
if not addr:
addr = arg
gdb.execute("_dd " + addr)
Dd()
class Dq(gdb.Command):
"""
Display 8 lines of a hex dump of address starting at ADDR.
Example:
dq eax
dq 0xB7FFF918
dq main
"""
def __init__(self):
gdb.Command.__init__(self, "dq", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = isreg(arg)
if not addr:
addr = arg
gdb.execute("_dq " + addr)
Dq()
class Uu(gdb.Command):
"""diassembly 8 instruction of address starting at ADDR."""
def __init__(self):
gdb.Command.__init__(self, "uu", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = isreg(arg)
if not addr:
addr = arg
gdb.execute("_uu " + addr)
Uu()
class Bp(gdb.Command):
"""
Set a normal breakpoint.
Example:
bp eax
bp 0xB7FFF918
bp main
"""
def __init__(self):
gdb.Command.__init__(self, "bp", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = ishex(arg)
if not addr:
addr = arg
gdb.execute("_bp " + addr)
Bp()
class Bpt(gdb.Command):
"""
Set a temporary breakpoint.
Example:
bpt eax
bpt 0xB7FFF918
bpt main
"""
def __init__(self):
gdb.Command.__init__(self, "bpt", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = ishex(arg)
if not addr:
addr = arg
gdb.execute("_bpt " + addr)
Bpt()
class Bph(gdb.Command):
"""
Set a hardware breakpoint.
Example:
bph eax
bph 0xB7FFF918
bph main
"""
def __init__(self):
gdb.Command.__init__(self, "bph", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = ishex(arg)
if not addr:
addr = arg
gdb.execute("_bph " + addr)
Bph()
class Bpht(gdb.Command):
"""
Set a temporary hardware breakpoint.
Example:
bpht eax
bpht 0xB7FFF918
bpht main
"""
def __init__(self):
gdb.Command.__init__(self, "bpht", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = ishex(arg)
if not addr:
addr = arg
gdb.execute("_bpht " + addr)
Bpht()
class Bpm(gdb.Command):
"""
Set a read/write breakpoint.
Example:
bpm eax
bpm 0xB7FFF918
bpm main
"""
def __init__(self):
gdb.Command.__init__(self, "bpm", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
addr = ishex(arg)
if not addr:
addr = arg
gdb.execute("_bpm " + addr)
Bpm()