def lines(self, lines): if isinstance(lines, str): lines = lines.split('\n') line_buf = (core.BNDisassemblyTextLine * len(lines))() for i in range(0, len(lines)): line = lines[i] if isinstance(line, str): line = function.DisassemblyTextLine([function.InstructionTextToken(InstructionTextTokenType.TextToken, line)]) if not isinstance(line, function.DisassemblyTextLine): line = function.DisassemblyTextLine(line) if line.address is None: if len(line.tokens) > 0: line_buf[i].addr = line.tokens[0].address else: line_buf[i].addr = 0 else: line_buf[i].addr = line.address if line.il_instruction is not None: line_buf[i].instrIndex = line.il_instruction.instr_index else: line_buf[i].instrIndex = 0xffffffffffffffff color = line.highlight if not isinstance(color, HighlightStandardColor) and not isinstance(color, highlight.HighlightColor): raise ValueError("Specified color is not one of HighlightStandardColor, highlight.HighlightColor") if isinstance(color, HighlightStandardColor): color = highlight.HighlightColor(color) line_buf[i].highlight = color._get_core_struct() line_buf[i].count = len(line.tokens) line_buf[i].tokens = function.InstructionTextToken.get_instruction_lines(line.tokens) core.BNSetFlowGraphNodeLines(self.handle, line_buf, len(lines))
def _get_lines_for_data(self, ctxt, view, addr, type, prefix, prefixCount, width, count, typeCtx, ctxCount): try: file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view = binaryview.BinaryView(file_metadata=file_metadata, handle=core.BNNewViewReference(view)) type = types.Type(handle=core.BNNewTypeReference(type)) prefixTokens = function.InstructionTextToken.get_instruction_lines( prefix, prefixCount) pycontext = [] for i in range(ctxCount): pycontext.append( TypeContext( types.Type(core.BNNewTypeReference(typeCtx[i].type)), typeCtx[i].offset)) result = self.perform_get_lines_for_data(ctxt, view, addr, type, prefixTokens, width, pycontext) count[0] = len(result) line_buf = (core.BNDisassemblyTextLine * len(result))() for i in range(len(result)): line = result[i] color = line.highlight if not isinstance( color, enums.HighlightStandardColor) and not isinstance( color, highlight.HighlightColor): raise ValueError( "Specified color is not one of HighlightStandardColor, highlight.HighlightColor" ) if isinstance(color, enums.HighlightStandardColor): color = highlight.HighlightColor(color) line_buf[i].highlight = color._get_core_struct() if line.address is None: if len(line.tokens) > 0: line_buf[i].addr = line.tokens[0].address else: line_buf[i].addr = 0 else: line_buf[i].addr = line.address if line.il_instruction is not None: line_buf[i].instrIndex = line.il_instruction.instr_index else: line_buf[i].instrIndex = 0xffffffffffffffff line_buf[i].count = len(line.tokens) line_buf[ i].tokens = function.InstructionTextToken.get_instruction_lines( line.tokens) return ctypes.cast(line_buf, ctypes.c_void_p).value except: log.log_error(traceback.format_exc()) return None
def highlight(self, color): if not isinstance(color, HighlightStandardColor) and not isinstance( color, highlight.HighlightColor): raise ValueError( "Specified color is not one of HighlightStandardColor, highlight.HighlightColor" ) if isinstance(color, HighlightStandardColor): color = highlight.HighlightColor(color) core.BNSetFlowGraphNodeHighlight(self.handle, color._get_core_struct())
def setcolor(self, address, color='0xff0000'): """ SetColor(int addr [, int color]) => None Set the location pointed by `address` with `color`. Example: binaryninja SetColor 0x40000 0xff0000 """ addr = int(address, 0) color = int(color, 0) R, G, B = (color >> 16) & 0xff, (color >> 8) & 0xff, (color & 0xff) color = highlight.HighlightColor(red=R, blue=G, green=B) return hl(self.view, addr, color)
def find_instruction_overlapping(bv): print("=" * 80) print("Instruction Overlapping") # set of addresses seen = {} functions_with_overlapping = set() # walk over all functions for function in bv.functions: # walk over all instructions for instruction in function.instructions: # parse address address = instruction[-1] # seen for the first time if address not in seen: # mark as instruction beginning seen[address] = 1 # seen before and not marked as instruction beginning elif seen[address] == 0: functions_with_overlapping.add(function.start) function.set_user_instr_highlight( address, highlight.HighlightColor(red=0xff, blue=0xff, green=0)) # walk over instruction length and mark bytes as seen for _ in range(1, bv.get_instruction_length(address)): address += 1 # if seen before and marked as instruction beginning if address in seen and seen[address] == 1: functions_with_overlapping.add(function.start) function.set_user_instr_highlight( address, highlight.HighlightColor(red=0xff, blue=0xff, green=0)) else: seen[address] = 0 for address in sorted(functions_with_overlapping): print( f"Overlapping instructions in function {hex(address)} ({bv.get_function_at(address).name}).")
def set_auto_highlight(self, color): """ ``set_auto_highlight`` highlights the current BasicBlock with the supplied color. ..warning:: Use only in analysis plugins. Do not use in regular plugins, as colors won't be saved to the database. :param HighlightStandardColor or highlight.HighlightColor color: Color value to use for highlighting """ if not isinstance(color, HighlightStandardColor) and not isinstance(color, highlight.HighlightColor): raise ValueError("Specified color is not one of HighlightStandardColor, highlight.HighlightColor") if isinstance(color, HighlightStandardColor): color = highlight.HighlightColor(color) core.BNSetAutoBasicBlockHighlight(self.handle, color._get_core_struct())
def set_user_highlight(self, color): """ ``set_user_highlight`` highlights the current BasicBlock with the supplied color :param HighlightStandardColor or highlight.HighlightColor color: Color value to use for highlighting :Example: >>> current_basic_block.set_user_highlight(highlight.HighlightColor(red=0xff, blue=0xff, green=0)) >>> current_basic_block.set_user_highlight(HighlightStandardColor.BlueHighlightColor) """ if not isinstance(color, HighlightStandardColor) and not isinstance(color, highlight.HighlightColor): raise ValueError("Specified color is not one of HighlightStandardColor, highlight.HighlightColor") if isinstance(color, HighlightStandardColor): color = highlight.HighlightColor(color) core.BNSetUserBasicBlockHighlight(self.handle, color._get_core_struct())