Skip to main content
Be Bitwise
x86 AssemblyShellcode Development

What Is Shellcode

Shellcode is position-independent machine code built to be injected into a running process. The name's a relic of its first job: spawning an interactive shell (usually /bin/sh) on a compromised box. These days the term covers any payload of raw machine instructions delivered through an exploitation primitive — shell-spawning, second-stage download, reverse connection, whatever the goal happens to be.

Reading and writing shellcode is core security territory. It's the lingua franca of exploit-dev payloads, the substrate of IDS/IPS signature work, and the meat of malware analysis. Detection rules, captured samples, CTF challenges — all of it rewards fluency.

For the theoretical context on exploitation primitives, see Exploitation Primitives.

The Shellcode Lifecycle

Writing shellcode follows a precise pipeline:

  1. Write the payload in assembly - craft the logic using raw syscalls, no library dependencies
  2. Assemble it - produce an object file with NASM or similar
  3. Extract the raw bytes - pull the machine code out of the object file, discarding ELF metadata
  4. Test it - verify execution in a controlled harness
  5. Inject it - deliver the bytes through an exploitation vector (buffer overflow, format string, etc.)

Each step layers on constraints that make shellcode behave nothing like ordinary assembly programming.

The Classic: execve("/bin/sh")

The most basic shellcode there is spawns a shell via execve. Here it is in x86-64 assembly:

nasm
; execve("/bin/sh", NULL, NULL) shellcode
; syscall number 59 = execve on x86-64 Linux
section .text
global _start
_start:
; Build the string "/bin/sh" on the stack
xor rsi, rsi ; rsi = 0 (argv = NULL)
push rsi ; push null terminator onto stack
mov rdi, 0x68732f2f6e69622f ; "/bin//sh" in little-endian
push rdi ; push the string onto stack
mov rdi, rsp ; rdi = pointer to "/bin/sh" on stack
; Set up the syscall
xor rdx, rdx ; rdx = 0 (envp = NULL)
xor rax, rax ; clear rax
mov al, 59 ; syscall number for execve
syscall ; execve("/bin/sh", NULL, NULL)

A line-by-line walkthrough:

  • xor rsi, rsi — zeros rsi via XOR, sidestepping the null bytes that a mov rsi, 0 would bake in. This becomes argv (NULL).
  • push rsi — pushes 8 bytes of zero, which we'll use as the null terminator on top of our string.
  • mov rdi, 0x68732f2f6e69622f — loads the ASCII bytes for /bin//sh packed into a 64-bit immediate. In little-endian memory order, 2f 62 69 6e 2f 2f 73 68 reads as /bin//sh. Why the doubled slash? /bin/sh is only seven characters, so its 64-bit immediate would need a 00 in the top byte, and that zero would land in the middle of the shellcode. Eight characters fill the register exactly, and the kernel treats // in a path the same as /.
  • push rdi — pushes the string onto the stack. Combined with the null terminator above, the stack now holds "/bin//sh\0".
  • mov rdi, rsp — aims rdi at the string we just stacked. That's the first argument to execve: the program path.
  • xor rdx, rdx — zeros rdx (envp = NULL).
  • xor rax, rax then mov al, 59 — sets rax to 59 (the execve syscall number) without leaking null bytes into the encoding.
  • syscall — into the kernel we go. On success, the current process image is replaced by /bin/sh.
Shellcode as the Assembly Programmer's Art Form

Writing shellcode pushes assembly to its breaking point. Every byte counts. You can't lean on the usual scaffolding — sections, linker-resolved labels, library calls. You stage your own strings, dodge forbidden bytes, stay position-independent, and pack complex behaviour into as few bytes as possible. It's the most constrained, weirdly creative form of assembly there is.

Why Shellcode Uses Raw Syscalls

Ordinary programs reach for library functions like printf, malloc, or system, which the dynamic linker resolves at load time. Shellcode can't:

  1. Library function addresses are unknown — ASLR shuffles where libraries land
  2. The dynamic linker may not be reachable — shellcode runs in whatever context the exploit dropped it into
  3. Library calls add dependencies — the target process might not even have the library you need

So shellcode talks straight to the kernel via the syscall instruction. The syscall interface is stable and well-documented: load the number into rax, arguments into rdi, rsi, rdx, r10, r8, r9, then syscall.

Common syscalls used in shellcode:

