Skip to content

Commit

Permalink
add ida 9.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Sep 8, 2024
1 parent 7a58008 commit 39b7a91
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 50 deletions.
5 changes: 2 additions & 3 deletions fa/commands/find_immediate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ def find_immediate(expression):
if isinstance(expression, str):
expression = eval(expression)

ea, imm = ida_search.find_imm(0, idc.SEARCH_DOWN, expression)
ea, imm = ida_search.find_imm(0, ida_search.SEARCH_DOWN, expression)
while ea != idc.BADADDR:
yield ea
ea, imm = idc.find_imm(ea + 1, idc.SEARCH_DOWN,
expression)
ea, imm = idc.find_imm(ea + 1, ida_search.SEARCH_DOWN, expression)


def run(segments, args, addresses, interpreter=None, **kwargs):
Expand Down
74 changes: 50 additions & 24 deletions fa/fa_types.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
from abc import abstractmethod
from collections import namedtuple


try:
import idc
import idaapi
import ida_auto
import ida_typeinf
import ida_bytes
import ida_enum
import ida_struct

if int(idaapi.get_kernel_version()[0]) < 9:
import ida_struct

IDA_MODULE = True
except ImportError:
pass


def del_struct_members(sid: int, offset1: int, offset2: int) -> None:
tif = ida_typeinf.tinfo_t()
if tif.get_type_by_tid(sid) and tif.is_udt():
udm = ida_typeinf.udm_t()
udm.offset = offset1 * 8
idx1 = tif.find_udm(udm, ida_typeinf.STRMEM_OFFSET)
udm = ida_typeinf.udm_t()
udm.offset = offset2 * 8
idx2 = tif.find_udm(udm, ida_typeinf.STRMEM_OFFSET)
idx1 &= 0xffffffff
idx2 &= 0xffffffff
tif.del_udms(idx1, idx2)


class FaType(object):
def __init__(self, name):
self._name = name
Expand All @@ -23,7 +38,7 @@ def get_name(self):
return self._name

def exists(self):
return -1 != ida_struct.get_struc_id(self._name)
return -1 != idc.get_struc_id(self._name)

@abstractmethod
def update_idb(self):
Expand All @@ -39,15 +54,15 @@ def add_value(self, name, value):
self._values[value] = name

def update_idb(self):
id = ida_enum.get_enum(self._name)
id = idc.get_enum(self._name)
if idc.BADADDR == id:
id = ida_enum.add_enum(idc.BADADDR, self._name, ida_bytes.dec_flag())
id = idc.add_enum(idc.BADADDR, self._name, ida_bytes.dec_flag())

keys = self._values.keys()
sorted(keys)

for k in keys:
ida_enum.add_enum_member(id, self._values[k], k)
idc.add_enum_member(id, self._values[k], k, 0xffffffff)


class FaStruct(FaType):
Expand All @@ -60,25 +75,36 @@ def __init__(self, name):
def add_field(self, name, type_, offset=0xffffffff):
self._fields.append(self.Field(name, type_, offset))

def update_idb(self, delete_existing_members=True):
sid = ida_struct.get_struc_id(self._name)
sptr = ida_struct.get_struc(sid)

if sid == idc.BADADDR:
sid = ida_struct.add_struc(idc.BADADDR, self._name, 0)
def update_idb(self, delete_existing_members: bool = True) -> None:
if int(idaapi.get_kernel_version()[0]) < 9:
sid = ida_struct.get_struc_id(self._name)
sptr = ida_struct.get_struc(sid)

if sid == idc.BADADDR:
sid = ida_struct.add_struc(idc.BADADDR, self._name, 0)
sptr = ida_struct.get_struc(sid)
else:
if delete_existing_members:
ida_struct.del_struc_members(sptr, 0, 0xffffffff)

for f in self._fields:
ida_struct.add_struc_member(sptr, f.name, f.offset, (idc.FF_BYTE | idc.FF_DATA) & 0xFFFFFFFF, None, 1)
member_name = f'{self._name}.{f.name}'
idc.SetType(idaapi.get_member_by_fullname(member_name)[0].id, f.type)
else:
if delete_existing_members:
ida_struct.del_struc_members(sptr, 0, 0xffffffff)

for f in self._fields:
ida_struct.add_struc_member(sptr, f.name, f.offset,
(idc.FF_BYTE | idc.FF_DATA)
& 0xFFFFFFFF,
None, 1)
member_name = "{}.{}".format(self._name, f.name)
idc.SetType(idaapi.get_member_by_fullname(member_name)[0].id,
f.type)
sid = idc.get_struc_id(self._name)

if sid == idc.BADADDR:
sid = idc.add_struc(idc.BADADDR, self._name, 0)
else:
if delete_existing_members:
del_struct_members(sid, 0, 0xffffffff)

