Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Emulate Code from an idalib Script

This how-to shows how to run Isolated System Emulation headlessly from an idalib script and read a decrypted stack string from memory.

This how-to uses d77e3ce835fdc84c8f286736309a1130008d39a03a7043ca078ca9ad4ac1f3e5 (xorstr-nopdb.exe), a non-malicious test executable, to demonstrate the feature.

Prerequisites

  • IDA Pro 9.5 or later with the Malware Analysis add-on installed. See Getting Started.

  • HCLI installed, to run the script.

  • idalib set up for your IDA installation. The IDA installer can do this for you.

  • Download the sample to follow along:

archive · 54KB
Open

Recover the string with a script

  1. Save the following script as stack-string-idalib.py. It performs the same steps as Recover a Stack String, using idalib:

    import idapro
    import ida_auto
    import ida_dbg
    import idc
    
    def wait_until_suspended():
        while ida_dbg.get_process_state() != ida_dbg.DSTATE_SUSP:
            ida_dbg.wait_for_next_event(ida_dbg.WFNE_ANY | ida_dbg.WFNE_SUSP, 1)
    
    # Open the database
    idapro.open_database("xorstr-nopdb.exe", True)
    ida_auto.auto_wait()
    
    # Start Isolated System Emulation
    assert idc.eval_idc('start_emulator_with_backend("speakeasy")') == 1
    try:
        # The session stops at the entry point; jump straight to main like 'Set IP' does.
        wait_until_suspended()
        assert ida_dbg.set_reg_val("RIP", 0x140001000) # main
        assert ida_dbg.run_to(0x1400010E0) # puts call
        wait_until_suspended()
        print(f"stopped at {hex(ida_dbg.get_ip_val())}")
    
        buffer_ea = ida_dbg.get_reg_val("RBP") - 0x30
        print("0x%x: %r" % (buffer_ea, idc.get_strlit_contents(buffer_ea, -1, idc.STRTYPE_C).decode()))
    finally:
        ida_dbg.exit_process()
        idapro.close_database(False)
  2. Run the script in IDAPython's environment through HCLI. You see the following output:

    % hcli ida python exec stack-string-idalib.py
    stopped at 0x1400010e0
    0x12fff78: 'Secret encrypted message'

Last updated

Was this helpful?