使用pwntools进程交互方式控制python3

Using pwntools process interactive mode to control python3

我正在尝试使用 pwntools 来控制 python3 会话。这是我的代码:

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

但是,在我输入 r.interactive() 后,当我输入终端时, python3 子进程有奇怪的反应。至少大多数时候我没有看到我的命令得到回应。

我也尝试在 bash 会话中调用 python3,但同样的事情发生了。

$ 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

为什么会这样?是 pwntools 的错误,还是我忽略了一些配置?

您需要在 shell 中指定 PTY,如下所示:

$ 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
>>>