Пример #1
0
        def gammaIso(G1, G2):

            nm = isomorphism.categorical_node_match(['fillcolor', 'shape'],
                                                    ['', ''])
            em = isomorphism.categorical_multiedge_match('color', '')

            #G1.remove_nodes_from(nx.isolates(G1)) #Drop Isolates
            #G2.remove_nodes_from(nx.isolates(G2)) #Drop Isolates

            #Create set of induced subgraphs for the Graphs and check for isomorphism.
            len_G1_nodes = len(G1.nodes())
            len_G2_nodes = len(G2.nodes())

            min_len = min(len_G1_nodes, len_G2_nodes)
            max_len = max(len_G1_nodes, len_G2_nodes)

            G1_list = createAllComboList(G1.nodes(), min_len)
            G2_list = createAllComboList(G2.nodes(), min_len)

            induced_subgraph_G1 = [
                G1.subgraph(node_list) for node_list in G1_list
            ]
            induced_subgraph_G2 = [
                G2.subgraph(node_list) for node_list in G2_list
            ]

            new_induced_subgraph_G1 = []
            new_induced_subgraph_G2 = []

            #Remove graphs with empty nodes and empty edges
            for g in induced_subgraph_G1:
                if (len(g.nodes()) == 0 or len(g.edges()) == 0):
                    continue
                new_induced_subgraph_G1.append(g)

            for g in induced_subgraph_G2:
                if (len(g.nodes()) == 0 or len(g.edges()) == 0):
                    continue
                new_induced_subgraph_G2.append(g)

            induced_subgraph_G1_rev = new_induced_subgraph_G1[::-1]
            induced_subgraph_G2_rev = new_induced_subgraph_G2[::-1]

            similarity = 0

            for g1 in induced_subgraph_G1_rev:
                for g2 in induced_subgraph_G2_rev:
                    if (len(g1.nodes()) == len(g2.nodes())
                            and len(g1.edges()) == len(g2.edges())):
                        GM = isomorphism.MultiDiGraphMatcher(g1, g2, nm, em)
                        if (GM.is_isomorphic()):
                            similarity = max(similarity,
                                             len(g1.nodes()) / float(max_len))
                            return similarity, GM.mapping
            return 0, {}
Пример #2
0
def match(pattern: type_base.SubgraphData,
          graph: nx.MultiDiGraph) -> typing.Iterable[typing.Mapping]:
    """
    :param graph: Graph to identify and replace isomorphisms (in place)
    :return bool indicating whether or not match was successful
    """

    mapping = isomorphism.MultiDiGraphMatcher(graph,
                                              pattern.graph,
                                              node_match=_node_match,
                                              edge_match=_edge_match)
    return mapping.subgraph_isomorphisms_iter()
Пример #3
0
def build_matcher(graph: nx.MultiDiGraph, nodes: list, edges: list, node_attrs: list = None,
                         edge_attrs: list = None):
    if node_attrs is not None or edge_attrs is not None:
        log.warning('\'edge_attrs\' or `\'node_attrs\'` parameter was passed to function \'find_pattern_matches\', '
                    'but they are not used anymore. Pattern matching proceeds according to \'nodes\' and \'edges\' '
                    'parameters. Please avoid passing \'edge_attrs\' and \'node_attrs\' parameters to any pattern '
                    'matching function like \'find_pattern_matches\', \'apply_pattern\' and \'pattern\' because it '
                    'will be deprecated in the next release.')

    subgraph = nx.MultiDiGraph(name='pattern')
    subgraph.add_nodes_from(nodes)
    subgraph.add_edges_from(edges)
    return ism.MultiDiGraphMatcher(graph, subgraph, node_match, edge_match)
                elements_2 = row[1].split(",")
                type = elements_2[len(elements_2) - 1]
                if 0 <= c:
                    if c > 0:
                        if c % (d + 1) != 0:
                            client_ip = elements_1[2]
                            server_ip = elements_1[4]
                            client_port = elements_1[3]
                            server_port = elements_1[5]
                            un_graph.add_node(client_ip)
                            un_graph.add_node(server_ip)
                            un_graph.add_edge(client_ip + client_port,
                                              server_ip + server_port)
                        else:
                            labels.append(type)
                            m0 = isomorphism.MultiDiGraphMatcher(
                                samples[0], un_graph)
                            m1 = isomorphism.MultiDiGraphMatcher(
                                samples[1], un_graph)
                            if m0.is_isomorphic() and not m1.is_isomorphic():
                                classification.append("LDAP")
                            elif not m0.is_isomorphic() and m1.is_isomorphic():
                                classification.append("NetBIOS")
                            else:
                                classification.append("BENIGN")
                            un_graph = nx.MultiDiGraph()

                c = c + 1
            print(c)
            # print(classification)
            print(classification_report(labels, classification))
            reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
            c = 0  # c counts for each edge
            un_graph = nx.MultiDiGraph()
            for row in reader:
                elements_1 = row[0].split(",")
                elements_2 = row[1].split(",")
                type = elements_2[len(elements_2) - 1]
                if 0 <= c:
                    if c > 0:
                        if c % (d + 1) != 0:
                            client_ip = elements_1[2]
                            server_ip = elements_1[4]
                            client_port = elements_1[3]
                            server_port = elements_1[5]
                            un_graph.add_node(client_ip)
                            un_graph.add_node(server_ip)
                            un_graph.add_edge(client_ip + client_port,
                                              server_ip + server_port)
                        else:
                            for s in samples:
                                m = isomorphism.MultiDiGraphMatcher(
                                    s, un_graph)
                                if m.is_isomorphic():
                                    # print(str(m))
                                    classification[samples.index(s)] += 1
                            un_graph = nx.MultiDiGraph()

                c = c + 1
            print(c)
            print(classification)