Changelog: bv.write and bv.insert require a bytes object in python3 Architecture.assemble outputs a bytes object in python3, a str in python2 Architecture.assemble will throw a value error if it cannot assemble the given instruction API install script should be run in the version of python you want it installed in Fundamental python changes to be aware of: Unicode-type strings are now just str, consequently anything that came out as a unicode string before (annotations) are now just str. Longs no longer exist. They're just ints.
| -rw-r--r-- | python/examples/bin_info.py | 10 | ||||
| -rw-r--r-- | python/examples/breakpoint.py | 2 | ||||
| -rwxr-xr-x | python/examples/export_svg.py | 6 | ||||
| -rw-r--r-- | python/examples/jump_table.py | 8 | ||||
| -rw-r--r-- | python/examples/nds.py | 5 | ||||
| -rw-r--r-- | python/examples/nes.py | 6 | ||||
| -rw-r--r-- | python/examples/notification_callbacks.py | 85 | ||||
| -rw-r--r-- | python/examples/version_switcher.py | 66 |
diff --git a/python/examples/bin_info.py b/python/examples/bin_info.py index 17a96685..05a72ed0 100644 --- a/python/examples/bin_info.py +++ b/python/examples/bin_info.py @@ -20,11 +20,15 @@ # IN THE SOFTWARE. import sys + import binaryninja.log as log from binaryninja.binaryview import BinaryViewType import binaryninja.interaction as interaction from binaryninja.plugin import PluginCommand +# 2-3 compatibility +from binaryninja import range + def get_bininfo(bv): if bv is None: @@ -48,13 +52,13 @@ def get_bininfo(bv): contents += "| Start | Name |\n" contents += "|------:|:-------|\n" - for i in xrange(min(10, len(bv.functions))): + for i in range(min(10, len(bv.functions))): contents += "| 0x%x | %s |\n" % (bv.functions[i].start, bv.functions[i].symbol.full_name) contents += "### First 10 Strings ###\n" contents += "| Start | Length | String |\n" contents += "|------:|-------:|:-------|\n" - for i in xrange(min(10, len(bv.strings))): + for i in range(min(10, len(bv.strings))): start = bv.strings[i].start length = bv.strings[i].length string = bv.read(start, length) @@ -67,6 +71,6 @@ def display_bininfo(bv): if __name__ == "__main__": - print get_bininfo(None) + print(get_bininfo(None)) else: PluginCommand.register("Binary Info", "Display basic info about the binary", display_bininfo) diff --git a/python/examples/breakpoint.py b/python/examples/breakpoint.py index a2801511..cbcb86d4 100644 --- a/python/examples/breakpoint.py +++ b/python/examples/breakpoint.py @@ -44,7 +44,7 @@ def write_breakpoint(view, start, length): if bkpt is None: log_error(err) return - view.write(start, bkpt * length / len(bkpt)) + view.write(start, bkpt * length // len(bkpt)) PluginCommand.register_for_range("Convert to breakpoint", "Fill region with breakpoint instructions.", write_breakpoint) diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py index 7814c9fd..5bb27824 100755 --- a/python/examples/export_svg.py +++ b/python/examples/export_svg.py @@ -48,15 +48,15 @@ def save_svg(bv, function): def instruction_data_flow(function, address): ''' TODO: Extract data flow information ''' - length = function.view.get_instruction_length(address) - bytes = function.view.read(address, length) + length = binaryninja.function.view.get_instruction_length(address) + bytes = binaryninja.function.view.read(address, length) hex = bytes.encode('hex') padded = ' '.join([hex[i:i + 2] for i in range(0, len(hex), 2)]) return 'Opcode: {bytes}'.format(bytes=padded) def render_svg(function, origname): - graph = function.create_graph() + graph = binaryninja.function.create_graph() graph.layout_and_wait() heightconst = 15 ratio = 0.48 diff --git a/python/examples/jump_table.py b/python/examples/jump_table.py index 419cc188..4ed8dda2 100644 --- a/python/examples/jump_table.py +++ b/python/examples/jump_table.py @@ -43,16 +43,16 @@ def find_jump_table(bv, addr): break jump_addr += info.length if jump_addr >= block.end: - print "Unable to find jump after instruction 0x%x" % addr + print("Unable to find jump after instruction 0x%x" % addr) continue - print "Jump at 0x%x" % jump_addr + print("Jump at 0x%x" % jump_addr) # Collect the branch targets for any tables referenced by the clicked instruction branches = [] for token in tokens: if InstructionTextTokenType(token.type) == InstructionTextTokenType.PossibleAddressToken: # Table addresses will be a "possible address" token tbl = token.value - print "Found possible table at 0x%x" % tbl + print("Found possible table at 0x%x" % tbl) i = 0 while True: # Read the next pointer from the table @@ -66,7 +66,7 @@ def find_jump_table(bv, addr): # If the pointer is within the binary, add it as a destination and continue # to the next entry if (ptr >= bv.start) and (ptr < bv.end): - print "Found destination 0x%x" % ptr + print("Found destination 0x%x" % ptr) branches.append((arch, ptr)) else: # Once a value that is not a pointer is encountered, the jump table is ended diff --git a/python/examples/nds.py b/python/examples/nds.py index 34d8a292..a99303cf 100644 --- a/python/examples/nds.py +++ b/python/examples/nds.py @@ -27,12 +27,15 @@ from binaryninja.log import log_error import struct import traceback +# 2-3 compatibility +from binaryninja import range + def crc16(data): crc = 0xffff for ch in data: crc ^= ord(ch) - for bit in xrange(0, 8): + for bit in range(0, 8): if (crc & 1) == 1: crc = (crc >> 1) ^ 0xa001 else: diff --git a/python/examples/nes.py b/python/examples/nes.py index 00451abd..d3f75cc6 100644 --- a/python/examples/nes.py +++ b/python/examples/nes.py @@ -31,6 +31,10 @@ from binaryninja.log import log_error from binaryninja.enums import (BranchType, InstructionTextTokenType, LowLevelILOperation, LowLevelILFlagCondition, FlagRole, SegmentFlag, SymbolType) +# 2-3 compatibility +from binaryninja import range + + InstructionNames = [ "brk", "ora", None, None, None, "ora", "asl", None, # 0x00 "php", "ora", "asl@", None, None, "ora", "asl", None, # 0x08 @@ -631,7 +635,7 @@ class NESView(BinaryView): banks = [] -for i in xrange(0, 32): +for i in range(0, 32): class NESViewBank(NESView): bank = i name = "NES Bank %X" % i diff --git a/python/examples/notification_callbacks.py b/python/examples/notification_callbacks.py index b7c9cde9..02c74db9 100644 --- a/python/examples/notification_callbacks.py +++ b/python/examples/notification_callbacks.py @@ -1,51 +1,74 @@ -from binaryninja import BinaryDataNotification, PluginCommand, log_info +# Copyright (c) 2015-2017 Vector 35 LLC +# +# 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. + import inspect +from binaryninja import BinaryDataNotification +from binaryninja import PluginCommand + + def reg_notif(view): - demo_notification = DemoNotification(view) - view.register_notification(demo_notification) + demo_notification = DemoNotification(view) + view.register_notification(demo_notification) class DemoNotification(BinaryDataNotification): - def __init__(self, view): - self.view = view + def __init__(self, view): + self.view = view - def data_written(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_written(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def data_inserted(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_inserted(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def data_removed(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_removed(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def function_added(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def function_added(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def function_removed(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def function_removed(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def function_updated(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def function_updated(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def data_var_added(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_var_added(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def data_var_updated(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_var_updated(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def data_var_removed(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def data_var_removed(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def string_found(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def string_found(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def string_removed(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def string_removed(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def type_defined(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def type_defined(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) - def type_undefined(self, *args): - log_info(inspect.stack()[0][3] + str(args)) + def type_undefined(self, *args): + log.log_info(inspect.stack()[0][3] + str(args)) PluginCommand.register("Register Notification", "", reg_notif) diff --git a/python/examples/version_switcher.py b/python/examples/version_switcher.py index 3e1cab40..c990a6c0 100644 --- a/python/examples/version_switcher.py +++ b/python/examples/version_switcher.py @@ -34,15 +34,15 @@ def load_channel(newchannel): global channel global versions if (channel is not None and newchannel == channel.name): - print "Same channel, not updating." + print("Same channel, not updating.") else: try: - print "Loading channel %s" % newchannel + print("Loading channel %s" % newchannel) channel = UpdateChannel[newchannel] - print "Loading versions..." + print("Loading versions...") versions = channel.versions except Exception: - print "%s is not a valid channel name. Defaulting to " % chandefault + print("%s is not a valid channel name. Defaulting to " % chandefault) channel = UpdateChannel[chandefault] @@ -50,12 +50,12 @@ def select(version): done = False date = datetime.datetime.fromtimestamp(version.time).strftime('%c') while not done: - print "Version:\t%s" % version.version - print "Updated:\t%s" % date - print "Notes:\n\n-----\n%s" % version.notes - print "-----" - print "\t1)\tSwitch to version" - print "\t2)\tMain Menu" + print("Version:\t%s" % version.version) + print("Updated:\t%s" % date) + print("Notes:\n\n-----\n%s" % version.notes) + print("-----") + print("\t1)\tSwitch to version") + print("\t2)\tMain Menu") selection = raw_input('Choice: ') if selection.isdigit(): selection = int(selection) @@ -65,44 +65,44 @@ def select(version): done = True elif (selection == 1): if (version.version == channel.latest_version.version): - print "Requesting update to latest version." + print("Requesting update to latest version.") else: - print "Requesting update to prior version." + print("Requesting update to prior version.") if are_auto_updates_enabled(): - print "Disabling automatic updates." + print("Disabling automatic updates.") set_auto_updates_enabled(False) if (version.version == core_version): - print "Already running %s" % version.version + print("Already running %s" % version.version) else: - print "version.version %s" % version.version - print "core_version %s" % core_version - print "Downloading..." - print version.update() - print "Installing..." + print("version.version %s" % version.version) + print("core_version %s" % core_version) + print("Downloading...") + print(version.update()) + print("Installing...") if is_update_installation_pending: #note that the GUI will be launched after update but should still do the upgrade headless install_pending_update() # forward updating won't work without reloading sys.exit() else: - print "Invalid selection" + print("Invalid selection") def list_channels(): done = False - print "\tSelect channel:\n" + print("\tSelect channel:\n") while not done: channel_list = UpdateChannel.list for index, item in enumerate(channel_list): - print "\t%d)\t%s" % (index + 1, item.name) - print "\t%d)\t%s" % (len(channel_list) + 1, "Main Menu") + print("\t%d)\t%s" % (index + 1, item.name)) + print("\t%d)\t%s" % (len(channel_list) + 1, "Main Menu")) selection = raw_input('Choice: ') if selection.isdigit(): selection = int(selection) else: selection = 0 if (selection <= 0 or selection > len(channel_list) + 1): - print "%s is an invalid choice." % selection + print("%s is an invalid choice." % selection) else: done = True if (selection != len(channel_list) + 1): @@ -118,23 +118,23 @@ def main(): done = False load_channel(chandefault) while not done: - print "\n\tBinary Ninja Version Switcher" - print "\t\tCurrent Channel:\t%s" % channel.name - print "\t\tCurrent Version:\t%s" % core_version - print "\t\tAuto-Updates On:\t%s\n" % are_auto_updates_enabled() + print("\n\tBinary Ninja Version Switcher") + print("\t\tCurrent Channel:\t%s" % channel.name) + print("\t\tCurrent Version:\t%s" % core_version) + print("\t\tAuto-Updates On:\t%s\n" % are_auto_updates_enabled()) for index, version in enumerate(versions): date = datetime.datetime.fromtimestamp(version.time).strftime('%c') - print "\t%d)\t%s (%s)" % (index + 1, version.version, date) - print "\t%d)\t%s" % (len(versions) + 1, "Switch Channel") - print "\t%d)\t%s" % (len(versions) + 2, "Toggle Auto Updates") - print "\t%d)\t%s" % (len(versions) + 3, "Exit") + print("\t%d)\t%s (%s)" % (index + 1, version.version, date)) + print("\t%d)\t%s" % (len(versions) + 1, "Switch Channel")) + print("\t%d)\t%s" % (len(versions) + 2, "Toggle Auto Updates")) + print("\t%d)\t%s" % (len(versions) + 3, "Exit")) selection = raw_input('Choice: ') if selection.isdigit(): selection = int(selection) else: selection = 0 if (selection <= 0 or selection > len(versions) + 3): - print "%d is an invalid choice.\n\n" % selection + print("%d is an invalid choice.\n\n" % selection) else: if (selection == len(versions) + 3): done = True |