Skip to main content
Be Bitwise
x86 AssemblyShellcode Development

Null Byte Elimination

A lot of exploitation vectors run through string functions — strcpy, strcat, gets, sprintf. They all treat \x00 as a terminator. One stray null byte in your shellcode and strcpy quits copying right there, leaving you with a truncated payload that won't run. Stripping null bytes is non-negotiable.

This lesson goes through every common source of null bytes in x86-64 assembly and gives you the null-free replacement for each.

Why Null Bytes Appear

Null bytes wander into machine code through several doors:

  1. Moving zero into a register: mov rax, 0 encodes as 48 c7 c0 00 00 00 00 — four nulls right there
  2. Small immediates in 64-bit registers: mov rax, 59 zero-extends with leading nulls
  3. Addresses with high zero bytes: x86-64 addresses like 0x0000000000401196 carry plenty of nulls
  4. 32-bit instructions with zero operands: mov edi, 0 encodes as bf 00 00 00 00

Time to walk through each source and its null-free replacement.

Technique 1: XOR Instead of MOV Zero

The biggest single source of null bytes is the humble register-zero:

nasm
; BEFORE: contains null bytes
mov rax, 0 ; 48 c7 c0 [00 00 00 00] -- 4 null bytes!
; AFTER: null-free
xor rax, rax ; 48 31 c0 -- no null bytes, same result

xor rax, rax zeroes rax because anything XORed with itself is zero. The encoding is 3 bytes, no nulls anywhere.

The trick works for every register:

nasm
xor rdi, rdi ; 48 31 ff -- zero rdi
xor rsi, rsi ; 48 31 f6 -- zero rsi
xor rdx, rdx ; 48 31 d2 -- zero rdx
xor rcx, rcx ; 48 31 c9 -- zero rcx

Technique 2: Partial Register Writes for Small Values

Drop a small value into a 64-bit register and the immediate gets zero-extended, dragging null bytes in with it:

nasm
; BEFORE: contains null bytes
mov rax, 59 ; 48 c7 c0 [3b 00 00 00] -- 3 null bytes!
; AFTER: null-free (two-step)
xor rax, rax ; 48 31 c0 -- zero rax first
mov al, 59 ; b0 3b -- write only the low byte

Zero rax first, then write only its low byte with mov al, and 59 lands in the register with no nulls in the encoding. mov al, N only encodes the low byte, and the upper bytes stay zero from the earlier XOR — no null bytes need to appear in the instruction stream.

Pick the sub-register that matches the value's range:

nasm
; For values 0-255: use 8-bit register
xor rax, rax
mov al, 59 ; b0 3b
; For values 256-65535: use 16-bit register (but beware: does not zero-extend)
xor rax, rax
mov ax, 1000 ; 66 b8 e8 03
; For values that fit in 32-bit: use 32-bit register (auto zero-extends)
mov eax, 0x12345678 ; b8 78 56 34 12 -- no nulls if value has no zero bytes
The XOR-then-MOV-AL Pattern

xor rax, rax; mov al, N is the most common pattern in null-free shellcode by a mile. It shows up ahead of nearly every syscall to set the syscall number, and ahead of every argument load that takes a small integer. Spot it instantly — it's the fingerprint of hand-crafted shellcode.

Technique 3: Build Constants Byte by Byte

For values that just won't avoid null bytes — addresses, awkward multi-byte constants — build them up a byte at a time, or push them and operate from the stack:

nasm
; Need the value 0x00000001 without null bytes in the encoding
xor rax, rax
inc rax ; rax = 1, encoded as 48 ff c0 (no nulls)
; Need the value 0x00000000004011a0
; This address has null bytes, so encode it differently:
xor rax, rax
mov eax, 0x4011a0 ; b8 a0 11 40 00 -- STILL has a null byte!
; Alternative: use arithmetic
mov eax, 0x4011a1 ; b8 a1 11 40 00 -- still has null...
; The 0x40 is fine, but the leading zeros in the 32-bit encoding are the problem
; Best approach: build the value most-significant byte first,
; shifting left to make room for each lower byte
xor rax, rax
mov al, 0x40
shl rax, 8 ; rax = 0x4000
mov al, 0x11
shl rax, 8 ; rax = 0x401100
mov al, 0xa0 ; rax = 0x4011a0, no null bytes in instruction stream

