diff options
| author | Peter LaFosse <peter@vector35.com> | 2017-01-07 16:08:08 -0500 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2017-01-07 16:08:08 -0500 |
| commit | 9d1cac8ee55553cdb20c36deb3b15c9219b02d9b (patch) | |
| tree | 1b3297c8ca4d7b58341095567173b2fff89e1ed2 /python | |
| parent | 27911e9d6625d8f8acf900a4753c1833dd96ffe0 (diff) | |
More fixes for refactor
Diffstat (limited to 'python')
| -rw-r--r-- | python/architecture.py | 2 | ||||
| -rw-r--r-- | python/binaryview.py | 24 | ||||
| -rw-r--r-- | python/examples/angr_plugin.py | 4 | ||||
| -rw-r--r-- | python/examples/breakpoint.py | 4 | ||||
| -rwxr-xr-x | python/examples/export_svg.py | 12 | ||||
| -rw-r--r-- | python/examples/instruction_iterator.py | 33 | ||||
| -rw-r--r-- | python/examples/jump_table.py | 4 | ||||
| -rw-r--r-- | python/examples/nes.py | 13 | ||||
| -rw-r--r-- | python/examples/nsf.py | 5 |
9 files changed, 49 insertions, 52 deletions
diff --git a/python/architecture.py b/python/architecture.py index 5d42ec35..c95c71a0 100644 --- a/python/architecture.py +++ b/python/architecture.py @@ -1143,7 +1143,7 @@ class Architecture(object): return None, 0 result = [] for i in xrange(0, count.value): - token_type = InstructionTextTokenType(tokens[i].type).name + token_type = InstructionTextTokenType(tokens[i].type) text = tokens[i].text value = tokens[i].value size = tokens[i].size diff --git a/python/binaryview.py b/python/binaryview.py index 41702dc8..b19ecbd6 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -1099,7 +1099,7 @@ class BinaryView(object): """ if arch is None: arch = self.arch - txt, size = arch.get_instruction_text(self.read(addr, self.arch.max_instr_length), addr) + txt, size = arch.get_instruction_text(self.read(addr, arch.max_instr_length), addr) self.next_address = addr + size if txt is None: return None @@ -1129,7 +1129,7 @@ class BinaryView(object): arch = self.arch if self.next_address is None: self.next_address = self.entry_point - txt, size = arch.get_instruction_text(self.read(self.next_address, self.arch.max_instr_length), self.next_address) + txt, size = arch.get_instruction_text(self.read(self.next_address, arch.max_instr_length), self.next_address) self.next_address += size if txt is None: return None @@ -1658,6 +1658,8 @@ class BinaryView(object): [<func: x86_64@0x1>] """ + if self.platform is None: + raise Exception("Default platform not set in BinaryView") if plat is None: plat = self.platform core.BNAddFunctionForAnalysis(self.handle, plat.handle, addr) @@ -1673,6 +1675,8 @@ class BinaryView(object): >>> bv.add_entry_point(0xdeadbeef) >>> """ + if self.platform is None: + raise Exception("Default platform not set in BinaryView") if plat is None: plat = self.platform core.BNAddEntryPointForAnalysis(self.handle, plat.handle, addr) @@ -2096,20 +2100,22 @@ class BinaryView(object): """ core.BNDefineAutoSymbol(self.handle, sym.handle) - def define_auto_symbol_and_var_or_function(self, sym, sym_type, platform = None): + def define_auto_symbol_and_var_or_function(self, sym, sym_type, plat=None): """ - ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects. + ``define_auto_symbol_and_var_or_function`` :param Symbol sym: the symbol to define + :param SymbolType sym_type: Type of symbol being defined + :param Platform plat: (optional) platform :rtype: None """ - if platform is None: - platform = self.platform - if platform is not None: - platform = platform.handle + if plat is None: + plat = self.plat + if plat is not None: + plat = plat.handle if sym_type is not None: sym_type = sym_type.handle - core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, platform, sym.handle, sym_type) + core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, plat, sym.handle, sym_type) def undefine_auto_symbol(self, sym): """ diff --git a/python/examples/angr_plugin.py b/python/examples/angr_plugin.py index 9c91d970..90217d65 100644 --- a/python/examples/angr_plugin.py +++ b/python/examples/angr_plugin.py @@ -116,7 +116,7 @@ def find_instr(bv, addr): blocks = bv.get_basic_blocks_at(addr) for block in blocks: block.set_auto_highlight(HighlightColor(HighlightStandardColor.GreenHighlightColor, alpha = 128)) - block.function.set_auto_instr_highlight(block.arch, addr, HighlightStandardColor.GreenHighlightColor) + block.function.set_auto_instr_highlight(addr, HighlightStandardColor.GreenHighlightColor) # Add the instruction to the list associated with the current view bv.session_data.angr_find.add(addr) @@ -127,7 +127,7 @@ def avoid_instr(bv, addr): blocks = bv.get_basic_blocks_at(addr) for block in blocks: block.set_auto_highlight(HighlightColor(HighlightStandardColor.RedHighlightColor, alpha = 128)) - block.function.set_auto_instr_highlight(block.arch, addr, HighlightStandardColor.RedHighlightColor) + block.function.set_auto_instr_highlight(addr, HighlightStandardColor.RedHighlightColor) # Add the instruction to the list associated with the current view bv.session_data.angr_avoid.add(addr) diff --git a/python/examples/breakpoint.py b/python/examples/breakpoint.py index e694329b..b1297e26 100644 --- a/python/examples/breakpoint.py +++ b/python/examples/breakpoint.py @@ -21,7 +21,6 @@ from binaryninja.plugin import PluginCommand from binaryninja.log import log_error -from binaryninja.architecture import Architecture def write_breakpoint(view, start, length): @@ -40,7 +39,8 @@ def write_breakpoint(view, start, length): if view.arch.name not in bkpt_str: log_error("Architecture %s not supported" % view.arch.name) return - bkpt, err = Architecture[view.arch.name].assemble(bkpt_str[view.arch.name]) + + bkpt, err = view.arch.assemble(bkpt_str[view.arch.name]) if bkpt is None: log_error(err) return diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py index bab00f5d..89bc41a1 100755 --- a/python/examples/export_svg.py +++ b/python/examples/export_svg.py @@ -7,7 +7,7 @@ except: from urllib.request import pathname2url # Python 3.x from binaryninja.interaction import get_save_filename_input, show_message_box -from binaryninja.enums import MessageBoxButtonSet +from binaryninja.enums import MessageBoxButtonSet, MessageBoxIcon, MessageBoxButtonResult, InstructionTextTokenType, BranchType from binaryninja.plugin import PluginCommand colors = {'green': [162, 217, 175], 'red': [222, 143, 151], 'blue': [128, 198, 233], 'cyan': [142, 230, 237], 'lightCyan': [176, 221, 228], 'orange': [237, 189, 129], 'yellow': [237, 223, 179], 'magenta': [218, 196, 209], 'none': [74, 74, 74]} @@ -39,15 +39,15 @@ def save_svg(bv, function): output.write(content) output.close() result = show_message_box("Open SVG", "Would you like to view the exported SVG?", - buttons = MessageBoxButtonSet.YesNoButtonSet, icon = MessageBoxButtonSet.QuestionIcon) - if result == MessageBoxButtonSet.YesButton: + buttons = MessageBoxButtonSet.YesNoButtonSet, icon = MessageBoxIcon.QuestionIcon) + if result == MessageBoxButtonResult.YesButton: url = 'file:{}'.format(pathname2url(outputfile)) webbrowser.open(url) def instruction_data_flow(function, address): ''' TODO: Extract data flow information ''' - length = function.view.get_instruction_length(function.arch, address) + length = function.view.get_instruction_length(address) bytes = function.view.read(address, length) hex = bytes.encode('hex') padded = ' '.join([hex[i:i + 2] for i in range(0, len(hex), 2)]) @@ -181,7 +181,7 @@ def render_svg(function): output += '<title>{hover}</title>'.format(hover=hover) for token in line.tokens: # TODO: add hover for hex, function, and reg tokens - output += '<tspan class="{tokentype}">{text}</tspan>'.format(text=escape(token.text), tokentype=token.type) + output += '<tspan class="{tokentype}">{text}</tspan>'.format(text=escape(token.text), tokentype=InstructionTextTokenType(token.type).name) output += '</tspan>\n' output += ' </text>\n' output += ' </g>\n' @@ -197,7 +197,7 @@ def render_svg(function): points += str(x * widthconst) + "," + str(y * heightconst) + " " x, y = edge.points[-1] points += str(x * widthconst) + "," + str(y * heightconst + 0) + " " - edges += ' <polyline class="edge {type}" points="{points}" marker-end="url(#arrow-{type})"/>\n'.format(type=edge.type, points=points) + edges += ' <polyline class="edge {type}" points="{points}" marker-end="url(#arrow-{type})"/>\n'.format(type=BranchType(edge.type).name, points=points) output += ' ' + edges + '\n' output += ' </g>\n' output += '</svg></html>' diff --git a/python/examples/instruction_iterator.py b/python/examples/instruction_iterator.py index 47717aa0..7ff2d692 100644 --- a/python/examples/instruction_iterator.py +++ b/python/examples/instruction_iterator.py @@ -22,21 +22,10 @@ import sys import binaryninja as binja - -if sys.platform.lower().startswith("linux"): - bintype = "ELF" -elif sys.platform.lower() == "darwin": - bintype = "Mach-O" -else: - raise Exception("%s is not supported on this plugin" % sys.platform) - if len(sys.argv) > 1: target = sys.argv[1] -else: - target = "/bin/ls" -bv = binja.BinaryViewType[bintype].open(target) -bv.update_analysis_and_wait() +bv = binja.BinaryViewType.get_view_of_file(target) binja.log_to_stdout(True) binja.log_info("-------- %s --------" % target) binja.log_info("START: 0x%x" % bv.start) @@ -46,19 +35,19 @@ binja.log_info("\n-------- Function List --------") """ print all the functions, their basic blocks, and their il instructions """ for func in bv.functions: - binja.log_info(repr(func)) - for block in func.low_level_il: - binja.log_info("\t{0}".format(block)) + binja.log_info(repr(func)) + for block in func.low_level_il: + binja.log_info("\t{0}".format(block)) - for insn in block: - binja.log_info("\t\t{0}".format(insn)) + for insn in block: + binja.log_info("\t\t{0}".format(insn)) """ print all the functions, their basic blocks, and their mc instructions """ for func in bv.functions: - binja.log_info(repr(func)) - for block in func: - binja.log_info("\t{0}".format(block)) + binja.log_info(repr(func)) + for block in func: + binja.log_info("\t{0}".format(block)) - for insn in block: - binja.log_info("\t\t{0}".format(insn)) + for insn in block: + binja.log_info("\t\t{0}".format(insn)) diff --git a/python/examples/jump_table.py b/python/examples/jump_table.py index f24eea12..439e2ab6 100644 --- a/python/examples/jump_table.py +++ b/python/examples/jump_table.py @@ -21,7 +21,7 @@ # This plugin will attempt to resolve simple jump tables (an array of code pointers) and add the destinations # as indirect branch targets so that the flow graph reflects the jump table's control flow. from binaryninja.plugin import PluginCommand -from binaryninja.enum import InstructionTextTokenType +from binaryninja.enums import InstructionTextTokenType import struct @@ -50,7 +50,7 @@ def find_jump_table(bv, addr): # Collect the branch targets for any tables referenced by the clicked instruction branches = [] for token in tokens: - if token.type == InstructionTextTokenType.PossibleAddressToken: # Table addresses will be a "possible address" token + 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 i = 0 diff --git a/python/examples/nes.py b/python/examples/nes.py index a1410398..4122cde0 100644 --- a/python/examples/nes.py +++ b/python/examples/nes.py @@ -24,11 +24,11 @@ import os from binaryninja.architecture import Architecture from binaryninja.lowlevelil import LowLevelILLabel, LLIL_TEMP -from binaryninja.function import RegisterInfo, InstructionInfo +from binaryninja.function import RegisterInfo, InstructionInfo, InstructionTextToken from binaryninja.binaryview import BinaryView from binaryninja.types import Symbol from binaryninja.log import log_error -from enums import (BranchType, InstructionTextToken, InstructionTextTokenType, +from binaryninja.enums import (BranchType, InstructionTextTokenType, LowLevelILOperation, LowLevelILFlagCondition, FlagRole, SegmentFlag, SymbolType) InstructionNames = [ @@ -513,6 +513,7 @@ class NESView(BinaryView): def __init__(self, data): BinaryView.__init__(self, parent_view = data, file_metadata = data.file) + self.platform = Architecture['6502'].standalone_platform @classmethod def is_valid_for_data(self, data): @@ -554,9 +555,9 @@ class NESView(BinaryView): self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, nmi, "_nmi")) self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, start, "_start")) self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, irq, "_irq")) - self.add_function(Architecture['6502'].standalone_platform, nmi) - self.add_function(Architecture['6502'].standalone_platform, irq) - self.add_entry_point(Architecture['6502'].standalone_platform, start) + self.add_function(nmi) + self.add_function(irq) + self.add_entry_point(start) # Hardware registers self.define_auto_symbol(Symbol(SymbolType.DataSymbol, 0x2000, "PPUCTRL")) @@ -605,7 +606,7 @@ class NESView(BinaryView): name = sym[1] self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, addr, name)) if addr >= 0x8000: - self.add_function(Architecture['6502'].standalone_platform, addr) + self.add_function(addr) return True except: diff --git a/python/examples/nsf.py b/python/examples/nsf.py index de775d34..b1bac3a8 100644 --- a/python/examples/nsf.py +++ b/python/examples/nsf.py @@ -39,6 +39,7 @@ class NSFView(BinaryView): def __init__(self, data): BinaryView.__init__(self, parent_view=data, file_metadata=data.file) + self.platform = Architecture["6502"].standalone_platform @classmethod def is_valid_for_data(self, data): @@ -94,8 +95,8 @@ class NSFView(BinaryView): self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, self.play_address, "_play")) self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, self.init_address, "_init")) - self.add_entry_point(Architecture['6502'].standalone_platform, self.init_address) - self.add_function(Architecture['6502'].standalone_platform, self.play_address) + self.add_entry_point(self.init_address) + self.add_function(self.play_address) # Hardware registers self.define_auto_symbol(Symbol(SymbolType.DataSymbol, 0x2000, "PPUCTRL")) |
