Buffer Overflow for Beginners: Overwriting EIP part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
Writing a ret2win exploit using pwntools

Need an address win(). Two ways: via pwntools — python3 -c "from pwn import *; e = ELF('./vuln'); print(hex(e.symbols['win']))"or in GDB — p &winLet's say, 0x08049196. PIE is disabled, the address is fixed at each startup.

from pwn import *

elf = ELF('./vuln')
p = process('./vuln')

offset = 76
payload = b'A' * offset # мусор — забиваем буфер и saved EBP
payload += p32(elf.symbols['win']) # а вот тут — адрес win() в little-endian

p.sendline(payload)
p.interactive()

Parsing by lines. ELF('./vuln')parses the binary and extracts the symbol table. process('./vuln')runs the process locally; for a remote CTF server, replace with remote('ctf.example.com', 1337). b'A' * offset— garbage bytes filling the buffer and saved EBP. p32(elf.symbols['win'])— key string: packs a 4-byte address into little-endian format. On x86, the least significant byte comes first: 0x08049196stored in memory as \x96\x91\x04\x08Don't assemble the address manually—it's easier to mix up the byte order than you think; I've seen it happen dozens of times. p.sendline()sends a payload with a newline character (that's what it's waiting for) gets()). p.interactive()transfers control - if win()worked, a shell will appear in the terminal.

Launch: python3 exploit.py. Appeared $— stack overflow occurred. retuploaded the address win()in EIP, the processor executed system("/bin/sh"). Done.
Common Buffer Overflow Mistakes

Incorrect offset. Symptom: EIP is not equal to 0x42424242with test payload BBBBReason: Manual counting did not take into account the padding, or the pattern was too short. Solution: Repeat with cyclicbuffer length + 100 bytes, re-execute cyclic -l.

Forgotten ASLR. Symptom: exploit works in GDB, but crashes without the debugger. GDB disables ASLR by default for the debugged process; outside of it, the system randomizes the stack. For ret2win, this is usually not critical (it jumps to an address inside the binary, not the stack), but if it still doesn't work, check: cat /proc/sys/kernel/randomize_va_spacemust return 0.

Endianness. Symptom: EIP contains the address backwards. The address is written in big-endian instead of little-endian. Solution: Always use p32() / p64() из pwntools.

Segfault on movaps (x86-64 only). Address win()Correct, but the program crashes inside libc. The stack isn't 16-byte aligned. Solution: add the address to the payload. ret- gadget (single instruction) retfrom binary) before the address win()to shift RSP by 8 bytes. Searched through ROPgadget --binary vuln | grep ': ret'.
Stack protections: why ret2win doesn't work in production

The exploit worked only because we intentionally disabled all protections during compilation. Real binaries have at least four of them.

Stack Canary (D3-SFCV, Stack Frame Canary Validation). The compiler inserts a random value—a canary—between local variables and saved EBP. retThe function checks its integrity. If the value has changed, __stack_chk_failkills the process before the overwritten address gets into the EIP. In Linux, when triggered, the kernel generates stack smashing detected- this event is caught by the Sigma rule lnx_buffer_overflows.ymlfrom SigmaHQ.

NX / DEP (D3-PSEP, Process Segment Execution Prevention). The stack is marked as non-executable. Even if shellcode is written to the buffer and EIP is redirected, the processor will refuse to execute code from the page without the execute bit. Workaround: Return-Oriented Programming (ROP), where the payload is assembled from fragments of existing executable code.

ASLR (D3-SAOR, Segment Address Offset Randomization). The OS randomizes the base addresses of the stack, heap, and shared libraries. Address win()In the non-PIE binary, the address is fixed, but the addresses of libc functions change with each run. Workaround: leak the address via GOT/PLT or brute-force (32-bit ASLR has limited entropy—sometimes a few thousand attempts are enough).

PIE (Position Independent Executable). The binary itself is loaded at a random base address. win()is unknown in advance, a leak and calculation of the base are required.

Each defense is a separate topic. For the first pwn task, the rule is simple: if checksecshows Canary: No, NX: disabled, PIE: No — this is a classic ret2win, and the described chain will work in its entirety.
 
Top Bottom