pwntools icon indicating copy to clipboard operation
pwntools copied to clipboard

Error when attempting to recv from process with redirected stdin

Open spencerpogo opened this issue 3 years ago • 1 comments

Update Pwntools First

When reporting an issue, be sure that you are running the latest released version of pwntools (pip install --upgrade pwntools).

Debug Output

+>>> from pwn import *
+>>> context.log_level = "DEBUG"
+>>> f = open("a.txt", "r")
+>>> p = process(["cat", "-"], stdin=f)
[x] Starting local process '/nix/store/y72i4llqf5zxvdp6b3j7ysixpdrid7qr-coreutils-9.1/bin/cat' argv=[b'cat', b'-']
[+] Starting local process '/nix/store/y72i4llqf5zxvdp6b3j7ysixpdrid7qr-coreutils-9.1/bin/cat' argv=[b'cat', b'-'] : pid 14872
+>>> p.recvall()
[x] Receiving all data
[x] Receiving all data: 0B
[*] Process '/nix/store/y72i4llqf5zxvdp6b3j7ysixpdrid7qr-coreutils-9.1/bin/cat' stopped with exit code 0 (pid 14872)
[DEBUG] Received 0x2 bytes:
    b'a\n'
[x] Receiving all data: 2B
[-] Receiving all data: Failed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/49zq894r42a1ncbxvg9i7chqp4ajlyd4-python3.9-pwntools-4.8.0/lib/python3.9/site-packages/pwnlib/tubes/tube.py", line 737, in recvall
    if not self._fillbuffer():
  File "/nix/store/49zq894r42a1ncbxvg9i7chqp4ajlyd4-python3.9-pwntools-4.8.0/lib/python3.9/site-packages/pwnlib/tubes/tube.py", line 154, in _fillbuffer
    data = self.recv_raw(self.buffer.get_fill_size())
  File "/nix/store/49zq894r42a1ncbxvg9i7chqp4ajlyd4-python3.9-pwntools-4.8.0/lib/python3.9/site-packages/pwnlib/tubes/process.py", line 685, in recv_raw
    self.shutdown("recv")
  File "/nix/store/49zq894r42a1ncbxvg9i7chqp4ajlyd4-python3.9-pwntools-4.8.0/lib/python3.9/site-packages/pwnlib/tubes/tube.py", line 1236, in shutdown
    self.shutdown_raw(self.shutdown_directions[direction])
  File "/nix/store/49zq894r42a1ncbxvg9i7chqp4ajlyd4-python3.9-pwntools-4.8.0/lib/python3.9/site-packages/pwnlib/tubes/process.py", line 771, in shutdown_raw
    if False not in [self.proc.stdin.closed, self.proc.stdout.closed]:
AttributeError: 'NoneType' object has no attribute 'closed'

Verify on Ubuntu

If possible, please verify that your issue occurs on 64-bit Ubuntu 18.04. We provide a Dockerfile based on Ubuntu 18.04 via docker.io to make this super simple, no VM required!

# Download the Docker image
$ docker pull pwntools/pwntools:stable

# Boot the image
$ docker run -it pwntools/pwntools:stable

pwntools@7dc3ef409476:~$ python
>>> from pwn import *
>>> # Test your code here

Dockerfile seems to be broken:

❯ sudo docker run -it pwntools/pwntools:stable
pwntools@ad1473d840de:~$ python
Python 2.7.17 (default, Feb 27 2021, 15:10:58)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pwn import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pwn/__init__.py", line 4, in <module>
    from pwn.toplevel import *
  File "/usr/local/lib/python2.7/dist-packages/pwn/toplevel.py", line 36, in <module>
    from pwnlib.filesystem import *
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/filesystem/__init__.py", line 5, in <module>
    from pwnlib.filesystem.path import Path
  File "/usr/local/lib/python2.7/dist-packages/pwnlib/filesystem/path.py", line 7, in <module>
    from pathlib2 import *
ImportError: No module named pathlib2

I got the same behavior on non-NixOS Linux so I don't think it's OS-specific

spencerpogo avatar Jun 23 '22 18:06 spencerpogo

stdin appears to be None on a regular Popen instance as well:

+>>> import subprocess as sp
+>>> f = open("a.txt", "r")
+>>> p = sp.Popen(["cat", "-"], stdin=f, stdout=sp.PIPE)
+>>> p.stdin
+>>> p.communicate()
(b'a\n', None)
+>>> p.communicate()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/553d7c4xcwp9j1a1gb9cb1s9ry3x1pi9-python3-3.9.13/lib/python3.9/subprocess.py", line 1121, in communicate
    stdout = self.stdout.read()
ValueError: read of closed file
+>>> p.stdin
+>>>

spencerpogo avatar Jun 23 '22 18:06 spencerpogo

This was fixed in https://github.com/Gallopsled/pwntools/commit/fa08f5a

peace-maker avatar Jan 03 '24 00:01 peace-maker