Skip to content

Commit fee9d92

Browse files
author
hugsy
committed
This PR slightly improves #579: instead of hardcoding some addresses - which would not work on non 64b archs, we completely assumes that the debugged binary is emulated by qemu and therefore show the qemu mem maping. This allows for a clean and effective way to have the correct page permissions set in gef (and might help oneself who tries to escape the qemu process :) )
1 parent 3b7a97c commit fee9d92

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

gef.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -2670,7 +2670,7 @@ def get_os():
26702670
@lru_cache()
26712671
def get_pid():
26722672
"""Return the PID of the debuggee process."""
2673-
return gdb.selected_inferior().pid
2673+
return gdb.selected_inferior().pid if not __gef_qemu_mode__ else gdb.selected_thread().ptid[1]
26742674

26752675

26762676
@lru_cache()
@@ -2752,7 +2752,7 @@ def download_file(target, use_cache=False, local_name=None):
27522752
gdb.execute("remote get {0:s} {1:s}".format(target, local_name))
27532753

27542754
except gdb.error:
2755-
# gdb-stub compat
2755+
# fallback memory view
27562756
with open(local_name, "w") as f:
27572757
if is_32bit():
27582758
f.write("00000000-ffffffff rwxp 00000000 00:00 0 {}\n".format(get_filepath()))
@@ -2768,7 +2768,7 @@ def download_file(target, use_cache=False, local_name=None):
27682768
def open_file(path, use_cache=False):
27692769
"""Attempt to open the given file, if remote debugging is active, download
27702770
it first to the mirror in /tmp/."""
2771-
if is_remote_debug():
2771+
if is_remote_debug() and not __gef_qemu_mode__:
27722772
lpath = download_file(path, use_cache)
27732773
if not lpath:
27742774
raise IOError("cannot open remote path {:s}".format(path))
@@ -2860,7 +2860,7 @@ def get_info_sections():
28602860
break
28612861

28622862
try:
2863-
parts = [x.strip() for x in line.split()]
2863+
parts = [x for x in line.split()]
28642864
addr_start, addr_end = [int(x, 16) for x in parts[1].split("->")]
28652865
off = int(parts[3][:-1], 16)
28662866
path = parts[4]
@@ -6323,9 +6323,17 @@ def prepare_qemu_stub(self, target):
63236323
else:
63246324
raise RuntimeError("unsupported architecture: {}".format(arch))
63256325

6326-
ok("Setting QEMU-stub for '{}' (memory mapping may be wrong)".format(current_arch.arch))
6326+
ok("Setting Qemu-user stub for '{}' (memory mapping may be wrong)".format(current_arch.arch))
6327+
hide_context()
63276328
gdb.execute("target remote {}".format(target))
6328-
__gef_qemu_mode__ = True
6329+
unhide_context()
6330+
6331+
if get_pid() == 1 and "ENABLE=1" in gdb.execute("maintenance packet Qqemu.sstepbits", to_string=True, from_tty=False):
6332+
__gef_qemu_mode__ = True
6333+
reset_all_caches()
6334+
info("Note: By using Qemu mode, GEF will display the memory mapping of the Qemu process where the emulated binary resides")
6335+
get_process_maps()
6336+
gdb.execute("context")
63296337
return
63306338

63316339

0 commit comments

Comments
 (0)