Pwntools bytes, ELF, and cyclic patterns
Context and architecture
Section titled “Context and architecture”from pwn import *
context.arch = "amd64"context.os = "linux"context.endian = "little"context.binary = ELF("./chall", checksec=False)context.binary can populate architecture and endianness from the ELF. Set values explicitly when the target is unusual or the script handles multiple binaries.
Pack and unpack integers
Section titled “Pack and unpack integers”from pwn import *
p64(0x401234) # little-endian 8-byte representationp32(0x080491f6) # little-endian 4-byte representationu64(b"\x34\x12\x40\x00\x00\x00\x00\x00")p8, p16, p32, and p64 pack values; u8, u16, u32, and u64 unpack them. flat() packs nested values and fills gaps with a cyclic pattern or a selected filler.
payload = flat( b"A" * offset, 0x401234, word_size=64, endianness="little",)Prefer flat({offset: address}) when a measured offset makes the intent clearer. Inspect payload with print(hexdump(payload)) before sending it.
ELF symbols, GOT, and PLT
Section titled “ELF symbols, GOT, and PLT”elf = ELF("./chall", checksec=False)print(elf.checksec())print(hex(elf.symbols["main"]))print(hex(elf.got["puts"]))print(hex(elf.plt["puts"]))Symbol, GOT, and PLT entries can be absent or named differently in stripped or unusual binaries. They are static addresses for a non-PIE executable; with PIE, add the loaded base or use pwntools’ relocated ELF after setting the observed base:
elf.address = runtime_baseDo not set a guessed base. Derive it from a runtime mapping or a valid disclosure and verify it belongs to this exact binary.
Cyclic patterns
Section titled “Cyclic patterns”from pwn import *
pattern = cyclic(300, n=8)offset = cyclic_find(0x6161616161616167, n=8)print(offset)The integer above is only an example. Replace it with the value observed at the overwritten slot. If you copied bytes from memory instead, pass the bytes directly. Use the same n in both calls; the default differs from common 64-bit workflows. Pattern alphabet and length also bound the result.
Bytes are not text
Section titled “Bytes are not text”Addresses can contain zero bytes and arbitrary byte values. Use byte literals, p64, and flat; avoid encoding a hex string into ASCII and accidentally sending the characters 4, 0, 1, 2 instead of the address bytes. recv() returns bytes; decode only after confirming the data is text.