Skip to main content

Sogen Windows user-space emulator bindings

Project description

Sogen

Sogen exposes Python bindings for its Windows user-space emulator. The Python API is meant for scripting runs, building small analysis helpers, and quickly iterating on callbacks and hooks without rebuilding C++.

Install from PyPI:

pip install sogen

Project links:

What you need

The Python package still needs an emulation root at runtime. Download ready-made root here:

Extract it somewhere convenient, for example:

./root

Most examples in this document use:

emulation_root="./root"

Quick start

import sogen

app = sogen.create_application(
    "c:/test-sample.exe",
    emulation_root="./root",
)

app.callbacks.on_stdout = lambda text: print(text, end="")
app.start()
print("exit status:", app.process.exit_status)

Minimal example with file + port mappings

from pathlib import Path
import sogen

app = sogen.create_application(
    "c:/test-sample.exe",
    emulation_root="./root",
    path_mappings={"c:/a.txt": Path("./a.txt")},
    port_mappings={28970: 28980},
)

app.callbacks.on_stdout = lambda text: print(text, end="")
app.start()
print("exit status:", app.process.exit_status)

Choosing backend

create_empty() and create_application() accept an explicit backend.

import sogen

emu = sogen.create_empty(
    emulation_root="./root",
    backend=sogen.Backend.unicorn,
)

Available values:

  • sogen.Backend.unicorn
  • sogen.Backend.icicle
  • sogen.Backend.whp

Default is sogen.Backend.unicorn.

High-level structure

Main entry points:

  • sogen.create_empty(...)
  • sogen.create_application(...)

Common objects exposed by the bindings:

  • WindowsEmulator
  • ProcessContext
  • Thread
  • MemoryManager
  • Hooks
  • Callbacks

Common things you will do:

  • run application with app.start()
  • watch output with app.callbacks.on_stdout
  • react to module loads with app.callbacks.on_module_load
  • intercept WinAPI calls with app.hooks.apis[...]
  • read/write emulator memory with read_memory() / write_memory()
  • save and restore state with save_snapshot() / restore_snapshot()

Callbacks

Example: print loaded modules.

import sogen

app = sogen.create_application(
    "c:/test-sample.exe",
    emulation_root="./root",
)


def on_module_load(module):
    print(f"loaded {module.name} @ 0x{module.entry_point:x}")


app.callbacks.on_module_load = on_module_load
app.start()

Useful callback slots include:

  • app.callbacks.on_stdout
  • app.callbacks.on_syscall
  • app.callbacks.on_memory_violate
  • app.callbacks.on_module_load
  • app.callbacks.on_module_unload

API hooks

API hooks are registered through app.hooks.apis.

Use @sogen.api_call(...) to describe calling convention and parameters.

Observe API call, then run original

import ctypes
import sogen

app = sogen.create_application(
    "c:/test-sample.exe",
    emulation_root="./root",
)


@sogen.api_call(cc=sogen.CallingConvention.stdcall, params=[ctypes.c_uint32])
def on_sleep(call, params):
    print(f"Sleep({params[0]})")


app.hooks.apis["Sleep"] = on_sleep
app.start()

Intercept API call and return custom value

import sogen

app = sogen.create_application(
    "c:/hook-sample.exe",
    emulation_root="./root",
)


@sogen.api_call(cc=sogen.CallingConvention.stdcall, params=[])
def on_get_current_process_id(call, params):
    call.return_value = 0xC0FFEE01
    return sogen.ApiContinuation.intercept


app.hooks.apis["GetCurrentProcessId"] = on_get_current_process_id
app.start()
print(app.process.exit_status)

Hook keys can be either:

  • bare API name, for example "Sleep"
  • qualified module form, for example "kernel32!Sleep"

Memory and state

The emulator exposes direct state access.

import sogen

emu = sogen.create_empty(emulation_root="./root")
base = emu.memory.allocate_memory(0x1000, sogen.MemoryPermission.read_write)
emu.write_memory(base, b"ABCD")
print(emu.read_memory(base, 4))

