pwntools icon indicating copy to clipboard operation
pwntools copied to clipboard

How to exit `interactive()` when EOF received?

Open gsingh93 opened this issue 2 years ago • 2 comments

If I call process.interactive() and the process terminates, I get a message about an EOFError but the interactive prompt still stays open. How can I break from this prompt when an error is received?

gsingh93 avatar Sep 05 '22 16:09 gsingh93

Here's an example I'm using to test:

#!/usr/bin/env python3

from pwn import *

def check_connection(p):
    print('p.proc.stdin.closed:', p.proc.stdin.closed)
    print('p.proc.stdout.closed:', p.proc.stdout.closed)
    print("p.connected('send'):", p.connected('send'))
    print("p.connected('recv')", p.connected('recv'))
    print("p.can_recv()", p.can_recv())
    print('')

p = process('/bin/ls')

check_connection(p)

p.interactive()

check_connection(p)

try:
    p.send(b'A')
except:
    pass

check_connection(p)

Output:

$ ./test-interactive.py
[+] Starting local process '/bin/ls': pid 2131696
p.proc.stdin.closed: False
p.proc.stdout.closed: False
p.connected('send'): True
p.connected('recv') True
p.can_recv() True

[*] Switching to interactive mode
[*] Process '/bin/ls' stopped with exit code 0 (pid 2131696)
a.out  aslr.c  test-interactive.py  test.py
[*] Got EOF while reading in interactive
$
[*] Interrupted
p.proc.stdin.closed: False
p.proc.stdout.closed: True
p.connected('send'): True
p.connected('recv') False
p.can_recv() False

p.proc.stdin.closed: False
p.proc.stdout.closed: True
p.connected('send'): True
p.connected('recv') False
p.can_recv() False

Traceback (most recent call last):
  File "/home/gsgx/.local/lib/python3.10/site-packages/pwnlib/tubes/process.py", line 746, in close
    fd.close()
BrokenPipeError: [Errno 32] Broken pipe

So the EOFError happened in the middle of interactive() on the recv side, so I can't check for this before calling it. Essentially, I'd like interactive() to end if the recv end disconnects, without waiting for the send end to disconnect.

gsingh93 avatar Sep 11 '22 20:09 gsingh93

This hacky patch seems to fix the issue: https://github.com/gsingh93/pwntools/commit/48734146f582c85599d511a6275828d8416c99bf

gsingh93 avatar Sep 11 '22 22:09 gsingh93