Ejemplo n.º 1
0
 def function(self):
     """Function for a flow graph"""
     func = core.BNGetFunctionForFlowGraph(self.handle)
     if func is None:
         return None
     return function.Function(
         binaryview.BinaryView(handle=core.BNGetFunctionData(func)), func)
Ejemplo n.º 2
0
    def basic_block(self):
        """Basic block associated with this part of the flow graph"""
        block = core.BNGetFlowGraphBasicBlock(self.handle)
        if not block:
            return None
        func_handle = core.BNGetBasicBlockFunction(block)
        if not func_handle:
            core.BNFreeBasicBlock(block)
            return None

        view = binaryview.BinaryView(
            handle=core.BNGetFunctionData(func_handle))
        func = function.Function(view, func_handle)

        if core.BNIsLowLevelILBasicBlock(block):
            block = lowlevelil.LowLevelILBasicBlock(
                view, block,
                lowlevelil.LowLevelILFunction(
                    func.arch, core.BNGetBasicBlockLowLevelILFunction(block),
                    func))
        elif core.BNIsMediumLevelILBasicBlock(block):
            block = mediumlevelil.MediumLevelILBasicBlock(
                view, block,
                mediumlevelil.MediumLevelILFunction(
                    func.arch,
                    core.BNGetBasicBlockMediumLevelILFunction(block), func))
        else:
            block = basicblock.BasicBlock(block, view)
        return block
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
	def _medium_level_il_instruction_action(cls, view, func, instr, action):
		try:
			file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
			view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
			owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
			func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
			action(view_obj, func_obj[instr])
		except:
			binaryninja.log.log_error(traceback.format_exc())
Ejemplo n.º 5
0
	def _show_graph_report(self, ctxt, view, title, graph):
		try:
			if view:
				view = binaryview.BinaryView(handle = core.BNNewViewReference(view))
			else:
				view = None
			self.show_graph_report(view, title, flowgraph.CoreFlowGraph(core.BNNewFlowGraphReference(graph)))
		except:
			log.log_error(traceback.format_exc())
Ejemplo n.º 6
0
	def _show_html_report(self, ctxt, view, title, contents, plaintext):
		try:
			if view:
				view = binaryview.BinaryView(handle = core.BNNewViewReference(view))
			else:
				view = None
			self.show_html_report(view, title, contents, plaintext)
		except:
			log.log_error(traceback.format_exc())
	def _recognize_medium_level_il(self, ctxt, data, func, il):
		try:
			file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(data))
			view = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(data))
			func = function.Function(view, handle = core.BNNewFunctionReference(func))
			il = mediumlevelil.MediumLevelILFunction(func.arch, handle = core.BNNewMediumLevelILFunctionReference(il))
			return self.recognize_medium_level_il(view, func, il)
		except:
			log.log_error(traceback.format_exc())
			return False
Ejemplo n.º 8
0
	def _medium_level_il_function_is_valid(cls, view, func, is_valid):
		try:
			if is_valid is None:
				return True
			file_metadata = filemetadata.FileMetadata(handle = core.BNGetFileForView(view))
			view_obj = binaryview.BinaryView(file_metadata = file_metadata, handle = core.BNNewViewReference(view))
			owner = function.Function(view_obj, core.BNGetMediumLevelILOwnerFunction(func))
			func_obj = binaryninja.mediumlevelil.MediumLevelILFunction(owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner)
			return is_valid(view_obj, func_obj)
		except:
			binaryninja.log.log_error(traceback.format_exc())
			return False
Ejemplo n.º 9
0
	def _get_address_input(self, ctxt, result, prompt, title, view, current_address):
		try:
			if view:
				view = binaryview.BinaryView(handle = core.BNNewViewReference(view))
			else:
				view = None
			value = self.get_address_input(prompt, title, view, current_address)
			if value is None:
				return False
			result[0] = value
			return True
		except:
			log.log_error(traceback.format_exc())
