-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork Packet Analyzer.txt
More file actions
36 lines (30 loc) · 1.1 KB
/
Copy pathNetwork Packet Analyzer.txt
File metadata and controls
36 lines (30 loc) · 1.1 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
from scapy.all import IP, TCP, UDP, sniff
def packet_callback(packet):
# Check if the packet has an IP layer
if IP in packet:
ip_layer = packet[IP]
src_ip = ip_layer.src
dst_ip = ip_layer.dst
protocol = ip_layer.proto
# Determine the protocol type
if protocol == 6: # TCP
proto_type = "TCP"
elif protocol == 17: # UDP
proto_type = "UDP"
else:
proto_type = "Other"
# Print packet details
print(f"Source IP: {src_ip} -> Destination IP: {dst_ip} | Protocol: {proto_type}")
# Display payload data
if proto_type == "TCP" and TCP in packet:
tcp_layer = packet[TCP]
payload = tcp_layer.payload
print(f"Payload: {payload}")
elif proto_type == "UDP" and UDP in packet:
udp_layer = packet[UDP]
payload = udp_layer.payload
print(f"Payload: {payload}")
else:
print("Payload: None or not TCP/UDP")
# Sniff packets
sniff(filter="ip", prn=packet_callback, store=0)