for f in self._fields:
idc.add_struc_member(sid, f.name, f.offset, (idc.FF_BYTE | idc.FF_DATA) & 0xFFFFFFFF, 0xFFFFFFFF, 1)
member_name = f'{self._name}.{f.name}'
member_struct_id = idc.get_struc_id(member_name)
idc.SetType(member_struct_id, f.type)

ida_auto.auto_wait()

Expand Down
3 changes: 1 addition & 2 deletions fa/fainterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ def get_module(name, filename):
if not os.path.exists(filename):
raise NotImplementedError("no such filename: {}".format(filename))

if sys.version == '3':
# TODO: support python 3.0-3.4
if sys.version[0] == '3':
import importlib.util
spec = importlib.util.spec_from_file_location(name, filename)
module = importlib.util.module_from_spec(spec)
Expand Down
24 changes: 12 additions & 12 deletions fa/ida_plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from collections import namedtuple
import pkg_resources
import binascii
import os
import re
import subprocess
import sys
import tempfile
import traceback
import binascii
import sys
import re
import os
from collections import namedtuple

import pkg_resources
import rpyc
from rpyc import OneShotServer

Expand All @@ -17,10 +17,10 @@
import click

from ida_kernwin import Form
import ida_ida
import ida_segment
import ida_kernwin
import ida_typeinf
import ida_struct
import ida_bytes
import idautils
import ida_auto
Expand Down Expand Up @@ -172,7 +172,7 @@ def verify_project(self):
try:
super(IdaLoader, self).verify_project()
except IOError as e:
ida_kernwin.warning(e.message)
ida_kernwin.warning(str(e))
raise e

def prompt_save_signature(self):
Expand Down Expand Up @@ -372,8 +372,8 @@ def OnFormChange(self, fid):
structs_buf):
f.write(
'typedef {struct_type} {struct_name} {struct_name};\n'
.format(struct_type=struct_type,
struct_name=struct_name))
.format(struct_type=struct_type,
struct_name=struct_name))

structs_buf = structs_buf.replace('__fastcall', '')
f.write('\n')
Expand All @@ -392,7 +392,7 @@ def set_input(self, input_):
:param input_: doesn't matter
:return: None
"""
self.endianity = '>' if idaapi.get_inf_structure().is_be() else '<'
self.endianity = '>' if ida_ida.inf_is_be() else '<'
self._input = input_
self.reload_segments()

Expand Down Expand Up @@ -680,11 +680,11 @@ class FaService(rpyc.Service):
ida_segment = ida_segment
ida_kernwin = ida_kernwin
ida_typeinf = ida_typeinf
ida_struct = ida_struct
ida_bytes = ida_bytes
idautils = idautils
ida_auto = ida_auto
ida_pro = ida_pro
ida_ida = ida_ida
idaapi = idaapi
idc = idc

Expand Down
19 changes: 10 additions & 9 deletions fa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import inspect
import os
import warnings
from typing import Union, Generator

IDA_MODULE = False

try:
import idc
import ida_struct
import ida_bytes

IDA_MODULE = True
except ImportError:
Expand Down Expand Up @@ -41,18 +42,17 @@ def find_raw(needle, segments=None):
address = segment_ea + offset + extra_offset
yield address

extra_offset += offset+1
data = data[offset+1:]
extra_offset += offset + 1
data = data[offset + 1:]

offset = index_of(needle, data)


def ida_find_all(payload):
ea = idc.find_binary(0, idc.SEARCH_DOWN | idc.SEARCH_REGEX, payload)
def ida_find_all(payload: Union[bytes, bytearray, str]) -> Generator[int, None, None]:
ea = ida_bytes.find_bytes(payload, 0)
while ea != idc.BADADDR:
yield ea
ea = idc.find_binary(ea + 1, idc.SEARCH_DOWN | idc.SEARCH_REGEX,
payload)
ea = ida_bytes.find_bytes(payload, ea + 1)


def read_memory(segments, ea, size):
Expand All @@ -62,7 +62,7 @@ def read_memory(segments, ea, size):
for segment_ea, data in segments.items():
if (ea <= segment_ea + len(data)) and (ea >= segment_ea):
offset = ea - segment_ea
return data[offset:offset+size]
return data[offset:offset + size]


def yield_unique(func):
Expand All @@ -72,6 +72,7 @@ def wrapper(*args, **kwargs):
if i not in results:
yield i
results.add(i)

return wrapper


Expand All @@ -96,7 +97,7 @@ def add_struct_to_idb(name):


def find_or_create_struct(name):
sid = ida_struct.get_struc_id(name)
sid = idc.get_struc_id(name)
if sid == idc.BADADDR:
sid = idc.add_struc(-1, name, 0)
print("added struct \"{0}\", id: {1}".format(name, sid))
Expand Down

0 comments on commit 39b7a91

Please sign in to comment.