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:
- Moving zero into a register:
mov rax, 0encodes as48 c7 c0 00 00 00 00— four nulls right there - Small immediates in 64-bit registers:
mov rax, 59zero-extends with leading nulls - Addresses with high zero bytes: x86-64 addresses like
0x0000000000401196carry plenty of nulls - 32-bit instructions with zero operands:
mov edi, 0encodes asbf 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:
; BEFORE: contains null bytesmov rax, 0 ; 48 c7 c0 [00 00 00 00] -- 4 null bytes!
; AFTER: null-freexor rax, rax ; 48 31 c0 -- no null bytes, same resultxor 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:
xor rdi, rdi ; 48 31 ff -- zero rdixor rsi, rsi ; 48 31 f6 -- zero rsixor rdx, rdx ; 48 31 d2 -- zero rdxxor rcx, rcx ; 48 31 c9 -- zero rcxTechnique 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:
; BEFORE: contains null bytesmov 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 firstmov al, 59 ; b0 3b -- write only the low byteZero 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:
; For values 0-255: use 8-bit registerxor rax, raxmov al, 59 ; b0 3b
; For values 256-65535: use 16-bit register (but beware: does not zero-extend)xor rax, raxmov 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 bytesxor 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:
; Need the value 0x00000001 without null bytes in the encodingxor rax, raxinc 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, raxmov eax, 0x4011a0 ; b8 a0 11 40 00 -- STILL has a null byte!
; Alternative: use arithmeticmov 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 bytexor rax, raxmov al, 0x40shl rax, 8 ; rax = 0x4000mov al, 0x11shl rax, 8 ; rax = 0x401100mov al, 0xa0 ; rax = 0x4011a0, no null bytes in instruction streamLoad 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:
; Need rax = 0, but want variety beyond xorsub rax, rax ; 48 29 c0 -- subtraction self-zeromul rdi ; rax = rax * rdi, if rdi = 0 then rax = 0 ; (but rdi must be zero first)
; Need rdi = 1 without null bytesxor rdi, rdiinc rdi ; 48 ff c7 -- increment from 0 to 1
; Need rdi = -1 (0xffffffffffffffff) for some syscallsxor rdi, rdidec rdi ; 48 ff cf -- decrement from 0 to -1not rdi ; alternate: invert all bits of zero = all onesTechnique 5: LEA for Computed Values
lea can compute values whose encoding stays free of null bytes:
; 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 + 1lea 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-64Hover over code lines or mapping badges to see correspondences
A full comparison of a simple write + exit shellcode:
Original (with null bytes):
; CONTAINS NULL BYTES -- will break with strcpymov rax, 1 ; 48 c7 c0 01 [00 00 00] -- sys_writemov rdi, 1 ; 48 c7 c7 01 [00 00 00] -- stdoutmov rsi, msg ; 48 be [xx xx xx xx 00 00 00 00] -- absolute addrmov rdx, 14 ; 48 c7 c2 0e [00 00 00] -- lengthsyscallmov rax, 60 ; 48 c7 c0 3c [00 00 00] -- sys_exitmov rdi, 0 ; 48 c7 c7 [00 00 00 00] -- exit code 0syscallNull-free version:
; 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:
; 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 .decodedecoded_start: ; ... encoded shellcode bytes here ... ; Each byte has been XORed with 0xAA ; The decoder reverses this, restoring the original bytesTry 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):
Found 7 null byte(s) — shellcode will be truncated by strcpy/string functions
16
Total bytes
7
Null bytes
7
Unique bytes
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
| Source | Bytes | Replacement | Bytes |
|---|---|---|---|
mov rax, 0 | 48 c7 c0 00 00 00 00 | xor rax, rax | 48 31 c0 |
mov rax, 59 | 48 c7 c0 3b 00 00 00 | xor rax, rax; mov al, 59 | 48 31 c0 b0 3b |
mov rdi, 0 | 48 c7 c7 00 00 00 00 | xor rdi, rdi | 48 31 ff |
mov rdi, 1 | 48 c7 c7 01 00 00 00 | xor rdi, rdi; mov dil, 1 | 48 31 ff 40 b7 01 |
mov rsi, addr | 48 be xx xx xx xx 00 00 00 00 | jmp-call-pop | varies |
cmp rax, 0 | 48 83 f8 00 | test rax, rax | 48 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.