From fe667912b5d12bd95420d3ef655c6da11f1b0aa4 Mon Sep 17 00:00:00 2001 From: teddav Date: Wed, 31 Jan 2024 17:31:30 +0100 Subject: [PATCH 1/4] fix: split current iterm window during gdb.debug process --- pwnlib/util/misc.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 22dff501a..03201b899 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -307,6 +307,10 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr elif 'STY' in os.environ and which('screen'): terminal = 'screen' args = ['-t','pwntools-gdb','bash','-c'] + elif 'TERM_PROGRAM' in os.environ and os.environ['TERM_PROGRAM'] == "iTerm.app" and which('osascript'): + # if we're on a mac, and using iTerm + terminal = "osascript" + args = [] elif 'TERM_PROGRAM' in os.environ and which(os.environ['TERM_PROGRAM']): terminal = os.environ['TERM_PROGRAM'] args = [] @@ -366,7 +370,6 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr args.extend(['wsl.exe', '-d', distro_name, 'bash', '-c']) else: args.extend(['bash.exe', '-c']) - if not terminal: log.error('Could not find a terminal binary to use. Set context.terminal to your terminal.') @@ -382,6 +385,27 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr if terminal == 'tmux': args += ['-F' '#{pane_pid}', '-P'] + # if we're on a Mac and use iTerm + # we use `osascript` to split the current window + if terminal == 'osascript': + osa_script = """ +tell application "iTerm" + tell current session of current window + set newSession to (split horizontally with default profile) + end tell + tell newSession + write text "{gdb_command}" + end tell +end tell +""" + osa_script = osa_script.format(gdb_command=" ".join(command)).lstrip() + with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp: + tmp.write(osa_script) + tmp.flush() + os.chmod(tmp.name, 0o700) + args = [tmp.name] + + argv = [which(terminal)] + args if isinstance(command, six.string_types): From c9cbb67482457ef10877af59c17d8b1149823f5f Mon Sep 17 00:00:00 2001 From: teddav Date: Wed, 31 Jan 2024 17:44:51 +0100 Subject: [PATCH 2/4] add change to changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 090209565..62c8ae917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,8 @@ The table below shows which release corresponds to each branch, and what date th - [#2323][2323] Retry failed lookups after one week in libcdb - [#2325][2325] Match against local system libc first in libcdb - [#2336][2336] Add `ELF.stripped` and `ELF.debuginfo` properties +- [#2338][2338] Fix: follow symlink for libs on ssh connection +- [#2341][2341] Launch GDB correctly in iTerm on Mac [2242]: https://github.com/Gallopsled/pwntools/pull/2242 [2277]: https://github.com/Gallopsled/pwntools/pull/2277 @@ -105,6 +107,8 @@ The table below shows which release corresponds to each branch, and what date th [2323]: https://github.com/Gallopsled/pwntools/pull/2323 [2325]: https://github.com/Gallopsled/pwntools/pull/2325 [2336]: https://github.com/Gallopsled/pwntools/pull/2336 +[2338]: https://github.com/Gallopsled/pwntools/pull/2338 +[2341]: https://github.com/Gallopsled/pwntools/pull/2341 ## 4.12.0 (`beta`) From fc070d0a2cd4512c05567b706affa16b40f34e8b Mon Sep 17 00:00:00 2001 From: teddav Date: Thu, 1 Feb 2024 08:34:58 +0100 Subject: [PATCH 3/4] escape cmd before writing osascript file --- pwnlib/util/misc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 03201b899..03725fab1 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -398,7 +398,8 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr end tell end tell """ - osa_script = osa_script.format(gdb_command=" ".join(command)).lstrip() + gdb_command = " ".join(command).replace('"', '\\"').replace("'", "\\'") + osa_script = osa_script.format(gdb_command=gdb_command).lstrip() with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp: tmp.write(osa_script) tmp.flush() From 5fe63fe1c3e57c349154c2a85a2112f12a9f9bc5 Mon Sep 17 00:00:00 2001 From: teddav Date: Thu, 1 Feb 2024 16:08:14 +0100 Subject: [PATCH 4/4] use the previously sanitized command to run the osascript --- pwnlib/util/misc.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 03725fab1..188be3457 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -385,28 +385,6 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr if terminal == 'tmux': args += ['-F' '#{pane_pid}', '-P'] - # if we're on a Mac and use iTerm - # we use `osascript` to split the current window - if terminal == 'osascript': - osa_script = """ -tell application "iTerm" - tell current session of current window - set newSession to (split horizontally with default profile) - end tell - tell newSession - write text "{gdb_command}" - end tell -end tell -""" - gdb_command = " ".join(command).replace('"', '\\"').replace("'", "\\'") - osa_script = osa_script.format(gdb_command=gdb_command).lstrip() - with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp: - tmp.write(osa_script) - tmp.flush() - os.chmod(tmp.name, 0o700) - args = [tmp.name] - - argv = [which(terminal)] + args if isinstance(command, six.string_types): @@ -435,6 +413,26 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr argv += [tmp.name] + # if we're on a Mac and use iTerm, we use `osascript` to split the current window + # `command` was sanitized on the previous step. It is now either a string, or was written to a tmp file + # we run the command, which is now `argv[-1]` + if terminal == 'osascript': + osa_script = f""" +tell application "iTerm" + tell current session of current window + set newSession to (split horizontally with default profile) + end tell + tell newSession + write text "{argv[-1]}" + end tell +end tell +""" + with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp: + tmp.write(osa_script.lstrip()) + tmp.flush() + os.chmod(tmp.name, 0o700) + argv = [which(terminal), tmp.name] + log.debug("Launching a new terminal: %r" % argv) stdin = stdout = stderr = open(os.devnull, 'r+b')