Skip to content

Commit

Permalink
use the previously sanitized command to run the osascript
Browse files Browse the repository at this point in the history
  • Loading branch information
teddav committed Feb 1, 2024
1 parent fc070d0 commit a3465bb
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions pwnlib/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit a3465bb

Please sign in to comment.