libdebug.utils package#

Submodules#

libdebug.utils.debugger_wrappers module#

libdebug.utils.debugger_wrappers.change_state_function_process(method: callable) callable[source]#

Decorator to perfom control flow checks before executing a method.

libdebug.utils.debugger_wrappers.change_state_function_thread(method: callable) callable[source]#

Decorator to perfom control flow checks before executing a method.

libdebug.utils.debugger_wrappers.background_alias(alias_method: callable) callable[source]#

Decorator that automatically resolves the call to a different method if coming from the background thread.

libdebug.utils.debugging_utils module#

libdebug.utils.debugging_utils.check_absolute_address(address: int, maps: list[MemoryMap]) bool[source]#

Checks if the specified address is an absolute address.

Returns:

True if the specified address is an absolute address, False otherwise.

Return type:

bool

libdebug.utils.debugging_utils.normalize_and_validate_address(address: int, maps: list[MemoryMap]) int[source]#

Normalizes and validates the specified address.

Returns:

The normalized address.

Return type:

int

Throws:

ValueError: If the specified address does not belong to any memory map.

libdebug.utils.debugging_utils.resolve_symbol_in_maps(symbol: str, maps: list[MemoryMap]) int[source]#

Returns the address of the specified symbol in the specified memory maps.

Parameters:
  • symbol (str) – The symbol whose address should be returned.

  • maps (list[MemoryMap]) – The memory maps.

Returns:

The address of the specified symbol in the specified memory maps.

Return type:

int

Throws:

ValueError: If the specified symbol does not belong to any memory map.

libdebug.utils.debugging_utils.resolve_address_in_maps(address: int, maps: list[MemoryMap]) str[source]#

Returns the symbol corresponding to the specified address in the specified memory maps.

Parameters:
  • address (int) – The address whose symbol should be returned.

  • maps (list[MemoryMap]) – The memory maps.

Returns:

The symbol corresponding to the specified address in the specified memory maps.

Return type:

str

Throws:

ValueError: If the specified address does not belong to any memory map.

libdebug.utils.elf_utils module#

libdebug.utils.elf_utils.resolve_symbol(path: str, symbol: str) int[source]#

Returns the address of the specified symbol in the specified ELF file.

Parameters:
  • path (str) – The path to the ELF file.

  • symbol (str) – The symbol whose address should be returned.

Returns:

The address of the specified symbol in the specified ELF file.

Return type:

int

libdebug.utils.elf_utils.resolve_address(path: str, address: int) str[source]#

Returns the symbol corresponding to the specified address in the specified ELF file.

Parameters:
  • path (str) – The path to the ELF file.

  • address (int) – The address whose symbol should be returned.

Returns:

The symbol corresponding to the specified address in the specified ELF file.

Return type:

str

libdebug.utils.elf_utils.is_pie(path: str) bool[source]#

Returns True if the specified ELF file is position independent, False otherwise.

Parameters:

path (str) – The path to the ELF file.

Returns:

True if the specified ELF file is position independent, False otherwise.

Return type:

bool

libdebug.utils.elf_utils.get_entry_point(path: str) int[source]#

Returns the entry point of the specified ELF file.

Parameters:

path (str) – The path to the ELF file.

Returns:

The entry point of the specified ELF file.

Return type:

int

libdebug.utils.gdb module#

libdebug.utils.libcontext module#

class libdebug.utils.libcontext.LibContext[source]#

Bases: object

A class that holds the global context of the library.

property sym_lvl: int#

Property getter for sym_lvl.

Returns:

the current symbol level.

Return type:

_sym_lvl (int)

property debugger_logger: str#

Property getter for debugger_logger.

Returns:

the current debugger logger level.

Return type:

_debugger_logger (str)

property pipe_logger: str#

Property getter for pipe_logger.

Returns:

the current pipe logger level.

Return type:

_pipe_logger (str)

property general_logger: str#

Property getter for general_logger.

Returns:

the current general logger level.

Return type:

