Skip to content

Commit

Permalink
Fixed struct bug from missing types
Browse files Browse the repository at this point in the history
IDA contains types in its typelibs that it doesn't export using
IDAPython API's. If an exception is thrown when defining a struct
members type, I re-type it to be a byte array. Also, I've added
OrderedDict's to structure dictionaries in hope that it prevents types
from being applied out of order.
  • Loading branch information
zznop committed Dec 27, 2019
1 parent 7c30da8 commit 70703ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions binja/binja_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from collections import OrderedDict
from binaryninja import *

"""
Expand Down Expand Up @@ -76,7 +77,7 @@ def open_json_file(self, json_file):
"""

f = open(json_file, 'rb')
return json.load(f)
return json.load(f, object_pairs_hook=OrderedDict)

def import_functions(self, functions, sections):
"""
Expand Down Expand Up @@ -146,7 +147,12 @@ def import_structures(self, structs):
for struct_name, struct_info in structs.items():
struct = types.Structure()
for member_name, member_info in struct_info['members'].items():
typ, _ = self.bv.parse_type_string('{}'.format(member_info['type']))
try:
typ, _ = self.bv.parse_type_string('{}'.format(member_info['type']))
except SyntaxError:
print('Failed to apply type ({}) to member ({}) in structure ({})'.format(
member_info['type'], member_name, struct_name))
typ, _ = self.bv.parse_type_string('uint8_t [{}]'.format(member_info['size']))
struct.insert(int(member_info['offset']), typ, member_name)

self.bv.define_user_type(struct_name, Type.structure_type(struct))
Expand Down
3 changes: 2 additions & 1 deletion ida/ida_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ida_bytes
import idautils
import json
from collections import OrderedDict

"""
Exports analysis data from IDA to a bnida json file
Expand Down Expand Up @@ -209,7 +210,7 @@ def get_structs():
:return: Dict containing structure info
"""

structs = {}
structs = OrderedDict()
for idx, sid, name in idautils.Structs():
struct = ida_struct.get_struc(sid)
structs[name] = {}
Expand Down

0 comments on commit 70703ac

Please sign in to comment.