def test_ICMP_parse(self):
     icmp_path = os.path.join(self.path, "icmp_packets",
                              "icmp_packet_1.pcap")
     with open(icmp_path, "rb") \
             as icmp_packet:
         pcap = Pcap()
         for packet in pcap.get_packets_from_pcap(icmp_packet):
             parsed_icmp_packet = NetworkSniffer().parse_packet(packet.data)
         self.assertEqual(str(parsed_icmp_packet.icmp_type), "8")
 def test_UDP_parse(self):
     udp_path = os.path.join(self.path, "udp_packets", "udp_packet_1.pcap")
     with open(udp_path, "rb") \
             as udp_packet:
         pcap = Pcap()
         for packet in pcap.get_packets_from_pcap(udp_packet):
             parsed_tcp_packet = NetworkSniffer().parse_packet(packet.data)
         self.assertEqual(str(parsed_tcp_packet.src), "127.0.0.1")
         self.assertEqual(str(parsed_tcp_packet.src_port), "40747")
 def test_TCP_parse(self):
     tcp_path = os.path.join(self.path, "tcp_packets", "tcp_packet_1.pcap")
     with open(tcp_path, "rb") \
             as tcp_packet:
         pcap = Pcap()
         for packet in pcap.get_packets_from_pcap(tcp_packet):
             parsed_tcp_packet = NetworkSniffer().parse_packet(packet.data)
         self.assertEqual(str(parsed_tcp_packet.src), "23.35.144.14")
         self.assertEqual(str(parsed_tcp_packet.src_port), "443")
Exemple #4
0
 def run_condition(self, condition, boolFunc):
     with tempfile.NamedTemporaryFile(dir=".", delete=False) as temp:
         self.name = "." + os.path.basename(temp.name)
         self.delete_list.append(self.name)
         self.path = "./test/saved_packets/saved_packets.pcap"
         run(['-r', self.path, '-c', condition, 'pcap', '-n', self.name])
     with open(self.name, "rb") as tmp:
         pcap = Pcap()
         for packet in NetworkSniffer(pcap.get_packets_from_pcap(tmp)). \
                 get_packets(math.inf):
             self.assertEqual(True, boolFunc(packet[0]))
Exemple #5
0
def run(argv):
    if os.name == 'nt':
        raise IncorrectOsError("Error : windows unsupported")
    parser = parse_args(argv)
    with parsing_arguments(parser) as arguments:
        output = arguments[OUTPUT_INDEX]
        maxpackets = arguments[MAXPACKETS_INDEX]
        settings_argument = arguments[SETTINGS_INDEX]
        sniffer_argument = arguments[SNIFFER_INDEX]
        packetSource = arguments[PACKET_SOURCE_INDEX]
        network_writer = NetworkWriter(settings_argument)
        is_pcap_write = False
        if output is not sys.stdout:
            is_pcap_write = True
        packet_generator = None
        network_sniffer = None
        if packetSource != PacketSource.network:
            packets_from_pcap = Pcap()
            packet_generator = packets_from_pcap.\
                get_packets_from_pcap(packetSource)
            network_sniffer = NetworkSniffer(packet_generator,
                                             flags=sniffer_argument)
        else:
            network_sniffer = NetworkSniffer(flags=sniffer_argument)
        for (parsed_packet, packet_info) in network_sniffer.\
                get_packets(maxpackets):
            if is_pcap_write:
                output.write(packet_info.data)
            else:
                output.write(
                    network_writer.get_packet_info(parsed_packet, packet_info))
 def test_count(self):
     path = os.path.join(".", "test", "saved_packets", "saved_packets.pcap")
     with tempfile.NamedTemporaryFile(suffix=".pcap", delete=False) as tmp:
         with open(path, "rb") as data:
             pcap = Pcap()
             packets = pcap.get_packets_from_pcap(data)
             flags = {"name": tmp.name, "filepackets": 10}
             pcap_manager = PcapFileManager(flags)
             for packet in packets:
                 pcap_manager.write(packet.data)
     pcap_manager.close()
     for file in pcap_manager.files:
         with open(file, "rb") as pcap_file:
             packets = pcap.get_packets_from_pcap(pcap_file)
             for (idx, _) in enumerate(packets):
                 pass
             self.assertLess(idx, 11)
     self.delete_list = pcap_manager.files
 def test_view_icmp(self):
     path = os.path.join(self.path, "icmp_packets", "icmp_packet_1.pcap")
     with open(path, "rb") as data:
         pcap = Pcap()
         packets = pcap.get_packets_from_pcap(data)
         network_sniffer = NetworkSniffer(packets)
         network_writer = NetworkWriter(["view", "data"])
         for packet in network_sniffer.get_packets(math.inf):
             packet_info = network_writer.get_packet_info(*packet)
         expected_lines = [
             "IPV4 Packet      :", "  Header Length  : 20",
             "  TTL            : 64", "  Source         : 10.155.61.128",
             "  Target         : 5.255.255.80", "ICMP Packet      :",
             "  Type           : 8", "  Code           : 0",
             "  Checksum       : 9913", "Packet Data      :"
         ]
         actual_lines = packet_info.splitlines()
         self.assertListEqual(expected_lines,
                              actual_lines[:len(expected_lines)])
 def test_view_udp(self):
     path = os.path.join(self.path, "udp_packets", "udp_packet_1.pcap")
     with open(path, "rb") as data:
         pcap = Pcap()
         packets = pcap.get_packets_from_pcap(data)
         network_sniffer = NetworkSniffer(packets)
         network_writer = NetworkWriter(["view", "data"])
         for packet in network_sniffer.get_packets(math.inf):
             packet_info = network_writer.get_packet_info(*packet)
         expected_lines = [
             "IPV4 Packet      :", "  Header Length  : 20",
             "  TTL            : 64", "  Source         : 127.0.0.1",
             "  Target         : 127.0.0.53", "UDP Segment      :",
             "  Source Port    : 40747", "  Dest Port      : 53",
             "  Length         : 65143", "Packet Data      :"
         ]
         actual_lines = packet_info.splitlines()
         self.assertListEqual(expected_lines,
                              actual_lines[:len(expected_lines)])
 def test_view_tcp(self):
     path = os.path.join(self.path, "tcp_packets", "tcp_packet_1.pcap")
     with open(path, "rb") as data:
         pcap = Pcap()
         packets = pcap.get_packets_from_pcap(data)
         network_sniffer = NetworkSniffer(packets)
         network_writer = NetworkWriter(["view", "data"])
         for packet in network_sniffer.get_packets(math.inf):
             packet_info = network_writer.get_packet_info(*packet)
         expected_lines = [
             "IPV4 Packet      :", "  Header Length  : 20",
             "  TTL            : 58", "  Source         : 23.35.144.14",
             "  Target         : 10.155.61.128", "TCP Segment      :",
             "  Sequence       : 568016820", "  Acknowledgment : 684033442",
             "  Flags          : ACK", "  Source         : 443",
             "  Destination    : 45186", "Packet Data      :"
         ]
         actual_lines = packet_info.splitlines()
         self.assertListEqual(expected_lines,
                              actual_lines[:len(expected_lines)])