_general_logger (str)

property arch: str#

Property getter for architecture.

Returns:

the current architecture.

Return type:

_arch (str)

property terminal: list[str]#

Property getter for terminal.

Returns:

the current terminal.

Return type:

_terminal (str)

update(**kwargs: ...) None[source]#

Update the context with the given values.

tmp(**kwargs: ...) ...[source]#

Context manager that temporarily changes the library context. Use “with” statement.

libdebug.utils.pipe_manager module#

class libdebug.utils.pipe_manager.PipeManager(stdin_write: int, stdout_read: int, stderr_read: int)[source]#

Bases: object

Class for managing pipes of the child process.

timeout_default: int = 2#
close() None[source]#

Closes all the pipes of the child process.

recv(numb: int | None = None, timeout: int = 2) bytes[source]#

Receives at most numb bytes from the child process stdout.

Parameters:
  • numb (int, optional) – number of bytes to receive. Defaults to None.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received bytes from the child process stdout.

Return type:

bytes

recverr(numb: int | None = None, timeout: int = 2) bytes[source]#

Receives at most numb bytes from the child process stderr.

Parameters:
  • numb (int, optional) – number of bytes to receive. Defaults to None.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received bytes from the child process stderr.

Return type:

bytes

recvuntil(delims: bytes, occurences: int = 1, drop: bool = False, timeout: int = 2) bytes[source]#

Receives data from the child process stdout until the delimiters are found.

Parameters:
  • delims (bytes) – delimiters where to stop.

  • occurences (int, optional) – number of delimiters to find. Defaults to 1.

  • drop (bool, optional) – drop the delimiter. Defaults to False.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received data from the child process stdout.

Return type:

bytes

recverruntil(delims: bytes, occurences: int = 1, drop: bool = False, timeout: int = 2) bytes[source]#

Receives data from the child process stderr until the delimiters are found.

Parameters:
  • delims (bytes) – delimiters where to stop.

  • occurences (int, optional) – number of delimiters to find. Defaults to 1.

  • drop (bool, optional) – drop the delimiter. Defaults to False.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received data from the child process stderr.

Return type:

bytes

recvline(numlines: int = 1, drop: bool = True, timeout: int = 2) bytes[source]#

Receives numlines lines from the child process stdout.

Parameters:
  • numlines (int, optional) – number of lines to receive. Defaults to 1.

  • drop (bool, optional) – drop the line ending. Defaults to True.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received lines from the child process stdout.

Return type:

bytes

recverrline(numlines: int = 1, drop: bool = True, timeout: int = 2) bytes[source]#

Receives numlines lines from the child process stderr.

Parameters:
  • numlines (int, optional) – number of lines to receive. Defaults to 1.

  • drop (bool, optional) – drop the line ending. Defaults to True.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received lines from the child process stdout.

Return type:

bytes

send(data: bytes) int[source]#

Sends data to the child process stdin.

Parameters:

data (bytes) – data to send.

Returns:

number of bytes sent.

Return type:

int

Raises:

RuntimeError – no stdin pipe of the child process.

sendline(data: bytes) int[source]#

Sends data to the child process stdin and append a newline.

Parameters:

data (bytes) – data to send.

Returns:

number of bytes sent.

Return type:

int

sendafter(delims: bytes, data: bytes, occurences: int = 1, drop: bool = False, timeout: int = 2) tuple[bytes, int][source]#

Sends data to the child process stdin after the delimiters are found.

Parameters:
  • delims (bytes) – delimiters where to stop.

  • data (bytes) – data to send.

  • occurences (int, optional) – number of delimiters to find. Defaults to 1.

  • drop (bool, optional) – drop the delimiter. Defaults to False.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received data from the child process stdout. int: number of bytes sent.

Return type:

bytes

sendlineafter(delims: bytes, data: bytes, occurences: int = 1, drop: bool = False, timeout: int = 2) tuple[bytes, int][source]#

Sends line to the child process stdin after the delimiters are found.

