Skip to content

Commit 75fbdbd

Browse files
committed
refactor: Placed packets into a dictionary
1 parent 67e9cd8 commit 75fbdbd

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

include/traffic.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,56 +84,38 @@ def analyze_traffic(pcap_file, executable_name):
8484
include_logger.debug(f"Analyzing traffic from {pcap_file}...")
8585
capture = pyshark.FileCapture(pcap_file)
8686

87-
dns_packets = []
88-
http_packets = []
89-
ssl_packets = []
90-
tcp_packets = []
91-
ip_packets = []
92-
udp_packets = []
87+
packets = {
88+
'DNS': [],
89+
'HTTP': [],
90+
'SSL': [],
91+
'TCP': [],
92+
'IP': [],
93+
'UDP': []
94+
}
9395

9496
for packet in capture:
9597
if 'DNS' in packet:
96-
include_logger.debug(f"DNS packet found in packet {pcap_file}")
97-
dns_packets.append(packet)
98+
packets['DNS'].append(packet)
9899
if 'HTTP' in packet:
99-
include_logger.debug(f"HTTP packet found in packet {pcap_file}")
100-
http_packets.append(packet)
101-
if 'SSL' in packet:
102-
include_logger.debug(f"SSL packet found in packet {pcap_file}")
103-
ssl_packets.append(packet)
100+
packets['HTTP'].append(packet)
101+
if 'SSL' in packet or 'TLS' in packet:
102+
packets['SSL'].append(packet)
104103
if 'TCP' in packet:
105-
include_logger.debug(f"TCP packet found in packet {pcap_file}")
106-
tcp_packets.append(packet)
104+
packets['TCP'].append(packet)
107105
if 'IP' in packet:
108-
include_logger.debug(f"IP packet found in packet {pcap_file}")
109-
ip_packets.append(packet)
106+
packets['IP'].append(packet)
110107
if 'UDP' in packet:
111-
include_logger.debug(f"UDP packet found in packet {pcap_file}")
112-
udp_packets.append(packet)
108+
packets['UDP'].append(packet)
113109

114-
include_logger.info(f"DNS packets: {len(dns_packets)}")
115-
include_logger.info(f"HTTP packets: {len(http_packets)}")
116-
include_logger.info(f"SSL packets: {len(ssl_packets)}")
117-
include_logger.info(f"TCP packets: {len(tcp_packets)}")
118-
include_logger.info(f"IP packets: {len(ip_packets)}")
119-
include_logger.info(f"UDP packets: {len(udp_packets)}")
110+
for proto, pkt_list in packets.items():
111+
include_logger.info(f"{proto} packets: {len(pkt_list)}")
120112

121-
open_csv('DNS', ['Filename', 'Protocol', 'Source IP',
122-
'Destination IP', 'Query Name', 'Response Flags', 'Time-to-Live'], executable_name, dns_packets)
123-
124-
open_csv('HTTP', ['Filename', 'Protocol', 'Source IP', 'Destination IP',
125-
'Hostname', 'Referrer', 'Cookie', 'User Agent', 'Content Type'], executable_name, http_packets)
126-
127-
open_csv('SSL', ['Filename', 'Protocol', 'Source IP', 'Destination IP',
128-
'Server Name', 'SSL Version', 'Certificate Expiry'], executable_name, ssl_packets)
129-
130-
open_csv('TCP', ['Destination Port', 'Packet Size', 'PUSH Bit Set',
131-
'Out-of-Order Packets'], executable_name, tcp_packets)
132-
133-
open_csv('IP', ['Destination IP', 'IP Geo-location',
134-
'IP Autonomous System Number'], executable_name, ip_packets)
135-
136-
open_csv('UDP', ['Ratio Sent/Received', 'Non-Existent Domain Responses'], executable_name, udp_packets)
113+
open_csv('DNS', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Query Name', 'Response Flags', 'TTL'], executable_name, packets['DNS'])
114+
open_csv('HTTP', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Hostname', 'User Agent', 'Content Type'], executable_name, packets['HTTP'])
115+
open_csv('SSL', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Server Name', 'SSL Version', 'Encrypted Traffic Ratio'], executable_name, packets['SSL'])
116+
open_csv('TCP', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Destination Port', 'Packet Size', 'PUSH Bit Set'], executable_name, packets['TCP'])
117+
open_csv('IP', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Geo-location', 'ASN', 'Repeated Connection Attempts'], executable_name, packets['IP'])
118+
open_csv('UDP', ['Filename', 'Protocol', 'Source IP', 'Destination IP', 'Ratio Sent/Received'], executable_name, packets['UDP'])
137119

138120

139121
def open_csv(protocol, headers, executable_name, packets):

0 commit comments

Comments
 (0)