PwnTools recv() on output that expects input directly after

Arwez picture Arwez · Oct 12, 2019 · Viewed 12.2k times · Source

Hi I have a problem that I cannot seem to find any solution for. (Maybe i'm just horrible at phrasing searches correctly in english)

I'm trying to execute a binary from python using pwntools and reading its output completely before sending some input myself.

The output from my binary is as follows:

Testmessage1
Testmessage2
Enter input: <binary expects me to input stuff here>

Where I would like to read the first line, the second line and the output part of the third line (with ':' being the last character).

The third line of the output does not contain a newline at the end and expects the user to make an input directly. However, I'm not able to read the output contents that the third line starts with, no matter what I try.

My current way of trying to achieve this:

from pwn import *

io = process("./testbin")
print io.recvline()
print io.recvline()
print io.recvuntil(":", timeout=1) # this get's stuck if I dont use a timeout
...
# maybe sending data here
# io.send(....)
io.close()

Do I missunderstand something about stdin and stdout? Is "Enter input:" of the third line not part of the output that I should be able to receive before making an input?

Thanks in advance

Answer

Arwez picture Arwez · Oct 18, 2019

I finally figured it out. I got the hint I needed from https://github.com/zachriggle/pwntools-glibc-buffering/blob/master/demo.py

It seems that Ubuntu is doing lots of buffering on its own. When manually making sure that pwnTools uses a pseudoterminal for stdin and stdout it works!

import * from pwn

pty = process.PTY
p = process(stdin=pty, stdout=pty)