Skip to main content
Shellcode Concepts, Syscalls & Null-Free Encoding
Lesson 3 of 7

Shellcode Concepts, Syscalls & Null-Free Encoding

Understand how position-independent machine code invokes Linux system calls directly via CPU registers, why null bytes break string buffers, and how NX and seccomp restrict execution.

Advanced 30 min Binary exploitation
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain how user-space machine code transitions to the kernel using the x86-64 Linux syscall ABI
  • Identify why hardcoded addresses and bad characters such as 0x00 fail inside string-copied memory buffers
  • Apply null-free instruction selection to build and inspect a harmless lab marker routine in NASM
  • Evaluate how NX and seccomp-bpf syscall policies constrain arbitrary machine code execution

Recommended first: Process Memory Layout, Stack Frames & Heap OverviewExploit Debugging with GDB-pwndbg & pwntools

#Why this matters

When a lab binary lacks non-executable stack protection or maps an executable memory buffer, redirected control flow lands directly on raw machine bytes rather than compiled functions.

Those machine bytes, commonly called shellcode even when they only print a marker or read a file, cannot call libc wrappers like printf by symbol name because no dynamic linker resolves imports for injected code. Instead, the instructions must be self-contained and call the operating system kernel directly. Understanding this mechanism helps defenders design tighter syscall sandboxes and helps analysts verify impact in controlled labs.

#From C library wrappers to direct syscalls

Standard C programs invoke libc wrappers such as write() or _exit(), which load arguments into CPU registers and trigger a kernel transition. On 64-bit x86-64 Linux, that instruction is syscall: the kernel reads the syscall number from rax, reads up to six arguments in a fixed register order, and writes the result (or negative error code) back to rax.

Role x86-64 32-bit x86 (int 0x80)
Syscall number rax eax
Arg 1 to 3 rdi, rsi, rdx ebx, ecx, edx
Arg 4 to 6 r10, r8, r9 esi, edi, ebp

Notice a common trap: x86-64 C function calls pass the fourth argument in rcx, whereas the Linux syscall ABI uses r10 because the CPU overwrites rcx with the saved RIP and r11 with RFLAGS during syscall.

Direct x86-64 Linux syscall executionDirect x86-64 Linux syscall executionUser CodeCPUKernelLoad rax=1 (sys_write), rdi=1, rsi=ptr, rdx=lenExecute syscall (save RIP in rcx, enter ring 0)Validate fd and buffer, write bytes to stdoutReturn byte count in rax, restore ring 3Resume next instruction with result in rax

Three syscalls appear constantly in x86-64 Linux labs: sys_read (0), sys_write (1), and sys_exit (60). Numbers differ across architectures, so always verify against your target syscall table.

#Position independence and bad characters

Injected or mapped machine code rarely knows its exact load address in advance because stack frames shift and ASLR randomizes memory regions. Hardcoding an absolute pointer such as 0x7fffffffe120 breaks as soon as the address moves. To remain position-independent (PIC), code locates data relative to rip (lea rsi, [rel msg]) or pushes short strings onto the stack.

A second constraint is bad characters, bytes at which input parsers stop or split buffers:

  • 0x00 (null byte) terminates C string routines such as strcpy and printf("%s").
  • 0x0a (newline) stops line readers such as fgets or readline.
  • 0x20 (space) and 0x09 (tab) break token readers such as scanf("%s").

A naive mov rax, 1 encodes as 48 c7 c0 01 00 00 00 because of its 32-bit sign-extended immediate, embedding three 00 bytes that truncate a C string buffer.

Naive instruction Encoded bytes Null-free alternative
mov rax, 0 48 c7 c0 00 00 00 00 xor eax, eax (31 c0)
mov rax, 1 48 c7 c0 01 00 00 00 xor eax, eax; mov al, 1 (31 c0 b0 01)
mov rdx, 4 48 c7 c2 04 00 00 00 push 4; pop rdx (6a 04 5a)

Writing to a 32-bit sub-register such as eax automatically zeroes the upper 32 bits of rax, keeping encodings compact and null-free.

#Building and inspecting a harmless lab marker