Load the top byte first: each shl rax, 8 opens up the low byte for the next mov al. Start from the low byte instead and you'd assemble 0xa01140 — the bytes in the wrong order.

Technique 4: Arithmetic Transformations

A bit of arithmetic can land you on the value you want without going through null-laden encodings:

nasm
; Need rax = 0, but want variety beyond xor
sub rax, rax ; 48 29 c0 -- subtraction self-zero
mul rdi ; rax = rax * rdi, if rdi = 0 then rax = 0
; (but rdi must be zero first)
; Need rdi = 1 without null bytes
xor rdi, rdi
inc rdi ; 48 ff c7 -- increment from 0 to 1
; Need rdi = -1 (0xffffffffffffffff) for some syscalls
xor rdi, rdi
dec rdi ; 48 ff cf -- decrement from 0 to -1
not rdi ; alternate: invert all bits of zero = all ones

Technique 5: LEA for Computed Values

lea can compute values whose encoding stays free of null bytes:

nasm
; Need rax = rbx + 4 (assuming rbx has a known value)
lea rax, [rbx + 4] ; no null bytes if the displacement is small and nonzero
; Need rax = 2 * rbx + 1
lea rax, [rbx * 2 + 1]

Side-by-Side Comparison

The diff viewer below sets the original (null-laden) shellcode against the null-free rewrite. Note the lea rsi, [rel msg] on the right: a short forward offset is itself mostly zero bytes, so truly null-free shellcode gets the data pointer from jmp-call-pop (a backward call) rather than a forward lea.

Null-Containing vs Null-Free Shellcode

x86-64
C Source
1mov eax, 1 ; b8 01 00 00 00
2mov edi, 1 ; bf 01 00 00 00
3movabs rsi, msg ; 48 be XX XX XX XX 00 00 00 00
4mov edx, 12 ; ba 0c 00 00 00
5syscall
Assembly
1xor eax, eax ; 31 c0
2inc eax ; ff c0
3xor edi, edi ; 31 ff
4inc edi ; ff c7
5lea rsi, [rel msg] ; 48 8d 35 .. (fwd offset still has 00s)
6mov dl, 12 ; b2 0c
7syscall

Hover over code lines or mapping badges to see correspondences

A full comparison of a simple write + exit shellcode:

Original (with null bytes):

nasm
; CONTAINS NULL BYTES -- will break with strcpy
mov rax, 1 ; 48 c7 c0 01 [00 00 00] -- sys_write
mov rdi, 1 ; 48 c7 c7 01 [00 00 00] -- stdout
mov rsi, msg ; 48 be [xx xx xx xx 00 00 00 00] -- absolute addr
mov rdx, 14 ; 48 c7 c2 0e [00 00 00] -- length
syscall
mov rax, 60 ; 48 c7 c0 3c [00 00 00] -- sys_exit
mov rdi, 0 ; 48 c7 c7 [00 00 00 00] -- exit code 0
syscall

Null-free version:

nasm
; NULL-FREE -- safe for string-based injection
jmp short .get_data
.write:
pop rsi ; 5e -- rsi = address of string (from call)
xor rax, rax ; 48 31 c0
mov al, 1 ; b0 01 -- sys_write
xor rdi, rdi ; 48 31 ff
mov dil, 1 ; 40 b7 01 -- stdout
xor rdx, rdx ; 48 31 d2
mov dl, 14 ; b2 0e -- length ("Hello, world!" + newline)
syscall ; 0f 05
xor rax, rax ; 48 31 c0
mov al, 60 ; b0 3c -- sys_exit
xor rdi, rdi ; 48 31 ff -- exit code 0
syscall ; 0f 05
.get_data:
call .write ; e8 xx xx ff ff -- relative call (no nulls)
db "Hello, world!", 10 ; string data (no null byte in the data itself)

