Skip to main content
Be Bitwise
x86 AssemblyBuffer Overflows

NOP Sleds and Payload Delivery

Last lesson handed you control of rip via the return address. So — where do you aim it? The 90s-and-early-2000s answer was to drop executable shellcode into the buffer itself and prepend a NOP sled to widen the landing zone. Modern defences have mostly retired the technique, but the mental model underneath it props up everything that follows.

For the defensive perspective, see DEP and NX.

The Classic Shellcode Injection Model

The original exploitation model works like this:

  1. Place machine code (shellcode) into the overflowed buffer
  2. Overwrite the return address to point back into the buffer
  3. When ret executes, rip jumps to the shellcode
  4. The shellcode runs with the privileges of the vulnerable process

The payload layout in the buffer looks like this:

Exploit Builder

nop-sled

Place NOP sled before shellcode so imprecise jumps still land in your code.

NOP
Shellcode
Address
Padding
Offset 0x00Buffer (64B)RET @ 0x48
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
??
??
??
??
Stack Layout
Buffer [64B]
SFP
RET [4B]
Target: 0x7fffffffe140

What Is a NOP Sled?

A NOP sled is a long stretch of NOP (No Operation) instructions in front of the shellcode. On x86-64 the NOP is a single byte:

nasm
nop ; opcode 0x90, does nothing, advances rip by 1

A sled of 200 NOPs:

c
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
... (200 bytes total)

Drop rip anywhere inside the sled and the CPU happily plows through NOPs one at a time, sliding forward until it hits the shellcode. Hence the name — execution literally slides down to the payload.

Why NOP Sleds Increase Reliability

The attacker's headache is imprecise targeting. To pull off the jump, you have to guess the exact address of your shellcode in memory, and several things conspire against you:

  • Stack addresses shift between runs (even without ASLR, environment variables can nudge the stack)
  • Different systems lay the stack out differently
  • The offset within the buffer is sometimes fuzzy

Without a sled you need to hit the first byte of your shellcode dead-on. With a 200-byte NOP run, you can miss by up to 200 bytes and still land it. The sled trades a knife-edge guess for a much more forgiving range:

c
Without NOP sled: must hit 1 exact byte (probability: 1/N)
With 200B sled: can hit any of 200 bytes (probability: 200/N)

Here N is the range the stack can shift by. Start with the 200-byte sled and a 512-byte shift, run 1000 attempts and check the observed rate against 200/512. Then grow the sled until the odds hit 100%, and flip ASLR on to watch even a 64 KiB sled shrink to roughly 1 in 262,000.

NOP sled landing odds

The return address is a fixed guess aimed at the middle of the sled. The real buffer moves by a random offset each run, so the guess lands somewhere in a window as wide as that offset range.

Without ASLR the stack still shifts a little, for example because gdb and a shell pass different environment variables. The range here is illustrative: a few hundred bytes is typical.

Chance of landing in the sled, exact

min(1, 200 / 512) = 39.1% (about 1 in 3)

Counting a dead-on hit on the first shellcode byte as well: 39.3%.

One attempt (strip not to scale, addresses grow to the right)

Try an attempt to see where execution lands.

Even a perfect landing only works if the stack is executable. With NX/DEP the first NOP fetched from a stack page faults, whatever the odds above. See DEP and NX.

Memory Layout of a Classic Exploit

Process Memory Layout

0xFFFF...0x0000...StackgrowsFree SpaceHeapgrowsBSSDataText

A concrete example. Our buffer holds 64 bytes and the offset to the return address is 72. A classic payload could be laid out like this:

python
# Classic buffer overflow payload structure
nop_sled = b'\x90' * 30 # 30 bytes of NOPs
shellcode = (b'\x48\x31\xc0' # ... (actual shellcode, say 25 bytes)
b'\x48\x31\xff'
b'...') # (simplified for illustration)
padding = b'A' * (72 - 30 - 25) # fill remaining space to reach ret addr
ret_addr = b'\x40\xe1\xff\xff\xff\x7f\x00\x00' # address inside the NOP sled

The memory layout after the overflow:

Exploit Builder

nop-sled

Place NOP sled before shellcode so imprecise jumps still land in your code.

NOP
Shellcode
Address
Padding
Offset 0x00Buffer (64B)RET @ 0x48
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
??
??
??
??
Stack Layout
Buffer [64B]
SFP
RET [4B]
Target: 0x7fffffffe140

When ret runs, rip jumps to 0x7fffffffe140 (a spot inside the sled), slides through the NOPs, and starts executing the shellcode.

Alternative Payload Placement Strategies

The buffer is far from the only landing spot for shellcode. Attackers have parked it in several places over the years:

In an environment variable:

bash
export SHELLCODE=$(python3 -c "print('\x90'*1000 + '<shellcode>')")
./vulnerable $(python3 -c "print('A'*72 + '<addr_of_env_var>')")

