-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgdb.py
469 lines (380 loc) · 14.5 KB
/
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
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
import os
import random
import re
import shlex
import tempfile
from . import atexit
from . import elf
from . import tubes
from .asm import make_elf
from .context import context
from .log import getLogger
from .util import misc
from .util import proc
log = getLogger(__name__)
def debug_shellcode(data, execute=None, **kwargs):
"""
Creates an ELF file, and launches it with GDB.
Arguments:
data(str): Assembled shellcode bytes
kwargs(dict): Arguments passed to context (e.g. arch='arm')
Returns:
A ``process`` tube connected to the shellcode on stdin/stdout/stderr.
"""
with context.local(**kwargs):
tmp_elf = tempfile.mktemp(prefix='pwn', suffix='.elf')
elf_data = make_elf(data)
with open(tmp_elf,'wb+') as f:
f.write(elf_data)
f.flush()
os.chmod(tmp_elf, 0777)
atexit.register(lambda: os.unlink(tmp_elf))
return debug(tmp_elf, execute=None, arch=context.arch)
def get_qemu_arch(arch):
if arch == 'mips' and context.endian == 'little':
return 'mipsel'
if arch == 'arm' and context.endian == 'big':
return 'armeb'
if arch == 'amd64':
return 'x86_64'
arch = arch.replace('powerpc', 'ppc')
return arch
def debug(args, exe=None, execute=None, ssh=None, arch=None):
"""debug(args) -> tube
Launch a GDB server with the specified command line,
and launches GDB to attach to it.
Arguments:
args: Same args as passed to pwnlib.tubes.process
ssh: Remote ssh session to use to launch the process.
Automatically sets up port forwarding so that gdb runs locally.
Returns:
A tube connected to the target process
"""
if isinstance(args, (str, unicode)):
args = [args]
orig_args = args
if not arch:
args = ['gdbserver', 'localhost:0'] + args
else:
qemu_port = random.randint(1024, 65535)
qemu_arch = get_qemu_arch(arch)
args = ['qemu-%s-static' % qemu_arch, '-g', str(qemu_port)] + args
if not ssh:
runner = tubes.process.process
which = misc.which
if ssh:
runner = ssh.run
which = ssh.which
# Make sure gdbserver is installed
if not which(args[0]):
log.error("%s is not installed" % args[0])
with context.local(log_level='debug'):
gdbserver = runner(args)
if not arch:
# Process /bin/bash created; pid = 14366
# Listening on port 34816
process_created = gdbserver.recvline()
listening_on = gdbserver.recvline()
port = int(listening_on.split()[-1])
else:
port = qemu_port
listener = remote = None
if ssh:
remote = ssh.connect_remote('127.0.0.1', port)
listener = tubes.listen.listen(0)
port = listener.lport
elif not exe:
exe = misc.which(orig_args[0])
attach(('127.0.0.1', port), exe=orig_args[0], execute=execute, arch=context.arch)
if ssh:
remote <> listener.wait_for_connection()
return gdbserver
def get_gdb_arch(arch):
return {
'amd64': 'i386:x86-64',
'powerpc': 'powerpc:403',
'powerpc64': 'powerpc:e5500'
}.get(arch, arch)
def attach(target, execute = None, exe = None, arch = None):
"""attach(target, execute = None, exe = None, arch = None) -> None
Start GDB in a new terminal and attach to `target`.
:func:`pwnlib.util.proc.pidof` is used to find the PID of `target` except
when `target` is a ``(host, port)``-pair. In that case `target` is assumed
to be a GDB server.
If it is running locally and `exe` is not given we will try to find the path
of the target binary from parsing the command line of the program running
the GDB server (e.g. qemu or gdbserver). Notice that if the PID is known
(when `target` is not a GDB server) `exe` will be read from
``/proc/<pid>/exe``.
If `gdb-multiarch` is installed we use that or 'gdb' otherwise.
Arguments:
target: The target to attach to.
execute (str or file): GDB script to run after attaching.
exe (str): The path of the target binary.
arch (str): Architechture of the target binary. If `exe` known GDB will
detect the architechture automatically (if it is supported).
Returns:
:const:`None`
"""
# if ptrace_scope is set and we're not root, we cannot attach to a running process
try:
ptrace_scope = open('/proc/sys/kernel/yama/ptrace_scope').read().strip()
if ptrace_scope != '0' and os.geteuid() != 0:
msg = 'Disable ptrace_scope to attach to running processes.\n'
msg += 'More info: https://askubuntu.com/q/41629'
log.warning(msg)
return
except IOError:
pass
# if execute is a file object, then read it; we probably need to run some
# more gdb script anyway
if execute:
if isinstance(execute, file):
fd = execute
execute = fd.read()
fd.close()
# enable gdb.attach(p, 'continue')
if execute and not execute.endswith('\n'):
execute += '\n'
# gdb script to run before `execute`
pre = ''
if arch:
if not misc.which('gdb-multiarch'):
log.warn_once('Cross-architecture debugging usually requires gdb-multiarch\n' \
'$ apt-get install gdb-multiarch')
pre += 'set endian %s\n' % context.endian
pre += 'set architecture %s\n' % get_gdb_arch(arch)
# let's see if we can find a pid to attach to
pid = None
if isinstance(target, (int, long)):
# target is a pid, easy peasy
pid = target
elif isinstance(target, str):
# pidof picks the youngest process
pids = proc.pidof(target)
if not pids:
log.error('no such process: %s' % target)
pid = pids[0]
log.info('attaching you youngest process "%s" (PID = %d)' %
(target, pid))
elif isinstance(target, tubes.ssh.ssh_channel):
if not target.pid:
log.error("PID unknown for channel")
shell = target.parent
tmpfile = shell.mktemp()
shell.upload_data(execute or '', tmpfile)
cmd = ['ssh', '-t', '-p', str(shell.port), '-l', shell.user, shell.host]
if shell.password:
cmd = ['sshpass', '-p', shell.password] + cmd
if shell.keyfile:
cmd += ['-i', shell.keyfile]
cmd += ['gdb %r %s -x "%s" ; rm "%s"' % (target.exe, target.pid, tmpfile, tmpfile)]
misc.run_in_new_terminal(' '.join(cmd))
return
elif isinstance(target, tubes.sock.sock):
pids = proc.pidof(target)
if not pids:
log.error('could not find remote process (%s:%d) on this machine' %
target.sock.getpeername())
pid = pids[0]
elif isinstance(target, tubes.process.process):
pid = proc.pidof(target)[0]
elif isinstance(target, tuple) and len(target) == 2:
host, port = target
pre += 'target remote %s:%d\n' % (host, port)
def findexe():
# hm no PID then, but wait! we might not be totally out of luck yet: if
# the gdbserver is running locally and we know the program who is
# hosting it (e.g qemu, gdbserver) we can figure out the `exe` from the
# command line
# find inode of the listen socket
inode = None
# XXX: do a proper check to see if we're hosting the server
if host not in ('localhost', '127.0.0.1', '0.0.0.0',
'::1', 'ip6-localhost', '::'):
return
for f in ['tcp', 'tcp6']:
with open('/proc/net/%s' % f) as fd:
# skip the first line with the column names
fd.readline()
for line in fd:
line = line.split()
loc = line[1]
lport = int(loc.split(':')[1], 16)
st = int(line[3], 16)
if st != 10: # TCP_LISTEN, see include/net/tcp_states.h
continue
if lport == port:
inode = int(line[9])
break
if inode:
break
# if we didn't find the inode, there's nothing we can do about it
if not inode:
return
# find the process who owns the socket
spid = proc.pid_by_inode(inode)
if not spid:
return
# let's have a look at the server exe
sexe = proc.exe(spid)
name = os.path.basename(sexe)
# XXX: parse cmdline
if name.startswith('qemu-') or name.startswith('gdbserver'):
exe = proc.cmdline(spid)[-1]
return os.path.join(proc.cwd(spid), exe)
exe = exe or findexe()
else:
log.error("don't know how to attach to target: %r" % target)
# if we have a pid but no exe, just look it up in /proc/
if pid and not exe:
exe = proc.exe(pid)
if not pid and not exe:
log.error('could not find target process')
cmd = None
for p in ('gdb-multiarch', 'gdb'):
if misc.which(p):
cmd = p
break
if not cmd:
log.error('no gdb installed')
if exe:
if not os.path.isfile(exe):
log.error('no such file: %s' % exe)
cmd += ' "%s"' % exe
if pid:
cmd += ' %d' % pid
execute = pre + (execute or '')
if execute:
tmp = tempfile.NamedTemporaryFile(prefix = 'pwn', suffix = '.gdb',
delete = False)
tmp.write(execute)
tmp.close()
atexit.register(lambda: os.unlink(tmp.name))
cmd += ' -x "%s" ; rm "%s"' % (tmp.name, tmp.name)
log.info('running in new terminal: %s' % cmd)
misc.run_in_new_terminal(cmd)
if pid:
proc.wait_for_debugger(pid)
def ssh_gdb(ssh, process, execute = None, arch = None, **kwargs):
if isinstance(process, (list, tuple)):
exe = process[0]
process = ["gdbserver", "127.0.0.1:0"] + process
else:
exe = process
process = "gdbserver 127.0.0.1:0 " + process
# Download the executable
local_exe = os.path.basename(exe)
ssh.download_file(exe, local_exe)
# Run the process
c = ssh.run(process, **kwargs)
# Find the port for the gdb server
c.recvuntil('port ')
line = c.recvline().strip()
gdbport = re.match('[0-9]+', line)
if gdbport:
gdbport = int(gdbport.group(0))
l = tubes.listen.listen(0)
forwardport = l.lport
attach(('127.0.0.1', forwardport), execute, local_exe, arch)
l.wait_for_connection() <> ssh.connect_remote('127.0.0.1', gdbport)
return c
def find_module_addresses(binary, ssh=None, ulimit=False):
"""
Cheat to find modules by using GDB.
We can't use ``/proc/$pid/map`` since some servers forbid it.
This breaks ``info proc`` in GDB, but ``info sharedlibrary`` still works.
Additionally, ``info sharedlibrary`` works on FreeBSD, which may not have
procfs enabled or accessible.
The output looks like this:
::
info proc mapping
process 13961
warning: unable to open /proc file '/proc/13961/maps'
info sharedlibrary
From To Syms Read Shared Object Library
0xf7fdc820 0xf7ff505f Yes (*) /lib/ld-linux.so.2
0xf7fbb650 0xf7fc79f8 Yes /lib32/libpthread.so.0
0xf7e26f10 0xf7f5b51c Yes (*) /lib32/libc.so.6
(*): Shared library is missing debugging information.
Note that the raw addresses provided by ``info sharedlibrary`` are actually
the address of the ``.text`` segment, not the image base address.
This routine automates the entire process of:
1. Downloading the binaries from the remote server
2. Scraping GDB for the information
3. Loading each library into an ELF
4. Fixing up the base address vs. the ``.text`` segment address
Arguments:
binary(str): Path to the binary on the remote server
ssh(pwnlib.tubes.tube): SSH connection through which to load the libraries.
If left as ``None``, will use a ``pwnlib.tubes.process.process``.
ulimit(bool): Set to ``True`` to run "ulimit -s unlimited" before GDB.
Returns:
A list of pwnlib.elf.ELF objects, with correct base addresses.
Example:
>>> with context.local(log_level=9999): # doctest: +SKIP
... shell = ssh(host='bandit.labs.overthewire.org',user='bandit0',password='bandit0')
... bash_libs = gdb.find_module_addresses('/bin/bash', shell)
>>> os.path.basename(bash_libs[0].path) # doctest: +SKIP
'libc.so.6'
>>> hex(bash_libs[0].symbols['system']) # doctest: +SKIP
'0x7ffff7634660'
"""
#
# Download all of the remote libraries
#
if ssh:
runner = ssh.run
local_bin = ssh.download_file(binary)
local_elf = elf.ELF(os.path.basename(binary))
local_libs = ssh.libs(binary)
else:
runner = tubes.process.process
local_elf = elf.ELF(binary)
local_libs = local_elf.libs
entry = local_elf.header.e_entry
#
# Get the addresses from GDB
#
libs = {}
cmd = "gdb --args %s" % (binary)
expr = re.compile(r'(0x\S+)[^/]+(.*)')
if ulimit:
cmd = 'sh -c "(ulimit -s unlimited; %s)"' % cmd
cmd = shlex.split(cmd)
with runner(cmd) as gdb:
gdb.send("""
set prompt
set disable-randomization off
break *%#x
run
""" % entry)
gdb.clean(2)
gdb.sendline('info sharedlibrary')
lines = gdb.recvrepeat(2)
for line in lines.splitlines():
m = expr.match(line)
if m:
libs[m.group(2)] = int(m.group(1),16)
gdb.sendline('kill')
gdb.sendline('y')
gdb.sendline('quit')
#
# Fix up all of the addresses against the .text address
#
rv = []
for remote_path,text_address in sorted(libs.items()):
# Match up the local copy to the remote path
try:
path = next(p for p in local_libs.keys() if remote_path in p)
except StopIteration:
print "Skipping %r" % remote_path
continue
# Load it
lib = elf.ELF(path)
# Find its text segment
text = lib.get_section_by_name('.text')
# Fix the address
lib.address = text_address - text.header.sh_addr
rv.append(lib)
return rv