Skip to content

Commit

Permalink
Automotive data compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezade authored and alirezade committed Oct 30, 2024
1 parent 36afb08 commit 64edf0e
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 82 deletions.
15 changes: 5 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
pytest~=7.4.3
scapy~=2.5.0
paho-mqtt~=1.6.1
keras>=2.15,<2.16
numpy~=1.26.3
pandas~=2.2.0
numpy~=1.26.2
pandas~=2.1.3
joblib~=1.3.2
scikit-learn~=1.4.0
scikeras~=0.12.0
tensorflow~=2.15.0
pip~=23.3.2
wheel~=0.42.0
setuptools~=60.2.0
scikit-learn~=1.3.2
scikeras~=0.11.0
keras~=2.14.0
2 changes: 1 addition & 1 deletion src/AgentExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def process_packets(self, ether_pkt, pkt_time):
packet_para = PacketParameter(ether_pkt, pkt_time)
flow_src = min(packet_para.get_src(), packet_para.get_dst())
flow_dst = max(packet_para.get_src(), packet_para.get_dst())
flow_proto = packet_para.protocol_name
flow_proto = packet_para.type_protocol_name
key = (flow_src, flow_dst, flow_proto)

if not self.processing_dict.keys().__contains__(key):
Expand Down
10 changes: 5 additions & 5 deletions src/AgentProcessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ProcessAnnotator import ProcessAnnotator
#from ProcessAnnotator import ProcessAnnotator
from ProcessFlowFileWriter import ProcessFlowFileWriter
from ProcessStatusSenderMQTT import ProcessStatusSenderMQTT
from Config import Config
Expand All @@ -9,15 +9,15 @@ class AgentProcessor:

def __init__(self, predictor_address, attacks_address, file_address, server_connection):
self.__processes = []
annotator = ProcessAnnotator(predictor_address, attacks_address)
#annotator = ProcessAnnotator(predictor_address, attacks_address)
flow_file_writer = ProcessFlowFileWriter(file_address)
status_sender = ProcessStatusSenderMQTT(server_connection, voting_interval=Config.StatusSender.voting_interval)
flow_sender = ProcessFlowSenderMQTT(server_connection)

self.__processes.append(annotator)
# self.__processes.append(annotator)
self.__processes.append(flow_file_writer)
if annotator.is_prediction_enabled():
self.__processes.append(status_sender)
# if annotator.is_prediction_enabled():
# self.__processes.append(status_sender)
#self.__processes.append(flow_sender)

def process(self, flow):
Expand Down
26 changes: 13 additions & 13 deletions src/Flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def start_time(self):
s_start = sys.float_info.max
r_start = sys.float_info.max
if len(self.sen_list) != 0:
s_start = self.sen_list[0].packet_time
s_start = self.sen_list[0].time_stamp
if len(self.rec_list) != 0:
r_start = self.rec_list[0].packet_time
r_start = self.rec_list[0].time_stamp

return min(s_start, r_start)

def end_time(self):
s_end = 0
r_end = 0
if len(self.sen_list) != 0:
s_end = self.sen_list[-1].packet_time
s_end = self.sen_list[-1].time_stamp
if len(self.rec_list) != 0:
r_end = self.rec_list[-1].packet_time
r_end = self.rec_list[-1].time_stamp

return max(s_end, r_end)

Expand All @@ -67,13 +67,13 @@ def add_packet(self, packet_parameter):
if packet_parameter.get_src() == self.src:
self.src_mac_list.add(packet_parameter.src_mac)
self.dst_mac_list.add(packet_parameter.dst_mac)
if packet_parameter.is_ip():
if packet_parameter.is_ip_based():
self.src_ip_list.add(packet_parameter.src_ip)
self.dst_ip_list.add(packet_parameter.dst_ip)
else:
self.src_mac_list.add(packet_parameter.dst_mac)
self.dst_mac_list.add(packet_parameter.src_mac)
if packet_parameter.is_ip():
if packet_parameter.is_ip_based():
self.src_ip_list.add(packet_parameter.dst_ip)
self.dst_ip_list.add(packet_parameter.src_ip)

