#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.
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 asstrcpyandprintf("%s").0x0a(newline) stops line readers such asfgetsorreadline.0x20(space) and0x09(tab) break token readers such asscanf("%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.
; 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
syscallYou 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.
$ 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 syscallEvery 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
.sfile without a.note.GNU-stacksection can causeldto mark the entire process stack executable unless-z noexecstackis enforced.
#Common mistakes
- Assuming null-byte truncation prevents all code injection: length-based inputs such as
read()orrecv()accept0x00bytes without stopping. - Using a
seccompdenylist instead of an allowlist: blockingexecvewhile leavingexecveat,openat, orptraceopen still permits unintended kernel actions. - Confusing C calling conventions (
rcxas fourth argument) with the LinuxsyscallABI (r10as fourth argument), causing silent errors orEFAULT.
#Summary
- User-space machine code communicates with the Linux kernel directly via
syscall, placing the syscall number inraxand arguments inrdi,rsi,rdx,r10,r8, andr9. - Position-independent routines avoid hardcoded addresses by using
rip-relative offsets or stack-constructed data. - Null-free encoding replaces instructions that embed
0x00bytes with compact equivalents such asxor eax, eax,mov al, imm8, andpush imm8; pop reg. - Strong defenses combine
W^Xpage permissions (NX) with default-denyseccomp-bpfallowlists and compiler flags that enforce non-executable stacks.