-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_analyzer.py
More file actions
243 lines (217 loc) · 8.41 KB
/
Copy pathpacket_analyzer.py
File metadata and controls
243 lines (217 loc) · 8.41 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
import os
import sys
import logging
import argparse
from datetime import datetime
from scapy.all import *
from scapy.layers.http import HTTPRequest, HTTPResponse
from scapy.layers.inet import IP, TCP, UDP, ICMP
from scapy.layers.dns import DNS
from scapy.layers.inet6 import IPv6
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('packet_analyzer.log')
]
)
logger = logging.getLogger("PacketAnalyzer")
# Silence Scapy completely
conf.verb = 0
conf.warning_threshold = 0
conf.logLevel = logging.ERROR
class PacketAnalyzer:
def __init__(self, interface, filter_exp=None, log_file=None):
self.interface = interface
self.filter_exp = filter_exp
self.log_file = log_file
self.packet_count = 0
self.running = False
self.socket = None
def start_capture(self, packet_count=0):
"""Start continuous packet capture session"""
logger.info(f"Starting continuous packet capture on {self.interface}...")
if self.filter_exp:
logger.info(f"Applying filter: '{self.filter_exp}'")
self.running = True
self.packet_count = packet_count
try:
# Create raw socket
self.socket = conf.L2socket(iface=self.interface)
# Continuous capture with no timeout
sniff(
opened_socket=self.socket,
filter=self.filter_exp,
prn=self.process_packet,
store=False,
count=packet_count if packet_count > 0 else 0,
promisc=False,
quiet=True
)
except PermissionError:
logger.error("Permission denied. Try running with sudo.")
sys.exit(1)
except KeyboardInterrupt:
logger.info("\nCapture stopped by user")
except Exception as e:
logger.error(f"Capture error: {str(e)}")
finally:
if self.socket:
self.socket.close()
self.running = False
logger.info("Capture session ended")
def process_packet(self, packet):
"""Process each captured packet"""
try:
if packet is None:
return
if IP in packet or IPv6 in packet:
self.analyze_ip_packet(packet)
except Exception as e:
logger.error(f"Error processing packet: {e}")
def analyze_ip_packet(self, packet):
"""Analyze IP layer and its protocols"""
try:
if IP in packet:
ip_src = packet[IP].src
ip_dst = packet[IP].dst
protocol = packet[IP].proto
elif IPv6 in packet:
ip_src = packet[IPv6].src
ip_dst = packet[IPv6].dst
protocol = packet[IPv6].nh
timestamp = datetime.fromtimestamp(packet.time).strftime('%Y-%m-%d %H:%M:%S')
base_info = f"[{timestamp}] {ip_src} -> {ip_dst} | Proto: {protocol}"
if TCP in packet:
self.analyze_tcp(packet, base_info)
elif UDP in packet:
self.analyze_udp(packet, base_info)
elif ICMP in packet:
logger.info(f"{base_info} | ICMP Packet")
elif protocol == 2: # IGMP
logger.info(f"{base_info} | IGMP Packet")
else:
logger.info(f"{base_info} | Unknown Protocol")
if self.log_file:
self.log_packet(packet)
except Exception as e:
logger.error(f"Error analyzing IP packet: {e}")
def analyze_tcp(self, packet, base_info):
"""Enhanced TCP analysis with sequence numbers"""
try:
sport = packet[TCP].sport
dport = packet[TCP].dport
flags = packet[TCP].flags
seq = packet[TCP].seq
ack = packet[TCP].ack
tcp_info = f"{base_info} | TCP {sport}->{dport} | Seq:{seq} Ack:{ack} Flags:{flags}"
if packet[TCP].dport == 80 or packet[TCP].sport == 80:
http_info = self.analyze_http(packet)
logger.info(f"{tcp_info} | {http_info}")
elif packet[TCP].dport == 443 or packet[TCP].sport == 443:
logger.info(f"{tcp_info} | HTTPS Traffic")
else:
logger.info(tcp_info)
except Exception as e:
logger.error(f"Error analyzing TCP packet: {e}")
def analyze_udp(self, packet, base_info):
"""Analyze UDP packets"""
try:
sport = packet[UDP].sport
dport = packet[UDP].dport
udp_info = f"{base_info} | UDP {sport} -> {dport}"
if packet[UDP].dport == 53 or packet[UDP].sport == 53:
if DNS in packet:
dns_info = self.analyze_dns(packet)
logger.info(f"{udp_info} | DNS: {dns_info}")
else:
logger.info(f"{udp_info} | DNS Traffic")
elif packet[UDP].dport == 1900: # SSDP
logger.info(f"{udp_info} | SSDP Traffic")
else:
logger.info(udp_info)
except Exception as e:
logger.error(f"Error analyzing UDP packet: {e}")
def analyze_http(self, packet):
"""Enhanced HTTP analysis to show full requests"""
try:
if packet.haslayer(HTTPRequest):
http = packet[HTTPRequest]
method = http.Method.decode(errors='ignore')
host = http.Host.decode(errors='ignore') if http.Host else "N/A"
path = http.Path.decode(errors='ignore') if http.Path else "N/A"
return f"HTTP Request: {method} {host}{path}"
elif packet.haslayer(HTTPResponse):
return f"HTTP Response: {packet[HTTPResponse].Status_Code.decode(errors='ignore')}"
else:
return "HTTP Data"
except Exception as e:
logger.error(f"Error analyzing HTTP: {e}")
return "HTTP Analysis Failed"
def analyze_dns(self, packet):
"""Extract DNS query information"""
try:
dns_layer = packet[DNS]
if dns_layer.qr == 0: # DNS query
query = dns_layer.qd.qname.decode(errors='ignore') if dns_layer.qd else "N/A"
return f"Query: {query}"
else: # DNS response
answers = [rr.rdata for rr in dns_layer.an if dns_layer.an]
return f"Response: {', '.join(str(a) for a in answers)}" if answers else "Empty Response"
except Exception as e:
logger.error(f"Error analyzing DNS: {e}")
return "DNS Analysis Failed"
def log_packet(self, packet):
"""Log packet to file"""
try:
with open(self.log_file, 'a') as f:
f.write(f"{packet.summary()}\n")
except Exception as e:
logger.error(f"Error logging packet: {e}")
def main():
# Redirect stderr to suppress any remaining warnings
sys.stderr = open(os.devnull, 'w')
parser = argparse.ArgumentParser(
description="Enhanced Python Network Packet Analyzer",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-i', '--interface',
required=True,
help="Network interface to capture on (use 'ip a' to check)"
)
parser.add_argument(
'-f', '--filter',
default=None,
help="BPF filter expression (e.g., 'tcp port 80')"
)
parser.add_argument(
'-c', '--count',
type=int,
default=0,
help="Number of packets to capture (0 for unlimited)"
)
parser.add_argument(
'-l', '--log',
default=None,
help="File to log packets to"
)
args = parser.parse_args()
analyzer = PacketAnalyzer(
interface=args.interface,
filter_exp=args.filter,
log_file=args.log
)
try:
analyzer.start_capture(packet_count=args.count)
except KeyboardInterrupt:
logger.info("\nCapture stopped by user")
sys.exit(0)
except Exception as e:
logger.error(f"Fatal error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()