Expand All @@ -83,13 +83,13 @@ def compute_delay(self, packet_parameter):
return

if packet_parameter.get_src() == self.src:
self.acc_sen_dic[packet_parameter.ack] = packet_parameter.packet_time
self.acc_sen_dic[packet_parameter.ack] = packet_parameter.time_stamp
if self.acc_rec_dic.keys().__contains__(packet_parameter.seq):
self.sen_delay.append(packet_parameter.packet_time - self.acc_rec_dic[packet_parameter.seq])
self.sen_delay.append(packet_parameter.time_stamp - self.acc_rec_dic[packet_parameter.seq])
else:
self.acc_rec_dic[packet_parameter.ack] = packet_parameter.packet_time
self.acc_rec_dic[packet_parameter.ack] = packet_parameter.time_stamp
if self.acc_sen_dic.keys().__contains__(packet_parameter.seq):
self.rec_delay.append(packet_parameter.packet_time - self.acc_sen_dic[packet_parameter.seq])
self.rec_delay.append(packet_parameter.time_stamp - self.acc_sen_dic[packet_parameter.seq])


def compute_parameters(self):
Expand Down Expand Up @@ -254,7 +254,7 @@ def inter_packets_avg(packets):
if Flow.packets_cnt(packets) == 1:
return ''

return (packets[-1].packet_time - packets[0].packet_time) / (Flow.packets_cnt(packets) - 1)
return (packets[-1].time_stamp - packets[0].time_stamp) / (Flow.packets_cnt(packets) - 1)

@staticmethod
def ttl_avg(packets):
Expand All @@ -264,7 +264,7 @@ def ttl_avg(packets):
if not packets[0].is_tcp():
return ''

if not packets[0].is_ip():
if not packets[0].is_ip_based():
return ''
else:
value = sum([pkt.ttl for pkt in packets]) / Flow.packets_cnt(packets)
Expand Down Expand Up @@ -311,7 +311,7 @@ def fragmentation_rate(packets):

if Flow.packets_cnt(packets) == 0:
return ''
if not packets[0].is_ip():
if not packets[0].is_ip_based():
return ''

