Skip to content

Commit

Permalink
bn: add IDE support for Binary Ninja plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpan committed Oct 22, 2024
1 parent 16a4211 commit 079c165
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions binary_ninja/jni_helper.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import json
import os
import binaryninja
from typing import List, Dict

from binaryninja import TypeParser, BinaryView
from binaryninja.typeparser import TypeParserResult
from binaryninja.types import StructureMember
from binaryninja.function import Function
from binaryninja.interaction import (
OpenFileNameField,
get_form_input,
show_message_box,
)

def log(msg, *args, **kwargs):
print("[+]", msg, *args, **kwargs)

class JNIHelper:

def __init__(self):
def __init__(self, bv: BinaryView):
self.bv = bv
self.jni_header = ""
self.pr = None
self.pr: TypeParserResult = None

def start(self):
log(f"plugin start, bv={self.bv}")
if not self.init_header():
return
self.apply_signatures()
Expand All @@ -29,8 +35,8 @@ def fix_cpp_symbols(self):
"""
if not self.pr:
return
funcs = []
for fn in bv.functions:
funcs: List[Function] = []
for fn in self.bv.functions:
name = fn.symbol.short_name
if name.startswith("_JNIEnv::"):
funcs.append(fn)
Expand All @@ -43,7 +49,8 @@ def fix_cpp_symbols(self):
if iface.name == 'JNINativeInterface_':
break
for member in iface.type.members[4:]:
sigmap[member.name] = member.type.children[0]
if isinstance(member, StructureMember):
sigmap[member.name] = member.type.children[0]
log("loaded {} JNI interface".format(len(sigmap)))
for fn in funcs:
name = fn.symbol.short_name[9:]
Expand Down Expand Up @@ -72,7 +79,7 @@ def init_header(self):
def parse_source(self, source, name="<source>"):
options = ["-fdeclspec"]
result, errors = TypeParser.default.parse_types_from_source(
source, name, bv.platform,
source, name, self.bv.platform,
options=options
)
if result is None:
Expand All @@ -95,11 +102,11 @@ def apply_signatures(self):
with open(file, 'r') as f:
meta = json.load(f)
jni_ext = self.jni_header + "\n"
func_map = {}
func_map: Dict[str, Function] = {}
for cls, methods in meta["dexInfo"].items():
for method in methods:
mangle = method["mangle"]
found = bv.get_functions_by_name(mangle)
found = self.bv.get_functions_by_name(mangle)
if not found:
continue
func = found[0]
Expand All @@ -116,7 +123,7 @@ def apply_signatures(self):
return
self.pr = pr
for pt in pr.types:
bv.define_user_type(pt.name, pt.type)
self.bv.define_user_type(pt.name, pt.type)
for pf in pr.functions:
if pf.name not in func_map:
continue
Expand All @@ -126,6 +133,5 @@ def apply_signatures(self):
func.reanalyze()


log(f"plugin start, bv={bv}")
jh = JNIHelper()
jh = JNIHelper(bv)
jh.start()

0 comments on commit 079c165

Please sign in to comment.