GadgetFinder Class
TheGadgetFinder class is responsible for discovering and analyzing ROP gadgets in binary executables. It performs both static analysis and symbolic execution to identify useful gadget candidates.
Constructor
The angr project containing the binary to analyze
Enable fast mode to skip complex gadgets. If
None, automatically decides based on binary size (enabled for binaries with >20,000 addresses to check). When enabled:- Skips gadgets with conditional branches
- Skips floating point operations
- Skips jumps
- Reduces maximum block size
- Limits symbolic memory accesses to 1
Only analyze blocks within
max_block_size bytes of return instructions. Significantly speeds up analysis. Only applicable for i386/amd64/aarch64 architectures.Maximum size of blocks to consider as gadgets. Longer blocks are less likely to be useful ROP gadgets. If not specified, uses architecture default.
Maximum number of symbolic memory accesses allowed in a gadget. Gadgets with more accesses are rejected. If not specified, uses architecture default.
Execute ROP chain in ARM Thumb mode. Only applicable for ARM architecture. angrop does not switch modes within a ROP chain.
Find kernel mode gadgets. When enabled, searches in the
.text section for kernel binaries.Number of controllable gadgets on the stack. The maximum allowable stack change for gadgets is
stack_gsize * arch.bytes.Whether to support conditional branches in gadgets. This option significantly impacts gadget finding speed.
Maximum number of basic blocks to traverse when analyzing a gadget.
Core Methods
analyze_gadget
The address to analyze for gadgets
Whether to allow gadgets with conditional branches. If
None, uses the class-level cond_br setting.- When
False: Returns a singleRopGadgetorNone - When
True: Returns a list ofRopGadgetinstances (one per execution path)
- If
allow_conditional_branches=False: Single gadget orNone - If
allow_conditional_branches=True: List of gadgets (empty list if none found)
find_gadgets
Number of worker processes to use for parallel analysis
Display a progress bar during analysis
Maximum time in seconds for gadget finding. Analysis stops when timeout is reached.
(gadgets, duplicates) where:
gadgets: Sorted list of all discovered ROP gadgetsduplicates: Dictionary mapping block hashes to sets of addresses with identical instructions
find_gadgets_single_threaded
find_gadgets()
analyze_gadget_list
List of addresses to analyze
Internal Analysis Pipeline
Static Analysis Phase
The gadget finder uses a two-phase approach:-
Static Filtering (
_multiprocess_static_analysis)- Loads and disassembles blocks near return instructions
- Filters based on:
- Block size (
max_block_size) - Jump kind (ret, syscall, boring, call)
- Conditional branches (if
cond_br=False) - Floating point operations (if
fast_mode=True) - Valid jump targets
- Block size (
- Builds cache of block hashes to deduplicate identical instruction sequences
-
Symbolic Analysis Phase (
_analyze_gadgets_multiprocess)- Symbolically executes each unique gadget candidate
- Timeout per gadget: 3 seconds
- Worker processes automatically restart if memory usage exceeds 500MB
Block Validation
Blocks are validated through multiple checks:Block Hashing and Deduplication
- For regular gadgets: Uses raw bytes of the block
- For syscall gadgets: Includes bytes of the next block after the syscall
_duplicates dictionary and can be used to avoid bad bytes.
Address Generation
The_slices_to_check() method generates address ranges to analyze:
-
With
only_check_near_rets=True:- Creates slices of
max_block_sizebytes before each return instruction - Merges overlapping slices to avoid redundant analysis
- Prioritizes syscall locations for early discovery
- Creates slices of
-
With
only_check_near_rets=False:- Analyzes all executable memory ranges
- Respects alignment boundaries
Multiprocessing Architecture
The finder uses two worker functions:Location Discovery
Return Instruction Locations
- Uses architecture-specific byte patterns (fast path for x86/x64/aarch64/riscv)
- Falls back to VEX jumpkind analysis for other architectures
- Returns sorted list of return instruction addresses
Syscall Instruction Locations
- Searches for architecture-specific syscall byte patterns
- Supported: i386, amd64, MIPS
- Returns sorted list of syscall instruction addresses
Performance Considerations
- Fast Mode: Automatically enabled for large binaries (>20k addresses)
- Worker Memory Management: Processes restart when RSS exceeds 500MB over baseline
- Timeout Handling: 3-second per-gadget timeout prevents hangs
- Cache Optimization: Deduplicates identical blocks to reduce symbolic execution