Environment variables sit on the stack at a predictable offset above main's frame. Stuffing a giant NOP sled into one widens the target window even further.

In a separate heap allocation:

If the program first reads input into a malloc'd buffer, the shellcode can live on the heap. The attacker then uses the stack overflow purely to redirect execution into that heap allocation.

In the input itself beyond the return address:

c
| Padding (72B) | RET addr (8B) | NOP sled | Shellcode |

Drop the shellcode after the return address (at rbp+16 and onward), then point the return address at the stack region just beyond the overwrite. The buffer's own size stops being the limiting factor.

NOP Sleds Are Mostly Obsolete

On modern systems, two defences combine to put NOP sleds out of business. First, DEP/NX (Data Execution Prevention / No-eXecute) marks stack and heap pages non-executable. If rip lands on a page without execute permission, the CPU faults; shellcode on the stack just doesn't run. Second, ASLR (Address Space Layout Randomisation) shuffles stack addresses every run, making "where did my NOP sled land" a guessing game with terrible odds. Together they ended the era of classic shellcode injection — but the underlying concepts still drive modern techniques like ROP.

Why DEP/NX Kills This Technique

Modern operating systems hand out page permissions per region:

c
Memory Region Permissions Classic Exploit Impact
.text (code) r-x Executable, but read-only (can't inject)
.data rw- Writable, but NOT executable
Stack rw- Writable, but NOT executable
Heap rw- Writable, but NOT executable

Only .text — the program's actual code — gets the x (execute) bit. Try to fetch instructions from a stack page lacking x and the hardware faults; the OS reaps the process.

At assembly level the NX bit on the page-table entry enforces this. No instruction-level trick gets around it. It's a hardware check on every fetch.

nasm
; When rip points to a stack address with NX set:
; The CPU checks the page table entry for the address in rip
; If the NX bit is set (no execute), the CPU raises #PF (page fault)
; The OS delivers SIGSEGV to the process
; The shellcode never executes a single instruction

The Transition to Code Reuse

DEP/NX forced an evolution. If injected code can't run, the next move is to reuse code that's already executable. Out of that came:

  • ret2libc - redirect execution to a libc function like system()
  • Return-Oriented Programming (ROP) - chain small snippets of existing code (gadgets)
  • JIT spraying - abuse Just-In-Time compilers that produce executable memory

Module 11 unpacks all three. They share the same buffer-overflow primitive you've already met — the only thing that changes is which address ends up in the return-address slot.

Multi-byte NOP Equivalents

0x90 is the canonical NOP, and it's the one a sled is built from. A single byte means the CPU can land on any byte of the sled and still decode a clean NOP before sliding on. Pick an encoding wider than one byte and that property breaks — a jump into the middle of it decodes as something else entirely:

nasm
nop ; 90 (1 byte -- safe in a sled)
xchg ax, ax ; 66 90 (2 bytes; `xchg eax,eax` assembles to 90)
nop word [rax] ; 66 0f 1f 00 (4-byte long NOP)
nop dword [rax] ; 0f 1f 00 (3-byte official long NOP)
nop dword [rax+0] ; 0f 1f 40 00 (4-byte long NOP)

Mind the 64-bit encodings. xchg eax, eax (operand eax) is special-cased to the one-byte 0x90, but the general two-register xchg and tricks like lea eax, [eax+0] write to a 32-bit register, which zero-extends and clears the top half of rax — a real side effect, not a no-op. The nop forms (0f 1f ...) are true NOPs, but only when execution enters at their first byte. For a sled that tolerates landing anywhere, single-byte fillers are the safe choice: 0x90, or cancelling pairs like inc eax/dec eax and push/pop.

Varied fillers do help against simple IDS signatures that flag long runs of 0x90, so there's a real tension between dodging pattern matches and keeping the land-anywhere property.

Security Context: IDS and NOP Sled Detection

Intrusion Detection Systems often look for long runs of 0x90 in traffic or input data as a NOP-sled tell. That's exactly why attackers reach for polymorphic sleds built from varied NOP equivalents, and why modern IDS engines have moved past raw pattern matching toward heuristics. Knowing how sleds work is the same skill you need to write or evaluate IDS signatures.

Try It Yourself

Play with the classic payload layout in the sandbox. DEP will block actual shellcode execution on a modern system, but you can still watch the control-flow hijack happen by pointing the return address at a known function.

Practise on your own machine

You need Linux on x86-64: a virtual machine, WSL 2 on Windows, or a container. Then install the tools with sudo apt install nasm gcc gdb binutils.

What is Next

Next up, you'll wire everything together in a hands-on exercise: dissect a vulnerable program, work out the offset, and build a payload that lands the stack-smashing primitive. The point of the exercise is the analytical process that security researchers actually use on real assessments.