Skip to content

Commit

Permalink
Enable basic tubes/process test on Windows
Browse files Browse the repository at this point in the history
Fix detecting EOF of receiving side in `process.can_recv_raw` by breaking the loop when the read thread terminates.
  • Loading branch information
peace-maker committed Dec 26, 2024
1 parent cf4f7be commit 996c09e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
4 changes: 0 additions & 4 deletions docs/source/tubes/processes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.process` --- Processes
===========================================================

Expand Down
4 changes: 0 additions & 4 deletions docs/source/tubes/serial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.serialtube` --- Serial Ports
===========================================================

Expand Down
79 changes: 65 additions & 14 deletions pwnlib/tubes/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,22 @@ class process(tube):
True
>>> p.connected('send')
False
>>> p.recvline()
b'Hello world\n'
>>> p.recvline() # doctest: +ELLIPSIS
b'Hello world...\n'
>>> p.recvuntil(b',')
b'Wow,'
>>> p.recvregex(b'.*data')
b' such data'
>>> p.recv()
b'\n'
>>> p.recv() # doctest: +ELLIPSIS
b'...\n'
>>> p.recv() # doctest: +ELLIPSIS
Traceback (most recent call last):
...
EOFError
.. doctest::
:options: +POSIX
>>> p = process('cat')
>>> d = open('/dev/urandom', 'rb').read(4096)
>>> p.recv(timeout=0.1)
Expand Down Expand Up @@ -513,6 +516,9 @@ def program(self):
Example:
.. doctest::
:options: +POSIX +TODO
>>> p = process('/bin/true')
>>> p.executable == '/bin/true'
True
Expand All @@ -528,6 +534,9 @@ def cwd(self):
Example:
.. doctest::
:options: +POSIX +TODO
>>> p = process('sh')
>>> p.sendline(b'cd /tmp; echo AAA')
>>> _ = p.recvuntil(b'AAA')
Expand Down Expand Up @@ -767,9 +776,13 @@ def can_recv_raw(self, timeout):

if IS_WINDOWS:
with self.countdown(timeout=timeout):
while self.timeout and self._read_queue.empty():
while self.timeout and self._read_queue.empty() and self._read_thread.is_alive():
time.sleep(0.01)
return not self._read_queue.empty()
if not self._read_queue.empty():
return True
if not self._read_thread.is_alive():
raise EOFError
return False

try:
if timeout is None:
Expand Down Expand Up @@ -896,7 +909,10 @@ def maps(self):
read, write, execute, private, shared, string
Example:
.. doctest::
:options: +POSIX +TODO
>>> p = process(['cat'])
>>> p.sendline(b"meow")
>>> p.recvline()
Expand Down Expand Up @@ -976,7 +992,10 @@ def get_mapping(self, path_value, single=True):
path_value.
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> mapping = p.get_mapping('[stack]')
>>> mapping.path == '[stack]'
Expand Down Expand Up @@ -1019,6 +1038,9 @@ def stack_mapping(self, single=True):
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> mapping = p.stack_mapping()
>>> mapping.path
Expand Down Expand Up @@ -1048,6 +1070,9 @@ def heap_mapping(self, single=True):
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> p.sendline(b'meow')
>>> p.recvline()
Expand Down Expand Up @@ -1080,6 +1105,9 @@ def vdso_mapping(self, single=True):
Example:
.. doctest::
:options: +LINUX
>>> p = process(['cat'])
>>> mapping = p.vdso_mapping()
>>> mapping.path
Expand Down Expand Up @@ -1109,6 +1137,9 @@ def vvar_mapping(self, single=True):
Example:
.. doctest::
:options: +LINUX
>>> p = process(['cat'])
>>> mapping = p.vvar_mapping()
>>> mapping.path
Expand Down Expand Up @@ -1139,6 +1170,9 @@ def libc_mapping(self, single=True):
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> p.sendline(b'meow')
>>> p.recvline()
Expand Down Expand Up @@ -1219,6 +1253,9 @@ def elf_mapping(self, single=True):
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> p.sendline(b'meow')
>>> p.recvline()
Expand Down Expand Up @@ -1257,7 +1294,9 @@ def lib_size(self, path_value):
Example:
>>> from pwn import *
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> libc_size = p.lib_size(p.libc.path)
>>> hex(libc_size) # doctest: +SKIP
Expand Down Expand Up @@ -1294,6 +1333,9 @@ def address_mapping(self, address):
Example:
.. doctest::
:options: +POSIX
>>> p = process(['cat'])
>>> p.sendline(b'meow')
>>> p.recvline()
Expand Down Expand Up @@ -1363,11 +1405,14 @@ def libc(self):
Example:
>>> p = process("/bin/cat")
>>> libc = p.libc
>>> libc # doctest: +SKIP
ELF('/lib64/libc-...so')
>>> p.close()
.. doctest::
:options: +POSIX
>>> p = process("/bin/cat")
>>> libc = p.libc
>>> libc # doctest: +SKIP
ELF('/lib64/libc-...so')
>>> p.close()
"""
from pwnlib.elf import ELF

Expand Down Expand Up @@ -1442,6 +1487,9 @@ def leak(self, address, count=1):
Example:
.. doctest::
:options: +POSIX +TODO
>>> e = ELF(which('bash-static'))
>>> p = process(e.path)
Expand Down Expand Up @@ -1478,6 +1526,9 @@ def writemem(self, address, data):
Let's write data to the beginning of the mapped memory of the ELF.
.. doctest::
:options: +POSIX +TODO
>>> context.clear(arch='i386')
>>> address = 0x100000
>>> data = cyclic(32)
Expand Down

0 comments on commit 996c09e

Please sign in to comment.