ASLR, DEP, and NX
ASLR (Address Space Layout Randomisation), DEP (Data Execution Prevention), and NX (No-eXecute) are the three pillars of modern memory protection. Together, they turn exploitation from a paint-by-numbers exercise into a much harder one that needs info leaks, code-reuse tricks, or both.
This lesson digs into how each defence works at the assembly and hardware level, how they reinforce each other, and what falls apart when one's missing.
ASLR: Randomizing the Address Space
Every time a program runs, ASLR scrambles the base addresses of its memory regions. An attacker can't guess in advance where code, data, or the stack will land.
Regions randomized by ASLR:
| Region | Without ASLR | With ASLR |
|---|---|---|
| Executable code (PIE) | Fixed (e.g., 0x400000) | Random base |
| Shared libraries | Fixed | Random base per library |
| Stack | Fixed | Random start address |
| Heap | Fixed (just above data) | Random start address |
| mmap regions | Predictable | Randomized |
# Demonstrate ASLR: run the same program twice$ cat /proc/self/maps | head -35596a2f00000-5596a2f01000 r--p ... /usr/bin/cat5596a2f01000-5596a2f06000 r-xp ... /usr/bin/cat5596a2f06000-5596a2f09000 r--p ... /usr/bin/cat
$ cat /proc/self/maps | head -355c8e4700000-55c8e4701000 r--p ... /usr/bin/cat55c8e4701000-55c8e4706000 r-xp ... /usr/bin/cat55c8e4706000-55c8e4709000 r--p ... /usr/bin/cat# Different addresses each time!ASLR Requires PIE
ASLR can only shuffle regions whose code is position-independent. A non-PIE executable has to be loaded at the address the linker baked in, so even when ASLR is turned on system-wide, that binary's code and data sit at fixed addresses.
That's why PIE matters so much. Check the ASLR status on your system:
$ cat /proc/sys/kernel/randomize_va_space# 0 = ASLR disabled# 1 = Stack and library randomization# 2 = Full ASLR (stack, libraries, heap, mmap, executable with PIE)Process Memory Layout
ASLR Entropy
How much ASLR actually buys you depends on the entropy — how many possible addresses are in play:
These are defaults for a stock x86-64 kernel, and they're tunable: the mmap figure comes from CONFIG_ARCH_MMAP_RND_BITS (28 by default, adjustable up to 32 through /proc/sys/vm/mmap_rnd_bits), and the brk heap start is randomised within 1 GiB, page-aligned. x86-64's huge address space makes ASLR genuinely strong. On 32-bit systems, the cramped address space leaves enough headroom for an attacker to brute-force a guess in minutes.
DEP/NX: Preventing Code Execution in Data
DEP (Data Execution Prevention) is the Windows name for what Linux implements via the NX bit. Same concept either way: mark data pages non-executable so injected shellcode never gets to run.
How DEP/NX and ASLR Complement Each Other
Either defence on its own has obvious gaps. Stack them and the wall gets a lot harder to climb:
| Attack | DEP/NX Only | ASLR Only | Both |
|---|---|---|---|
| Inject shellcode on stack | Blocked (NX) | Works only if the stack address leaks | Blocked |
| Return to libc function | Works (code is exec) | Blocked (unknown address) | Blocked |
| ROP with known gadgets | Works (code is exec) | Blocked (unknown address) | Blocked |
| Leak address, then ROP | Works | Works (but address changes) | Requires leak first |
The combination forces an attacker to find an info leak (to defeat ASLR), then string code-reuse gadgets together (to defeat NX), and chain it all into a working exploit. The bar gets noticeably higher.
Viewing Protections on a Binary
To see what a binary's been compiled with, reach for checksec (ships with pwntools) or readelf:
# Using checksec$ checksec --file=./program Arch: amd64-64-little RELRO: Full RELRO Stack: Canary found NX: NX enabled PIE: PIE enabled
# Using readelf to check for NX$ readelf -l ./program | grep GNU_STACK GNU_STACK 0x000000 0x000000 0x000000 0x00000 0x00000 RW 0x10# ^^# RW = Read+Write (no E for Execute) -> NX is enabled# RWE would mean stack is executable -> NX is disabledExamining /proc/self/maps in Detail
A live view of memory protections on a running process:
; Reading /proc/self/maps programmatically:section .data maps_path db "/proc/self/maps", 0
section .bss maps_buf resb 4096
section .text_start: ; Open /proc/self/maps mov rax, 2 ; sys_open lea rdi, [maps_path] xor rsi, rsi ; O_RDONLY xor rdx, rdx syscall mov r12, rax
; Read and print mov rax, 0 mov rdi, r12 lea rsi, [maps_buf] mov rdx, 4096 syscall mov r13, rax
mov rax, 1 mov rdi, 1 lea rsi, [maps_buf] mov rdx, r13 syscall
; Close and exit mov rax, 3 mov rdi, r12 syscall mov rax, 60 xor rdi, rdi syscallPractical: Observing ASLR in Action
Build and run this program multiple times to see ASLR randomizing addresses:
section .data fmt_stack db "Stack variable at: " fmt_stack_len equ $ - fmt_stack newline db 10
section .bss hex_buf resb 20
section .text global _start
_start: ; Print label mov rax, 1 mov rdi, 1 lea rsi, [fmt_stack] mov rdx, fmt_stack_len syscall
; Convert RSP to hex string and print it mov rax, rsp ; the stack address lea rdi, [hex_buf] mov rcx, 16 ; 16 hex digits for 64-bit address.hex_loop: rol rax, 4 ; rotate next nibble into lowest position mov rdx, rax and rdx, 0x0F ; isolate lowest nibble add dl, '0' cmp dl, '9' jle .store add dl, 7 ; 'A' - '0' - 10 = 7.store: mov [rdi], dl inc rdi dec rcx jnz .hex_loop
mov rax, 1 mov rdi, 1 lea rsi, [hex_buf] mov rdx, 16 syscall
; Newline mov rax, 1 mov rdi, 1 lea rsi, [newline] mov rdx, 1 syscall
mov rax, 60 xor rdi, rdi syscallRun it a few times. With ASLR on, the stack address changes every time. Run it under setarch -R ./program (or setarch x86_64 -R ./program on older util-linux) and the same address comes back on every invocation: -R turns off randomisation for that one process. The system-wide switch, echo 0 > /proc/sys/kernel/randomize_va_space, needs root and disables ASLR for everything on the machine until you set it back to 2, so save it for a throwaway VM.
Security Perspective: Information Leaks Defeating ASLR
ASLR's whole game is keeping those randomised addresses out of the attacker's hands. Any bug that coughs up a memory address — an info leak — torpedoes the assumption.
Where leaks tend to come from:
- Format string bugs:
printf(user_input)happily reads stack values, including saved return addresses and pointers - Out-of-bounds reads: walking off the end of an array can expose neighbouring pointers
- Chatty error messages: some handlers helpfully include memory addresses in their output
- Timing side channels: cache-timing tricks can tell you whether a guessed address is mapped
Leak even one address from a region — say, a single libc pointer — and you can subtract its known offset to get the base. From there, every function in that region is just an addition away. One pointer kills ASLR for the whole region.
That's the case for defence in depth: canaries, NX, ASLR, and the rest each break a different link in the exploitation chain. Snapping one isn't enough as long as the others hold.
For deeper coverage, see ASLR and PIE.
What is Next
The closing lesson zooms out: what does it mean to write assembly through a security lens? And why does understanding the CPU matter so much, whether you're playing offence or defence?