Core Design Principles
Syscall-Level Emulation
Sogen emulates Windows by intercepting and handling Windows NT syscalls rather than emulating the entire kernel. When a Windows binary executes:- CPU instructions run natively (or through a CPU emulator backend)
- When a
syscallinstruction is encountered, Sogen intercepts it - The syscall is dispatched to a handler that emulates Windows kernel behavior
- Execution returns to the application
- Performance: Most code runs at near-native speed
- Compatibility: Focus on API-level compatibility rather than hardware emulation
- Flexibility: Easy to add or modify syscall handlers without kernel development
Architecture Overview
Main Components
windows_emulator
Thewindows_emulator class (defined in windows_emulator.hpp:82) is the central orchestrator:
- Execution lifecycle: Starting, stopping, and controlling binary execution
- Component coordination: Connecting subsystems (memory, file system, etc.)
- Thread scheduling: Round-robin cooperative multitasking
- Hooks and callbacks: Extensibility for analysis and instrumentation
CPU Backend Abstraction
Sogen uses a pluggable backend architecture for CPU emulation: Theemulator interface (see emulator/emulator.hpp:9) provides three core interfaces:
- cpu_interface: Register access, instruction pointer control
- memory_interface: Read/write memory
- hook_interface: Attach hooks to instructions, memory access, etc.
- Unicorn: Battle-tested, based on QEMU
- Icicle: High-performance JIT emulator
Process Context
Theprocess_context structure (see process_context.hpp:39) maintains all process-level state:
- PEB/TEB management: Windows process and thread environment blocks
- Handle tables: Kernel object handles (files, events, threads, etc.)
- WOW64 support: Running 32-bit binaries on 64-bit emulator
- Thread management: Thread storage and scheduling state
Memory Manager
Thememory_manager class wraps the backend’s memory interface with Windows-specific semantics:
- Region tracking: Reserved vs. committed memory
- Permission management: Windows memory protection flags
- Memory types: Private allocations, mapped sections, MMIO regions
- Allocation strategy: Finding free memory regions
Syscall Dispatcher
Thesyscall_dispatcher routes syscall requests to appropriate handlers:
- Extracts syscall ID from
EAXregister - Looks up handler in the syscall table
- Invokes handler with syscall context
- Returns control to the emulated process
Execution Flow
Process Initialization
When starting a Windows binary:- Parse PE file: Extract executable headers and sections
- Map image: Load executable into emulated memory
- Load dependencies: Resolve imports, load required DLLs (ntdll.dll, kernel32.dll, etc.)
- Initialize process context: Set up PEB, TEB, environment variables
- Create initial thread: Set instruction pointer to entry point
- Start execution: Begin instruction execution loop
Instruction Execution Loop
Fromwindows_emulator.cpp:659:
- Executes up to
MAX_INSTRUCTIONS_PER_TIME_SLICE(0x20000) instructions - Checks for thread switches
- Handles pending APCs, I/O completions, etc.
- Switches to next ready thread (round-robin)
WOW64 Support
Sogen supports running 32-bit Windows binaries through WOW64 emulation:- Dual PEB/TEB: Maintains both 64-bit and 32-bit structures
- Heaven’s Gate: Transitions between 32-bit and 64-bit mode
- Syscall translation: Maps 32-bit syscalls to 64-bit handlers
- Thunking: Converts data structures between 32-bit and 64-bit layouts
is_wow64_process flag in process_context indicates whether the process is running in WOW64 mode.
Extension Points
Sogen provides several hooks for instrumentation and analysis:- Tracing: Log every instruction, syscall, memory access
- Analysis: Detect suspicious behavior, track data flow
- Debugging: Breakpoints, single-stepping, inspection
- Modification: Change syscall behavior, inject code
Next Steps
- Syscall Emulation - How syscalls are intercepted and dispatched
- Memory Management - Windows memory model implementation
- Threading - Round-robin cooperative multitasking
- Exception Handling - SEH and vectored exception handling