Esempio n. 1
0
    def render_capa_doc(self, doc):
        """render capa features specified in doc

        @param doc: capa result doc
        """
        # inform model that changes are about to occur
        self.beginResetModel()

        for rule in rutils.capability_rules(doc):
            rule_name = rule["meta"]["name"]
            rule_namespace = rule["meta"].get("namespace")
            parent = CapaExplorerRuleItem(self.root_node,
                                          rule_name, rule_namespace,
                                          len(rule["matches"]), rule["source"])

            for (location, match
                 ) in doc["rules"][rule["meta"]["name"]]["matches"].items():
                if rule["meta"]["scope"] == capa.rules.FILE_SCOPE:
                    parent2 = parent
                elif rule["meta"]["scope"] == capa.rules.FUNCTION_SCOPE:
                    parent2 = CapaExplorerFunctionItem(parent, location)
                elif rule["meta"]["scope"] == capa.rules.BASIC_BLOCK_SCOPE:
                    parent2 = CapaExplorerBlockItem(parent, location)
                else:
                    raise RuntimeError("unexpected rule scope: " +
                                       str(rule["meta"]["scope"]))

                self.render_capa_doc_match(parent2, match, doc)

        # inform model changes have ended
        self.endResetModel()
Esempio n. 2
0
    def render_capa_doc_by_program(self, doc):
        """ """
        for rule in rutils.capability_rules(doc):
            rule_name = rule["meta"]["name"]
            rule_namespace = rule["meta"].get("namespace")
            parent = CapaExplorerRuleItem(
                self.root_node, rule_name, rule_namespace, len(rule["matches"]), rule["source"]
            )

            for (location, match) in doc["rules"][rule["meta"]["name"]]["matches"].items():
                if rule["meta"]["scope"] == capa.rules.FILE_SCOPE:
                    parent2 = parent
                elif rule["meta"]["scope"] == capa.rules.FUNCTION_SCOPE:
                    parent2 = CapaExplorerFunctionItem(parent, location)
                elif rule["meta"]["scope"] == capa.rules.BASIC_BLOCK_SCOPE:
                    parent2 = CapaExplorerBlockItem(parent, location)
                else:
                    raise RuntimeError("unexpected rule scope: " + str(rule["meta"]["scope"]))

                self.render_capa_doc_match(parent2, match, doc)
Esempio n. 3
0
    def render_capa_doc_feature(self,
                                parent,
                                feature,
                                location,
                                doc,
                                display="-"):
        """render capa feature read from doc

        @param parent: parent node to which new child is assigned
        @param feature: feature read from doc
        @param doc: capa feature doc
        @param location: address of feature
        @param display: text to display in plugin UI
        """
        # special handling for characteristic pending type
        if feature["type"] == "characteristic":
            if feature[feature["type"]] in ("embedded pe", ):
                return CapaExplorerByteViewItem(parent, display, location)

            if feature[feature["type"]] in ("loop", "recursive call",
                                            "tight loop"):
                return CapaExplorerFeatureItem(parent, display=display)

            # default to instruction view for all other characteristics
            return CapaExplorerInstructionViewItem(parent, display, location)

        if feature["type"] == "match":
            # display content of rule for all rule matches
            return CapaExplorerRuleMatchItem(parent,
                                             display,
                                             source=doc["rules"].get(
                                                 feature[feature["type"]],
                                                 {}).get("source", ""))

        if feature["type"] == "regex":
            return CapaExplorerFeatureItem(parent,
                                           display,
                                           location,
                                           details=feature["match"])

        if feature["type"] == "basicblock":
            return CapaExplorerBlockItem(parent, location)

        if feature["type"] in (
                "bytes",
                "api",
                "mnemonic",
                "number",
                "offset",
                "number/x32",
                "number/x64",
                "offset/x32",
                "offset/x64",
        ):
            # display instruction preview
            return CapaExplorerInstructionViewItem(parent, display, location)

        if feature["type"] in ("section", ):
            # display byte preview
            return CapaExplorerByteViewItem(parent, display, location)

        if feature["type"] in ("string", ):
            # display string preview
            return CapaExplorerStringViewItem(parent, display, location)

        if feature["type"] in ("import", "export"):
            # display no preview
            return CapaExplorerFeatureItem(parent, display=display)

        raise RuntimeError("unexpected feature type: " + str(feature["type"]))
Esempio n. 4
0
File: model.py Progetto: clayne/capa
    def render_capa_doc_feature(self,
                                parent,
                                feature,
                                location,
                                doc,
                                display="-"):
        """render capa feature read from doc

        @param parent: parent node to which new child is assigned
        @param feature: feature read from doc
        @param doc: capa feature doc
        @param location: address of feature
        @param display: text to display in plugin UI
        """
        # special handling for characteristic pending type
        if feature["type"] == "characteristic":
            if feature[feature["type"]] in ("embedded pe", ):
                return CapaExplorerByteViewItem(parent, display, location)

            if feature[feature["type"]] in ("loop", "recursive call",
                                            "tight loop"):
                return CapaExplorerFeatureItem(parent, display=display)

            # default to instruction view for all other characteristics
            return CapaExplorerInstructionViewItem(parent, display, location)

        if feature["type"] == "match":
            # display content of rule for all rule matches
            return CapaExplorerRuleMatchItem(parent,
                                             display,
                                             source=doc["rules"].get(
                                                 feature[feature["type"]],
                                                 {}).get("source", ""))

        if feature["type"] in ("regex", "substring"):
            for s, locations in feature["matches"].items():
                if location in locations:
                    return CapaExplorerStringViewItem(
                        parent, display, location,
                        '"' + capa.features.common.escape_string(s) + '"')

            # programming error: the given location should always be found in the regex matches
            raise ValueError("regex match at location not found")

        if feature["type"] == "basicblock":
            return CapaExplorerBlockItem(parent, location)

        if feature["type"] in (
                "bytes",
                "api",
                "mnemonic",
                "number",
                "offset",
        ):
            # display instruction preview
            return CapaExplorerInstructionViewItem(parent, display, location)

        if feature["type"] in ("section", ):
            # display byte preview
            return CapaExplorerByteViewItem(parent, display, location)

        if feature["type"] in ("string", ):
            # display string preview
            return CapaExplorerStringViewItem(
                parent, display, location, '"%s"' %
                capa.features.common.escape_string(feature[feature["type"]]))

        if feature["type"] in ("import", "export", "function-name"):
            # display no preview
            return CapaExplorerFeatureItem(parent,
                                           location=location,
                                           display=display)

        if feature["type"] in ("arch", "os", "format"):
            return CapaExplorerFeatureItem(parent, display=display)

        raise RuntimeError("unexpected feature type: " + str(feature["type"]))