pythonpwntools

Using pwntools process interactive mode to control python3


I am trying to use pwntools to control a python3 session. Here is my code:

from pwn import process
r = process(['python3'])
r.interactive()

However, after I enter r.interactive(), when I type into the terminal, the python3 sub-process has strange reactions. At least I do not see my commands echoed back most of the times.

I also tried to call python3 in a bash session, but the same thing happens.

$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import process
>>> r = process(['bash'])
[x] Starting local process '/usr/bin/bash'
[+] Starting local process '/usr/bin/bash': pid 119080
>>> r.interactive()
[*] Switching to interactive mode
echo hello 
hello
echo this is bash
this is bash
python3
print(1)
print(2)
print(3)
exit
echo hello
  File "<stdin>", line 5
    echo hello
         ^
SyntaxError: invalid syntax

Why is this happening? Is it a bug in pwntools, or are there some configurations I overlook?


Solution

  • You need to specify the PTY in your shell, so like this:

    $ python3
    Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
    [GCC 10.2.1 20210110] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from pwn import *; r = process(['python3'], stdin=PTY, raw=False); r.interactive()
    [x] Starting local process '/usr/bin/python3'
    [+] Starting local process '/usr/bin/python3': pid 2984281
    [*] Switching to interactive mode
    Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
    [GCC 10.2.1 20210110] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 1+1
    1+1
    2
    >>>