def test__generate_features_twotuplebi(): ''' ''' n_features = 41 ip_src = '172.16.0.5' ip_dst = '192.168.50.1' key = (ip_src, ip_dst) afg = AnubisFG(bidirectional=True) # Tuple that is not on the memory. empty = afg._generate_features_twotuplebi(key) assert empty == [0] * n_features # Duration 0 capture = rdpcap('tests/data/test_100_rows.pcap') # Second packet is a SYN TCP packet. packet = capture[1] timestamp = datetime(2018, 12, 1, 13, 17, 11, 183810) afg._update_twotuplebi(packet) expected = [ # fwd 1, # qt_pkt 1, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 1, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 1, # frq_pkt 63, # avg_ttl # bck 0, # qt_pkt 0, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 0, # qt_prtcl 0, # qt_src_prt 0, # qt_dst_prt 0, # qt_fin_fl 0, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 0, # avg_hdr_len 0, # avg_pkt_len 0, # frq_pkt 0, # avg_ttl # non-directional 0, # tm_dur_s ] ftrs = afg._generate_features_twotuplebi(key) assert ftrs == expected # Inverted expected = [ # bck 0, # qt_pkt 0, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 0, # qt_prtcl 0, # qt_src_prt 0, # qt_dst_prt 0, # qt_fin_fl 0, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 0, # avg_hdr_len 0, # avg_pkt_len 0, # frq_pkt 0, # avg_ttl # fwd 1, # qt_pkt 1, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 1, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 1, # frq_pkt 63, # avg_ttl # non-directional 0, # tm_dur_s ] ftrs = afg._generate_features_twotuplebi((key[1], key[0])) assert ftrs == expected # Duration > 0 # Updating # Third packet is another SYN TCP packet with same IPs and Ports packet = capture[2] afg._update_twotuplebi(packet) new_timestamp = datetime(2018, 12, 1, 13, 17, 11, 183813) dur = (new_timestamp - timestamp).total_seconds() expected = [ 2, # qt_pkt 2, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 2, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 2 / dur, # frq_pkt 63, # avg_ttl # bck 0, # qt_pkt 0, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 0, # qt_prtcl 0, # qt_src_prt 0, # qt_dst_prt 0, # qt_fin_fl 0, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 0, # avg_hdr_len 0, # avg_pkt_len 0, # frq_pkt 0, # avg_ttl # non-directional dur, # tm_dur_s ] ftrs = afg._generate_features_twotuplebi(key) assert ftrs == expected # Using now datetime. new_timestamp = datetime.now() dur = (new_timestamp - timestamp).total_seconds() expected = [ 2, # qt_pkt 2, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 2, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 2 / dur, # frq_pkt 63, # avg_ttl # bck 0, # qt_pkt 0, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 0, # qt_prtcl 0, # qt_src_prt 0, # qt_dst_prt 0, # qt_fin_fl 0, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 0, # avg_hdr_len 0, # avg_pkt_len 0, # frq_pkt 0, # avg_ttl # non-directional dur, # tm_dur_s ] ftrs = afg._generate_features_twotuplebi(key, now=True) assert np.isclose(ftrs, expected).all() # Backward features # Updating # Fourth packet is a SYN ACK response TCP packet with inverted IPs and packet = capture[3] afg._update_twotuplebi(packet) new_timestamp = datetime(2018, 12, 1, 13, 17, 11, 183932) dur = (new_timestamp - timestamp).total_seconds() expected = [ 2, # qt_pkt 2, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 2, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 0, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 2 / dur, # frq_pkt 63, # avg_ttl # bck 1, # qt_pkt 1, # qt_pkt_tcp 0, # qt_pkt_udp 0, # qt_pkt_icmp 0, # qt_pkt_ip 1, # qt_prtcl 1, # qt_src_prt 1, # qt_dst_prt 0, # qt_fin_fl 1, # qt_syn_fl 0, # qt_res_fl 0, # qt_psh_fl 1, # qt_ack_fl 0, # qt_urg_fl 0, # qt_ecn_fl 0, # qt_cwr_fl 20, # avg_hdr_len 74, # avg_pkt_len 1 / dur, # frq_pkt 64, # avg_ttl # non-directional dur, # tm_dur_s ] ftrs = afg._generate_features_twotuplebi(key) # breakpoint() assert np.isclose(ftrs, expected).all() # Zero forward packets on existing flow ip_src_1 = '192.168.0.1' ip_dst_1 = '192.168.0.2' key_1 = (ip_src_1, ip_dst_1) t2_1 = TwoTupleBidirectionalNode() memory_twotup_1 = {key_1: t2_1} afg_1 = AnubisFG(bidirectional=True, memory_twotup=memory_twotup_1) ftrs = afg_1._generate_features_twotuplebi(key_1) assert ftrs == [0] * n_features
def test_generate_features(): capture = rdpcap('tests/data/test_100_rows.pcap') # Flow to be tested. packet = capture[1] ip_src = packet[IP].src ip_dst = packet[IP].dst src_port = packet[TCP].sport dst_port = packet[TCP].dport protocol = packet[IP].proto # Will be tested considering all possible sets of attributes. for bidir in [True, False]: for onlytwo in [True, False]: for onlyfive in set([not onlytwo, False]): afg_1 = AnubisFG(bidirectional=bidir, only_twotuple=onlytwo, only_fivetuple=onlyfive) # Will be tested along 3 updates. for i in range(1, 4): packet = capture[i] afg_1.update(packet) if bidir: if onlytwo: key = (ip_src, ip_dst) assert afg_1.generate_features( key) == afg_1._generate_features_twotuplebi( key) elif onlyfive: key = (ip_src, src_port, ip_dst, dst_port, protocol) assert afg_1.generate_features( key) == afg_1._generate_features_fivetuplebi( key) else: key2 = (ip_src, ip_dst) key5 = (ip_src, src_port, ip_dst, dst_port, protocol) ftrs_1 = afg_1.generate_features(key5) ftrs_2 = afg_1._generate_features_twotuplebi( key2) + afg_1._generate_features_fivetuplebi( key5) assert ftrs_1 == ftrs_2 else: if onlytwo: key = (ip_src, ip_dst) assert afg_1.generate_features( key) == afg_1._generate_features_twotupleuni( key) elif onlyfive: key = (ip_src, src_port, ip_dst, dst_port, protocol) assert afg_1.generate_features( key) == afg_1._generate_features_fivetupleuni( key) else: key2 = (ip_src, ip_dst) key5 = (ip_src, src_port, ip_dst, dst_port, protocol) ftrs_1 = afg_1.generate_features(key5) ftrs_2 = afg_1._generate_features_twotupleuni( key2) + afg_1._generate_features_fivetupleuni( key5) assert ftrs_1 == ftrs_2