From e364ccdf018915fcf8d6da36aa9e2906d2d42628 Mon Sep 17 00:00:00 2001 From: netanelc305 Date: Thu, 21 Dec 2023 10:02:48 +0200 Subject: [PATCH 1/2] libvmi: libvmi_cdef: Add `JSON` and `FILE_PATH` config options --- libvmi/libvmi.py | 6 ++++-- libvmi/libvmi_cdef.h | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/libvmi/libvmi.py b/libvmi/libvmi.py index b71c215..6d02da5 100644 --- a/libvmi/libvmi.py +++ b/libvmi/libvmi.py @@ -150,6 +150,8 @@ class VMIConfig(Enum): GLOBAL_FILE_ENTRY = lib.VMI_CONFIG_GLOBAL_FILE_ENTRY STRING = lib.VMI_CONFIG_STRING DICT = lib.VMI_CONFIG_GHASHTABLE + JSON_PATH = lib.VMI_CONFIG_JSON_PATH + FILE_PATH = lib.VMI_CONFIG_FILE_PATH class VMIStatus(Enum): @@ -318,8 +320,8 @@ def __init__(self, domain, init_flags=INIT_DOMAINNAME, init_data=None, # from str to bytes if init_flags & INIT_DOMAINNAME or init_flags & INIT_DOMAINID: domain = domain.encode() - # same for VMI_CONFIG_STRING - if config_mode == VMIConfig.STRING: + # same for VMI_CONFIG_STRING | VMI_CONFIG_FILE_PATH | VMI_CONFIG_JSON_PATH + if config_mode in [VMIConfig.STRING, VMIConfig.FILE_PATH, VMIConfig.JSON_PATH]: config = config.encode() elif config_mode == VMIConfig.DICT: # need to convert config to a GHashTable diff --git a/libvmi/libvmi_cdef.h b/libvmi/libvmi_cdef.h index 0dba2d2..102d693 100644 --- a/libvmi/libvmi_cdef.h +++ b/libvmi/libvmi_cdef.h @@ -122,6 +122,10 @@ typedef enum vmi_config { VMI_CONFIG_STRING, /**< config string provided */ VMI_CONFIG_GHASHTABLE, /**< config GHashTable provided */ + + VMI_CONFIG_JSON_PATH, /**< config in json file at the location provided */ + + VMI_CONFIG_FILE_PATH, /**< config file path provided */ } vmi_config_t; // vmi_mode From 5f854c8b73266d1c3c2046e4d5a6380efc125a9a Mon Sep 17 00:00:00 2001 From: netanelc305 Date: Wed, 27 Dec 2023 12:28:54 +0200 Subject: [PATCH 2/2] libvmi: Add `__str__` method to `Registers` --- libvmi/libvmi.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libvmi/libvmi.py b/libvmi/libvmi.py index 6d02da5..77af1e2 100644 --- a/libvmi/libvmi.py +++ b/libvmi/libvmi.py @@ -139,6 +139,22 @@ def __setitem__(self, index, value): raise_from(RuntimeError('Unknown field {} in regs.x86' .format(index.name.lower())), e) + def __str__(self): + # Heuristic to determine the architecture + if hasattr(self.cffi_regs.x86, 'rip') or hasattr(self.cffi_regs.x86, 'eip'): + regs_to_print = self.cffi_regs.x86 + elif hasattr(self.cffi_regs.arm, 'pc'): + regs_to_print = self.cffi_regs.arm + else: + raise RuntimeError("Unable to determine architecture") + attributes = [] + for attr in dir(regs_to_print): + if not attr.startswith('_'): + value = getattr(regs_to_print, attr) + attributes.append(f"{attr.lower()} = {value:#x}") + + return '\n'.join(attributes) + class VMIMode(Enum): XEN = lib.VMI_XEN