Skip to content
🎉 Welcome! Enjoy your reading, and I hope you will learn something new.

Pwntools

Templates

pwntools_template.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
from pwn import *
import sys

# ============================================================
# setup
# ============================================================
BINARY = './chall'
LIBC   = ''  # e.g. './libc.so.6'
HOST   = ''
PORT   = 0
USER   = ''  # user
PASS   = ''  # password

# Manual context
context.arch    = 'amd64'
context.os      = 'linux'
context.log_level = 'debug'
context.terminal  = ['tmux', 'splitw', '-h']

# Automatic context
elf  = context.binary = ELF(BINARY)
libc = ELF(LIBC) if LIBC else None

# ============================================================
# connection
# ============================================================
def conn():
    if args.REMOTE:
        return remote(HOST, PORT, USER)
    elif args.SSH:
        s = ssh(
            host=HOST,
            port=PORT,
            user=USER,
            password=PASS
        )
        return s.process(['env', '-', BINARY])
    elif args.GDB:
        return gdb.debug(['stdbuf', '-o0', BINARY], gdbscript=GDB_SCRIPT)
    else:
        return process(['stdbuf', '-o0', BINARY])

GDB_SCRIPT = '''
set pagination off
unset env COLUMNS
unset env LINES
b *main
c
'''

# ============================================================
# exploit
# ============================================================
def exploit():

    # ============================================================
    # utils
    # ============================================================
    def trecv(v, show=False):
        if isinstance(v, str):
            io = v.encode()
        else:
            io = v
        z = r.recvuntil(io)
        if show:
            print(z.decode())

    def prompt(v, line=True):
        if isinstance(v, str):
            io = v.encode()
        else:
            io = v

        if line:
            r.sendline(io)
        else:
            r.send(io)
    # ============================================================
    # ============================================================
    p = conn()

    # --- exploit goes steps goes here く ---

    p.interactive()

if __name__ == '__main__':
    exploit()

Cheat sheets

GitHub

Resources

Last updated on