In authorised labs, a safe check verifies execution with a harmless marker that writes a short label to stdout and exits cleanly. The NASM listing below prints LAB\n via sys_write (1) and exits 0 via sys_exit (60), pushing the 4-byte string onto the stack without 0x00 opcodes.

nasm
; marker.asm - harmless x86-64 lab marker (prints "LAB\n" and exits 0)
bits 64
global _start

_start:
    xor eax, eax
    mov al, 1               ; syscall 1: sys_write
    mov edi, eax            ; fd 1: stdout
    mov edx, 0x0a42414c     ; "LAB\n" in little-endian
    push rdx
    mov rsi, rsp            ; pointer to "LAB\n" on stack
    push 4
    pop rdx                 ; count = 4 bytes
    syscall

    xor eax, eax
    mov al, 60              ; syscall 60: sys_exit
    xor edi, edi            ; status = 0
    syscall

You can assemble this file as raw machine bytes or as a standalone ELF for testing, then inspect the bytes with objdump or Python to confirm zero-free encoding.

Assembling and checking opcodes for null bytes
$ nasm -f elf64 marker.asm -o marker.o
$ ld marker.o -o marker
$ ./marker
LAB
$ objdump -d -M intel marker.o | grep -E "^\s+[0-9a-f]+:"
   0:   31 c0                   xor    eax,eax
   2:   b0 01                   mov    al,0x1
   4:   89 c7                   mov    edi,eax
   6:   ba 4c 41 42 0a          mov    edx,0xa42414c
   b:   52                      push   rdx
   c:   48 89 e6                mov    rsi,rsp
   f:   6a 04                   push   0x4
  11:   5a                      pop    rdx
  12:   0f 05                   syscall
  14:   31 c0                   xor    eax,eax
  16:   b0 3c                   mov    al,0x3c
  18:   31 ff                   xor    edi,edi
  1a:   0f 05                   syscall

Every byte in the disassembly above is non-zero. In pwntools, pwnlib.shellcraft provides architecture-specific assembly builders for lab prototyping (for example, shellcraft.amd64.linux.echo("LAB\n") or shellcraft.amd64.linux.exit(0)), and asm() converts assembly text into bytes so you can assert b"\x00" not in payload_bytes inside your test harness before running a lab check.

#Defending against arbitrary machine code

Preventing unauthorized machine-code execution relies on defense in depth across hardware, kernel, and compiler controls:

  • Non-Executable Memory (NX / W^X): Modern binaries mark stack and heap pages writable but not executable (rw-). Jumping to data memory raises a segmentation fault (SIGSEGV) at the CPU page-table level.
  • Strict seccomp-bpf allow-lists: Production services such as container runtimes and browser renderers run with default-deny syscall filters that permit only the exact syscalls a worker needs.
  • Avoid RWX mappings: Applications and JIT engines should never map memory simultaneously writable and executable (PROT_READ | PROT_WRITE | PROT_EXEC).
  • Audit assembly objects: Linking a single legacy .s file without a .note.GNU-stack section can cause ld to mark the entire process stack executable unless -z noexecstack is enforced.

#Common mistakes

  • Assuming null-byte truncation prevents all code injection: length-based inputs such as read() or recv() accept 0x00 bytes without stopping.
  • Using a seccomp denylist instead of an allowlist: blocking execve while leaving execveat, openat, or ptrace open still permits unintended kernel actions.
  • Confusing C calling conventions (rcx as fourth argument) with the Linux syscall ABI (r10 as fourth argument), causing silent errors or EFAULT.

#Summary

  • User-space machine code communicates with the Linux kernel directly via syscall, placing the syscall number in rax and arguments in rdi, rsi, rdx, r10, r8, and r9.
  • Position-independent routines avoid hardcoded addresses by using rip-relative offsets or stack-constructed data.
  • Null-free encoding replaces instructions that embed 0x00 bytes with compact equivalents such as xor eax, eax, mov al, imm8, and push imm8; pop reg.
  • Strong defenses combine W^X page permissions (NX) with default-deny seccomp-bpf allowlists and compiler flags that enforce non-executable stacks.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge