def prepare_packets(self, conv_type): packet1 = mono_packet.create_ping_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1") packet2 = mono_packet.create_ping_packet(ip_src="192.168.0.6", ip_dst="192.168.0.7") packet3 = mono_packet.create_ping_packet(ip_src="192.168.0.8", ip_dst="192.168.0.9") if (conv_type == 1): #udp packet1 = mono_packet.create_udp_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1", port_src=1028, port_dst=80) packet2 = mono_packet.create_udp_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1", port_src=1029, port_dst=80) packet3 = mono_packet.create_udp_packet(ip_src="192.168.0.8", ip_dst="192.168.0.9", port_src=1028, port_dst=80) if (conv_type == 1): #tcp packet1 = mono_packet.create_tcp_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1", port_src=1028, port_dst=80) packet2 = mono_packet.create_tcp_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1", port_src=1029, port_dst=80) packet3 = mono_packet.create_tcp_packet(ip_src="192.168.0.8", ip_dst="192.168.0.9", port_src=1028, port_dst=80) return (packet1, packet2, packet3)
def test_add_packet(self): print("\n#test packet") db = connect(mc.db_host, mc.db_user_name, mc.db_password, mc.db_db_name) #host, user, password, db print("creating session") id_session = mono_session.add_session( { "name": "test_add_packet", "iface": "wlan0" }, db) print("create dummy ping packet") ip1 = "133.29.243.23" ip2 = "192.168.30.33" packet1 = mono_packet.create_ping_packet(ip_dst=ip1, ip_src=ip2) p_summary1 = mono_packet.packet_summary(packet1) print("insert and retrieve packet from db ") id_packet = mono_packet.packet_into_db(packet1, id_session, db) packet2 = mono_packet.get_scapy_packet_from_id(id_packet, db) p_summary2 = mono_packet.packet_summary(packet2) print(packet1.command()) print(packet2.command()) #TODO solve problem with packet length -> done johnny boy self.assertEqual(bytes(packet1), bytes(packet2)) self.assertEqual(packet2.command(), packet2.command()) print("Removing session") mono_session.remove_session(id_session, db) db.close()
def test_add_conv_ipv4(self): #setup id_session = 0 conv_type = 0 print("\n#test_add_conv") db = connect(mc.db_host, mc.db_user_name, mc.db_password, mc.db_db_name) #host, user, password, db ip1 = "133.219.243.23" ip2 = "192.168.30.33" names = mono_conv.get_table_name_and_id_key(conv_type) id_name = names["id_name"] #step1 test that packets are added to the same conversation if #(p1.ip_src == p2.ip_src and p1.ip_dst == p2.ip_dst) packet1 = mono_packet.create_ping_packet(ip_src=ip2, ip_dst=ip1) print(packet1.summary()) conv1 = mono_conv.get_or_create_conv(packet1, db, conv_type, id_session) print("create conversation 1 ") print(conv1) conv2 = mono_conv.get_or_create_conv(packet1, db, conv_type, id_session) print("get conversation 2 ") print(conv2) self.assertEqual(conv1["id_conversation"], conv2[id_name], "conv id should be equal") self.assertGreaterEqual(conv1[id_name], 1) self.assertGreaterEqual(conv2[id_name], 1) # step2 test that packets are added to the same conversation if # (p1.ip_src == p2.ip_dst and p1.ip_dst == p2.ip_dst) print("swap ip source and destination") src = packet1.getlayer(IP).src packet1.getlayer(IP).src = packet1.getlayer(IP).dst packet1.getlayer(IP).dst = src print(packet1.summary()) print("get conversation 3 ") conv3 = mono_conv.get_or_create_conv(packet1, db, conv_type, id_session) print(conv3) self.assertGreaterEqual(conv3[id_name], 1) self.assertEqual(conv1["id_conversation"], conv3[id_name]) # step3 test that packets are NOT added to the same conversation if # (p1.ip_src == p2.ip_src and p1.ip_dst != p2.ip_dst) packet1.getlayer(IP).dst = "12.12.12.12" conv4 = mono_conv.get_or_create_conv(packet1, db, conv_type, id_session) self.assertGreaterEqual(conv4["id_conversation"], 1) self.assertEqual(conv4[id_name], conv3["id_conversation"] + 1) print("get conversation 4") print(conv4) print("Now removing conversations") mono_conv.remove_conversations(id_session, db, conv_type) db.close()
def test_select_all_packets_session(self): print("\n#test select_all_packets_session") db = connect(mc.db_host, mc.db_user_name, mc.db_password, mc.db_db_name) #host, user, password, db id_session = 0 mono_session.remove_session(id_session, db) print("create first packet and select it") packet1 = mono_packet.create_ping_packet() id_packet1 = mono_packet.packet_into_db(packet1, id_session, db) mono_packet.select_packet(id_packet1, 1, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), True) print("create second packet and select it ") packet2 = mono_packet.create_ping_packet() id_packet2 = mono_packet.packet_into_db(packet2, 0, db) mono_packet.select_packet(id_packet2, True, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), True) print("unselect one packet") mono_packet.select_packet(id_packet2, False, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), False) print("unselect all packets") mono_packet.select_packet(id_packet1, False, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), False) print("select all packets at the same time") mono_packet.set_selected_all_packets_in_session(id_session, True, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), True) print( "unselect all packets at the same time - None of them should be selected" ) mono_packet.set_selected_all_packets_in_session(id_session, False, db) self.assertEqual( mono_packet.are_packets_selected_in_session(id_session, db), False) self.assertEqual(mono_packet.is_packet_selected(id_packet1, db), False) self.assertEqual(mono_packet.is_packet_selected(id_packet2, db), False) #clean mono_session.remove_session(id_session, db)
def test_select_packet(self): print("\n#test select packet") db = connect(mc.db_host, mc.db_user_name, mc.db_password, mc.db_db_name) #host, user, password, db packet1 = mono_packet.create_ping_packet() id_packet = mono_packet.packet_into_db(packet1, 0, db) self.assertEqual(mono_packet.is_packet_selected(id_packet, db), 0) mono_packet.select_packet(id_packet, 1, db) self.assertEqual(mono_packet.is_packet_selected(id_packet, db), 1) mono_packet.select_packet(id_packet, False, db) self.assertEqual(mono_packet.is_packet_selected(id_packet, db), 0) mono_packet.select_packet(id_packet, True, db) self.assertEqual(mono_packet.is_packet_selected(id_packet, db), 1) #clean mono_packet.delete_packet_from_db(id_packet, db) db.close()
def test_add_pc(self): print("\n#test_add_pc") db = connect(mc.db_host, mc.db_user_name, mc.db_password, mc.db_db_name) #host, user, password, db id_session = 0 conv_type = 0 #Fresh start mono_session.remove_session(id_session, db) mono_pc.remove_packetconversations(id_session, db) # create packets packet1 = mono_packet.create_ping_packet(ip_src="192.168.0.33", ip_dst="192.168.0.1") packet2 = mono_packet.create_ping_packet(ip_src="192.168.0.6", ip_dst="192.168.0.7") packet3 = mono_packet.create_ping_packet(ip_src="192.168.0.1", ip_dst="192.168.0.33") # insert packets into db id_packet1 = mono_packet.packet_into_db(packet1, id_session, db) id_packet2 = mono_packet.packet_into_db(packet2, id_session, db) id_packet3 = mono_packet.packet_into_db(packet3, id_session, db) # create conversations (packet 1 and 3 belong to the same conversation) conv1 = mono_conv.get_or_create_conv(packet1, db, conv_type, id_session) conv2 = mono_conv.get_or_create_conv(packet2, db, conv_type, id_session) conv3 = mono_conv.get_or_create_conv(packet3, db, conv_type, id_session) id_conv1 = conv1["id_conversation"] id_conv2 = conv2["id_conversation"] id_conv3 = conv3["id_conversation"] #testing the function add_packetconversation #Add link between packet and conversation id_pc1 = mono_pc.add_packetconversation(id_session, id_packet1, id_conv1, conv_type, db) id_pc2 = mono_pc.add_packetconversation(id_session, id_packet2, id_conv2, conv_type, db) id_pc3 = mono_pc.add_packetconversation(id_session, id_packet3, id_conv3, conv_type, db) #see if db insertion worked pk_from_conv1 = mono_pc.get_packets_from_conversation( id_conv1, conv_type, db) pk_from_conv2 = mono_pc.get_packets_from_conversation( id_conv2, conv_type, db) self.assertEqual(len(pk_from_conv1), 2) self.assertEqual(id_conv1, id_conv3) self.assertEqual(len(pk_from_conv2), 1) ppacket2 = pk_from_conv2[0] self.assertEqual(ppacket2["id_packet"], id_packet2) self.assertEqual(ppacket2["id_conversation"], id_conv2) self.assertEqual(ppacket2["conversation_type"], conv_type) #test remove_packetconversation mono_pc.remove_packetconversation(id_pc2, db) #test get_packets_from_conversation pk_from_conv1 = mono_pc.get_packets_from_conversation( id_conv1, conv_type, db) self.assertEqual(len(pk_from_conv2), 1) #test remove_packetconversations (with an s) #We remove all packetconversations, dictionnaries should be empty mono_pc.remove_packetconversations(id_session, db) pk_from_conv1 = mono_pc.get_packets_from_conversation( id_conv1, conv_type, db) pk_from_conv2 = mono_pc.get_packets_from_conversation( id_conv2, conv_type, db) self.assertEqual(len(pk_from_conv1), 0) self.assertEqual(len(pk_from_conv2), 0) print("Clean up") #delete session mono_session.remove_session(id_session, db) #delete packets mono_packet.delete_packet_from_db(id_packet1, db) mono_packet.delete_packet_from_db(id_packet2, db) mono_packet.delete_packet_from_db(id_packet3, db) db.close()