From 6812c973c9fa9b4ad642ab81856c05f87bd6fcc4 Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Thu, 27 Jan 2022 22:43:28 -0500 Subject: Format All Files --- python/examples/export_svg.py | 343 ++++++++++++++++++++++-------------------- 1 file changed, 177 insertions(+), 166 deletions(-) (limited to 'python/examples/export_svg.py') diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py index c96873e1..8166b27a 100755 --- a/python/examples/export_svg.py +++ b/python/examples/export_svg.py @@ -12,119 +12,126 @@ from binaryninja.enums import MessageBoxButtonSet, MessageBoxIcon, MessageBoxBut from binaryninja.function import DisassemblySettings 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], - 'disabled': [144, 144, 144]} - -escape_table = { - "'": "'", - ">": ">", - "<": "<", - '"': """, - ' ': " " +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], 'disabled': [144, 144, 144] } +escape_table = {"'": "'", ">": ">", "<": "<", '"': """, ' ': " "} + def escape(toescape): - # handle extended unicode - toescape = toescape.encode('ascii', 'xmlcharrefreplace') - # still escape the basics - if sys.version_info[0] == 3: - return ''.join(escape_table.get(chr(i), chr(i)) for i in toescape) - else: - return ''.join(escape_table.get(i, i) for i in toescape) + # handle extended unicode + toescape = toescape.encode('ascii', 'xmlcharrefreplace') + # still escape the basics + if sys.version_info[0] == 3: + return ''.join(escape_table.get(chr(i), chr(i)) for i in toescape) + else: + return ''.join(escape_table.get(i, i) for i in toescape) def save_svg(bv, function): - sym = bv.get_symbol_at(function.start) - if sym: - offset = sym.name - else: - offset = "%x" % function.start - path = Path(os.path.dirname(bv.file.filename)) - origname = os.path.basename(bv.file.filename) - filename = path / f'binaryninja-{origname}-{offset}.html' + sym = bv.get_symbol_at(function.start) + if sym: + offset = sym.name + else: + offset = "%x" % function.start + path = Path(os.path.dirname(bv.file.filename)) + origname = os.path.basename(bv.file.filename) + filename = path / f'binaryninja-{origname}-{offset}.html' - functionChoice = TextLineField("Blank to accept default") - # TODO: implement linear disassembly settings and output - modeChoices = ["Graph"] - modeChoiceField = ChoiceField("Mode", modeChoices) - if Settings().get_bool('ui.debugMode'): - formChoices = ["Assembly", "Lifted IL", "LLIL", "LLIL SSA", "Mapped Medium", "Mapped Medium SSA", "MLIL", "MLIL SSA", "HLIL", "HLIL SSA"] - formChoiceField = ChoiceField("Form", formChoices) - else: - formChoices = ["Assembly", "LLIL", "MLIL", "HLIL"] - formChoiceField = ChoiceField("Form", formChoices) + functionChoice = TextLineField("Blank to accept default") + # TODO: implement linear disassembly settings and output + modeChoices = ["Graph"] + modeChoiceField = ChoiceField("Mode", modeChoices) + if Settings().get_bool('ui.debugMode'): + formChoices = [ + "Assembly", "Lifted IL", "LLIL", "LLIL SSA", "Mapped Medium", "Mapped Medium SSA", "MLIL", "MLIL SSA", "HLIL", + "HLIL SSA" + ] + formChoiceField = ChoiceField("Form", formChoices) + else: + formChoices = ["Assembly", "LLIL", "MLIL", "HLIL"] + formChoiceField = ChoiceField("Form", formChoices) - showOpcodes = ChoiceField("Show Opcodes", ["Yes", "No"]) - showAddresses = ChoiceField("Show Addresses", ["Yes", "No"]) + showOpcodes = ChoiceField("Show Opcodes", ["Yes", "No"]) + showAddresses = ChoiceField("Show Addresses", ["Yes", "No"]) - saveFileChoices = SaveFileNameField("Output file", 'HTML files (*.html)', str(filename)) - if not get_form_input([f'Current Function: {offset}', functionChoice, formChoiceField, modeChoiceField, showOpcodes, showAddresses, saveFileChoices], "SVG Export") or saveFileChoices.result is None: - return - if saveFileChoices.result == '': - outputfile = filename - else: - outputfile = saveFileChoices.result - content = render_svg(function, offset, modeChoices[modeChoiceField.result], formChoices[formChoiceField.result], showOpcodes.result == 0, showAddresses.result == 0, origname) - output = open(outputfile, 'w') - output.write(content) - output.close() - result = show_message_box("Open SVG", "Would you like to view the exported SVG?", - buttons=MessageBoxButtonSet.YesNoButtonSet, icon=MessageBoxIcon.QuestionIcon) - if result == MessageBoxButtonResult.YesButton: - # might need more testing, latest py3 on windows seems.... broken with these APIs relative to other platforms - if sys.platform == 'win32': - webbrowser.open(outputfile) - else: - webbrowser.open('file://' + str(outputfile)) + saveFileChoices = SaveFileNameField("Output file", 'HTML files (*.html)', str(filename)) + if not get_form_input([ + f'Current Function: {offset}', functionChoice, formChoiceField, modeChoiceField, showOpcodes, showAddresses, + saveFileChoices + ], "SVG Export") or saveFileChoices.result is None: + return + if saveFileChoices.result == '': + outputfile = filename + else: + outputfile = saveFileChoices.result + content = render_svg( + function, offset, modeChoices[modeChoiceField.result], formChoices[formChoiceField.result], showOpcodes.result == 0, + showAddresses.result == 0, origname + ) + output = open(outputfile, 'w') + output.write(content) + output.close() + result = show_message_box( + "Open SVG", "Would you like to view the exported SVG?", buttons=MessageBoxButtonSet.YesNoButtonSet, + icon=MessageBoxIcon.QuestionIcon + ) + if result == MessageBoxButtonResult.YesButton: + # might need more testing, latest py3 on windows seems.... broken with these APIs relative to other platforms + if sys.platform == 'win32': + webbrowser.open(outputfile) + else: + webbrowser.open('file://' + str(outputfile)) def instruction_data_flow(function, address): - # TODO: Extract data flow information - length = function.view.get_instruction_length(address) - func_bytes = function.view.read(address, length) - if sys.version_info[0] == 3: - hex = func_bytes.hex() - else: - hex = func_bytes.encode('hex') - padded = ' '.join([hex[i:i + 2] for i in range(0, len(hex), 2)]) - return 'Opcode: {bytes}'.format(bytes=padded) + # TODO: Extract data flow information + length = function.view.get_instruction_length(address) + func_bytes = function.view.read(address, length) + if sys.version_info[0] == 3: + hex = func_bytes.hex() + else: + hex = func_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, offset, mode, form, showOpcodes, showAddresses, origname): - settings = DisassemblySettings() - if showOpcodes: - settings.set_option(DisassemblyOption.ShowOpcode, True) - if showAddresses: - settings.set_option(DisassemblyOption.ShowAddress, True) - if form == "LLIL": - graph_type = FunctionGraphType.LowLevelILFunctionGraph - elif form == "LLIL SSA": - graph_type = FunctionGraphType.LowLevelILSSAFormFunctionGraph - elif form == "Lifted IL": - graph_type = FunctionGraphType.LiftedILFunctionGraph - elif form == "Mapped Medium": - graph_type = FunctionGraphType.MappedMediumLevelILFunctionGraph - elif form == "Mapped Medium SSA": - graph_type = FunctionGraphType.MappedMediumLevelILSSAFormFunctionGraph - elif form == "MLIL": - graph_type = FunctionGraphType.MediumLevelILFunctionGraph - elif form == "MLIL SSA": - graph_type = FunctionGraphType.MediumLevelILSSAFormFunctionGraph - elif form == "HLIL": - graph_type = FunctionGraphType.HighLevelILFunctionGraph - elif form == "HLIL SSA": - graph_type = FunctionGraphType.HighLevelILSSAFormFunctionGraph - else: - graph_type = FunctionGraphType.NormalFunctionGraph - graph = function.create_graph(graph_type=graph_type, settings=settings) - graph.layout_and_wait() - heightconst = 15 - ratio = 0.48 - widthconst = heightconst * ratio + settings = DisassemblySettings() + if showOpcodes: + settings.set_option(DisassemblyOption.ShowOpcode, True) + if showAddresses: + settings.set_option(DisassemblyOption.ShowAddress, True) + if form == "LLIL": + graph_type = FunctionGraphType.LowLevelILFunctionGraph + elif form == "LLIL SSA": + graph_type = FunctionGraphType.LowLevelILSSAFormFunctionGraph + elif form == "Lifted IL": + graph_type = FunctionGraphType.LiftedILFunctionGraph + elif form == "Mapped Medium": + graph_type = FunctionGraphType.MappedMediumLevelILFunctionGraph + elif form == "Mapped Medium SSA": + graph_type = FunctionGraphType.MappedMediumLevelILSSAFormFunctionGraph + elif form == "MLIL": + graph_type = FunctionGraphType.MediumLevelILFunctionGraph + elif form == "MLIL SSA": + graph_type = FunctionGraphType.MediumLevelILSSAFormFunctionGraph + elif form == "HLIL": + graph_type = FunctionGraphType.HighLevelILFunctionGraph + elif form == "HLIL SSA": + graph_type = FunctionGraphType.HighLevelILSSAFormFunctionGraph + else: + graph_type = FunctionGraphType.NormalFunctionGraph + graph = function.create_graph(graph_type=graph_type, settings=settings) + graph.layout_and_wait() + heightconst = 15 + ratio = 0.48 + widthconst = heightconst * ratio - output = ''' + output = '''