Skip to content

Commit

Permalink
initial 1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
psifertex committed Apr 7, 2021
1 parent 64f5cc8 commit 720ef16
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ dmypy.json

# Pyre type checker
.pyre/

# BN Plugin helpers that don't need to be commited but are linked in for ease of use
do_release.py
generate_plugininfo.py

22 changes: 4 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
MIT License
Copyright 2021 Josh Watson / Jordan Wiens

Copyright (c) 2021 Jordan
Permission 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:

Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE 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.
THE 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.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# callgraph
Binary Ninja Call Graph plugin
# Callgraph
Author: **Josh Watson / Jordan Wiens**

_Callgraph generator for called or calling funcs_

## Description:

Simple call graph visualizer based on work Josh Watson did on stream in his F-ing Around with Binary Ninja, Episode 2.

Original code: https://github.com/joshwatson/f-ing-around-with-binaryninja/blob/master/ep2-callgraph/callgraph.py

Stream: https://www.twitch.tv/videos/358093527

![](https://github.com/psifertex/callgraph/blob/master/images/callgraph.png?raw=true)

## Minimum Version

This plugin requires the following minimum version of Binary Ninja:

* 1500

## License

This plugin is released under a [MIT license](./LICENSE).
129 changes: 129 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from binaryninja import *
import ctypes

class CallgraphTask(BackgroundTaskThread):
def __init__(self, view, rootfunction = None):
super(CallgraphTask, self).__init__('Generating callgraph...')
self.view = view
self.rootfunction = rootfunction

def run(self):
collect_calls(self.view, self.rootfunction)

def get_or_set_call_node(callgraph, function_nodes, function):
# create a new node if one doesn't exist already
if function not in function_nodes:
node = FlowGraphNode(callgraph)

function_nodes[function] = node

if function.symbol.type == SymbolType.ImportedFunctionSymbol:
token_type = InstructionTextTokenType.ImportToken
else:
token_type = InstructionTextTokenType.CodeSymbolToken

# Set the node's text to be the name of the function
node.lines = [
DisassemblyTextLine(
[
InstructionTextToken(
token_type,
function.name,
function.start
)
]
)
]

callgraph.append(node)
else:
node = function_nodes[function]

return node

def collect_calls(view, rootfunction):
log_info("collect_calls")

# dict containing callee -> set(callers)
calls = {}
if (rootfunction == None):
functions = view.functions
rootlines = ['ROOT']
else:
functions = map(lambda x: x.function, view.get_code_refs(rootfunction.start))
rootlines = [rootfunction.name]

for function in view.functions:
for ref in view.get_code_refs(function.start):
caller = ref.function
calls[function] = calls.get(function, set())

call_il = caller.get_low_level_il_at(ref.address)
if (call_il.operation in (
LowLevelILOperation.LLIL_CALL,
LowLevelILOperation.LLIL_TAILCALL,
LowLevelILOperation.LLIL_CALL_STACK_ADJUST
) and call_il.dest.operation == LowLevelILOperation.LLIL_CONST_PTR):
calls[function].add(caller)

callgraph = FlowGraph()
callgraph.function = view.get_function_at(view.entry_point)
root_node = FlowGraphNode(callgraph)
root_node.lines = rootlines
callgraph.append(root_node)
function_nodes = {}

call_queue = view.functions

while call_queue:
# get the next called function
callee = call_queue.pop()

# create a new node if one doesn't exist already
callee_node = get_or_set_call_node(callgraph, function_nodes, callee)

# create nodes for the callers, and add edges
callers = calls.get(callee, set())

if not callers:
root_node.add_outgoing_edge(
BranchType.FalseBranch, callee_node
)

for caller in callers:
caller_node = get_or_set_call_node(callgraph, function_nodes, caller)

# Add the edge between the caller and the callee
if ctypes.addressof(callee_node.handle.contents) not in [
ctypes.addressof(edge.target.handle.contents)
for edge in caller_node.outgoing_edges]:
caller_node.add_outgoing_edge(
BranchType.TrueBranch,
callee_node
)

callgraph.layout_and_wait()
callgraph.show('Callgraph')


def generate_callgraph(view):
log_info("generate_callgraph")
callgraph_task = CallgraphTask(view)
callgraph_task.start()

def generate_callersgraph(view, function):
log_info("generate_callersgraph")
callgraph_task = CallgraphTask(view, function)
callgraph_task.start()

PluginCommand.register(
'Generate Callgraph',
'Generate a callgraph of the binary',
generate_callgraph
)

PluginCommand.register_for_function(
'Generate Callers graph',
'Generate a graph of all callers to a given function',
generate_callersgraph
)
Binary file added images/callgraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"pluginmetadataversion": 2,
"name": "Callgraph",
"author": "Josh Watson / Jordan Wiens",
"type": [
"ui"
],
"api": [
"python3"
],
"description": "Callgraph generator for called or calling funcs",
"license": {
"name": "MIT",
"text": "Copyright 2021 Josh Watson / Jordan Wiens\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",
"Windows",
"Linux"
],
"installinstructions": {
"Darwin": "",
"Windows": "",
"Linux": ""
},
"version": "1.0",
"minimumbinaryninjaversion": 1500
}

0 comments on commit 720ef16

Please sign in to comment.