Skip to content

Commit

Permalink
Convert the script to a BN plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng6 committed Jun 9, 2023
1 parent 6af2c17 commit c13abf6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 71 deletions.
81 changes: 81 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from binaryninja import *


def get_xfg_hashes(bv):
IMAGE_GUARD_FLAG_FID_XFG = 8
xfg_hashes = {}

sym = bv.get_symbol_by_raw_name('__gfids_table')
if not sym:
return

var = bv.get_data_var_at(sym.address)
if not var:
return

for func_table in var.value:
if not isinstance(func_table, dict):
continue

addr = func_table['rvAddr'] + bv.start
if not (func_table['metadata'] & IMAGE_GUARD_FLAG_FID_XFG):
continue

try:
xfg_hash = bv.read_int(addr - 8, 8) & 0xffffffffffffffff
except:
continue

if xfg_hash in xfg_hashes:
xfg_hashes[xfg_hash].append(addr)
else:
xfg_hashes[xfg_hash] = [addr]

return xfg_hashes


def get_xfg_pointer(bv):
sym = bv.get_symbol_by_raw_name('__load_configuration_directory_table')
if not sym:
return

var = bv.get_data_var_at(sym.address)
if not var:
return

if var.value['guardFlags'] == 0:
return

xfg_pointer = var.value['guardXFGDispatchFunctionPointer']
return xfg_pointer


def add_xfg_xref(bv, function):
if bv.view_type != 'PE':
log_warn('xfg only works with PE files')
return

xfg_pointer = get_xfg_pointer(bv)
# log_warn('xfg_pointer: 0x%x' % xfg_pointer)

xfg_hashes = get_xfg_hashes(bv)
# log_warn('xfg_hashes: %s' % xfg_hashes)

bv.begin_undo_actions()
for ref in bv.get_code_refs(xfg_pointer):
value = ref.function.get_reg_value_at(ref.address, 'r10')
if value.type != RegisterValueType.ConstantValue:
continue

val = value.value & 0xffffffffffffffff | 1
if val not in xfg_hashes:
continue

for addr in xfg_hashes[val]:
log_warn('adding xref: 0x%x ==> 0x%x' % (ref.address, addr))
ref.function.add_user_code_ref(ref.address, addr)

bv.commit_undo_actions()


PluginCommand.register_for_address("Add XFG Xref", "Add XFG Xref", add_xfg_xref)
71 changes: 0 additions & 71 deletions add_xfg_xrefs.py

This file was deleted.

39 changes: 39 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"pluginmetadataversion": 2,
"name": "Add XFG Xref",
"type": [
"helper"
],
"api": [
"python3"
],
"description": "Add xref from caller to callee based on XFG hashes",
"longdescription": "",
"license": {
"name": "MIT",
"text": "Copyright (c) <year> <copyright holders>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
},
"platforms": [
"Darwin",
"Linux",
"Windows"
],
"installinstructions": {
"Darwin": "",
"Linux": "",
"Windows": ""
},
"dependencies": {
"pip": [
],
"apt": [
],
"installers": [
],
"other": [
]
},
"version": "1.0.0",
"author": "Xusheng",
"minimumbinaryninjaversion": 3164
}

0 comments on commit c13abf6

Please sign in to comment.