|
| 1 | +""" |
| 2 | +ARM through OpenOCD support for GEF |
| 3 | +
|
| 4 | +To use, source this file *after* gef |
| 5 | +
|
| 6 | +Author: Grazfather |
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Optional |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import gdb |
| 13 | + |
| 14 | +assert 'gef' in globals(), "This file must be source after gef.py" |
| 15 | + |
| 16 | + |
| 17 | +class ARMOpenOCD(ARM): |
| 18 | + arch = "ARMOpenOCD" |
| 19 | + aliases = ("ARMOpenOCD",) |
| 20 | + all_registers = ("$r0", "$r1", "$r2", "$r3", "$r4", "$r5", "$r6", |
| 21 | + "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$sp", |
| 22 | + "$lr", "$pc", "$xPSR") |
| 23 | + flag_register = "$xPSR" |
| 24 | + @staticmethod |
| 25 | + def supports_gdb_arch(arch: str) -> Optional[bool]: |
| 26 | + if "arm" in arch and arch.endswith("-m"): |
| 27 | + return True |
| 28 | + return None |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def maps(): |
| 32 | + yield from GefMemoryManager.parse_info_mem() |
| 33 | + |
| 34 | + |
| 35 | +@register |
| 36 | +class OpenOCDRemoteCommand(GenericCommand): |
| 37 | + """This command is intended to replace `gef-remote` to connect to an |
| 38 | + OpenOCD-hosted gdbserver. It uses a special session manager that knows how |
| 39 | + to connect and manage the server.""" |
| 40 | + |
| 41 | + _cmdline_ = "gef-openocd-remote" |
| 42 | + _syntax_ = f"{_cmdline_} [OPTIONS] HOST PORT" |
| 43 | + _example_ = [f"{_cmdline_} --file /path/to/binary.elf localhost 3333", |
| 44 | + f"{_cmdline_} localhost 3333"] |
| 45 | + |
| 46 | + def __init__(self) -> None: |
| 47 | + super().__init__(prefix=False) |
| 48 | + return |
| 49 | + |
| 50 | + @parse_arguments({"host": "", "port": 0}, {"--file": ""}) |
| 51 | + def do_invoke(self, _: list[str], **kwargs: Any) -> None: |
| 52 | + if gef.session.remote is not None: |
| 53 | + err("You're already in a remote session. Close it first before opening a new one...") |
| 54 | + return |
| 55 | + |
| 56 | + # argument check |
| 57 | + args: argparse.Namespace = kwargs["arguments"] |
| 58 | + if not args.host or not args.port: |
| 59 | + err("Missing parameters") |
| 60 | + return |
| 61 | + |
| 62 | + # Try to establish the remote session, throw on error |
| 63 | + # Set `.remote_initializing` to True here - `GefRemoteSessionManager` invokes code which |
| 64 | + # calls `is_remote_debug` which checks if `remote_initializing` is True or `.remote` is None |
| 65 | + # This prevents some spurious errors being thrown during startup |
| 66 | + gef.session.remote_initializing = True |
| 67 | + session = GefOpenOCDRemoteSessionManager(args.host, args.port, args.file) |
| 68 | + |
| 69 | + dbg(f"[remote] initializing remote session with {session.target} under {session.root}") |
| 70 | + |
| 71 | + # Connect can return false if it wants us to disconnect |
| 72 | + if not session.connect(): |
| 73 | + gef.session.remote = None |
| 74 | + gef.session.remote_initializing = False |
| 75 | + return |
| 76 | + if not session.setup(): |
| 77 | + gef.session.remote = None |
| 78 | + gef.session.remote_initializing = False |
| 79 | + raise EnvironmentError("Failed to setup remote target") |
| 80 | + |
| 81 | + gef.session.remote_initializing = False |
| 82 | + gef.session.remote = session |
| 83 | + reset_all_caches() |
| 84 | + gdb.execute("context") |
| 85 | + return |
| 86 | + |
| 87 | + |
| 88 | +# We CANNOT use the normal session manager because it assumes we have a PID |
| 89 | +class GefOpenOCDRemoteSessionManager(GefRemoteSessionManager): |
| 90 | + """This subclass of GefRemoteSessionManager specially handles the |
| 91 | + intricacies involved with connecting to an OpenOCD-hosted GDB server. |
| 92 | + Specifically, it does not have the concept of PIDs which we need to work |
| 93 | + around.""" |
| 94 | + def __init__(self, host: str, port: str, file: str="") -> None: |
| 95 | + self.__host = host |
| 96 | + self.__port = port |
| 97 | + self.__file = file |
| 98 | + self.__local_root_fd = tempfile.TemporaryDirectory() |
| 99 | + self.__local_root_path = Path(self.__local_root_fd.name) |
| 100 | + class OpenOCDMode(): |
| 101 | + def prompt_string(self) -> str: |
| 102 | + return Color.boldify("(OpenOCD) ") |
| 103 | + |
| 104 | + self._mode = OpenOCDMode() |
| 105 | + |
| 106 | + def __str__(self) -> str: |
| 107 | + return f"OpenOCDRemoteSessionManager(='{self.__tty}', file='{self.__file}', attach={self.__attach})" |
| 108 | + |
| 109 | + def close(self) -> None: |
| 110 | + self.__local_root_fd.cleanup() |
| 111 | + try: |
| 112 | + gef_on_new_unhook(self.remote_objfile_event_handler) |
| 113 | + gef_on_new_hook(new_objfile_handler) |
| 114 | + except Exception as e: |
| 115 | + warn(f"Exception while restoring local context: {str(e)}") |
| 116 | + return |
| 117 | + |
| 118 | + @property |
| 119 | + def target(self) -> str: |
| 120 | + return f"{self.__host}:{self.__port}" |
| 121 | + |
| 122 | + @property |
| 123 | + def root(self) -> Path: |
| 124 | + return self.__local_root_path.absolute() |
| 125 | + |
| 126 | + def sync(self, src: str, dst: Optional[str] = None) -> bool: |
| 127 | + # We cannot sync from this target |
| 128 | + return None |
| 129 | + |
| 130 | + @property |
| 131 | + def file(self) -> Optional[Path]: |
| 132 | + if self.__file: |
| 133 | + return Path(self.__file).expanduser() |
| 134 | + return None |
| 135 | + |
| 136 | + def connect(self) -> bool: |
| 137 | + """Connect to remote target. If in extended mode, also attach to the given PID.""" |
| 138 | + # before anything, register our new hook to download files from the remote target |
| 139 | + dbg(f"[remote] Installing new objfile handlers") |
| 140 | + try: |
| 141 | + gef_on_new_unhook(new_objfile_handler) |
| 142 | + except SystemError: |
| 143 | + # the default objfile handler might already have been removed, ignore failure |
| 144 | + pass |
| 145 | + |
| 146 | + gef_on_new_hook(self.remote_objfile_event_handler) |
| 147 | + |
| 148 | + # Connect |
| 149 | + with DisableContextOutputContext(): |
| 150 | + self._gdb_execute(f"target extended-remote {self.target}") |
| 151 | + |
| 152 | + try: |
| 153 | + with DisableContextOutputContext(): |
| 154 | + if self.file: |
| 155 | + self._gdb_execute(f"file '{self.file}'") |
| 156 | + except Exception as e: |
| 157 | + err(f"Failed to connect to {self.target}: {e}") |
| 158 | + # a failure will trigger the cleanup, deleting our hook |
| 159 | + return False |
| 160 | + |
| 161 | + return True |
| 162 | + |
| 163 | + def setup(self) -> bool: |
| 164 | + dbg(f"Setting up as remote session") |
| 165 | + |
| 166 | + # refresh gef to consider the binary |
| 167 | + reset_all_caches() |
| 168 | + if self.file: |
| 169 | + gef.binary = Elf(self.file) |
| 170 | + # We'd like to set this earlier, but we can't because of this bug |
| 171 | + # https://sourceware.org/bugzilla/show_bug.cgi?id=31303 |
| 172 | + reset_architecture("ARMOpenOCD") |
| 173 | + return True |
| 174 | + |
| 175 | + def _gdb_execute(self, cmd): |
| 176 | + dbg(f"[remote] Executing '{cmd}'") |
| 177 | + gdb.execute(cmd) |
0 commit comments