From c92fe20a304570e8bf4d8082a7154da8a0777c74 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Sun, 22 Dec 2024 12:01:25 +0100 Subject: [PATCH] Add POSIX optionflag Run test on other UNIX systems too if they don't use Linux specifics. Add a TODO optionflag too to mark platform restrictions that might be too strict and should be looked at. --- docs/source/asm.rst | 3 ++- docs/source/conf.py | 6 ++++++ docs/source/elf/corefile.rst | 2 +- docs/source/elf/elf.rst | 3 ++- docs/source/encoders.rst | 3 ++- docs/source/filesystem.rst | 3 ++- docs/source/gdb.rst | 3 ++- docs/source/intro.rst | 2 +- docs/source/libcdb.rst | 3 ++- docs/source/qemu.rst | 3 ++- docs/source/rop/rop.rst | 1 + docs/source/runner.rst | 3 ++- docs/source/shellcraft.rst | 3 ++- docs/source/shellcraft/amd64.rst | 1 + docs/source/shellcraft/i386.rst | 2 +- docs/source/tubes.rst | 3 ++- docs/source/tubes/buffer.rst | 3 ++- docs/source/tubes/processes.rst | 3 ++- docs/source/tubes/serial.rst | 3 ++- docs/source/tubes/sockets.rst | 3 ++- docs/source/tubes/ssh.rst | 3 ++- docs/source/ui.rst | 2 +- pwnlib/context/__init__.py | 6 +++--- pwnlib/fmtstr.py | 2 +- pwnlib/memleak.py | 2 +- pwnlib/util/iters.py | 2 +- pwnlib/util/misc.py | 4 ++-- pwnlib/util/packing.py | 2 +- pwnlib/util/sh_string.py | 2 +- 29 files changed, 52 insertions(+), 29 deletions(-) diff --git a/docs/source/asm.rst b/docs/source/asm.rst index 2dccfcc13..d87b14333 100644 --- a/docs/source/asm.rst +++ b/docs/source/asm.rst @@ -4,8 +4,9 @@ import subprocess from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.asm` --- Assembler functions ========================================= diff --git a/docs/source/conf.py b/docs/source/conf.py index d658bc574..37cfbd93a 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -402,6 +402,10 @@ class _DummyClass(object): pass # they are skipped on other platforms WINDOWS = doctest.register_optionflag('WINDOWS') LINUX = doctest.register_optionflag('LINUX') +POSIX = doctest.register_optionflag('POSIX') + +# doctest optionflag for tests that haven't been looked at yet +TODO = doctest.register_optionflag('TODO') class Py2OutputChecker(_DummyClass, doctest.OutputChecker): def check_output(self, want, got, optionflags): @@ -448,6 +452,8 @@ def filter_platform(example): return False if (optionflags & LINUX) == LINUX and sys.platform != 'linux': return False + if (optionflags & POSIX) == POSIX and os.name != 'posix': + return False return True test.examples[:] = [example for example in test.examples if filter_platform(example)] diff --git a/docs/source/elf/corefile.rst b/docs/source/elf/corefile.rst index bdbe046a1..85668ae79 100644 --- a/docs/source/elf/corefile.rst +++ b/docs/source/elf/corefile.rst @@ -19,7 +19,7 @@ os.environ.setdefault('SHELL', '/bin/sh') import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.elf.corefile` --- Core Files diff --git a/docs/source/elf/elf.rst b/docs/source/elf/elf.rst index be7e915fd..7501ca20c 100644 --- a/docs/source/elf/elf.rst +++ b/docs/source/elf/elf.rst @@ -5,8 +5,9 @@ from pwnlib.elf.maps import CAT_PROC_MAPS_EXIT import shutil + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.elf.elf` --- ELF Files =========================================================== diff --git a/docs/source/encoders.rst b/docs/source/encoders.rst index ad9806a15..8132023f4 100644 --- a/docs/source/encoders.rst +++ b/docs/source/encoders.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.encoders` --- Encoding Shellcode =============================================== diff --git a/docs/source/filesystem.rst b/docs/source/filesystem.rst index d80ddb40c..26cc62ce3 100644 --- a/docs/source/filesystem.rst +++ b/docs/source/filesystem.rst @@ -6,8 +6,9 @@ from pwnlib.tubes.ssh import ssh from pwnlib.filesystem import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.filesystem` --- Manipulating Files Locally and Over SSH ==================================================================== diff --git a/docs/source/gdb.rst b/docs/source/gdb.rst index 99b2263aa..5f4f30406 100644 --- a/docs/source/gdb.rst +++ b/docs/source/gdb.rst @@ -4,8 +4,9 @@ context.arch = 'amd64' context.terminal = [os.path.join(os.path.dirname(pwnlib.__file__), 'gdb_faketerminal.py')] + # TODO: Test on cygwin too import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.gdb` --- Working with GDB ====================================== diff --git a/docs/source/intro.rst b/docs/source/intro.rst index 7ead5ad88..3832dde32 100644 --- a/docs/source/intro.rst +++ b/docs/source/intro.rst @@ -3,7 +3,7 @@ from pwn import * import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] Getting Started ======================== diff --git a/docs/source/libcdb.rst b/docs/source/libcdb.rst index 8536fada5..a31dd7eb4 100644 --- a/docs/source/libcdb.rst +++ b/docs/source/libcdb.rst @@ -3,8 +3,9 @@ from pwn import * from pwnlib.libcdb import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.libcdb` --- Libc Database =========================================== diff --git a/docs/source/qemu.rst b/docs/source/qemu.rst index b8f28b23f..bf8884de2 100644 --- a/docs/source/qemu.rst +++ b/docs/source/qemu.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.qemu` --- QEMU Utilities diff --git a/docs/source/rop/rop.rst b/docs/source/rop/rop.rst index 944467630..553cac97d 100644 --- a/docs/source/rop/rop.rst +++ b/docs/source/rop/rop.rst @@ -19,6 +19,7 @@ context.clear() + # TODO: Remove global LINUX flag import doctest doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] diff --git a/docs/source/runner.rst b/docs/source/runner.rst index 008db6bab..f0c33d7e8 100644 --- a/docs/source/runner.rst +++ b/docs/source/runner.rst @@ -3,8 +3,9 @@ from pwnlib.runner import * from pwnlib.asm import asm + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.runner` --- Running Shellcode =========================================== diff --git a/docs/source/shellcraft.rst b/docs/source/shellcraft.rst index c00031bf3..5d9e37c24 100644 --- a/docs/source/shellcraft.rst +++ b/docs/source/shellcraft.rst @@ -2,8 +2,9 @@ from pwnlib import shellcraft + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.shellcraft` --- Shellcode generation ================================================= diff --git a/docs/source/shellcraft/amd64.rst b/docs/source/shellcraft/amd64.rst index c63cccc0c..27c65547c 100644 --- a/docs/source/shellcraft/amd64.rst +++ b/docs/source/shellcraft/amd64.rst @@ -3,6 +3,7 @@ from pwn import * context.clear(arch='amd64') + # TODO: POSIX/WINDOWS shellcode test import doctest doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] diff --git a/docs/source/shellcraft/i386.rst b/docs/source/shellcraft/i386.rst index 6cf72dcd4..3d72adc5c 100644 --- a/docs/source/shellcraft/i386.rst +++ b/docs/source/shellcraft/i386.rst @@ -4,7 +4,7 @@ context.clear(arch='i386') import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.shellcraft.i386` --- Shellcode for Intel 80386 =========================================================== diff --git a/docs/source/tubes.rst b/docs/source/tubes.rst index 291a18768..02f46ec0f 100644 --- a/docs/source/tubes.rst +++ b/docs/source/tubes.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes` --- Talking to the World! ============================================= diff --git a/docs/source/tubes/buffer.rst b/docs/source/tubes/buffer.rst index db115d546..30adaf480 100644 --- a/docs/source/tubes/buffer.rst +++ b/docs/source/tubes/buffer.rst @@ -2,8 +2,9 @@ from pwnlib.tubes.buffer import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes.buffer` --- buffer implementation for tubes ============================================================== diff --git a/docs/source/tubes/processes.rst b/docs/source/tubes/processes.rst index b46e0bef3..c4d891b35 100644 --- a/docs/source/tubes/processes.rst +++ b/docs/source/tubes/processes.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes.process` --- Processes =========================================================== diff --git a/docs/source/tubes/serial.rst b/docs/source/tubes/serial.rst index cc52f9d71..599ca35c4 100644 --- a/docs/source/tubes/serial.rst +++ b/docs/source/tubes/serial.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes.serialtube` --- Serial Ports =========================================================== diff --git a/docs/source/tubes/sockets.rst b/docs/source/tubes/sockets.rst index 616f344fc..9198794c0 100644 --- a/docs/source/tubes/sockets.rst +++ b/docs/source/tubes/sockets.rst @@ -3,8 +3,9 @@ from pwn import * from pwnlib.tubes.server import server + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes.sock` --- Sockets =========================================================== diff --git a/docs/source/tubes/ssh.rst b/docs/source/tubes/ssh.rst index 2de8bad79..21c78e2f1 100644 --- a/docs/source/tubes/ssh.rst +++ b/docs/source/tubes/ssh.rst @@ -2,8 +2,9 @@ from pwn import * + # TODO: Remove global POSIX flag import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.tubes.ssh` --- SSH =========================================================== diff --git a/docs/source/ui.rst b/docs/source/ui.rst index 62be83e00..173f39bf9 100644 --- a/docs/source/ui.rst +++ b/docs/source/ui.rst @@ -4,7 +4,7 @@ import io import doctest - doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX'] + doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX'] :mod:`pwnlib.ui` --- Functions for user interaction =================================================== diff --git a/pwnlib/context/__init__.py b/pwnlib/context/__init__.py index e4c24ea45..82f71c315 100644 --- a/pwnlib/context/__init__.py +++ b/pwnlib/context/__init__.py @@ -310,7 +310,7 @@ class ContextType(object): 32 .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> def nop(): ... print(enhex(pwnlib.asm.asm('nop'))) @@ -875,7 +875,7 @@ def binary(self, binary): Examples: .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> context.clear() >>> context.arch, context.bits @@ -1412,7 +1412,7 @@ def cache_dir(self): True >>> os.chmod(cache_dir, 0o000) >>> context.cache_dir = True - >>> context.cache_dir is None # doctest: +LINUX + >>> context.cache_dir is None # doctest: +POSIX +TODO True >>> os.chmod(cache_dir, 0o755) >>> cache_dir == context.cache_dir diff --git a/pwnlib/fmtstr.py b/pwnlib/fmtstr.py index 88fb39e2e..975efb250 100644 --- a/pwnlib/fmtstr.py +++ b/pwnlib/fmtstr.py @@ -34,7 +34,7 @@ We can automate the exploitation of the process like so: .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> program = pwnlib.data.elf.fmtstr.get('i386') >>> def exec_fmt(payload): diff --git a/pwnlib/memleak.py b/pwnlib/memleak.py index 35525eafa..49909c19b 100644 --- a/pwnlib/memleak.py +++ b/pwnlib/memleak.py @@ -40,7 +40,7 @@ def some_leaker(addr): Example: .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> import pwnlib >>> binsh = pwnlib.util.misc.read('/bin/sh') diff --git a/pwnlib/util/iters.py b/pwnlib/util/iters.py index 726eb7913..a044e3079 100644 --- a/pwnlib/util/iters.py +++ b/pwnlib/util/iters.py @@ -889,7 +889,7 @@ def mbruteforce(func, alphabet, length, method = 'upto', start = None, threads = Example: .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> mbruteforce(lambda x: x == 'hello', string.ascii_lowercase, length = 10) 'hello' diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index 75790c596..f2e111edd 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -123,7 +123,7 @@ def read(path, count=-1, skip=0): Examples: - >>> read('/proc/self/exe')[:4] # doctest: +LINUX + >>> read('/proc/self/exe')[:4] # doctest: +LINUX +TODO b'\x7fELF' """ path = os.path.expanduser(os.path.expandvars(path)) @@ -163,7 +163,7 @@ def which(name, all = False, path=None): Example: - >>> which('sh') # doctest: +ELLIPSIS +LINUX + >>> which('sh') # doctest: +ELLIPSIS +POSIX +TODO '.../bin/sh' """ # If name is a path, do not attempt to resolve it. diff --git a/pwnlib/util/packing.py b/pwnlib/util/packing.py index 3fca19457..03d93bd37 100644 --- a/pwnlib/util/packing.py +++ b/pwnlib/util/packing.py @@ -990,7 +990,7 @@ def dd(dst, src, count = 0, skip = 0, seek = 0, truncate = False): ['H', 'e', 'l', 'l', 'o', b'?'] .. doctest:: - :options: +LINUX + :options: +POSIX +TODO >>> _ = open('/tmp/foo', 'w').write('A' * 10) >>> dd(open('/tmp/foo'), open('/dev/zero'), skip = 3, count = 4).read() diff --git a/pwnlib/util/sh_string.py b/pwnlib/util/sh_string.py index b10931bd2..52699edd2 100644 --- a/pwnlib/util/sh_string.py +++ b/pwnlib/util/sh_string.py @@ -281,7 +281,7 @@ def test(original): r"""Tests the output provided by a shell interpreting a string .. doctest:: - :options: +LINUX + :options: +POSIX >>> test(b'foobar') >>> test(b'foo bar')