Process API¶
The Process API is the main interface for dynamic analysis with Pyda. It provides methods for hooking execution, intercepting system calls, and manipulating process state.
Bases: ProcessTube
Obtain a Process
via the module-level process
function.
Example
Obtain a Process
with process()
.
The Process class provides the primary API for instrumenting and analyzing target processes. It allows you to set hooks, intercept system calls, and manipulate process state during execution.
This class extends ProcessTube
, providing pwntools-compatible I/O operations
alongside the dynamic analysis capabilities.
Attributes:
Name | Type | Description |
---|---|---|
regs |
ProcessRegisters
|
Interface for reading/writing CPU registers |
mem |
ProcessMemory
|
Interface for reading/writing process memory |
maps |
ProcessMaps
|
Interface for querying memory mappings |
exe_path |
str
|
Path to the main executable being analyzed |
tid |
int
|
Current thread ID (valid during hook execution) |
Example
Basic usage pattern:
The following options can be passed to process()
:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
io
|
bool
|
Whether to capture I/O for pwntools compatibility. When True, enables recv/send operations. |
False
|
tid
property
¶
Get the current thread ID (starts from 1).
Note
This property is only valid during hook execution or when the process is stopped.
hook(addr, callback, priority=False, later=False)
¶
Register a hook at the specified address.
Hooks are functions called before executing the instruction at the specified program counter.
Note
Multiple hooks can be registered at the same address and will be called in registration order (unless priority=True).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Memory address where the hook should be installed |
required |
callback
|
callable
|
Function to call when the hook is hit. Should accept one argument (the Process instance) |
required |
priority
|
bool
|
If True, adds this hook before existing hooks at the same address |
False
|
later
|
bool
|
If True, registers the hook but doesn't install it until the address is reached for the first time |
False
|
unhook(addr, callback=None, unregister=True)
¶
Remove a hook from the specified address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Address where the hook is installed |
required |
callback
|
callable
|
Specific callback to remove. If None, removes all hooks at the address |
None
|
unregister
|
bool
|
Whether to unregister the hook from DynamoRIO if no callbacks remain |
True
|
hook_after_call(addr, callback)
¶
Register a hook that fires when a function call returns.
This is a convenience method that hooks a function entry point and automatically sets up a hook at the return address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Address of the function to hook |
required |
callback
|
callable
|
Function to call when the function returns |
required |
Example
Note
This assumes x86-64 calling convention where return addresses are stored on the stack.
syscall_pre(syscall_num, callback)
¶
Register a pre-syscall hook for the specified system call.
Pre-syscall hooks are called before the system call is executed, allowing inspection and modification of arguments. They can also prevent the syscall from executing by returning False.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
syscall_num
|
int
|
System call number to hook (e.g. 1 for write) |
required |
callback
|
callable
|
Function to call before the syscall. Should accept (process, syscall_num) arguments. Can return False to skip the syscall. |
required |
Example
Raises:
Type | Description |
---|---|
RuntimeError
|
If called after the process has started running |
syscall_post(syscall_num, callback)
¶
Register a post-syscall hook for the specified system call.
Post-syscall hooks are called after the system call completes, allowing inspection of return values and side effects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
syscall_num
|
int
|
System call number to hook |
required |
callback
|
callable
|
Function to call after the syscall. Should accept (process, syscall_num) arguments. |
required |
Example
Raises:
Type | Description |
---|---|
RuntimeError
|
If called after the process has started running |
set_thread_entry(callback)
¶
Register a callback for when new threads are created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
callable
|
Function to call when a new thread starts. Should accept one argument (the Process instance). |
required |
on_module_load(callback)
¶
Register a callback for when new modules are loaded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
callable
|
Function to call when a module loads. Should accept appropriate arguments from DynamoRIO. |
required |
read(addr, size)
¶
write(addr, data)
¶
run()
¶
run_until(addr)
¶
run_from_to(start, end)
¶
Jump to a specific address and run until another address is reached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start
|
int
|
Address to jump to |
required |
end
|
int
|
Address to run until |
required |
Note
This cannot be used from within hooks.
callable(addr)
¶
Create a callable that executes instrumented target code.
Returns a function that, when called, will execute the function at the specified address with the given arguments, following standard calling conventions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr
|
int
|
Address of the function to call |
required |
Example
Note
- Cannot be used from within hooks
- Requires the process to have a valid stack
- Currently supports up to 6 arguments (x86-64 limitation)
backtrace()
¶
backtrace_cpp(short=False)
¶
Get a C++ demangled backtrace of the current call stack.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
short
|
bool
|
If True, truncate long symbol names |
False
|
Memory and Register Interfaces¶
pyda.proc.ProcessRegisters(p)
¶
Interface for reading and writing CPU registers.
Provides convenient access to CPU registers using both attribute and dictionary-style syntax.
Example
pyda.proc.ProcessMemory(p)
¶
Interface for reading and writing process memory.
Provides convenient slice-based access to target process memory.
Example
__getitem__(key)
¶
pyda.proc.ProcessMaps(p)
¶
pyda.proc.Map(vaddr: int, size: int, path: str, perms: int)
dataclass
¶
Represents a memory mapping in the target process.
base
property
¶
Base address of this mapping.
start
property
¶
Start address (same as base).
end
property
¶
End address of this mapping.
executable
property
¶
True if mapping has execute permissions.
writable
property
¶
True if mapping has write permissions.
readable
property
¶
True if mapping has read permissions.