Sogen exposes Python bindings for Windows and Linux userspace emulation. The Python API is meant for scripting runs, building small analysis helpers, and quickly iterating on callbacks; Windows bindings also expose hooks for deeper analysis.
sogen.linux exposes its own Linux-scoped factories, including sogen.linux.create_application(...) and sogen.linux.create_empty(...); it does not use the root-level compatibility aliases.
root_mode sets the access mode of the drives in the emulation root. Each entry in mounts is
(host_path, guest_path) or (host_path, guest_path, mode). The host path can be a file or a directory. The mode is
"ro" or "rw", and both settings default to "ro". A guest write to a "ro" path fails with
STATUS_ACCESS_DENIED, and the host files do not change. path_mappings={guest_path: host_path} is the same as
mounts with the default mode.
Positional arguments after the application are joined into the process command line. An argument that is empty or
contains a space, a tab or a quote is quoted, and its own quotes are escaped.
command_line="..." sets the whole command line verbatim instead, program name included, as CreateProcess would
receive it. When command_line is given, positional arguments are ignored.
Linux applications also accept guest cwd, filesystem mapping, and TCP port mapping kwargs:
Linux working_directory is normalized as an absolute guest path and is active
before application load/startup, so it applies to interpreter/loader work as well
as getcwd() and cwd-relative syscalls such as open() and
openat(AT_FDCWD, ...). path_mappings and read_only_path_mappings are
also installed before load/startup, making mapped guest paths visible to
interpreter startup and later filesystem operations. They accept either
{guest_path: host_path} dictionaries or [(guest_path, host_path), ...]
sequences, and the longest matching guest prefix wins. Read-only mappings reject
mutating file operations including write-open, write, truncate, chmod, unlink,
and rename. port_mappings accepts {emulator_port: host_port} or
[(emulator_port, host_port), ...]; mapped IPv4 loopback TCP connect() calls
are proxied to the configured host loopback port, while unmapped ports keep the
synthetic Linux socket behavior.
Path and port mappings explicitly opt into host filesystem and network access. They are convenience bridges for tests and controlled analysis workflows, not a malware sandbox boundary.
Windows emulators accept network="offline" (default) or network="live". Other strings raise ValueError, and non-string values raise TypeError. In offline mode, guest sockets use an in-process loopback that cannot reach the host network, DNS queries resolve to no addresses, and the Steam bridge is disabled. port_mappings apply only in live mode. A dns_resolver callback that returns None falls back to the resolver of the selected mode, which resolves to no addresses in offline mode.
app.callbacks.on_socket_activity receives (action, address) when the guest calls connect, bind, or sendto. The action is one of "connect", "bind", or "sendto", and the address is a string like "127.0.0.1:28970" with the port as the guest sees it.
Choosing backend
sogen.windows.create_empty(), sogen.windows.create_application(), sogen.linux.create_empty(), and sogen.linux.create_application() accept backend=sogen.Backend.unicorn.
Linux on_syscall callbacks use callback(syscall_id: int, syscall_name: str) -> sogen.HookContinuation | bool | None.
None, False, and sogen.HookContinuation.run continue the built-in syscall handler.
True and sogen.HookContinuation.skip suppress the built-in handler, so an intercepting callback must update guest registers and memory itself.
Linux on_module_load callbacks use callback(module: sogen.linux.LinuxMappedModule) -> None.
Assigning app.callbacks.on_module_load replays modules already mapped during
application construction in ascending image-base order, then observes future
ELF modules discovered from runtime file-backed mmap() activity (for example
libc mapped by the Linux dynamic loader). Linux currently exposes no module-unload
callback because there is no native Linux unload event source in the emulator.
Linux thread controls expose the active emulated thread and cooperative native
scheduler hooks:
app.current_thread returns the active sogen.linux.Thread or None.
app.current_thread_id returns the active TID or None.
app.activate_thread(tid) switches to a live thread, saving/restoring CPU
registers, and returns False for missing or terminated threads.
app.perform_thread_switch() chooses the next runnable non-terminated thread
in TID order and returns whether a switch happened.
app.yield_thread() performs that switch and stops the backend if it switched.
app.process.threads lists non-terminated Linux threads; each thread exposes
tid, current_ip, start_address, wait_state, setup_done, stack/TLS
fields, termination state, exit code, and executed-instruction count.
previous_ip raises NotImplementedError until Linux tracks a prior
instruction pointer instead of silently returning a placeholder value.
Linux thread callbacks use native lifecycle/switch paths:
app.callbacks.on_thread_create(thread) fires after clone(CLONE_THREAD)
creates a thread.
app.callbacks.on_thread_terminated(thread) fires when a Linux thread exits.
app.callbacks.on_thread_switch(old_tid, new_tid) fires when the active TID
changes through activate_thread() or the scheduler.
Linux emulators expose stop diagnostics after every start() call:
app.last_stop_reason is a string: "none", "unknown_syscall",
"unimplemented_syscall", "syscall_exception", "instruction_limit",
"normal_exit", "signal_termination", "unhandled_memory_violation",
"explicit_stop", "backend_error", "breakpoint", or "watchpoint".
app.last_stop_reason_code is the integer enum value for integrations that
prefer stable numeric storage.
app.last_stop_detail contains reason-specific detail, for example
address=0x... size=... for an unhandled memory violation or count=...
for an instruction limit.
Linux signal callbacks use app.callbacks.on_signal(signum, fault_addr, si_code).
The callback fires at the start of native signal delivery, before the emulated
process's handler or Linux's default termination path runs. It is observational:
return values are ignored and cannot suppress guest delivery. on_exception is a
Linux alias for the exact same callback slot and receives the same (signum, fault_addr, si_code) payload because Linux exceptions surface as signals. This
alias is also shown by repr(app.callbacks) for runtime discoverability.
Linux memory violation callbacks use
callback(address: int, size: int, operation: sogen.MemoryOperation, type: sogen.MemoryViolationType) -> sogen.MemoryViolationContinuation | bool | None.
sogen.MemoryOperation is an alias of sogen.MemoryPermission, matching the
backend operation values. None, True, and
sogen.MemoryViolationContinuation.resume resume execution; False and
sogen.MemoryViolationContinuation.stop stop with
last_stop_reason == "unhandled_memory_violation"; and
sogen.MemoryViolationContinuation.restart asks the backend to retry the
faulting instruction. If no Linux memory violation callback or hook is
installed, the emulator keeps its native SIGSEGV/default-stop path.
Linux low-level hooks
Linux emulators expose low-level hooks through app.hooks. Most methods are
backend hooks; memory_violation(callback) is a Linux-managed observer that
coexists with the emulator's internal SIGSEGV delivery path. Hook methods return
sogen.linux.Hook handles with a read-only active property and an idempotent
remove() method. Returned handles also stay active if you do not store them:
app.hooks owns the hook until it is removed or the emulator is destroyed.
Linux Hooks exposes Linux symbol hooks as app.hooks.symbols. Windows-only
hooks.apis remains absent under sogen.linux.
Linux modules and symbol hooks
Linux applications expose initial ELF modules before app.start() and append
runtime-loaded ELF modules as the dynamic loader maps them:
import ctypes
import sogen
app = sogen.linux.create_application("/bin/true", emulation_root="/")
for module in app.modules:
print(module.name, hex(module.image_base), module.path)
Each sogen.linux.LinuxMappedModule has name, path, image_base,
size_of_image, entry_point, exports, needed_libraries, sections,
rpath, and runpath. Exports are sogen.linux.ExportedSymbol objects with
name, rva, and address; sections are sogen.linux.MappedSection objects
with name, start, length, and permissions. Executable ELF sections and
regions expose composite permissions such as sogen.MemoryPermission.read_exec;
write-execute mappings use sogen.MemoryPermission.write_exec. Use
app.find_module_by_address(address) or app.find_module_by_name(name) to look
up one module.
Symbol hooks are registered with app.hooks.symbols["symbol"] = callback or
app.hooks.symbols["module!symbol"] = callback. The qualified form matches the
Linux module name or filename stem case-sensitively. Assign None to a key to
delete that hook, or call app.hooks.symbols.clear() to remove all symbol hooks.
Use @sogen.linux.symbol_call(params=[...], restype=...) to describe an
x86_64 System V integer/pointer signature. Supported ctypes are integer and
pointer-sized scalar/pointer ctypes up to 8 bytes; unsupported ctypes fail when
the hook is registered. Parameters are decoded from rdi, rsi, rdx, rcx,
r8, r9, then stack slots after the return address.
Callbacks receive sogen.linux.LinuxSymbolCall with read-only module, name,
address, and return_address, plus read-write return_value. Returning
None, False, or sogen.ApiContinuation.run_original continues the original
symbol. Returning True or sogen.ApiContinuation.intercept skips the original
symbol by writing return_value to rax, setting rip to the saved return
address, and advancing rsp by 8.
Backend support differs by engine. Unicorn and Icicle support fine-grained
execution, read/write, instruction, interrupt, basic-block, and symbol hooks.
KVM registers these hooks for API compatibility, but read/write, execution,
basic-block, and symbol callbacks may not fire on that backend.
Linux debugger facade
Linux emulators expose app.debug, a Python-native debugger facade over the
same backend hooks and CPU state used by low-level Linux hooks. Breakpoints are
execute hooks: app.debug.set_breakpoint(address) arms one,
clear_breakpoint(address) removes it, and list_breakpoints() returns the
currently armed addresses. When execution reaches a breakpoint,
app.last_stop_reason == "breakpoint", app.last_stop_detail includes the
address, and app.debug.registers()["rip"] is the breakpoint address.
Stepping and run control methods are step_into(), step_over() (currently the
same single-instruction primitive), run_to(address), continue_execution(),
and pause(). step_out() uses a frame-pointer walk and raises diagnostic
RuntimeErrors that distinguish zero RBP, an unreadable saved-return-address
slot, and a zero saved return address. The messages suggest run_to(address)
when an explicit destination is more reliable. Introspection methods return
Python-native data:
registers() returns a register dictionary; modules() and threads() return
lists of dictionaries; disassemble(address, count_or_size) returns instruction
dictionaries with address, bytes, mnemonic, operands, and size; and
call_stack() returns a bounded best-effort frame-pointer walk.
API hooks
API hooks are registered through app.hooks.apis.
Use @sogen.windows.api_call(...) to describe calling convention and parameters.
Top-level sogen.api_call(...) remains as compatibility alias.
qualified module form, for example "kernel32!Sleep"
Memory and state
The emulator exposes direct state access. Windows and Linux emulators both
provide serialize_state() / deserialize_state(bytes) for explicit byte
checkpoints and save_snapshot() / restore_snapshot() for an in-object
snapshot slot.
read_register and write_register move 64-bit integers. The Windows emulator
also provides read_register_bytes(register, size) and
write_register_bytes(register, data), which move raw little-endian bytes and so
reach the 128-bit XMM registers on x86-64 and sogen.arm64.Register.v0 to v31
on ARM64. A value shorter than the register is zero-extended, bytes past the
register's width read as zero, and both accept at most 64 bytes.
sogen.Register.fs_base and gs_base name the segment bases; on 64-bit Windows,
gs_base is the current thread's TEB.
Linux serialized state uses version tag linux-emulator-state-v1 and includes
backend CPU state, executed instruction count, stop diagnostics, mapped memory
bytes and permissions, process ids, brk, argv/envp/auxv, threads, directory and
epoll caches, ELF module metadata, signal actions, and mutable vDSO metadata.
Open Linux file descriptors are serialized only for stdio, procfs/memory files,
eventfds, directory descriptors, epoll descriptors, and host files that can be
reopened by path and restored to their file offset. Live pipes, sockets, unnamed
host descriptors, and unreopenable host descriptors raise during serialization
or restore rather than being silently dropped.
Linux memory managers expose mapped-region introspection through
app.memory.mapped_regions, app.memory.get_mapped_regions(),
app.memory.get_region_info(address), and app.memory.compute_memory_stats().
mapped_regions returns sogen.linux.MemoryRegionInfo objects with start,
length, permissions, allocation_base, allocation_length, is_reserved,
is_committed, initial_permissions, and kind. Composite region permissions
include sogen.MemoryPermission.read_exec and
sogen.MemoryPermission.write_exec. Linux does not have the
Windows reserve/commit split. Each mapped region reports:
allocation_base == start
allocation_length == length
is_reserved is False
is_committed is True
initial_permissions == permissions
kind == sogen.MemoryRegionKind.private_allocation
compute_memory_stats() returns a dictionary with:
region_count
mapped_bytes
executable_bytes
Linux memory lifecycle callbacks observe successful native memory operations:
app.callbacks.on_memory_allocate(address, length, permissions, committed)
fires after mmap/allocation succeeds. Linux always passes committed=True
because there is no reserve-only state.
app.callbacks.on_memory_protect(address, length, permissions) fires after a
protection change succeeds.
app.callbacks.on_memory_release(address, length) fires after unmap/release
succeeds.
For checkpoint-style workflows, use snapshots:
emu.save_snapshot()
# ... mutate state ...
emu.restore_snapshot()
Examples
Small runnable examples:
examples/python/basic_usage.py for Windows
examples/python/linux_true.py for Linux
Example setup notes:
examples/python/README.md
Current limitations / expectations
Windows bindings require an emulation root
Windows samples in this repo assume Windows-style guest paths like c:/...
Windows workflows are easiest to validate against repo sample binaries such as test-sample.exe and hook-sample.exe
backend availability depends on platform and how Sogen was built
Works well with
Use together with sogen: extensions, drivers, servers and companion tools.
unicornPythie’s Index: 20Unicorn CPU emulator engine2.5M downloads/mo