Skip to main content

IDA Library Python module

Project description

IDA Library Python Module

⚠️ This is a dev pre-release version. APIs may change without notice and pre-release versions may be deleted at any time.

The IDA Library Python module enables running IDA Pro as an independent Python package outside the IDA UI environment, allowing for programmatic binary analysis and reverse engineering tasks.

Prerequisites

Environment Setup

Set the IDADIR environment variable to point to your IDA installation directory:

Linux/macOS:

export IDADIR="/path/to/your/ida/installation"

Windows:

set IDADIR="C:\Program Files\IDA Pro 9.1"

Example paths:

  • macOS: /Applications/IDA Professional 9.1.app/Contents/MacOS/
  • Windows: C:\Program Files\IDA Pro 9.1\
  • Linux: /opt/ida-9.1/

Note: If you have already installed and configured the idapro Python package in the past using script provided with IDA installation package, setting IDADIR is not required.

Usage example:

#!/usr/bin/env python3
"""
IDA Pro Python Library Usage Example
This example demonstrates how to analyze a binary file using IDA Pro Python library.
"""

import argparse
import importlib.metadata

# You need to import first idapro module
import idapro

# After idapro module was loaded, you can simply import IDA Python modules
import ida_entry
import ida_nalt
import idaapi
import idc
import idautils
import ida_ida
import ida_funcs
import ida_name
import ida_bytes
import ida_typeinf

# Read versions, configured IDA pro kernel and python package
kern_major, kern_minor, kern_build = idapro.get_library_version()
idapro_version = importlib.metadata.version('idapro')
kernel_path = idapro.get_ida_install_dir()

# Parse input arguments
parser = argparse.ArgumentParser(description=f"IDA Library usage example, idapro version: {idapro_version}, "
    f"kernel version: {kern_major}.{kern_minor}.{kern_build}, kernel libraries path: {kernel_path}")
parser.add_argument(
    "-f",
    "--input-file",
    help="Binary input file to be loaded",
    type=str,
    required=True,
    default=None
)
args = parser.parse_args()

# Print banner with components versions
print(f"{parser.description}\n")

# Open database
if idapro.open_database(args.input_file, True) == 0:
    # Extract minimum/maxim addresses
    min_ea = ida_ida.inf_get_min_ea()
    max_ea = ida_ida.inf_get_max_ea()
    print(f"Addresses range: {hex(min_ea)} - {hex(max_ea)}")

    # Print entry point
    entry_point = ida_entry.get_entry_ordinal(0)
    if entry_point != idaapi.BADADDR:
        entry_addr = ida_entry.get_entry(entry_point)
        print(f"Entry point: {hex(entry_addr)}")

    # Print some metadata
    print(f"Metadata:")
    print(f" path: {idaapi.get_input_file_path()}")
    print(f" module: {idaapi.get_root_filename()}")
    print(f" base: {hex(idaapi.get_imagebase())}")
    print(f" filesize: {hex(ida_nalt.retrieve_input_file_size())}")
    print(f" md5: {ida_nalt.retrieve_input_file_md5()}")
    print(f" sha256: {ida_nalt.retrieve_input_file_sha256()}")
    print(f" crc32: {hex(ida_nalt.retrieve_input_file_crc32())}")

    # Iterate functions
    for func_ea in idautils.Functions():
        f = ida_funcs.get_func(func_ea)
        print(f"Function - name {f.name}, start ea {hex(f.start_ea)}, end ea {hex(f.end_ea)}")

    for seg_ea in idautils.Segments():
        name = idc.get_segm_name(seg_ea)
        print(f"Segment - name {name}")

    # Iterate types
    til = ida_typeinf.get_idati()
    if til:
      max_ord = ida_typeinf.get_ordinal_limit(til)
      current_index = -1
      current_named_type = None

      while current_index < max_ord - 1:
          current_index += 1
          tinfo = ida_typeinf.tinfo_t()
          if tinfo.get_numbered_type(til, current_index):
              print(f"Type - id {tinfo.get_tid()}")

      while True:
          if current_named_type is None:
              current_named_type = ida_typeinf.first_named_type(til, ida_typeinf.NTF_TYPE)
          else:
              current_named_type = ida_typeinf.next_named_type(til, current_named_type, ida_typeinf.NTF_TYPE)

          if not current_named_type:
              break

          tinfo = ida_typeinf.tinfo_t()
          if tinfo.get_named_type(til, current_named_type):
              print(f"Type - name {tinfo.get_type_name()}, id {tinfo.get_tid()}")

    # Iterate comments
    for ea in idautils.Heads(min_ea, max_ea):
        cmt = idc.get_cmt(ea, 0)
        if cmt:
            print(f"Comment - value {cmt}")

    # Iterate strings
    for s in idautils.Strings():
        print(f"String - value {s}")

    for ea, name in idautils.Names():
        print(f"Name - value {name}")

    # Iterate basic blocks
    for func_ea in idautils.Functions():
        func = ida_funcs.get_func(func_ea)
        if func:
            fc = idaapi.FlowChart(func)
            for b in fc:
                print(f"Basic block - start ea {hex(b.start_ea)}, end ea {hex(b.end_ea)}")

    # Iterate binary instructions
    for ea in idautils.Heads(min_ea, max_ea):
        if ida_bytes.is_code(ida_bytes.get_flags(ea)):
            disasm = idc.generate_disasm_line(ea, 0)
            print(f"Instruction - ea {hex(ea)}, asm {disasm}")

    # Close database discarding the changes
    idapro.close_database(False)

Type Hints

This package automatically installs type stubs for all IDA Python modules, providing IDE support with autocompletion and type checking for IDA Python modules

Type hints are installed automatically during package installation!

Troubleshooting

Common Issues

ModuleNotFoundError: No module named 'idapro'

  • Ensure IDADIR is set correctly
  • Verify IDA Pro is properly installed

License Issues

  • Ensure you have a valid IDA Pro license
  • Check that IDA can run normally in GUI mode first

Type hints not working

  • Restart your IDE after installation
  • Check that .pyi files exist in your site-packages directory

API Reference

For detailed API documentation, refer to:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

idapro-0.0.7.dev1.tar.gz (967.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

idapro-0.0.7.dev1-py3-none-any.whl (2.0 MB view details)

Uploaded Python 3

File details

Details for the file idapro-0.0.7.dev1.tar.gz.

File metadata

  • Download URL: idapro-0.0.7.dev1.tar.gz
  • Upload date:
  • Size: 967.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for idapro-0.0.7.dev1.tar.gz
Algorithm Hash digest
SHA256 b67ff46eb14b49bfb79e751232672b4a26f83a9533cca130a03abf8dafd6d1e4
MD5 e170a09ae9ee3d0ec8c5a5694ecf7ba1
BLAKE2b-256 483caa7a890533e066743c029b5a1bce8e9335ff62219566d9cf451650030e6c

See more details on using hashes here.

File details

Details for the file idapro-0.0.7.dev1-py3-none-any.whl.

File metadata

  • Download URL: idapro-0.0.7.dev1-py3-none-any.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for idapro-0.0.7.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f774953953049afcf4fbbc0b0cfa26f7ba4ddcf8a4144456c9715dc85ee4b1c
MD5 d394fd9add7d383c7569d883048ac701
BLAKE2b-256 0a26861807324db23b2a32a2ac4716e01e40f648937f7f1ea06e4b42a5459923

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page