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.
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
Recover the string with a script
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)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?
