Skip to content

Commit

Permalink
big refactor to move away from the stateless design of pwnshop. Chall…
Browse files Browse the repository at this point in the history
…enge instances now maintain a workdir to avoid redoing work
  • Loading branch information
zardus committed Nov 3, 2024
1 parent ef76e0f commit 976fa0b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 195 deletions.
4 changes: 2 additions & 2 deletions example_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class ShellExample(ShellBase):
shellcode_size = 0x1000
allocation_size = 0x1000

def verify(self, binary=None, **kwargs):
def verify(self, **kwargs):
"""
Read 0x1000 bytes onto the stack (address varies every time that it is run)
"""
with self.run_challenge(binary, **kwargs) as process:
with self.run_challenge(**kwargs) as process:
shellcode = pwn.asm(
pwn.shellcraft.open("/flag") + pwn.shellcraft.sendfile(1, 3, 0, 1024) + pwn.shellcraft.exit(0)
)
Expand Down
40 changes: 12 additions & 28 deletions pwnshop/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import functools
import argparse
import pwnshop
import inspect
import signal
import random
import yaml
Expand Down Expand Up @@ -72,8 +71,6 @@ def handle_build(args, challenges):
with open(f"{args.out.name.replace('.exe', '.pdb')}", 'wb') as f:
f.write(pdb)

class TimeoutError(Exception):
pass
def raise_timeout(signum, stack):
raise TimeoutError("ACTION TIMED OUT")

Expand Down Expand Up @@ -113,7 +110,7 @@ def verify_many(args, challenges):
failures.append(challenge)
print(f"FAILED: {name}")

return False if failures else True
return not failures

@with_challenges
def handle_verify(args, challenges):
Expand All @@ -138,41 +135,28 @@ def handle_apply(args):

challenge = pwnshop.ALL_CHALLENGES[c['challenge']](
walkthrough=walkthrough,
seed=seed + v
seed=seed + v,
work_dir=out_dir,
basename=binary_name,
)

src_path = f"{out_dir}/{binary_name}.c"
if args.no_render:
src = open(src_path).read()
challenge.source = open(challenge.src_path).read()
else:
src = challenge.render()
if keep_source:
open(src_path, "w").write(src)
challenge.render()


bin_path = f"{out_dir}/{binary_name}"
if args.no_build:
binary = open(bin_path, "rb").read()
libs = [ ]
challenge.binary = open(challenge.bin_path, "rb").read()
else:
binary, libs, _ = challenge.build(source=src)

open(bin_path, "wb").write(binary)
os.chmod(bin_path, 0o755)

libs_path = f"{out_dir}/lib"
if libs:
os.makedirs(libs_path, exist_ok=True)
for lib_name, lib_bytes in libs:
lib_path = libs_path + f'/{lib_name}'
with open(lib_path, 'wb+') as f:
f.write(lib_bytes)
os.chmod(lib_path, 0o755)
challenge.build()

if not args.no_verify:
challenge.verify(binary=binary)
challenge.verify()
print("... verification passed")

if not keep_source:
os.unlink(challenge.src_path)

#if pdb:
# with open(f"{args.out.name.replace('.exe', '.pdb')}", 'wb') as f:
# f.write(pdb)
Expand Down
Loading

0 comments on commit 976fa0b

Please sign in to comment.