11import os
2+ import pickle
23from abc import ABC
34from enum import Enum , unique
45from typing import Type
56
6- from scapy .all import (Packet , RandIP , RandIP6 , RandMAC , Raw , rdpcap , sniff ,
7- wrpcap )
7+ import cryptography
8+ from scapy .all import (Packet , RandIP , RandIP6 , RandMAC , Raw , load_layer ,
9+ rdpcap , sniff , wrpcap )
810from scapy .layers .dns import DNS
911from scapy .layers .http import HTTP , HTTPRequest , HTTPResponse
1012from scapy .layers .inet import IP , TCP , UDP , Ether
@@ -41,7 +43,11 @@ def __init__(
4143 self ,
4244 file_extension = "pcap" ,
4345 ) -> None :
44- pass
46+ self .hash_dict = set ()
47+ # if os.path.isfile('hashes_pkt.pkl'):
48+ # with open('hashes_pkt.pkl', 'rb') as f:
49+ # self.hash_dict = pickle.load(f)
50+ load_layer ("tls" )
4551
4652 def write_packet (self ) -> None :
4753 # Write pcap
@@ -53,16 +59,19 @@ def read_packets_file(self, file: str, preprocessing_type: PacketProcessorType)
5359 # Read PCAP file with Scapy
5460 packets = []
5561 # TODO Only read max number of packets
56- pcap = sniff (offline = file )
62+ pcap = sniff (offline = file , count = 64 )
5763 for pkt in pcap :
5864 # Start preprocessing for each packet
5965 processed_packet = self .__preprocessing (pkt , preprocessing_type )
6066 # TODO Run extract here to reduce amount of loops in code. Atm very inefficient for computation time and memory
6167 # In case packet returns None
6268 if processed_packet != None :
63- packets .append (processed_packet )
69+ if not processed_packet .hash in self .hash_dict :
70+ self .hash_dict .add (processed_packet .hash )
71+ packets .append (processed_packet )
6472 return packets
6573
74+
6675 def read_packets_packet (self , packet : [Packet ], preprocessing_type : PacketProcessorType ) -> [FIPPacket ]:
6776 # Read PCAP file with Scapy
6877 packets = []
@@ -71,7 +80,9 @@ def read_packets_packet(self, packet: [Packet], preprocessing_type: PacketProces
7180 processed_packet = self .__preprocessing (pkt , preprocessing_type )
7281 # In case packet returns None
7382 if processed_packet != None :
74- packets .append (processed_packet )
83+ if not processed_packet .hash in self .hash_dict :
84+ self .hash_dict .add (processed_packet .hash )
85+ packets .append (processed_packet )
7586 return packets
7687
7788 def __preprocessing (self , packet : Packet , preprocessing_type : PacketProcessorType ) -> FIPPacket :
@@ -92,7 +103,7 @@ def __preprocessing(self, packet: Packet, preprocessing_type: PacketProcessorTyp
92103 elif Ether in fippacket .layer_map :
93104 fippacket = fippacket .convert (EtherPacket , fippacket )
94105
95- if preprocessing_type == PacketProcessorType . HEADER :
106+ if preprocessing_type == " HEADER" :
96107 fippacket .header_preprocessing ()
97108
98109 return fippacket
0 commit comments