state = emu.serialize_state()
emu.write_memory(base, b"WXYZ")
emu.deserialize_state(state)
print(emu.read_memory(base, 4))

For checkpoint-style workflows, use snapshots:

emu.save_snapshot()
# ... mutate state ...
emu.restore_snapshot()

Examples

Small runnable example:

  • examples/python/basic_usage.py

Example setup notes:

  • examples/python/README.md

Current limitations / expectations

  • bindings require an emulation root
  • samples in this repo assume Windows-style guest paths like c:/...
  • some 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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sogen-0.0.1.dev3578.tar.gz (24.9 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sogen-0.0.1.dev3578-cp39-cp39-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.9Windows x86-64

sogen-0.0.1.dev3578-cp39-cp39-manylinux_2_39_x86_64.whl (6.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.39+ x86-64

sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.9macOS 15.0+ x86-64

sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_arm64.whl (4.7 MB view details)

Uploaded CPython 3.9macOS 15.0+ ARM64

File details

Details for the file sogen-0.0.1.dev3578.tar.gz.

File metadata

  • Download URL: sogen-0.0.1.dev3578.tar.gz
  • Upload date:
  • Size: 24.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sogen-0.0.1.dev3578.tar.gz
Algorithm Hash digest
SHA256 750ca460df121de95f1c62984a65e9c45a4d617d0e9831cddab1fdebc5989c40
MD5 9cbace5cf2d930e25b53d57eee4194dc
BLAKE2b-256 1a18e5976c8a73983ec40ad747fb28d35f39ea6134acd6a9a6a49f84a9c884f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for sogen-0.0.1.dev3578.tar.gz:

Publisher: build-push.yml on momo5502/sogen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sogen-0.0.1.dev3578-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for sogen-0.0.1.dev3578-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c99e1a2ad3373d2945b270528209095f2ef1807acbd0b87b338fbafa6b3d1ad4
MD5 c7b1c03f176563428cb4055bf26c4fc2
BLAKE2b-256 46b62fb77b8f7bf8480476f71540dfb87f0466bfd3bee55dd8118ec24b73cf55

See more details on using hashes here.

Provenance

The following attestation bundles were made for sogen-0.0.1.dev3578-cp39-cp39-win_amd64.whl:

Publisher: build-push.yml on momo5502/sogen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sogen-0.0.1.dev3578-cp39-cp39-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for sogen-0.0.1.dev3578-cp39-cp39-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 00510540f61fe00edf80fe44763bf97f844dfa4d5ed802bf03e5693e04b1f1b0
MD5 24a2f036b4efb2d157bf0c6b5e519433
BLAKE2b-256 1e4ba12cda924ecfa5ac205031aadb255da3b5c1fd4285163e936d1d254809db

See more details on using hashes here.

Provenance

The following attestation bundles were made for sogen-0.0.1.dev3578-cp39-cp39-manylinux_2_39_x86_64.whl:

Publisher: build-push.yml on momo5502/sogen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 2c86c962d775c5d44a54a50203251789ad3615224379b9b4ca08b7c7dc25f3e4
MD5 bb1a91ace96d9f9532456b9de74381f4
BLAKE2b-256 66848cbdd13796ba99e4fa005931b281266ac8cd144e6b51ae716f2f42dcc6b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_x86_64.whl:

Publisher: build-push.yml on momo5502/sogen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 753a3a596305de4957b41c6766f32b332cdbf6b49fc39b4a9020530b9c2321b4
MD5 a6fc6a0f6a8fecc7c9c569ba5614d8c9
BLAKE2b-256 8173077695e38e999d3669e8cd0f08873504f55cb8df4c91eab0fa7ce14cf080

See more details on using hashes here.

Provenance

The following attestation bundles were made for sogen-0.0.1.dev3578-cp39-cp39-macosx_15_0_arm64.whl:

Publisher: build-push.yml on momo5502/sogen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page