What are ROP Gadgets?
Return-Oriented Programming (ROP) gadgets are small sequences of executable instructions ending in a return instruction (or similar control flow transfer). These gadgets are chained together to perform arbitrary computation by controlling the stack and hijacking the normal program flow. In angrop, gadgets are discovered through symbolic execution and analyzed to understand their effects on registers, memory, and control flow.Gadget Types
angrop categorizes gadgets into several types based on their functionality:RopGadget
The base gadget type used for general-purpose ROP operations. ARopGadget represents a sequence of instructions that can be used to manipulate registers, memory, or control flow.
Key properties:
addr: The address where the gadget startsstack_change: How much the stack pointer changes after executionchanged_regs: Set of registers modified by the gadgetreg_pops: Registers that are popped from the stackreg_moves: Register-to-register data movementsmem_reads,mem_writes,mem_changes: Memory access operationsbbl_addrs: List of basic block addresses in the gadget
- pop_pc - Returns normally (e.g.,
ret,pop pc). These are “self-contained” gadgets. - jmp_reg - Jumps to a register value (e.g.,
jmp rax,call rbx) - jmp_mem - Jumps to a memory location (e.g.,
jmp [rax+8],call [rbp+0x10])
PivotGadget
Stack pivot gadgets allow you to change the stack pointer to an arbitrary location. This is essential for exploits where you need to move execution to a controlled memory region. Key properties:stack_change_before_pivot: Stack change before the pivot occursstack_change_after_pivot: Stack change after the pivotsp_reg_controllers: Registers that can control the new stack pointer valuesp_stack_controllers: Stack values that control the new stack pointer
mov rsp, rbp; retxchg rsp, rax; retpop rsp; ret
SyscallGadget
Gadgets that invoke system calls, enabling direct interaction with the operating system kernel. Two types:- With return:
syscall; ret- Can continue the ROP chain after the syscall - Without return:
syscall; <other instructions>- Terminal gadgets that don’t return control
can_return: Whether the gadget returns after the syscallprologue: Optional setup gadget executed before the syscall
- x86:
int 0x80 - x64:
syscall - ARM/MIPS:
syscall
FunctionGadget
Represents a function call that can be used in ROP chains. Key properties:symbol: The function symbol name (e.g., “system”, “execve”)- Used for invoking library functions like
system("/bin/sh")
Self-Contained vs Non-Self-Contained Gadgets
Self-Contained Gadgets
Gadgets that can be used independently without requiring other gadgets to set up their execution. Criteria (from rop_gadget.py:34):- It has no conditional branches
- It uses
pop_pctransit type (normal return) - It has no out-of-patch (oop) memory accesses
pop rax; ret✓ Self-containedpop rax; pop rbx; ret✓ Self-containedmov rax, [rsp]; add rsp, 8; jmp rax✓ Self-contained (treated as pop_pc)
Non-Self-Contained Gadgets
Gadgets that require setup or normalization before use. Examples:-
jmp_reg gadgets - Require setting the target register first:
Needs another gadget to set
raxbefore jumping. -
jmp_mem gadgets - Require setting up memory:
Needs to write the target address to
[rax+0x10]. -
Conditional branch gadgets:
Requires setting
raxto control the branch.
Gadget Effects
angrop analyzes each gadget to understand its effects: Register effects:changed_regs: All modified registerspopped_regs: Registers loaded from the stackreg_moves: Register-to-register copiesreg_dependencies: Which registers affect other registersreg_controllers: Which registers can fully control other registersconcrete_regs: Registers set to concrete values
mem_reads: Memory load operationsmem_writes: Memory store operationsmem_changes: In-place memory modifications (add, xor, etc.)
transit_type: How control is transferredhas_conditional_branch: Whether execution path depends on register valuesbranch_dependencies: Registers that affect branch decisions
Finding Gadgets
Gadgets are discovered through several methods:- Near-ret scanning: Searching backwards from return instructions
- Symbolic execution: Analyzing instruction sequences to determine effects
- Constraint solving: Understanding what values enable desired effects
Gadget Selection
When building chains, angrop selects gadgets based on:- Stack change - Smaller is better (less payload size)
- Side effects - Fewer modified registers is better
- Symbolic memory accesses - Fewer is better (easier to constrain)
- Instruction count - Shorter gadgets are simpler
- Conditional branches - Avoided when possible (harder to control)