Skip to content

Commit

Permalink
[run_in_new_terminal] Create a wrapper script automatically if we are…
Browse files Browse the repository at this point in the history
… given a list or tuple
  • Loading branch information
heapcrash committed Mar 3, 2021
1 parent e897992 commit dedc4e8
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions pwnlib/util/misc.py
Expand Up @@ -10,6 +10,8 @@
import stat
import string
import subprocess
import sys
import tempfile

from pwnlib import atexit
from pwnlib.context import context
Expand Down Expand Up @@ -261,9 +263,26 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
log.error("Cannot use commands with semicolon. Create a script and invoke that directly.")
argv += [command]
elif isinstance(command, (list, tuple)):
if any(';' in c for c in command):
log.error("Cannot use commands with semicolon. Create a script and invoke that directly.")
argv += list(command)
# Dump the full command line to a temporary file so we can be sure that
# it is parsed correctly, and we do not need to account for shell expansion
script = '''
#!{executable!s}
import os
os.execve({argv0!r}, {argv!r}, os.environ)
'''
script = script.format(executable=sys.executable,
argv=command,
argv0=which(command[0]))
script = script.lstrip()

log.debug("Created script for new terminal:\n%s" % script)

with tempfile.NamedTemporaryFile(delete=False, mode='wt+') as tmp:
tmp.write(script)
tmp.flush()
os.chmod(tmp.name, 0o700)
argv += [tmp.name]


log.debug("Launching a new terminal: %r" % argv)

Expand Down

0 comments on commit dedc4e8

Please sign in to comment.