Ejemplo n.º 10
0
 def _get_form_input(self, ctxt, fields, count, title):
     try:
         field_objs = []
         for i in range(0, count):
             if fields[i].type == FormInputFieldType.LabelFormField:
                 field_objs.append(LabelField(fields[i].prompt))
             elif fields[i].type == FormInputFieldType.SeparatorFormField:
                 field_objs.append(SeparatorField())
             elif fields[i].type == FormInputFieldType.TextLineFormField:
                 field_objs.append(TextLineField(fields[i].prompt))
             elif fields[
                     i].type == FormInputFieldType.MultilineTextFormField:
                 field_objs.append(MultilineTextField(fields[i].prompt))
             elif fields[i].type == FormInputFieldType.IntegerFormField:
                 field_objs.append(IntegerField(fields[i].prompt))
             elif fields[i].type == FormInputFieldType.AddressFormField:
                 view = None
                 if fields[i].view:
                     view = binaryview.BinaryView(
                         handle=core.BNNewViewReference(fields[i].view))
                 field_objs.append(
                     AddressField(fields[i].prompt, view,
                                  fields[i].currentAddress))
             elif fields[i].type == FormInputFieldType.ChoiceFormField:
                 choices = []
                 for j in range(0, fields[i].count):
                     choices.append(fields[i].choices[j])
                 field_objs.append(ChoiceField(fields[i].prompt, choices))
             elif fields[
                     i].type == FormInputFieldType.OpenFileNameFormField:
                 field_objs.append(
                     OpenFileNameField(fields[i].prompt, fields[i].ext))
             elif fields[
                     i].type == FormInputFieldType.SaveFileNameFormField:
                 field_objs.append(
                     SaveFileNameField(fields[i].prompt, fields[i].ext,
                                       fields[i].defaultName))
             elif fields[
                     i].type == FormInputFieldType.DirectoryNameFormField:
                 field_objs.append(
                     DirectoryNameField(fields[i].prompt,
                                        fields[i].defaultName))
             else:
                 field_objs.append(LabelField(fields[i].prompt))
         if not self.get_form_input(field_objs, title):
             return False
         for i in range(0, count):
             field_objs[i]._fill_core_result(fields[i])
         return True
     except:
         log.log_error(traceback.format_exc())
Ejemplo n.º 11
0
 def _is_valid_for_data(self, ctxt, view, addr, type, context, 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))
         pycontext = []
         for i in range(0, ctxCount):
             pycontext.append(
                 types.Type(core.BNNewTypeReference(context[i])))
         return self.perform_is_valid_for_data(ctxt, view, addr, type,
                                               pycontext)
     except:
         log.log_error(traceback.format_exc())
         return False
Ejemplo n.º 12
0
	def _report_from_index(self, i):
		report_type = core.BNGetReportType(self.handle, i)
		title = core.BNGetReportTitle(self.handle, i)
		view = core.BNGetReportView(self.handle, i)
		if view:
			view = binaryview.BinaryView(handle = view)
		else:
			view = None
		if report_type == ReportType.PlainTextReportType:
			contents = core.BNGetReportContents(self.handle, i)
			return PlainTextReport(title, contents, view)
		elif report_type == ReportType.MarkdownReportType:
			contents = core.BNGetReportContents(self.handle, i)
			plaintext = core.BNGetReportPlainText(self.handle, i)
			return MarkdownReport(title, contents, plaintext, view)
		elif report_type == ReportType.HTMLReportType:
			contents = core.BNGetReportContents(self.handle, i)
			plaintext = core.BNGetReportPlainText(self.handle, i)
			return HTMLReport(title, contents, plaintext, view)
		elif report_type == ReportType.FlowGraphReportType:
			graph = flowgraph.CoreFlowGraph(core.BNGetReportFlowGraph(self.handle, i))
			return FlowGraphReport(title, graph, view)
		raise TypeError("invalid report type %s" % repr(report_type))
Ejemplo n.º 13
0
 def view(self):
     """Binary view for a flow graph"""
     view = core.BNGetViewForFlowGraph(self.handle)
     if view is None:
         return None
     return binaryview.BinaryView(handle=view)