Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions scripts/render-binja-import-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,30 @@ def render_binja_script(result_document: ResultDocument) -> str:


def AppendComment(ea, s):

s = s.encode('ascii')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Removing this encoding is correct as the Binary Ninja API expects strings. However, there is a critical pre-existing bug in this function. The line fn = fnc[0] (line 85) will raise an IndexError if bv.get_functions_containing(addr.address) returns an empty list. This can happen if a code reference points outside of any defined function. You should add a check to handle this case, for example:

fnc = bv.get_functions_containing(addr.address)
if not fnc:
    continue
fn = fnc[0]

refAddrs = []
for ref in bv.get_code_refs(ea):
refAddrs.append(ref)
ea = int(ea)
refAddrs = list(bv.get_code_refs(ea))

if not refAddrs:
fnc = bv.get_functions_containing(ea)
if fnc:
fn = fnc[0]
string = fn.get_comment_at(ea)
if not string:
fn.set_comment_at(ea, s)
elif s not in string:
fn.set_comment_at(ea, string + "\\n" + s)
else:
string = bv.get_comment_at(ea)
if not string:
bv.set_comment_at(ea, s)
elif s not in string:
bv.set_comment_at(ea, string + "\\n" + s)
return

for addr in refAddrs:
fnc = bv.get_functions_containing(addr.address)
if not fnc:
continue
fn = fnc[0]

string = fn.get_comment_at(addr.address)
Expand All @@ -91,29 +107,34 @@ def AppendComment(ea, s):
string = s # no existing comment
else:
if s in string: # ignore duplicates
return
continue
string = string + "\\n" + s

fn.set_comment_at(addr.address, string)

def AppendLvarComment(fva, s):

def AppendLvarComment(fva, s):
# stack var comments are not a thing in Binary Ninja so just add at top of function
# and at location where it's used as an arg
s = s.encode('ascii')
fva = int(fva)
fn = bv.get_function_at(fva)
if not fn:
string = bv.get_comment_at(fva)
if not string:
bv.set_comment_at(fva, s)
elif s not in string:
bv.set_comment_at(fva, string + "\\n" + s)
return

for addr in [fva, pc]:
string = fn.get_comment_at(addr)
string = fn.get_comment_at(fva)

if not string:
string = s
else:
if s in string: # ignore duplicates
return
string = string + "\\n" + s
if not string:
string = s
else:
if s in string: # ignore duplicates
return
string = string + "\\n" + s

fn.set_comment(addr, string)
fn.set_comment_at(fva, string)

print("Annotating %d strings from FLOSS for %s")
%s
Expand Down