-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5.py
31 lines (25 loc) · 1.06 KB
/
task5.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
from scapy.all import sniff, IP, TCP, UDP
def packet_callback(packet):
if IP in packet:
ip_src = packet[IP].src
ip_dst = packet[IP].dst
protocol = packet[IP].proto
payload = packet[IP].load if hasattr(packet[IP], 'load') else None
print(f"Source IP: {ip_src}")
print(f"Destination IP: {ip_dst}")
print(f"Protocol: {protocol}")
if protocol == 6: # TCP
print("Protocol: TCP")
if TCP in packet:
print(f"Source Port: {packet[TCP].sport}")
print(f"Destination Port: {packet[TCP].dport}")
elif protocol == 17: # UDP
print("Protocol: UDP")
if UDP in packet:
print(f"Source Port: {packet[UDP].sport}")
print(f"Destination Port: {packet[UDP].dport}")
if payload:
print(f"Payload Data: {payload.decode(errors='ignore')}")
print("\n" + "="*50 + "\n")
# Sniff packets on the network
sniff(prn=packet_callback, store=0, count=10) # Adjust count as needed