return sum([int(pkt.fragment) for pkt in packets]) / Flow.packets_cnt(packets)
Expand Down
3 changes: 2 additions & 1 deletion src/Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def get_packet_time(pkt_metadata):
:param pkt_metadata: meta data received from PCAP file.
:return: formatted packet time.
"""
return pkt_metadata.sec + pkt_metadata.usec / pow(10, 6)
first_pkt_timestamp = (pkt_metadata.tshigh << 32) | pkt_metadata.tslow
return first_pkt_timestamp / pkt_metadata.tsresol


def format_decimal(value, rnd=3):
Expand Down
80 changes: 80 additions & 0 deletions src/PacketInfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class PacketInfo:
TYPE_IP = "IP" # Internet Protocol (IPv4)
TYPE_ARP = "ARP" # Address Resolution Protocol
TYPE_IPv6 = "IPv6" # Internet Protocol (IPv6)
TYPE_IPX = "IPX" # Internetwork Packet Exchange
TYPE_VLAN = "VLAN" # IEEE 802.1Q (VLAN tagging)
TYPE_PPP = "PPP" # Point-to-Point Protocol
TYPE_MPLS = "MPLS" # Multiprotocol Label Switching
TYPE_MPLS = "MPLS" # MPLS with downstream-assigned label
TYPE_PPPoE = "PPPoE" # PPP over Ethernet (Discovery stage)
TYPE_PPPoE = "PPPoE" # PPP over Ethernet (Session stage)
TYPE_QinQ = "QinQ" # 802.1ad (Q-in-Q VLAN tagging)
TYPE_Realtek = "Realtek" # Realtek protocol
TYPE_LLDP = "LLDP" # Link Layer Discovery Protocol
TYPE_FCoE = "FCoE" # Fibre Channel over Ethernet
TYPE_FCoE = "FCoE" # FCoE Initialization Protocol

# Define packet types (Ethertypes)
TYPES = {
0x0800: TYPE_IP, # Internet Protocol (IPv4)
0x0806: TYPE_ARP, # Address Resolution Protocol
0x86DD: TYPE_IPv6, # Internet Protocol (IPv6)
0x8137: TYPE_IPX, # Internetwork Packet Exchange
0x8100: TYPE_VLAN, # IEEE 802.1Q (VLAN tagging)
0x880B: TYPE_PPP, # Point-to-Point Protocol
0x8847: TYPE_MPLS, # Multiprotocol Label Switching
0x8848: TYPE_MPLS, # MPLS with downstream-assigned label
0x8863: TYPE_PPPoE, # PPP over Ethernet (Discovery stage)
0x8864: TYPE_PPPoE, # PPP over Ethernet (Session stage)
0x88A8: TYPE_QinQ, # 802.1ad (Q-in-Q VLAN tagging)
0x8899: TYPE_Realtek, # Realtek protocol
0x88CC: TYPE_LLDP, # Link Layer Discovery Protocol
0x8906: TYPE_FCoE, # Fibre Channel over Ethernet
0x8914: TYPE_FCoE, # FCoE Initialization Protocol
}

PROTOCOL_ICMP = "ICMP" # Internet Control Message Proto
PROTOCOL_IGMP = "IGMP" # Internet Group Management Prot
PROTOCOL_TCP = "TCP" # Transmission Control Protocol
PROTOCOL_UDP = "UDP" # User Datagram Protocol
PROTOCOL_IPv6 = "IPv6" # IPv6 encapsulation
PROTOCOL_GRE = "GRE" # Generic Routing Encapsulation
PROTOCOL_ESP = "ESP" # Encapsulating Security Payload
PROTOCOL_AH = "AH" # Authentication Header
PROTOCOL_ICMPv6 = "ICMPv6" # Internet Control Message Pr
PROTOCOL_OSPF = "OSPF" # Open Shortest Path First
PROTOCOL_SCTP = "SCTP" # Stream Control Transmission
PROTOCOL_MPLS = "MPLS" # MPLS-in-IP
PROTOCOL_FCoE = "FCoE" # Fibre Channel over Ethernet

# Define IP-based protocols
PROTOCOLS = {
1: PROTOCOL_ICMP, # Internet Control Message Protocol
2: PROTOCOL_IGMP, # Internet Group Management Protocol
6: PROTOCOL_TCP, # Transmission Control Protocol
17: PROTOCOL_UDP, # User Datagram Protocol
41: PROTOCOL_IPv6, # IPv6 encapsulation
47: PROTOCOL_GRE, # Generic Routing Encapsulation
50: PROTOCOL_ESP, # Encapsulating Security Payload
51: PROTOCOL_AH, # Authentication Header
58: PROTOCOL_ICMPv6, # Internet Control Message Protocol for IPv6
89: PROTOCOL_OSPF, # Open Shortest Path First
132: PROTOCOL_SCTP, # Stream Control Transmission Protocol
137: PROTOCOL_MPLS, # MPLS-in-IP
138: PROTOCOL_FCoE, # Fibre Channel over Ethernet
}

@classmethod
def get_packet_type(cls, eth_type):
if eth_type in cls.TYPES:
return cls.TYPES[eth_type]
else:
return f'{hex(eth_type)}'

@classmethod
def get_packet_protocol(cls, protocol):
if protocol in cls.PROTOCOLS:
return cls.PROTOCOLS[protocol]
else:
return f'{protocol}'
Loading

0 comments on commit 64edf0e

Please sign in to comment.