IDAPython Documentation

repository·master·Indexed 23 days ago

https://github.com/idapython/src

A Python scripting interface for the IDA (Interactive Disassembler) SDK used to automate disassembly, analysis, and debugging tasks. This documentation includes API examples categorized by difficulty and functional domain (UI, Disassembly, Decompilation, Debuggers, and Types), best practices for script development, and technical guides on SWIG wrapping and the inject_pydoc.py documentation generation process.

Tokens
15.8K
Snippets
23
Records
148
Agent score
81%

What's inside IDAPython

  1. Overview of IDAPython example categories

    master

    IDAPython examples are organized into several functional domains to help you find specific implementation patterns:

    • User interface: Creating and manipulating UI widgets, using Python Qt bindings, prompting users with forms, and enriching existing IDA widgets.
    • Disassembly: Querying or modifying the disassembly listing, altering analysis, and reacting to IDB changes.
    • Decompilation: Querying the decompiler, manipulating microcode or C-trees, and intervening in decompilation output.
    • Debuggers: Driving debugging sessions and reacting to debugging events.
    • Working with types: Using Type APIs to manage types, such as programmatically creating structures or enums and adding members.
    • Miscellaneous: General purpose examples that do not fit into the specific categories above.
  2. Implement custom data types and printers

    master

    IDA can be extended to support unknown data types through 'custom data types' (defining size/type) and 'custom data formats' (defining how the data is displayed).

    Use ida_bytes.register_data_types_and_formats to register your custom logic. This allows you to control how specific bytes are interpreted and formatted in the UI, leveraging ida_bytes.data_type_t and ida_bytes.data_format_t.

  3. How IDAPython linking works on Apple Silicon Macs

    master

    On Apple Silicon Macs, strict codesigning rules prevent traditional methods of patching libpython load commands in IDAPython modules, as modifying binaries invalidates their signature and causes macOS to kill the process.

    To solve this, IDAPython uses .tbd (text-based stub) files. Instead of linking directly to a specific libpython binary, IDAPython modules link to a .tbd file that defines an install-name pointing to a symlink (e.g., @executable_path/libpython3.link.dylib). This allows users to switch between different Python versions simply by updating the symlink target, without ever modifying the signed IDAPython binaries.

  4. Iterate over function items and addresses

    master

    The ida_funcs.func_t type provides several iterators to traverse the contents of a function.

    Key Iterators:

    • __iter__: The default iterator, iterates over instructions.
    • .data_items: Iterates over data items contained within the function.
    • .head_items: Iterates over 'heads' (addresses containing the start of an instruction or data item).
    • .addresses: Iterates over all addresses within the function (including those in the middle of items).

    Advanced Iterators:

    • func_tail_iterator_t: Iterates over all chunks of the function (including the main chunk).
    • func_parent_iterator_t: Iterates over all parent functions that include the current chunk.
  5. How `inject_pydoc.py` generates IDAPython documentation

    master

    The inject_pydoc.py tool automates the creation of IDAPython documentation by extracting information from the C++ SDK headers and injecting it into the Python documentation. The process follows these stages:

    1. Extraction: The build system runs doxygen against the C++ SDK headers, outputting the extracted information in XML format.
    2. Processing: tools/inject_pydoc.py processes this XML content to identify functions, classes, methods, and variables present in the IDAPython modules.
    3. Parameter Refinement: To avoid misleading documentation, the tool compares the C++ header documentation with the actual SWIG-generated Python prototypes. It removes documentation for C++ parameters that are converted into Python output values (e.g., a C++ pointer parameter that becomes a Python list).
    4. Return Value Refinement: Since SWIG cannot always reliably determine the return type (especially for PyObject * in pywraps/), the tool uses a tracing mechanism to collect actual return types from runtime tests and uses that data to supplement the documentation.
  6. Mark a register as 'spoiled' by a function

    master

    There are two ways to indicate that a function modifies (spoils) specific registers:

    1. Parsing a declaration: Use a string like int _spoils<rsi> main(); with ida_typeinf.tinfo_t and apply it using ida_typeinf.apply_tinfo with the ida_typeinf.TINFO_DEFINITE flag.
    2. Directly modifying the tinfo_t object: Retrieve the function's type info and modify the func_type_data_t directly to include the ida_typeinf.FTI_SPOILED flag for the target register.
  7. Implement merge functionality in a custom plugin

    master

    When building a plugin that requires permanent data storage, you can use two types of data:

    1. Database-wide data: Use idbattr_info_t for options or settings common to the entire database.
    2. Address-specific data: Use merge_node_info_t for data tied to specific addresses.

    To implement merging (similar to IDA Teams), you must handle merge conflicts. When filling chooser columns for conflicts, use the following pattern:

    columns.clear()
    NAME = print_diffpos_name()
    if ui_complex_name():
        columns.add(split NAME by ui_split_char())
    else:
        columns[0] = NAME
    
    if not ui_complex_details():
        columns.add(print_diffpos_details())

    Key methods for diff_source_t to populate UI details:

    • print_diffpos_name()
    • print_diffpos_details()

    Key UI hints from merge_handler_params_t:

    • ui_has_details()
    • ui_complex_details()
    • ui_complex_name()
  8. Create custom actions with icons and tooltips

    master

    For professional UI integration, create custom actions using ida_kernwin.register_action.

    Capabilities:

    • Contextual Availability: Use the ida_kernwin.action_handler_t.update callback to enable/disable the action based on the current context (e.g., which widget is focused).
    • Visuals: Load custom icons with ida_kernwin.load_custom_icon and provide tooltips.
    • Placement: Actions can be attached to menus (attach_action_to_menu), toolbars (attach_action_to_toolbar), or popups (attach_action_to_popup).
    • Context: When triggered, actions receive a 'context' containing relevant information about the current state.
  9. Refining documentation for converted input parameters

    master

    When C++ functions are wrapped for Python, some parameters change behavior (e.g., an out pointer in C++ becomes a return value in Python). To ensure the IDAPython documentation is accurate, inject_pydoc.py uses SWIG-generated prototypes to identify which parameters are actually present in the Python version. It then strips the corresponding documentation for the non-relevant C++ parameters from the Doxygen-extracted XML before injection.

    Example of parameter transformation:

    C++ Header:

    inline void get_registered_actions(qstrvec_t *out)

    Python API: ida_kernwin.get_registered_actions() (returns list(str))

    In this case, the documentation for the out parameter is removed so users don't see instructions for a parameter that no longer exists in the Python signature.

  10. Show tabular data using the Choose class

    master

    To display data in a table format, subclass ida_kernwin.Choose.

    Key Features:

    • Selection Modes: Supports single selection or multiple selection (using Choose.CH_MULTI).
    • Callbacks: Handle events like Choose.ALL_CHANGED or Choose.NOTHING_CHANGED.
    • Column Configuration: Define columns using constants like Choose.CHCOL_HEX or Choose.CHCOL_FNAME.
    • Actions: You can register actions that can be applied specifically to the chooser widget.
  11. React to decompiler events using Hexrays_Hooks

    master

    By subclassing ida_hexrays.Hexrays_Hooks, you can listen to various notifications sent by the decompiler. This allows your plugin to react to changes in the decompiler state, such as function changes, variable changes, or UI interactions.

    Key APIs:

    • ida_hexrays.Hexrays_Hooks
    • ida_hexrays.cfunc_t
    • ida_hexrays.lvar_t
    • ida_hexrays.vdui_t
  12. Override Function Chooser colors using UI_Hooks

    master

    To customize how items appear in the Function window (e.g., coloring functions based on size), override ida_kernwin.UI_Hooks.get_chooser_item_attrs.

    Workflow:

    1. Subclass ida_kernwin.UI_Hooks.
    2. Implement get_chooser_item_attrs to return custom attributes.
    3. Enable the hooks using ida_kernwin.enable_chooser_item_attrs.