Skip to content

Instantly share code, notes, and snippets.

@joshwatson
Last active April 26, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshwatson/47cada73f4fca06fa2f5035b770001c1 to your computer and use it in GitHub Desktop.
Save joshwatson/47cada73f4fca06fa2f5035b770001c1 to your computer and use it in GitHub Desktop.
PDB Loading Plugin for binaryninja
import os
import threading
import pdbparse
from pdbparse.pe import Sections
from pdbparse.omap import Omap
import binaryninja as bn
def load_pdb_thread(bv):
# PDB file is assumed to be named the same as the file opened and be
# located in the same directory as the file.
# TODO: Verifying the PDB matches the GUID in the binary is an
# exercise left to the user.
pdb_path = os.path.splitext(bv.file.filename)[0] + '.pdb'
pdb = pdbparse.parse(pdb_path)
try:
sections = pdb.STREAM_SECT_HDR_ORIG.sections
except AttributeError as e:
sections = pdb.STREAM_SECT_HDR.sections
gsyms = pdb.STREAM_GSYM
for sym in gsyms.globals:
try:
if sym.symtype == 2:
function_addr = (bv.start +
sym.offset +
sections[sym.segment-1].VirtualAddress)
bv.add_function(bv.platform, function_addr)
func = bv.get_function_at(bv.platform, function_addr)
# Demangle and name the function
if func:
demangled_name = bn.demangle_ms(bv.arch, sym.name)[1]
# sometimes the demangled names are a list?
if isinstance(demangled_name, list):
bn.log_info(demangled_name)
demangled_name = demangled_name[0]
func.name = demangled_name
except AttributeError:
pass
bv.update_analysis_and_wait()
def load_pdb(bv):
loader_thread = threading.Thread(target=load_pdb_thread, args=(bv,))
loader_thread.start()
bn.PluginCommand.register(
'Load PDB',
'Load a PDB in the same directory as the binary.',
load_pdb
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment