Created
March 23, 2019 15:04
-
-
Save AndrewFasano/bd74b19cf6398ebe44364278ab03c82f to your computer and use it in GitHub Desktop.
Ghidra plugin to map all function names in a binary to a list of the basic blocks it contains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Print a mapping of function names to basic blocks in JSON | |
#@author Andrew Fasano & Brendan Dolan-Gavitt | |
#@category CodeAnalysis | |
#@keybinding | |
#@menupath | |
#@toolbar | |
from ghidra.program.model.block import BasicBlockModel | |
import json | |
bbmodel = BasicBlockModel(currentProgram) | |
blockIterator = bbmodel.getCodeBlocks(monitor) | |
block_map = {} | |
def add_block(fn, block): | |
if fn not in block_map.keys(): | |
block_map[fn] = [] | |
block_map[fn].append(str(block)) | |
# For each block, look through the function list until we find a match | |
# This is terribly inefficient (O(N^2)) | |
while blockIterator.hasNext(): | |
cur_block = blockIterator.next().getMinAddress() | |
function = getFirstFunction() | |
found = False | |
# Search functions until we find a match or run out of functions | |
while function is not None: | |
b = function.getBody() | |
if b.contains(cur_block): | |
add_block(function.getName(), cur_block) | |
found=True | |
break | |
# Update function to next and loop again | |
function = getFunctionAfter(function) | |
# Done searching functions. If we never found it, add to unknown list | |
if not found: | |
add_block("_unknown", cur_block) | |
with open("mapping.json", "w") as f: | |
f.write(json.dumps(block_map)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment