def better_clusters(gr):
    gr = nt.reset_trust_graph(gr)
    bl = nr.load_benign("benign50k")
    ml = nr.load_mal("finaldom_list.txt")
    il = nr.load_mal("finalip_list.txt")
    gr = nr.load_benign_graph(bl, gr)
    gr = nr.load_mal_graph(ml, gr)
    gr = nr.load_mal_graph(il, gr)
    f1 = open("benign_neigh2",'w')
    f2 = open("mal_neigh2",'w')
    for n in gr.nodes():
        if gr.node[n]['trust_state'] < 0:
            for e in gr.edge[n]:
                ct1 = 0
                if gr.node[e]['trust_state'] < 0:
                    ct1 += 1
            f2.write("%s, length: %d, mal_number: %d \n" %(n, len(gr.neighbors(n)), ct1))
        elif gr.node[n]['trust_state'] > 0:
            for e in gr.edge[n]:
                ct2 = 0
                if gr.node[e]['trust_state'] > 0:
                    ct2 += 1
            f1.write("%s, length: %d, benign_number: %d \n" %(n, len(gr.neighbors(n)), ct2))
        else:
            pass
    f1.close()
    f2.close()
Exemple #2
0
def better_clusters(gr):
    gr = nt.reset_trust_graph(gr)
    bl = nr.load_benign("benign50k")
    ml = nr.load_mal("finaldom_list.txt")
    il = nr.load_mal("finalip_list.txt")
    gr = nr.load_benign_graph(bl, gr)
    gr = nr.load_mal_graph(ml, gr)
    gr = nr.load_mal_graph(il, gr)
    f1 = open("benign_neigh2", 'w')
    f2 = open("mal_neigh2", 'w')
    for n in gr.nodes():
        if gr.node[n]['trust_state'] < 0:
            for e in gr.edge[n]:
                ct1 = 0
                if gr.node[e]['trust_state'] < 0:
                    ct1 += 1
            f2.write("%s, length: %d, mal_number: %d \n" %
                     (n, len(gr.neighbors(n)), ct1))
        elif gr.node[n]['trust_state'] > 0:
            for e in gr.edge[n]:
                ct2 = 0
                if gr.node[e]['trust_state'] > 0:
                    ct2 += 1
            f1.write("%s, length: %d, benign_number: %d \n" %
                     (n, len(gr.neighbors(n)), ct2))
        else:
            pass
    f1.close()
    f2.close()
def singlehop_clusters(gr,mal,benign): # gr = networkx graph, malfile_name, benignfile_name
    nr.reset_graph_trust(gr)
    ml = nr.load_mal(mal)
    bl = nr.load_benign(benign)
    
    gr = nr.load_mal_graph(ml,gr) # marks nodes with -ve trust
    gr = nr.load_benign_graph(bl,gr) # marks nodes with +ve trust

    tstr = mal+"_clusters"
    tstr2 = benign+"_clusters"
    f1 = open(tstr,'w')
    f2 = open(tstr2,'w')
    ct1 = 0
    ct2 = 0
    for n in gr.nodes():
        ct1 = 0
        ct2 = 0
        if gr.node[n]['trust_state'] < 0: # malicious
            for e in gr.edge[n]:
                if gr.node[e]['trust_state'] < 0 :
                    ct1 += 1
            if ct1>0:
                f1.write("%d %s %s %s %s \n" %(ct1, n,e, gr.node[n]['type'],gr.node[e]['type']))

        elif gr.node[n]['trust_state'] > 0: # benign
            for e in gr.edge[n]:
                ct2 = 0
                if gr.node[e]['trust_state'] > 0 :
                    ct2 += 1
            if ct2 > 0:
                f2.write("%d %s %s %s %s \n" %(ct2, n,e, gr.node[n]['type'],gr.node[e]['type']))
    f1.close()
    f2.close()
def singlehop_clusters(
        gr, mal, benign):  # gr = networkx graph, malfile_name, benignfile_name
    nr.reset_graph_trust(gr)
    ml = nr.load_mal(mal)
    bl = nr.load_benign(benign)

    gr = nr.load_mal_graph(ml, gr)  # marks nodes with -ve trust
    gr = nr.load_benign_graph(bl, gr)  # marks nodes with +ve trust

    tstr = mal + "_clusters"
    tstr2 = benign + "_clusters"
    f1 = open(tstr, 'w')
    f2 = open(tstr2, 'w')
    ct1 = 0
    ct2 = 0
    for n in gr.nodes():
        ct1 = 0
        ct2 = 0
        if gr.node[n]['trust_state'] < 0:  # malicious
            for e in gr.edge[n]:
                if gr.node[e]['trust_state'] < 0:
                    ct1 += 1
            if ct1 > 0:
                f1.write("%d %s %s %s %s \n" %
                         (ct1, n, e, gr.node[n]['type'], gr.node[e]['type']))

        elif gr.node[n]['trust_state'] > 0:  # benign
            for e in gr.edge[n]:
                ct2 = 0
                if gr.node[e]['trust_state'] > 0:
                    ct2 += 1
            if ct2 > 0:
                f2.write("%d %s %s %s %s \n" %
                         (ct2, n, e, gr.node[n]['type'], gr.node[e]['type']))
    f1.close()
    f2.close()