nasm
; Key x86-64 Linux syscall numbers
; rax = 0 -> sys_read(fd, buf, count)
; rax = 1 -> sys_write(fd, buf, count)
; rax = 2 -> sys_open(path, flags, mode)
; rax = 41 -> sys_socket(domain, type, protocol)
; rax = 42 -> sys_connect(fd, addr, addrlen)
; rax = 49 -> sys_bind(fd, addr, addrlen)
; rax = 50 -> sys_listen(fd, backlog)
; rax = 59 -> sys_execve(path, argv, envp)
; rax = 60 -> sys_exit(code)

Extracting the Raw Bytes

Once your shellcode is assembled, you need just the machine-code bytes — ELF headers and metadata have to go:

bash
# Assemble
nasm -f elf64 shellcode.asm -o shellcode.o
# Link into an ELF so objcopy can carve out .text
# (position-independent shellcode has no relocations to resolve;
# this step just gives us a normal binary to extract from)
ld shellcode.o -o shellcode
# Extract raw bytes using objcopy
objcopy -O binary -j .text shellcode shellcode.bin
# View the bytes
xxd shellcode.bin

Or pull the bytes straight out of the object file:

bash
objdump -d shellcode.o | grep '^ ' | cut -f2 | tr -d ' \n'

This produces a hex string like:

c
4831f65648bf2f62696e2f2f7368574889e74831d24831c0b03b0f05

That's raw machine code, 28 bytes of it, ready to inject. Before you use it, check for zero bytes, since a single 00 would cut a strcpy-delivered payload short. This prints how many there are, and the answer should be 0:

bash
xxd -p -c1 shellcode.bin | grep -c '^00$'

The analyser below is loaded with those same 28 bytes. The Analyze tab counts them and flags any 00. Try changing 2f 2f back to a single 2f and appending a 00 to see what the seven-character /bin/sh version would have cost you.

Shellcode Analyzer & Encoder

Byte Analysis (28 bytes, 0 null bytes):

48
31
f6
56
48
bf
2f
62
69
6e
2f
2f
73
68
57
48
89
e7
48
31
d2
48
31
c0
b0
3b
0f
05

No null bytes detected — shellcode is string-safe

28

Total bytes

0

Null bytes

20

Unique bytes

Shellcode vs Normal Assembly

Stack shellcode against ordinary assembly and the differences pile up fast:

AspectNormal AssemblyShellcode
AddressingAbsolute addresses, linker-resolvedPosition-independent only
StringsIn .data or .rodata sectionBuilt on the stack at runtime
Functionscall to known addressesDirect syscalls only
Null bytesNo restrictionOften must avoid \x00 entirely
SizeNot a concernEvery byte counts
DependenciesCan use libc, dynamic linkingZero external dependencies
Entry point_start or mainExecution starts at byte 0

Security Context: Why Understanding Shellcode Matters

Even with classic injection mostly knocked out by DEP/NX, shellcode literacy still pulls weight:

IDS/IPS signature writing: network IDS engines hunt for shellcode patterns in traffic. Good signatures demand knowing what shellcode looks like at the byte level — NOP sleds, syscall fingerprints, encoding schemes.

Malware analysis: malware tends to ship in stages, and shellcode is often the first one. Analysing a dropper means disassembling shellcode that may be obfuscated, encoded, or polymorphic.

CTF competitions: Capture The Flag challenges routinely demand shellcode under tight constraints — size budgets, character whitelists, awkward architectures.

Red team operations: authorised pentesters write custom shellcode for engagements where off-the-shelf payloads light up endpoint protection.

Understanding defences: you can't judge whether DEP, ASLR, or a sandbox is doing its job unless you understand the attack it's trying to stop.

Security Note: The Arms Race

Shellcode has driven decades of back-and-forth innovation. Every new technique invited a defensive answer: NOP sleds bred IDS signatures, which bred polymorphic shellcode, which bred behavioural analysis. Shellcode injection bred DEP, which bred ROP, which bred CFI. The arms race is the reason modern security is layered and the reason no single defence holds the line on its own.

A Minimal Example: exit(42)

The smallest shellcode worth writing just exits the process with a chosen code:

nasm
; exit(42) -- 13 bytes of shellcode
xor rdi, rdi ; rdi = 0
mov dil, 42 ; rdi = 42 (exit code)
xor rax, rax ; rax = 0
mov al, 60 ; rax = 60 (sys_exit)
syscall ; exit(42)

Assembled, that's 13 bytes: 48 31 ff 40 b7 2a 48 31 c0 b0 3c 0f 05. Every byte is part of a CPU instruction. No headers, no metadata, no sections — just raw, runnable code.

What is Next

The execve shellcode above hides a problem: how do you point to "/bin/sh" when you have no idea where in memory you're running? The next lesson covers the position-independent tricks that solve it, including the call/pop pattern every shellcode author leans on.