|
13 | 13 | import sys
|
14 | 14 | import tempfile
|
15 | 15 | import inspect
|
| 16 | +import time |
16 | 17 | import types
|
17 | 18 |
|
18 | 19 | from pwnlib import atexit
|
19 | 20 | from pwnlib.context import context
|
20 | 21 | from pwnlib.log import getLogger
|
| 22 | +from pwnlib.timeout import Timeout |
21 | 23 | from pwnlib.util import fiddling
|
22 | 24 | from pwnlib.util import lists
|
23 | 25 | from pwnlib.util import packing
|
@@ -439,6 +441,11 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
|
439 | 441 | tmp.flush()
|
440 | 442 | os.chmod(tmp.name, 0o700)
|
441 | 443 | argv = [which(terminal), tmp.name]
|
| 444 | + # cmd.exe does not support WSL UNC paths as working directory |
| 445 | + # so it gets reset to %WINDIR% before starting wsl again. |
| 446 | + # Set the working directory correctly in WSL. |
| 447 | + elif terminal == 'cmd.exe': |
| 448 | + argv[-1] = "cd '{}' && {}".format(os.getcwd(), argv[-1]) |
442 | 449 |
|
443 | 450 | log.debug("Launching a new terminal: %r" % argv)
|
444 | 451 |
|
@@ -472,10 +479,26 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
|
472 | 479 | kittyid = None
|
473 | 480 | if kittyid is None:
|
474 | 481 | log.error("Could not parse kitty window ID from output (%r)", out)
|
| 482 | + elif terminal == 'cmd.exe': |
| 483 | + # p.pid is cmd.exe's pid instead of the WSL process we want to start eventually. |
| 484 | + # I don't know how to trace the execution through Windows and back into the WSL2 VM. |
| 485 | + # Do a best guess by waiting for a new process matching the command to be run. |
| 486 | + # Otherwise it's better to return nothing instead of a know wrong pid. |
| 487 | + from pwnlib.util.proc import pid_by_name |
| 488 | + pid = None |
| 489 | + ran_program = command.split(' ')[0] if isinstance(command, six.string_types) else command[0] |
| 490 | + t = Timeout() |
| 491 | + with t.countdown(timeout=5): |
| 492 | + while t.timeout: |
| 493 | + new_pid = pid_by_name(ran_program) |
| 494 | + if new_pid and new_pid[0] > p.pid: |
| 495 | + pid = new_pid[0] |
| 496 | + break |
| 497 | + time.sleep(0.01) |
475 | 498 | else:
|
476 | 499 | pid = p.pid
|
477 | 500 |
|
478 |
| - if kill_at_exit: |
| 501 | + if kill_at_exit and pid: |
479 | 502 | def kill():
|
480 | 503 | try:
|
481 | 504 | if terminal == 'qdbus':
|
|
0 commit comments