Reference
https://github.com/Gallopsled/pwntools
http://docs.pwntools.com/en/latest/
https://github.com/Gallopsled/pwntools-tutorial#readme
Install
sudo apt update
sudo apt install python3 python3-pip python3-dev git libssl-dev libffi-dev build-essential
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade pwntools
Command Line Tools
http://docs.pwntools.com/en/latest/commandline.html
- checkseec
- cyclic
Usage
1. Tubes
Tubes are effectively I/O wrappers for most types of I/O you'll need to perform: Local processes, Remote TCP or UDP connections, Processes running on a remote server over SSH, Serial port I/O.
- Basic IO
- Receiving data:
recv(n)
,recline()
,recvuntil(delim)
,recvregex(pattern)
,recvrepeat(timeout)
,clean()
- Sending data:
send(data)
,send line(line)
- Manipulating integers:
pack(int)
,unpack()
- Receiving data:
- Processes
process()
process(['sh', '-c', 'echo $MYENV'], env={"MYENV": "MYVAL"})
- Interactive shells,
io.interactive()
- Networking
remote(host, port, fam, typ, ssl, sock, ssl_context, ssl_args, sni, ...)
listen(port, bindaddr, fam, typ, ...)
listen(8888).wait_for_connection()
- Secure shell
ssh(user, host, port, password, ...)
ssh(...).process(...)
- Serial ports,
serialtube('/dev/ttyUSB0', baudrate=115200)
2. Utility
- Packing and unpacking integers: The main
pack
andunpack
functions are aware of the global settings incontext
such asendian
,bits
, andsign
. You can also specify them explitily in the function call.pack(n, endian='big', bits, sign)
unpack(s)
,u16(s)
- File IO
write(filename, data)
read(filename, len)
- Hashing and encoding
- Base64:
b64e(s)
,b64d(s)
- Hashes:
md5sumhex(s.encode('utf-8'))
,md5filehex(filename)
,sha1sumhex(s.encode('utf-8'))
- URL encoding:
urlencode(s)
- Hex encoding:
enhex(s)
,unhex(s)
- Bit manipulation:
bits(c), bits(0b1010)
,unbits(list)
- Hex dumping:
print(hexdump(read('/dev/urandom', 32)))
- Base64:
- Pattern generation: a very handy way to find offsets without needing to do math.
cyclic(512)
cyclic_find(s or hex)
3. Bytes vs. Strings
x = b'abc'
flat(x)
: This function takes an arbitrary number of arbitrarily nested lists, tuples and dictionaries. It will then find every string and number inside those and flatten them out.
4. Context
The context
object is a global, thread-aware object which contains various settins used by pwntools
. Context settings are as follow:
-
arch: The target architecture. The first time this is set, it automatically sets the default
context.bits
andcontext.endian
to the most likely values. e.g.i386
(defualt),aarch64
,arm
,amd64
. -
bits: How many bits make up a word in the target binary, e.g. 32 or 64.
-
binary: Absorb settings from an ELF file.
-
endian:
little
(defualt),big
-
log_file: File to send all of the logging output into.
-
log_level: Verbosity of logs. Valid values are integers (lower is more verbose), and string values like
"debug"
,"info"
, and"error"
. -
sign: Sets the default signed-ness of integer packing / unpacking. Default is
"unsigned"
. -
terminal: Preferred terminal program to open new windows with. By default, uses
x-terminal-emulator
ortmux
. -
timeout: Default timeout for tube operations.
-
update:
context.update(arch='mips', bits=64, endian='big')
5. ELFs
https://pwntools.readthedocs.io/en/latest/elf.html
- Loading ELF files:
ELF(path)
- Using Symbols: EFL files have symbols, contained in a dict
{name: data}
.ELF.symbols
: lists all known symbols.ELF.got
: contains GOT entries.ELF.plt
: contains PLT entries.ELF.functions
: contains functions (requires DWARF symbols).elf.functions['list_all_jobs'].address
- Changing the base address: adjust for ASLR.
elf.address = 0x12340000
- Reading ELF files:
elf.read(x, 4)
elf.write(x, 4)
elf.pack(x), elf.unpack(x)
elf.disasm(addr, 12)