Overview
Thegenerator::exception policy uses Windows Vectored Exception Handlers (VEH) to intercept illegal instruction exceptions and redirect execution to syscalls. This creates an additional layer of indirection that can evade certain detection mechanisms.
How It Works
The exception-based approach follows this flow:- Setup: Registers a VEH during manager initialization
- Stub Generation: Creates stubs with illegal instructions (
UD2) - Execution: When a stub runs, it triggers an exception
- Handler: The VEH catches the exception and modifies CPU registers
- Redirection: Execution continues at a syscall gadget with the correct syscall number
Architecture
Complete Example
Generated Stub
The exception generator creates minimal stubs:Exception Handler Implementation
The VEH is registered during initialization:Handler Logic
The VEH inspects and modifies the exception context:Exception Context Guard
A RAII guard manages the thread-local exception context:Security Benefits
Call Stack Obfuscation
Call Stack Obfuscation
The exception handler modifies the instruction pointer, creating a non-linear execution flow that’s harder to trace and analyze.
No Direct Syscall Instructions
No Direct Syscall Instructions
Your code never executes a syscall instruction directly. The stub only contains an illegal instruction.
Dynamic Gadget Selection
Dynamic Gadget Selection
Each invocation can use a different random gadget from ntdll.dll, providing execution diversity.
Thread-Local Context
Thread-Local Context
Using thread-local storage prevents race conditions and enables safe multi-threaded use.
Use Cases
- Advanced Evasion: When you need maximum stealth
- Research: Studying exception-based execution techniques
- Testing: Validating security product behavior with non-standard execution flows
- Obfuscation: Making control flow analysis more difficult
Performance Considerations
Performance Benchmarks
| Generator | Average Latency | Relative Speed |
|---|---|---|
| Direct | ~100ns | 1.0x (baseline) |
| Gadget | ~150ns | 1.5x slower |
| Exception | ~5-10μs | 50-100x slower |
Limitations
Thread Safety
The implementation usesthread_local storage for the exception context:
Expected Output
When to Use Exception-Based Syscalls
✅ Use when:- You need maximum evasion capabilities
- Performance is not critical
- You’re researching advanced techniques
- You want to test security product responses to unusual execution flows
- Performance matters
- You need simple, maintainable code
- You’re working with high-frequency syscalls
- Debugging is a priority
Comparison with Other Generators
| Feature | Direct | Gadget | Exception |
|---|---|---|---|
| Platform | x64 + x86 | x64 only | x64 + x86 |
| Performance | Fastest | Fast | Slow |
| Stealth | Low | High | Highest |
| Complexity | Simple | Moderate | Complex |
| Stub Size | 18 bytes | 32 bytes | 8 bytes |
| Debugging | Easy | Moderate | Hard |
See Also
- Direct Syscalls - Fast, simple approach
- Gadget Syscalls - Balanced stealth and performance
- Custom Generators - Build your own approach
- Architecture - Framework design details