Skip to content

Find the saved-return offset

The saved-return offset is the number of bytes from the start of the input to the saved return address. It is not automatically the size of the local buffer: compiler padding, saved registers, and frame layout can add bytes. Measure it on the exact binary and input path you will use.

Simplified x86-64 stack frameHigher addresses appear above lower addresses. The saved return address and saved frame pointer sit above local variables. The stack pointer is near the lower end of the frame.HIGHER ADDRESSESLOWERADDRESSESCaller data / argumentsSome arguments are passed in registersSaved return addressThe address used when this function returnsSaved frame pointer, if usedLocal buffer and variablesAn unchecked write may pass the buffer boundaryPadding / other localsRSP nearbyreturn slotTeaching sketch only. Exact frames vary by compiler, architecture, and function.

This diagram is a teaching sketch for a common x86-64 frame. A 32-bit target uses 4-byte words and EIP/ESP; optimization and compiler settings can also remove or rearrange frame data.

  1. Work with a local copy of the authorized challenge binary. Confirm the architecture with file ./chall and the protections with checksec --file=./chall.
  2. Identify the input operation and prompt in Ghidra or the source. A line read, menu, length limit, or NUL byte can change what reaches the buffer.
  3. Use a repeatable local run. Do not measure the offset against a different build or infer it from char buf[N].

The n value tells pwntools how many bytes must uniquely identify each pattern position. Use the same n when generating the pattern and looking it up. The pwntools default is 4, so set it explicitly for 64-bit patterns. See the pwntools cyclic reference.

For a 32-bit binary, use n=4; for a 64-bit binary, use n=8. The complete helper at the end of this page picks that value from the ELF and writes a newline-terminated pattern.bin file.

from pwn import *
context.binary = elf = ELF("./chall", checksec=False)
n = elf.bits // 8
pattern = cyclic(400, n=n)
open("pattern.bin", "wb").write(pattern + b"\n")

400 is only a starting length. If the input does not reach the saved return address, generate a longer pattern and check whether the program truncates input. The pattern is bytes, not printable text you should copy by hand.

Start GDB from the directory containing both chall and pattern.bin:

gdb -q ./chall
(gdb) set disassembly-flavor intel
(gdb) run < pattern.bin

The redirection gives the same bytes to standard input each run. It works for a simple one-read challenge. If the program needs several menu choices or inputs, send the pattern at the correct prompt with pwntools instead; do not count bytes from an input path the target never consumes.

The target should stop or exit after the pattern reaches the control data. If it exits normally, the pattern may be too short, the vulnerable path may not have run, or a mitigation may have stopped execution earlier. Recheck the input path, length handling, and crash location.

At the stop, inspect the current instruction and registers, then inspect nearby stack memory:

(gdb) info registers
(gdb) x/8gx $rsp # 64-bit: eight 8-byte words
(gdb) x/32bx $rsp # 64-bit: exact bytes
(gdb) x/16wx $esp # 32-bit: sixteen 4-byte words
(gdb) x/32bx $esp # 32-bit: exact bytes

With pwndbg, context refreshes the current stop and telescope $rsp 16 or telescope $esp 16 gives a pointer-aware stack view. Use the GDB x/... commands when you need the exact raw bytes.

Locate the bytes that occupied the saved return slot. On 32-bit x86, the pattern may appear in EIP. On x86-64, a non-canonical pattern address may fault while executing ret, leaving RIP at the ret instruction; in that case, read the saved value from the stack instead. Use the disassembly and stack frame to confirm which word is the return slot, rather than treating every value near RSP as the answer.

Copy the bytes in increasing memory-address order from x/4bx on 32-bit or x/8bx on 64-bit. For example, if GDB shows:

0xffffd1ac: 0x62 0x61 0x61 0x61

the observed bytes are 62 61 61 61. Give those bytes directly to cyclic_find; that avoids manually reversing a little-endian integer.

from pwn import *
cyclic_find(b"baaa", n=4) # 4
cyclic_find(b"baaaaaaa", n=8) # 8

These are small examples from matching patterns generated with the same n. For an integer copied from a register, pwntools packs it using the configured endianness:

cyclic_find(0x61616162, n=4) # 4 on a little-endian target

The helper script below can also accept the exact saved-slot bytes from GDB and print the result. If cyclic_find returns -1, the bytes were not found in that pattern; check that the input reached the slot and that the pattern alphabet and n match.

Replace the pattern with a marker at the measured position:

payload = b"A" * offset + b"B" * (elf.bits // 8)

Run that input under GDB. Confirm that the saved-return slot contains the expected B bytes. If the marker begins early or late, check for a prefix in the input, a delimiter consumed by the program, a partial overwrite, or the wrong stack word. Only use the measured value in an exploit after this check.

  • Using the buffer size: the distance includes everything between the buffer start and saved return slot, including padding and saved frame data.
  • Forgetting n=8: cyclic_find defaults to a 4-byte subsequence and truncates longer inputs unless n is supplied.
  • Reading the wrong crash value: the current RIP may still point to ret; inspect the saved return slot in memory.
  • Mixing patterns: generate and search with the same n, alphabet, and input bytes.
  • Sending a different input: line endings, NULs, menu prompts, maximum lengths, and repeated reads can change the offset measurement.
  • Reusing a value from another build: compiler options, architecture, and stack layout change the measured distance.

Save this as find_offset.py beside chall. It has three modes: generate a pattern file, calculate an offset from bytes copied from GDB, or make a marker file to verify the result.

#!/usr/bin/env python3
from pathlib import Path
from pwn import *
BINARY = "./chall"
PATTERN_LENGTH = 400
context.binary = elf = ELF(BINARY, checksec=False)
context.log_level = "info"
pattern_width = elf.bits // 8
if args.FIND:
raw = input("Saved-return bytes in memory order (hex): ").strip().replace("0x", "")
try:
observed = bytes.fromhex(raw)
except ValueError:
log.error("Enter hex bytes such as: 62 61 61 61")
if len(observed) < pattern_width:
log.error("Need at least %d bytes for this target", pattern_width)
offset = cyclic_find(observed[:pattern_width], n=pattern_width)
if offset < 0:
log.error("Bytes not found. Check the pattern, n, and saved-return slot.")
log.success("Saved-return offset: %d bytes", offset)
elif args.VERIFY:
if not args.OFFSET:
log.error("Supply the measured value, for example: VERIFY OFFSET=72")
offset = int(args.OFFSET, 0)
payload = b"A" * offset + b"B" * pattern_width
Path("verify.bin").write_bytes(payload + b"\n")
log.success("Wrote %d bytes to verify.bin; run it under GDB", len(payload))
else:
pattern = cyclic(PATTERN_LENGTH, n=pattern_width)
Path("pattern.bin").write_bytes(pattern + b"\n")
log.success("Wrote %d pattern bytes to pattern.bin (n=%d)", len(pattern), pattern_width)
log.info("Run: gdb -q %s", elf.path)
log.info("At the GDB prompt, run: run < pattern.bin")

Example sequence:

Terminal window
python3 find_offset.py
gdb -q ./chall
# At the GDB prompt: run < pattern.bin
python3 find_offset.py FIND
python3 find_offset.py VERIFY OFFSET=72

Replace 72 with the value the helper calculated. Copy raw bytes from the saved-return slot, not a nearby local variable. The offset is only useful for this exact binary and input path.

Use the measured value in a ret2win script or ROP chain. For an example that switches between a local process and an authorized challenge service, see the complete remote pwntools script.