Skip to content

Instantly share code, notes, and snippets.

@Manouchehri
Forked from sboesen/LICENSE
Created August 21, 2016 01:55
Show Gist options
  • Save Manouchehri/47ec6b11d6d1ccdf004507455106d1d2 to your computer and use it in GitHub Desktop.
Save Manouchehri/47ec6b11d6d1ccdf004507455106d1d2 to your computer and use it in GitHub Desktop.
obj-c binja plugin

Binja Obj-C Plugin

This is a hacky parser, requiring class-dump (unfortunately OS X only). You can also find it here.

Note it takes a moderate amount of time on binaries with large number of functions. For example, Photo Booth.app takes ~24 seconds for me, with 2123 functions (it renames 1355, 63.8%).

Install

  1. Install class-dump, linked above.

  2. Copy the file obj-c.py below to your plugins directory (~/Library/Application Support/Binary Ninja/plugins). Check here for paths on other platforms (but note this requires class-path, OS X only)

  3. Point the class_dump variable on line 6 to the path you installed it to.

License

Feel free to make changes, or comment about bugs. MIT license.

Copyright (c) 2016 Stefan Boesen
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 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.
from binaryninja import *
import os
from collections import defaultdict
from pipes import quote
from datetime import datetime
class_dump = quote('/Users/stefan/Downloads/class-dump')
def log_info(msg):
log(1,"LOG - "+ str(datetime.now()) + " " + msg)
def parse_objc(bv):
log_info("Parsing obj-c metadata")
file = bv.file.filename
command = class_dump + ' -A ' + quote(file) + ' | grep -E "interface|//" | grep -v "@property" | sed -ne \'/@interface/,$p\' | less'
parsed_functions = defaultdict(list)
last_interface = None
replacements = os.popen(command).read()
for line in replacements.split("\n"):
if line is None:
continue
interface_pos = line.find("@interface")
colon_pos = line.find(":")
comment_pos = line.find("//")
method_pos = line.find("(")
if interface_pos != -1:
last_interface = line[11:colon_pos]
if comment_pos > 0 and last_interface is not None:
# We have a method for an interface
method_type_pos = line.find("+")
if method_type_pos != -1:
method_type = "+"
else:
method_type = "-"
method_name = line[method_pos:comment_pos].rstrip()
method_name = method_name[:-1] + ":" # replace semicolon with colon
addr_pos = line.find("0x")
address = line[addr_pos:]
method_signature = method_type + '[' + last_interface + method_name + ']'
parsed_functions[address] = method_signature
log_info("Done parsing class-dump output. Replacing functions...")
functions_replaced = 0
for binja_func in bv.functions:
for method_addr, method_signature in parsed_functions.iteritems():
address = int(method_addr, 16)
binja_addr = binja_func.symbol.address
if binja_addr == address:
# print "Replacing binja_func! " + binja_func.name + " -> " + method_signature
functions_replaced += 1
binja_func.name = method_signature
log_info("Done replacing functions.")
log_info(str(functions_replaced) + " functions replaced!")
PluginCommand.register("Parse Obj-c", "parse obj-c metadata", parse_objc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment