ctf::templates

scripting python ยท pwntools solve templates
Python Templates Helpers
01Python Templates
Remote harness
from pwn import *

def start():
    return remote('host', 31337)

io = start()
io.recvuntil(b'> ')
io.sendline(b'test')
print(io.recvall())
HTTP loop
import requests

s = requests.Session()
r = s.post(URL, data={'u':'a','p':'b'})
print(r.status_code)
print(r.text[:300])
Bruteforce skeleton
for cand in candidates:
    out = attempt(cand)
    if b'flag{' in out.lower():
        print('hit', cand)
        break
02Common Helpers
XOR / hex / b64 helpers
import base64, binascii

hex_bytes = binascii.unhexlify('7069636f')
b64_bytes = base64.b64decode('cGljbw==')

def xor(data, key):
    return bytes(b ^ key[i % len(key)] for i,b in enumerate(data))
Pwntools packing
from pwn import *
p64(0xdeadbeef)
u64(data.ljust(8, b'\x00'))
cyclic(200)
cyclic_find(0x6161616c)
Logging template
import logging
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
logging.info('starting')
Checklist before running
CheckReason
bytes vs strmost CTF bugs come from mixing them
timeoutsremote services hang; set them
save raw outputlog every attempt worth keeping
parameterize host/portavoid hardcoding challenge changes