x86-64 Assembly
To exploit binaries and do reversing you need to read (and sometimes write) assembly: it’s the language code compiles to and the level you work at when manipulating execution. You don’t need to master it like a systems developer, but you do need to understand registers, basic instructions, how the stack works, and calling conventions. This card is the practical base for pwn-reversing.
What it is
Section titled “What it is”Assembly is the textual representation of the machine code the CPU runs. Each instruction (mov, call, ret…) maps to a few bytes (opcodes). When a compiler translates C into a binary, it produces these instructions; when you disassemble with objdump or Ghidra, you walk the reverse path. In pwn you work on x86-64 (AMD64) most of the time, with forays into ARM/MIPS on specific challenges.
Theory: registers (x86-64)
Section titled “Theory: registers (x86-64)”General-purpose registers are 64-bit, with 32/16/8-bit sub-registers:
RAX RBX RCX RDX RSI RDI RBP RSPandR8–R15.- Size equivalences on
RAX:RAX(64) →EAX(32) →AX(16) →AL(8).
The special ones that drive the exploit:
RIP— instruction pointer: what runs. It’s the ultimate target of the exploit.RSP— stack pointer: top of the stack.RBP— base pointer: anchor of the function frame.RFLAGS— flags from comparison results (ZFzero,CFcarry,SFsign…).
Essential instructions
Section titled “Essential instructions”mov dst, src— copy;lea dst, [addr]— load the address (not the contents).add/sub dst, src— arithmetic;xor/and/or— logical (xor rax, raxzeroesRAX).push/pop— push/pop the stack (adjustRSP).call func— pushes the return address and jumps;ret— popsRIP.cmp a, b— compare (sets flags);test a, a— check for zero.jmp addr— unconditional jump;je/jne/jg/jl… — jumps based on flags.nop— no operation (0x90), useful as a NOP sled in shellcode.syscall— transition into the kernel (on 64-bit it replacesint 0x80).
The stack and function frames
Section titled “The stack and function frames”The stack grows toward low addresses. On entering a function a prologue runs, and on exit an epilogue:
; typical prologuepush rbp ; save the caller's RBPmov rbp, rsp ; new framesub rsp, 0x40 ; reserve 64 bytes for locals; ... body ...; epilogueleave ; equivalent to: mov rsp, rbp ; pop rbpret ; pop rip -> returns to the saved return addressret pops the address to return to off the top of the stack and loads it into RIP. If you control what’s at that stack position, you control RIP —the basis of the stack overflow (see Stack Buffer Overflow).
flowchart TD
subgraph Stack["Stack inside a function (grows downward)"]
A["local buffer (char buf[64])"]
B["saved RBP"]
C["return address ← overflow target"]
D["caller arguments / frame"]
end
A --> B --> C --> D
C -.->|"ret loads this into RIP"| E["RIP"]
Calling convention (System V, Linux x86-64)
Section titled “Calling convention (System V, Linux x86-64)”Key for exploits: knowing which registers hold the arguments.
- The first 6 arguments go, in order, in
RDI, RSI, RDX, RCX, R8, R9. - The return value goes in
RAX. - Arguments 7+ go on the stack.
- Example:
system("/bin/sh")→RDI= pointer to"/bin/sh", thencall system.
That’s why in ROP you look for gadgets like pop rdi ; ret: to place the argument in RDI before calling a function.
Intel vs AT&T syntax
Section titled “Intel vs AT&T syntax”; Intel (what most pwn tools use, more readable)mov rax, rbx ; destination, sourceIn AT&T (GDB and GCC default) the order is reversed and prefixed: mov %rbx, %rax. To work comfortably in GDB: set disassembly-flavor intel (or use pwndbg/GEF, which already do).
Exploitation (lab): reading and writing assembly
Section titled “Exploitation (lab): reading and writing assembly”Disassembling and assembling are everyday operations:
objdump -d -M intel ./binary # disassemble in Intel syntaxgdb -q ./binary -ex 'disassemble main'With pwntools you can assemble your own instructions and generate shellcode:
from pwn import *context.arch = 'amd64'
sc = asm('xor rsi, rsi; xor rdx, rdx') # assemble to bytessh = asm(shellcraft.amd64.linux.sh()) # execve("/bin/sh") shellcodeprint(disasm(sc)) # disassemble back to verifyMitigations / Blue Team
Section titled “Mitigations / Blue Team”Understanding assembly isn’t a defense in itself, but it’s the basis for auditing your own binaries, analyzing malware, and writing precise detections. On the development side, compilers insert the mitigation checks at the assembly level:
- The stack canary shows up as a
movfromfs:0x28in the prologue and acmpbefore theret. - CFI (
-fcf-protection) and Intel CET shadow stacks addendbr64at function entry and validate returns. - FORTIFY_SOURCE replaces
memcpy/strcpywith size-checked variants (__memcpy_chk).
Recognizing these patterns in the disassembly tells you which defenses are active without running anything.
Checklist
Section titled “Checklist”- I recognize the registers and their sizes (
RAX/EAX/AX/AL) - I understand
RIP/RSP/RBPand their roles - I read the basic instructions (
mov,call,ret,jmp,cmp…) - I explain the prologue/epilogue and how
retcontrolsRIP - I know the calling convention (
RDI, RSI, RDX…) - I tell Intel syntax from AT&T
- I disassemble with objdump/GDB/Ghidra
- I understand how
syscallmakes kernel calls