Skip to content

Commit

Permalink
fix: split current iterm window during gdb.debug process (#2341)
Browse files Browse the repository at this point in the history
* fix: split current iterm window during gdb.debug process

* add change to changelog

* escape cmd before writing osascript file

* use the previously sanitized command to run the osascript

---------

Co-authored-by: peace-maker <[email protected]>
  • Loading branch information
teddav and peace-maker authored Feb 8, 2024
1 parent 604b98c commit 73f2a31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ The table below shows which release corresponds to each branch, and what date th
- [#2160][2161] Fix invalid shellcraft.mov on arm64
- [#2284][2161] Fix invalid shellcraft.pushstr_array on arm64
- [#2345][2345] Fix pwn constgrep when it matches a non-constant type
- [#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
Expand All @@ -112,6 +114,8 @@ The table below shows which release corresponds to each branch, and what date th
[2336]: https://github.com/Gallopsled/pwntools/pull/2336
[2161]: https://github.com/Gallopsled/pwntools/pull/2161
[2345]: https://github.com/Gallopsled/pwntools/pull/2345
[2338]: https://github.com/Gallopsled/pwntools/pull/2338
[2341]: https://github.com/Gallopsled/pwntools/pull/2341

## 4.12.0 (`beta`)

Expand Down
25 changes: 24 additions & 1 deletion pwnlib/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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.')
Expand Down Expand Up @@ -410,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')
Expand Down

0 comments on commit 73f2a31

Please sign in to comment.