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:
- Write the payload in assembly - craft the logic using raw syscalls, no library dependencies
- Assemble it - produce an object file with NASM or similar
- Extract the raw bytes - pull the machine code out of the object file, discarding ELF metadata
- Test it - verify execution in a controlled harness
- 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:
; 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— zerosrsivia XOR, sidestepping the null bytes that amov rsi, 0would bake in. This becomesargv(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//shpacked into a 64-bit immediate. In little-endian memory order,2f 62 69 6e 2f 2f 73 68reads as/bin//sh. Why the doubled slash?/bin/shis only seven characters, so its 64-bit immediate would need a00in 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— aimsrdiat the string we just stacked. That's the first argument toexecve: the program path.xor rdx, rdx— zerosrdx(envp = NULL).xor rax, raxthenmov al, 59— setsraxto 59 (theexecvesyscall 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.
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:
- Library function addresses are unknown — ASLR shuffles where libraries land
- The dynamic linker may not be reachable — shellcode runs in whatever context the exploit dropped it into
- 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:
; 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:
# Assemblenasm -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 objcopyobjcopy -O binary -j .text shellcode shellcode.bin
# View the bytesxxd shellcode.binOr pull the bytes straight out of the object file:
objdump -d shellcode.o | grep '^ ' | cut -f2 | tr -d ' \n'This produces a hex string like:
4831f65648bf2f62696e2f2f7368574889e74831d24831c0b03b0f05That'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:
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):
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:
| Aspect | Normal Assembly | Shellcode |
|---|---|---|
| Addressing | Absolute addresses, linker-resolved | Position-independent only |
| Strings | In .data or .rodata section | Built on the stack at runtime |
| Functions | call to known addresses | Direct syscalls only |
| Null bytes | No restriction | Often must avoid \x00 entirely |
| Size | Not a concern | Every byte counts |
| Dependencies | Can use libc, dynamic linking | Zero external dependencies |
| Entry point | _start or main | Execution 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.
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:
; exit(42) -- 13 bytes of shellcodexor rdi, rdi ; rdi = 0mov dil, 42 ; rdi = 42 (exit code)xor rax, rax ; rax = 0mov 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.