Return-Oriented Programming (ROP)
You'll learn how attackers bypass modern memory protections by repurposing existing code snippets to execute malicious payloads.
What Is ROP?
Modern systems use protections like Data Execution Prevention (DEP) or No-eXecute (NX) bits that mark stack and heap memory as non-executable. This blocks traditional buffer overflow exploits that inject shellcode directly into memory and jump to it.
Return-Oriented Programming defeats these protections by never injecting executable code at all. Instead, attackers chain together tiny existing code fragments already present in executable memory—called gadgets—to perform arbitrary operations.
How It Works
Think of ROP like building a sentence using words cut from a magazine. You're not creating new words (injecting code); you're arranging existing ones to say something entirely different.
Each gadget typically consists of a few assembly instructions ending in a ret (return) instruction. For example:
pop rdi; ret— loads a value into registerrdiadd rax, rbx; ret— performs arithmeticmov [rdx], rax; ret— writes to memory
By carefully crafting the stack with addresses pointing to these gadgets in sequence, an attacker controls the return address after each gadget executes. The ret instruction pops the next gadget address off the stack, creating a chain that performs complex operations: setting up function parameters, calling system functions, or disabling security features.
The Attack Process
After exploiting a buffer overflow to corrupt the return address, instead of pointing to injected shellcode, you point to your first gadget. Each gadget "returns" to the next, executing your payload using only legitimate, executable code already in memory.
Key Takeaway: ROP bypasses non-executable memory protections by chaining small existing code fragments (gadgets) to achieve arbitrary execution without injecting new code—turning the program's own instructions against itself.