IDAPython for IDA 9.1

This commit is contained in:
Arnaud Diederen
2025-03-13 16:17:53 +01:00
parent ae62cd4df5
commit af978e79e6
348 changed files with 62240 additions and 180466 deletions
+18 -17
View File
@@ -26,7 +26,7 @@ import ida_name
import ida_netnode
import ida_segment
import ida_strlist
import ida_struct
import ida_typeinf
import ida_ua
import ida_xref
@@ -310,13 +310,14 @@ def Structs():
"""
Get a list of structures
@return: List of tuples (idx, sid, name)
@return: List of tuples (ordinal, sid, name)
"""
idx = idc.get_first_struc_idx()
while idx != ida_idaapi.BADADDR:
sid = idc.get_struc_by_idx(idx)
yield (idx, sid, idc.get_struc_name(sid))
idx = idc.get_next_struc_idx(idx)
limit = ida_typeinf.get_ordinal_limit()
for ordinal in range(1, limit):
tif = ida_typeinf.tinfo_t()
tif.get_numbered_type(None, ordinal)
if tif.is_udt():
yield (ordinal, tif.get_tid(), tif.get_type_name())
def StructMembers(sid):
@@ -325,21 +326,21 @@ def StructMembers(sid):
@param sid: ID of the structure.
@return: List of tuples (offset, name, size)
@return: List of tuples (offset_in_bytes, name, size_in_bytes)
@note: If 'sid' does not refer to a valid structure,
an exception will be raised.
@note: This will not return 'holes' in structures/stack frames;
it only returns defined structure members.
"""
sptr = ida_struct.get_struc(sid)
if sptr is None:
tif = ida_typeinf.tinfo_t()
if not tif.get_type_by_tid(sid) or not tif.is_udt():
raise Exception("No structure with ID: 0x%x" % sid)
for m in sptr.members:
name = idc.get_member_name(sid, m.soff)
if name:
size = ida_struct.get_member_size(m)
yield (m.soff, name, size)
udt = ida_typeinf.udt_type_data_t()
tif.get_udt_details(udt)
for udm in udt:
if not udm.is_gap():
yield (udm.offset//8, udm.name, udm.size//8)
def DecodePrecedingInstruction(ea):
@@ -738,8 +739,8 @@ class peutils_t(object):
header_offset = property(lambda self: self.__penode.altval(peutils_t.PE_ALT_PEHDR_OFF))
"""Offset of PE header"""
def __str__(self):
return "peutils_t(imagebase=%x, header=%x)" % (self.imagebase, self.header_offset)
def __repr__(self):
return f"{self.__class__.__module__}.{self.__class__.__name__}(imagebase=%x, header=%x)" % (self.imagebase, self.header_offset)
header = lambda self: self.__penode.valobj()
"""Returns the complete PE header as an instance of peheader_t (defined in the SDK)."""
+1004 -729
View File
File diff suppressed because it is too large Load Diff
+54 -23
View File
@@ -35,28 +35,35 @@ if not os.path.isabs(IDAPYTHON_DYNLOAD_BASE):
IDAPYTHON_DYNLOAD_BASE = os.path.abspath(IDAPYTHON_DYNLOAD_BASE);
# Prepare sys.path so loading of the shared objects works
lib_dynload = os.path.join(
IDAPYTHON_DYNLOAD_BASE,
"python",
str(sys.version_info.major))
lib_dynload = os.path.join(IDAPYTHON_DYNLOAD_BASE, "python")
# We always want our own lib-dynload to come first:
# the PyQt (& sip) modules that might have to be loaded, should
# be the ones shipped with IDA and not those possibly available
# on the system.
sys.path.insert(0, os.path.join(lib_dynload, IDAPYTHON_DYNLOAD_RELPATH))
sys.path.insert(0, os.path.join(lib_dynload, "lib-dynload"))
sys.path.insert(0, lib_dynload)
try:
import ida_idaapi
import ida_kernwin
import ida_diskio
except ImportError as e:
print("Import failed: %s. Current sys.path:" % str(e))
for p in sys.path:
print("\t%s" % p)
raise
# We want all ida_* modules to be available
all_mods = "${MODULES}"
for mod in all_mods.split(","):
try:
# Import module and make it visible at global scope
globals()[f"ida_{mod}"] = __import__(f"ida_{mod}")
except ImportError as e:
print("Import failed: %s. Current sys.path:" % str(e))
for p in sys.path:
print("\t%s" % p)
raise
except Exception as e:
print("Cannot load module ida_%s: %s" % (mod, str(e)))
import traceback
traceback.print_exc()
raise
except ModuleNotFoundError as e:
# Silently skip modules not present in current installation
continue
# -----------------------------------------------------------------------
# Take over the standard text outputs
@@ -96,7 +103,7 @@ def runscript(script):
def print_banner():
banner = [
"Python %s " % sys.version,
"IDAPython" + (" 64-bit" if ida_idaapi.__EA64__ else "") + " v%d.%d.%d %s (serial %d) (c) The IDAPython Team <idapython@googlegroups.com>" % IDAPYTHON_VERSION
"IDAPython" + (" 64-bit" if ida_idaapi.__EA64__ else "") + " v%d.%d.%d (c) The IDAPython Team <idapython@googlegroups.com>" % IDAPYTHON_VERSION
]
sepline = '-' * (max([len(s) for s in banner])+1)
@@ -107,9 +114,10 @@ def print_banner():
# -----------------------------------------------------------------------
# Redirect stderr and stdout to the IDA message window
_orig_stdout = sys.stdout;
_orig_stderr = sys.stderr;
sys.stdout = sys.stderr = IDAPythonStdOut()
if IDAPYTHON_OWNING_INTERPRETER:
_orig_stdout = sys.stdout
_orig_stderr = sys.stderr
sys.stdout = sys.stderr = IDAPythonStdOut()
# -----------------------------------------------------------------------
# Initialize the help, with our own stdin wrapper, that'll query the user
@@ -131,7 +139,8 @@ class IDAPythonHelp(pydoc.Helper):
help = IDAPythonHelp()
# Assign a default sys.argv
sys.argv = [""]
if IDAPYTHON_OWNING_INTERPRETER:
sys.argv = [""]
# Have to make sure Python finds our modules
sys.path.append(ida_diskio.idadir("python"))
@@ -147,12 +156,11 @@ if os.getcwd() in sys.path:
if not IDAPYTHON_REMOVE_CWD_SYS_PATH:
sys.path.append(os.getcwd())
# Additional $IDAUSR-derived paths
# Additional IDAUSR-derived paths
if IDAPYTHON_IDAUSR_SYSPATH:
idausr_python_list = ida_diskio.get_ida_subdirs("python")
for idausr_python in idausr_python_list:
one = os.path.join(idausr_python, str(sys.version_info.major))
if one not in sys.path:
for one in idausr_python_list:
if one not in sys.path and os.path.exists(one):
sys.path.append(one)
if IDAPYTHON_COMPAT_AUTOIMPORT_MODULES:
@@ -183,4 +191,27 @@ if sys.version_info.major >= 3:
if sp not in sys.path:
sys.path.append(sp)
# Prepare PySide6 path, if found
def prepare_PySide6_import():
python_name = f"python{sys.version_info[0]}.{sys.version_info[1]}"
pyside_subdir = f"PySide6-{python_name}"
candidate_PySide6_dist = os.path.join(IDAPYTHON_DYNLOAD_BASE, "python", pyside_subdir)
if os.path.isdir(candidate_PySide6_dist):
candidate_PySide6_dir = os.path.join(candidate_PySide6_dist, python_name, "site-packages")
if os.path.isdir(candidate_PySide6_dir):
if "linux" in sys.platform:
import ctypes
try:
# preload libraries
ctypes.cdll.LoadLibrary(os.path.join(candidate_PySide6_dist, "libshiboken6.abi3.so.6.8"))
ctypes.cdll.LoadLibrary(os.path.join(candidate_PySide6_dist, "libpyside6.abi3.so.6.8"))
except:
import traceback
traceback.print_exc()
sys.path.append(candidate_PySide6_dir)
if sys.version_info[:2] >= (3, 9):
prepare_PySide6_import()
# All done, ready to rock.