Skip to content

Commit

Permalink
added a PythonChallenge
Browse files Browse the repository at this point in the history
  • Loading branch information
zardus committed Nov 28, 2024
1 parent 8b2cd63 commit 0ba855d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions example_deploy/pwnshop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ challenges:
- id: shell-1604
challenge: Shell1604
variants: 1
- id: python-pass
challenge: PythonPass
binary_name: pp
variants: 2
17 changes: 16 additions & 1 deletion example_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

class ShellBase(pwnshop.Challenge, register=False): # don't register this as an actual challenge
TEMPLATE_PATH = "example_shell.c"
TEMPLATE_PATH = "template_shell.c"
EXEC_STACK = True
CANARY = True
LINK_LIBRARIES = ["capstone"]
Expand Down Expand Up @@ -80,3 +80,18 @@ class Shell1604InVitu(Shell1604):
BUILD_IMAGE = "ubuntu:16.04"
VERIFY_IMAGE = "ubuntu:16.04"
PIN_LIBRARIES = False

class PythonPass(pwnshop.PythonChallenge):
"""
Simple templated python example.
"""
TEMPLATE_PATH = "template_pypass.py"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.password = self.random_word(8)

def verify(self, **kwargs):
with self.run_challenge() as p:
p.sendline(self.password)
assert self.flag in p.readall()
6 changes: 6 additions & 0 deletions example_module/template_pypass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/opt/pwn.college/python

if input("Password? ").strip() == "{{challenge.password}}":
print(open("/flag").read())
else:
print("Nope")
File renamed without changes.
2 changes: 1 addition & 1 deletion pwnshop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
ALL_MODULES = { }
MODULE_LEVELS = { }

from .challenge import Challenge, KernelChallenge, WindowsChallenge, ChallengeGroup
from .challenge import Challenge, PythonChallenge, KernelChallenge, WindowsChallenge, ChallengeGroup
from .register import register_challenge, register_challenges
from .util import did_segfault, did_timeout, did_abort, did_sigsys, retry
2 changes: 1 addition & 1 deletion pwnshop/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def handle_apply(args):
shutil.copy2(challenge.src_path, os.path.join(out_dir, os.path.basename(challenge.src_path)))
print(f"... copying binary {os.path.basename(challenge.bin_path)} to {out_dir}")
shutil.copy2(challenge.bin_path, os.path.join(out_dir, os.path.basename(challenge.bin_path)))
if os.path.exists(challenge.lib_path):
if hasattr(challenge, "lib_path") and os.path.exists(challenge.lib_path):
print(f"... copying libraries {os.path.basename(challenge.lib_path)} to {out_dir}")
shutil.copytree(challenge.lib_path, os.path.join(out_dir, os.path.basename(challenge.lib_path)), dirs_exist_ok=True)

Expand Down
18 changes: 18 additions & 0 deletions pwnshop/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def verify(self, **kwargs):
def run_challenge(self, *, argv=None, close_stdin=False, flag_symlink=None, **kwargs):
self.ensure_containers()

assert argv

if flag_symlink:
self.run_sh(f"ln -s /flag {flag_symlink}")

Expand Down Expand Up @@ -278,6 +280,22 @@ def local_context(self):
if not e.startswith("_") and e == e.upper()
}

class PythonChallenge(TemplatedChallenge, register=False):
@property
def bin_path(self):
return self.src_path

def build(self):
os.chmod(self.src_path, 0o4755)

@contextlib.contextmanager
def run_challenge(self, argv=None, **kwargs):
if not self.source:
self.render()

argv = argv or ["python3", self.src_path]
with super().run_challenge(argv=argv, **kwargs) as y:
yield y

class Challenge(TemplatedChallenge, register=False):
COMPILER = "gcc"
Expand Down
5 changes: 3 additions & 2 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cat /tmp/out | grep "SUCCEEDED: ShellOptimized"
cat /tmp/out | grep "FAILED: ShellBadVerifier"
cat /tmp/out | grep "SUCCEEDED: Shell1604"
cat /tmp/out | grep "SUCCEEDED: Shell1604InVitu"
cat /tmp/out | grep "SUCCEEDED: PythonPass"

pwnshop apply ../example_deploy/pwnshop.yml
SOURCES=( ../example_deploy/*/*/*.c )
Expand All @@ -25,7 +26,7 @@ FILES=( ../example_deploy/*/*/* )
LIBS=( ../example_deploy/*/*/lib/* )
[ "${#SOURCES[@]}" -eq 1 ] || exit 1
[ "${#BINS[@]}" -eq 6 ] || exit 1
[ "${#FILES[@]}" -eq 8 ] || exit 1
[ "${#FILES[@]}" -eq 10 ] || exit 1
[ "${#LIBS[@]}" -eq 3 ] || exit 1

rm -rf ../example_deploy/*/*/*
Expand All @@ -36,7 +37,7 @@ FILES=( ../example_deploy/*/*/* )
LIBS=( ../example_deploy/*/*/lib/* )
[ "${#SOURCES[@]}" -eq 1 ] || exit 1
[ "${#BINS[@]}" -eq 6 ] || exit 1
[ "${#FILES[@]}" -eq 8 ] || exit 1
[ "${#FILES[@]}" -eq 10 ] || exit 1
[ "${#LIBS[@]}" -eq 3 ] || exit 1

cd /
Expand Down

0 comments on commit 0ba855d

Please sign in to comment.