Every mov reg, immediate that leaked null bytes is now xor reg, reg plus a partial-register write. The absolute address reference is gone too, replaced by the jmp-call-pop pattern.

Encoding as an Alternative

Rather than scrubbing nulls instruction by instruction, you can encode the whole shellcode with an XOR key:

nasm
; XOR encoder stub (prepended to the encoded shellcode)
jmp short .get_shellcode
.decode:
pop rsi ; rsi -> encoded shellcode bytes
xor rcx, rcx
mov cl, SHELLCODE_LEN ; length of encoded payload
.decode_loop:
xor byte [rsi], 0xAA ; XOR each byte with key 0xAA
inc rsi
loop .decode_loop
jmp short decoded_start ; jump to the now-decoded shellcode
.get_shellcode:
call .decode
decoded_start:
; ... encoded shellcode bytes here ...
; Each byte has been XORed with 0xAA
; The decoder reverses this, restoring the original bytes

Try it on the null-laden exit(0) from the original version: mov rax, 60, mov rdi, 0, syscall, in the unoptimised 48 c7 form. (NASM's default optimiser would shrink mov rax, 60 to the 5-byte b8 form, mov eax, 60, which still carries three zero bytes — the point holds either way.) The Analyze tab finds seven zero bytes in sixteen. Switch to XOR Encode and, with the default key 0x41, every one of them becomes 41 and the output is null-free. Now slide the key to 0x3c and watch the encoder complain. The shellcode already contains a 3c, and anything XORed with itself is zero, so a usable key is one that appears nowhere in the payload.

Shellcode Analyzer & Encoder

Byte Analysis (16 bytes, 7 null bytes):

48
c7
c0
3c
00
00
00
48
c7
c7
00
00
00
00
0f
05

Found 7 null byte(s) — shellcode will be truncated by strcpy/string functions

16

Total bytes

7

Null bytes

7

Unique bytes

Encoding vs Manual Elimination

XOR encoding wraps any shellcode — even one drowning in null bytes — in a decoder stub that restores the original bytes at runtime. The cost is size: 20–30 extra bytes of decoder. Manual elimination ships smaller code but takes real work. Most researchers use both — hand-elimination for small, bespoke shellcode, encoders for bigger or fiddlier payloads. Encoding is also the foundation for polymorphic shellcode, which rotates a fresh random key per build to slip signature-based detection.

Complete Null Byte Source Reference

SourceBytesReplacementBytes
mov rax, 048 c7 c0 00 00 00 00xor rax, rax48 31 c0
mov rax, 5948 c7 c0 3b 00 00 00xor rax, rax; mov al, 5948 31 c0 b0 3b
mov rdi, 048 c7 c7 00 00 00 00xor rdi, rdi48 31 ff
mov rdi, 148 c7 c7 01 00 00 00xor rdi, rdi; mov dil, 148 31 ff 40 b7 01
mov rsi, addr48 be xx xx xx xx 00 00 00 00jmp-call-popvaries
cmp rax, 048 83 f8 00test rax, rax48 85 c0

Security Context: Why Shellcode Looks Strange

Stare at a hex dump of shellcode for the first time and it looks otherworldly next to normal compiled code: XOR everywhere, partial register writes, jumps that seem pointless, simple tasks done the long way around. Now the reasoning's clear. Every weird pattern exists to dodge null bytes or stay position-independent. That's why shellcode is so recognisable in hex dumps — and why IDS engines hunt for those exact patterns as fingerprints of an exploitation attempt.

What is Next

Position-independent, null-free shellcode — you can write it now. The remaining question is whether it works. The next lesson covers the testing pipeline: a C harness, mmap with execute permissions, GDB sessions for the awkward bugs, and pulling out the final byte string ready for deployment.