Pwntools

4.15.0 · active · verified Thu Apr 16

Pwntools is a CTF (Capture The Flag) framework and exploit development library for Python. It provides a comprehensive set of tools for writing exploits, interacting with binaries and remote services, performing assembly/disassembly, ROP chain generation, and much more. The current version is 4.15.0, and it maintains an active release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a remote service, send data (e.g., a simple buffer overflow payload using `p64` for packing), and receive responses. It highlights setting `context.arch` and `context.os`, which are crucial for correct behavior in many pwntools operations. Replace `HOST` and `PORT` with your target challenge details.

from pwn import *

# Configure global context for architecture and OS (important for assembly/disassembly, packing)
context.arch = 'amd64' # Example: ARM, i386, amd64
context.os = 'linux'  # Example: windows, freebsd
context.log_level = 'info' # Debug, info, warn, error, critical

# --- Example: Interact with a remote service ---
# Replace with the actual challenge host and port
HOST = 'challenge.example.com'
PORT = 1337

try:
    log.info(f"Connecting to {HOST}:{PORT}...")
    # Establish a connection to the remote service
    io = remote(HOST, PORT)
    log.success("Connected!")

    # Receive initial data (e.g., a banner)
    banner = io.recvline()
    log.info(f"Received banner: {banner.decode(errors='ignore').strip()}")

    # Send some input (e.g., a simple payload for a buffer overflow)
    # pwntools handles bytes automatically for send/recv
    payload = b'A' * 72 + p64(0xdeadbeef) # 72 bytes of 'A', then an 8-byte address
    io.sendline(payload)
    log.info(f"Sent payload: {payload!r}")

    # Receive the response after sending data
    response = io.recvall()
    log.info(f"Received full response: {response.decode(errors='ignore').strip()}")

    io.close()
    log.success("Connection closed.")

except PwnlibException as e:
    log.error(f"Pwntools error: {e}")
except Exception as e:
    log.error(f"General error: {e}")

view raw JSON →