Parameters:
  • delims (bytes) – delimiters where to stop.

  • data (bytes) – data to send.

  • occurences (int, optional) – number of delimiters to find. Defaults to 1.

  • drop (bool, optional) – drop the delimiter. Defaults to False.

  • timeout (int, optional) – timeout in seconds. Defaults to timeout_default.

Returns:

received data from the child process stdout. int: number of bytes sent.

Return type:

bytes

libdebug.utils.posix_spawn module#

libdebug.utils.posix_spawn.posix_spawn(file: str, argv: list, env: dict, file_actions: list, setpgroup: bool) int[source]#

Spawn a new process, emulating the POSIX spawn function.

libdebug.utils.print_style module#

class libdebug.utils.print_style.PrintStyle[source]#

Bases: object

Class to define colors for the terminal.

RED = '\x1b[91m'#
BLUE = '\x1b[94m'#
GREEN = '\x1b[92m'#
BRIGHT_YELLOW = '\x1b[93m'#
YELLOW = '\x1b[33m'#
PINK = '\x1b[95m'#
CYAN = '\x1b[96m'#
BOLD = '\x1b[1m'#
UNDERLINE = '\x1b[4m'#
STRIKE = '\x1b[9m'#
DEFAULT_COLOR = '\x1b[39m'#
RESET = '\x1b[0m'#

libdebug.utils.process_utils module#

libdebug.utils.process_utils.get_process_maps(process_id: int) list[MemoryMap][source]#

Returns the memory maps of the specified process.

Parameters:

process_id (int) – The PID of the process whose memory maps should be returned.

Returns:

A list of MemoryMap objects, each representing a memory map of the specified process.

Return type:

list

libdebug.utils.process_utils.get_open_fds(process_id: int) list[int][source]#

Returns the file descriptors of the specified process.

Parameters:

process_id (int) – The PID of the process whose file descriptors should be returned.

Returns:

A list of integers, each representing a file descriptor of the specified process.

Return type:

list

libdebug.utils.process_utils.invalidate_process_cache() None[source]#

Invalidates the cache of the functions in this module. Must be executed any time the process executes code.

libdebug.utils.process_utils.disable_self_aslr() None[source]#

Disables ASLR for the current process.

libdebug.utils.signal_utils module#

libdebug.utils.signal_utils.create_signal_mappings() tuple[dict, dict][source]#

Create mappings between signal names and numbers.

libdebug.utils.signal_utils.resolve_signal_number(name: str) int[source]#

Resolve a signal name to its number.

Parameters:

name (str) – the signal name.

Returns:

the signal number.

Return type:

int

libdebug.utils.signal_utils.resolve_signal_name(number: int) str[source]#

Resolve a signal number to its name.

Parameters:

number (int) – the signal number.

Returns:

the signal name.

Return type:

str

libdebug.utils.signal_utils.get_all_signal_numbers() list[source]#

Get all the signal numbers.

Returns:

the list of signal numbers.

Return type:

list

libdebug.utils.syscall_utils module#

libdebug.utils.syscall_utils.get_remote_definition_url(arch: str) str[source]#

Get the URL of the remote syscall definition file.

libdebug.utils.syscall_utils.fetch_remote_syscall_definition(arch: str) dict[source]#

Fetch the syscall definition file from the remote server.

libdebug.utils.syscall_utils.get_syscall_definitions(arch: str) dict[source]#

Get the syscall definitions for the specified architecture.

libdebug.utils.syscall_utils.resolve_syscall_number(name: str) int[source]#

Resolve a syscall name to its number.

libdebug.utils.syscall_utils.resolve_syscall_name(number: int) str[source]#

Resolve a syscall number to its name.

libdebug.utils.syscall_utils.resolve_syscall_arguments(number: int) list[str][source]#

Resolve a syscall number to its argument definition.

libdebug.utils.syscall_utils.get_all_syscall_numbers() list[int][source]#

Retrieves all the syscall numbers.

Module contents#