Skip to content

Commit

Permalink
get_pid() -> gef.session.pid
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Dec 14, 2021
1 parent e709aeb commit 43a9920
Showing 1 changed file with 21 additions and 81 deletions.
102 changes: 21 additions & 81 deletions gef.py
Expand Up @@ -1931,16 +1931,6 @@ def checksec(filename):
- Partial/Full RelRO.
Return a dict() with the different keys mentioned above, and the boolean
associated whether the protection was found."""

if is_macho(filename):
return {
"Canary": False,
"NX": False,
"PIE": False,
"Fortify": False,
"Partial RelRO": False,
}

readelf = gef.session.constants["readelf"]

def __check_security_property(opt, filename, pattern):
Expand Down Expand Up @@ -3088,10 +3078,6 @@ def is_qemu_system():
response = gdb.execute('maintenance packet QOffsets', to_string=True, from_tty=False)
return 'received: ""' in response

@deprecated("Use `gef.session.pid`")
def get_pid():
return gef.session.pid


@lru_cache()
def get_filepath():
Expand All @@ -3101,7 +3087,7 @@ def get_filepath():
if is_remote_debug():
# if no filename specified, try downloading target from /proc
if filename is None:
pid = get_pid()
pid = gef.session.pid
if pid > 0:
return download_file("/proc/{:d}/exe".format(pid), use_cache=True)
return None
Expand All @@ -3125,38 +3111,13 @@ def get_filepath():
return get_path_from_info_proc()


@deprecated("Use `gef.session.file`")
def get_filename():
return gef.session.file


@lru_cache()
def inferior_is_macho():
"""Return True if the current file is a Mach-O binary."""
for x in gdb.execute("info files", to_string=True).splitlines():
if "file type mach-o" in x:
return True
return False


@lru_cache()
def is_macho(filename):
"""Return True if the specified file is a Mach-O binary."""
file_bin = gef.session.constants["file"]
cmd = [file_bin, filename]
out = gef_execute_external(cmd)
if "Mach-O" in out:
return True
return False


def download_file(target, use_cache=False, local_name=None):
"""Download filename `target` inside the mirror tree inside the gef.config["gef.tempdir"].
The tree architecture must be gef.config["gef.tempdir"]/gef/<local_pid>/<remote_filepath>.
This allow a "chroot-like" tree format."""

try:
local_root = os.path.sep.join([gef.config["gef.tempdir"], str(get_pid())])
local_root = os.path.sep.join([gef.config["gef.tempdir"], str(gef.session.pid)])
if local_name is None:
local_path = os.path.sep.join([local_root, os.path.dirname(target)])
local_name = os.path.sep.join([local_path, os.path.basename(target)])
Expand Down Expand Up @@ -3232,38 +3193,12 @@ def get_process_maps_linux(proc_map_file):
return


def get_mach_regions():
sp = gef.arch.sp
for line in gdb.execute("info mach-regions", to_string=True).splitlines():
line = line.strip()
addr, perm, _ = line.split(" ", 2)
addr_start, addr_end = [int(x, 16) for x in addr.split("-")]
perm = Permission.from_process_maps(perm.split("/")[0])

zone = file_lookup_address(addr_start)
if zone:
path = zone.filename
else:
path = "[stack]" if sp >= addr_start and sp < addr_end else ""

yield Section(page_start=addr_start,
page_end=addr_end,
offset=0,
permission=perm,
inode=None,
path=path)
return


@lru_cache()
def get_process_maps():
"""Return the mapped memory sections"""

if inferior_is_macho():
return list(get_mach_regions())

try:
pid = get_pid()
pid = gef.session.pid
fpath = "/proc/{:d}/maps".format(pid)
return list(get_process_maps_linux(fpath))
except FileNotFoundError as e:
Expand Down Expand Up @@ -4006,6 +3941,13 @@ def gef_getpagesize():
def gef_read_canary():
return gef.session.canary

@deprecated("Use `gef.session.pid`")
def get_pid():
return gef.session.pid

@deprecated("Use `gef.session.file`")
def get_filename():
return gef.session.file

#
# GDB event hooking
Expand Down Expand Up @@ -5061,7 +5003,7 @@ def do_invoke(self, argv):

canary, location = res
info("Found AT_RANDOM at {:#x}, reading {} bytes".format(location, gef.arch.ptrsize))
info("The canary of process {} is {:#x}".format(get_pid(), canary))
info("The canary of process {} is {:#x}".format(gef.session.pid, canary))
return


Expand Down Expand Up @@ -5113,7 +5055,7 @@ def get_children_pids(self, pid):

def show_info_proc(self):
info("Process Information")
pid = get_pid()
pid = gef.session.pid
cmdline = self.get_cmdline_of(pid)
gef_print("\tPID {} {}".format(RIGHT_ARROW, pid))
gef_print("\tExecutable {} {}".format(RIGHT_ARROW, self.get_process_path_of(pid)))
Expand All @@ -5122,7 +5064,7 @@ def show_info_proc(self):

def show_ancestor(self):
info("Parent Process Information")
ppid = int(self.get_state_of(get_pid())["PPid"])
ppid = int(self.get_state_of(gef.session.pid)["PPid"])
state = self.get_state_of(ppid)
cmdline = self.get_cmdline_of(ppid)
gef_print("\tParent PID {} {}".format(RIGHT_ARROW, state["Pid"]))
Expand All @@ -5131,7 +5073,7 @@ def show_ancestor(self):

def show_descendants(self):
info("Children Process Information")
children = self.get_children_pids(get_pid())
children = self.get_children_pids(gef.session.pid)
if not children:
gef_print("\tNo child process")
return
Expand All @@ -5146,7 +5088,7 @@ def show_descendants(self):
return

def show_fds(self):
pid = get_pid()
pid = gef.session.pid
path = "/proc/{:d}/fd".format(pid)

info("File Descriptors:")
Expand Down Expand Up @@ -5198,7 +5140,7 @@ def show_connections(self):
}

info("Network Connections")
pid = get_pid()
pid = gef.session.pid
sockets = self.list_sockets(pid)
if not sockets:
gef_print("\tNo open connections")
Expand Down Expand Up @@ -5624,7 +5566,7 @@ def do_invoke(self, argv):
self.usage()
return

if not os.access("/proc/{:d}/fd/{:s}".format(get_pid(), argv[0]), os.R_OK):
if not os.access("/proc/{:d}/fd/{:s}".format(gef.session.pid, argv[0]), os.R_OK):
self.usage()
return

Expand Down Expand Up @@ -6547,7 +6489,7 @@ def do_invoke(self, argv, *args, **kwargs):
if not self.connect_target(target, args.is_extended_remote):
return

pid = args.pid if args.is_extended_remote and args.pid else get_pid()
pid = args.pid if args.is_extended_remote and args.pid else gef.session.pid
if args.is_extended_remote:
ok("Attaching to {:d}".format(pid))
hide_context()
Expand Down Expand Up @@ -6622,7 +6564,7 @@ def setup_remote_environment(self, pid, update_solib=False):
err("Source binary is not readable")
return

directory = os.path.sep.join([gef.config["gef.tempdir"], str(get_pid())])
directory = os.path.sep.join([gef.config["gef.tempdir"], str(gef.session.pid)])
# gdb.execute("file {:s}".format(infos["exe"]))
self["root"] = ( directory, "Path to store the remote data")
ok("Remote information loaded to temporary path '{:s}'".format(directory))
Expand Down Expand Up @@ -6703,7 +6645,7 @@ def prepare_qemu_stub(self, target):
gdb.execute("target remote {}".format(target))
unhide_context()

if get_pid() == 1 and "ENABLE=1" in gdb.execute("maintenance packet Qqemu.sstepbits", to_string=True, from_tty=False):
if gef.session.pid == 1 and "ENABLE=1" in gdb.execute("maintenance packet Qqemu.sstepbits", to_string=True, from_tty=False):
__gef_qemu_mode__ = True
reset_all_caches()
info("Note: By using Qemu mode, GEF will display the memory mapping of the Qemu process where the emulated binary resides")
Expand Down Expand Up @@ -10334,9 +10276,7 @@ class SyscallArgsCommand(GenericCommand):

def __init__(self):
super().__init__()
path = pathlib.Path(self["path"]) / "syscall-tables"
if not path.exists():
raise EnvironmentError("Syscall tables directory not found")
path = pathlib.Path(gef.config["gef.tempdir"]) / "syscall-tables"
self["path"] = (str(path.absolute()), "Path to store/load the syscall tables files")
return

Expand Down

0 comments on commit 43a9920

Please sign in to comment.