-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_analyzer.py
216 lines (186 loc) · 7.75 KB
/
packet_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pyshark
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
import json
import httpx
class PcapAnalyzer:
TCP_FLAG_MAP = {
0x0001: 'URG',
0x0002: 'ACK',
0x0004: 'PSH',
0x0010: 'RST',
0x0020: 'SYN',
0x0040: 'FIN',
0x0080: 'ECE',
0x0100: 'CWR',
0x0200: 'NS'
}
def __init__(self, pcap_file,osN='',arch='',hostname='',id=''):
self.pcap_file = pcap_file
self.packet_count = 0
self.ip_addresses = set()
self.protocols = Counter()
self.tcp_flags = Counter()
self.ports = Counter()
self.packet_sizes = []
self.src_ip_counter = Counter()
self.dst_ip_counter = Counter()
self.application_protocols = Counter()
self.osN = osN
self.arch = arch
self.hostname = hostname
self.id = id
self.analyze()
def decode_tcp_flags(self, flag_value):
"""
Decode the TCP flag value into a list of human-readable flag names.
"""
flags = []
for flag_mask, flag_name in self.TCP_FLAG_MAP.items():
if flag_value & flag_mask:
flags.append(flag_name)
return flags
def analyze_packet(self, packet):
""" Analyze a single packet. """
self.packet_count += 1
self.packet_sizes.append(len(packet))
if 'IP' in packet:
self.src_ip_counter[packet.ip.src] += 1
self.dst_ip_counter[packet.ip.dst] += 1
self.ip_addresses.add(packet.ip.src)
self.ip_addresses.add(packet.ip.dst)
if hasattr(packet, 'transport_layer'):
self.protocols[packet.transport_layer] += 1
if 'TCP' in packet:
self.ports[packet.tcp.srcport] += 1
self.ports[packet.tcp.dstport] += 1
flag_value = int(packet.tcp.flags, 16)
flags = self.decode_tcp_flags(flag_value)
for flag in flags:
self.tcp_flags[flag] += 1
elif 'UDP' in packet:
self.ports[packet.udp.srcport] += 1
self.ports[packet.udp.dstport] += 1
elif 'ICMP' in packet:
self.ports[packet.icmp.type] += 1
self.ports[packet.icmp.code] += 1
if 'HTTP' in packet:
self.application_protocols['HTTP'] += 1
elif 'DNS' in packet:
self.application_protocols['DNS'] += 1
elif 'SSL' in packet:
self.application_protocols['SSL'] += 1
elif 'SSH' in packet:
self.application_protocols['SSH'] += 1
elif 'FTP' in packet:
self.application_protocols['FTP'] += 1
elif 'SMTP' in packet:
self.application_protocols['SMTP'] += 1
elif 'POP' in packet:
self.application_protocols['POP'] += 1
elif 'IMAP' in packet:
self.application_protocols['IMAP'] += 1
elif 'TELNET' in packet:
self.application_protocols['TELNET'] += 1
elif 'SMB' in packet:
self.application_protocols['SMB'] += 1
elif 'DHCP' in packet:
self.application_protocols['DHCP'] += 1
elif 'ARP' in packet:
self.application_protocols['ARP'] += 1
elif 'NTP' in packet:
self.application_protocols['NTP'] += 1
elif 'SNMP' in packet:
self.application_protocols['SNMP'] += 1
elif 'LDAP' in packet:
self.application_protocols['LDAP'] += 1
elif 'RDP' in packet:
self.application_protocols['RDP'] += 1
elif 'RTP' in packet:
self.application_protocols['RTP'] += 1
elif 'RTCP' in packet:
self.application_protocols['RTCP'] += 1
elif 'QUIC' in packet:
self.application_protocols['QUIC'] += 1
def analyze(self):
""" Analyze the PCAP file and gather statistics. """
cap = pyshark.FileCapture(self.pcap_file, keep_packets=False)
with ThreadPoolExecutor(max_workers=12) as executor:
# Process each packet concurrently
executor.map(self.analyze_packet, cap)
def get_packet_count(self):
return self.packet_count
def get_unique_ip_count(self):
return len(self.ip_addresses)
def get_protocol_stats(self):
return self.protocols
def get_packet_size_stats(self):
total_size = sum(self.packet_sizes)
average_size = total_size / len(self.packet_sizes) if len(self.packet_sizes) > 0 else 0
return total_size, average_size
def get_top_ips(self, top_n=5):
return {
'source_ips': self.src_ip_counter.most_common(top_n),
'destination_ips': self.dst_ip_counter.most_common(top_n)
}
def get_top_ports(self, top_n=5):
return self.ports.most_common(top_n)
def get_tcp_flags(self):
return self.tcp_flags
def get_application_protocols(self):
return self.application_protocols
def display_stats(self):
print(f"Total Packets: {self.get_packet_count()}")
print(f"Unique IP Addresses: {self.get_unique_ip_count()}")
total_size, average_size = self.get_packet_size_stats()
print(f"Total Data Size: {total_size} bytes")
print(f"Average Packet Size: {average_size:.2f} bytes")
print("\nTop 5 Source IPs:")
for ip, count in self.get_top_ips()['source_ips']:
print(f"{ip}: {count} packets")
print("\nTop 5 Destination IPs:")
for ip, count in self.get_top_ips()['destination_ips']:
print(f"{ip}: {count} packets")
print("\nProtocol Counts:")
for protocol, count in self.get_protocol_stats().items():
print(f"{protocol}: {count} packets")
print("\nApplication Protocol Counts:")
for protocol, count in self.get_application_protocols().items():
print(f"{protocol}: {count} packets")
print("\nTop 5 Ports:")
for port, count in self.get_top_ports():
print(f"Port {port}: {count} packets")
print("\nTCP Flag Counts:")
for flag, count in self.get_tcp_flags().items():
print(f"Flag {flag}: {count} packets")
def get_json(self):
total_size, average_size = self.get_packet_size_stats()
json_data = {
"agent":{
"os": self.osN,
"arch": self.arch,
"hostname": self.hostname,
"id": self.id
},
"total_packets": self.get_packet_count(),
"unique_ip_addresses": self.get_unique_ip_count(),
"total_data_size": total_size,
"average_packet_size": average_size,
"top_source_ips": [{"ip": ip, "count": count} for ip, count in self.get_top_ips()['source_ips']],
"top_destination_ips": [{"ip": ip, "count": count} for ip, count in self.get_top_ips()['destination_ips']],
"protocol_counts": {protocol: count for protocol, count in self.get_protocol_stats().items()},
"application_protocol_counts": {protocol: count for protocol, count in self.get_application_protocols().items()},
"top_ports": [{"port": port, "count": count} for port, count in self.get_top_ports()],
"tcp_flag_counts": {flag: count for flag, count in self.get_tcp_flags().items()}
}
return json.dumps(json_data, indent=4)
async def send_to_elastic(self):
json = self.get_json()
async with httpx.AsyncClient() as client:
response = await client.post("http://elasticsearch:9200/packets_v2/_doc", data=json, headers={"Content-Type": "application/json"})
print(response.text)
if __name__ == "__main__":
pcap_file = "data.pcap"
analyzer = PcapAnalyzer(pcap_file)
analyzer.display_stats()
json_data = analyzer.get_json()