zoukankan      html  css  js  c++  java
  • 图算法检测僵尸网络

    # -*- coding: utf-8 -*-
    import networkx as nx
    import matplotlib.pyplot as plt
    
    
    iplist={}
    goodiplist={}
    #相似度
    N=0.5
    #黑客团伙IP最少个数
    M=3
    #黑客IP攻击目标最小个数
    R=2
    
    #jarccard系数
    def get_len(d1,d2):
        ds1=set()
        for d in d1.keys():
            ds1.add(d)
    
        ds2=set()
        for d in d2.keys():
            ds2.add(d)
        return len(ds1&ds2)/len(ds1|ds2)
    
    filename="../data/etl-ip-domain-train.txt"
    G=nx.Graph()
    
    with open(filename) as f:
        for line in f:
            (ip,domain)=line.split("	")
            if not ip=="0.0.0.0":
                if not iplist.has_key(ip):
                    iplist[ip]={}
    
                iplist[ip][domain]=1
    
    for ip in iplist.keys():
        if len(iplist[ip]) >= R:
            goodiplist[ip]=1
    
    for ip1 in iplist.keys():
        for ip2 in iplist.keys():
            if not ip1 == ip2 :
                weight=get_len(iplist[ip1],iplist[ip2])
                if (weight >= N) and (ip1 in goodiplist.keys()) and (ip2 in goodiplist.keys()):
                    #点不存在会自动添加
                    G.add_edge(ip1,ip2,weight=weight)
    
    
    
    
    n_sub_graphs=nx.number_connected_components(G)
    sub_graphs=nx.connected_component_subgraphs(G)
    
    
    for i,sub_graph in enumerate(sub_graphs):
        n_nodes=len(sub_graph.nodes())
        if n_nodes >= M:
            print("Subgraph {0} has {1} nodes {2}".format(i,n_nodes,sub_graph.nodes()))
    
    
    nx.draw(G)
    plt.show()
    
  • 相关阅读:
    背景透明,文字不透明
    判断数组类型
    前端工作流程自动化——Grunt/Gulp 自动化
    tools安装
    总结
    CSS Hack
    getBoundingClientRect()兼容性处理
    Math.random获得随机数
    spring RestTemplate 工程导入
    系统架构演变
  • 原文地址:https://www.cnblogs.com/bonelee/p/9382885.html
Copyright © 2011-2022 走看看