-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task_05.py
30 lines (24 loc) · 1.01 KB
/
Task_05.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
import scapy.all as scapy
def packet_callback(packet):
if packet.haslayer(scapy.IP):
src_ip = packet[scapy.IP].src
dst_ip = packet[scapy.IP].dst
protocol = packet[scapy.IP].proto
print(f"Source IP: {src_ip} | Destination IP: {dst_ip} | Protocol: {protocol}")
if packet.haslayer(scapy.TCP):
try:
payload = packet[scapy.Raw].load
decoded_payload = payload.decode('utf-8', 'ignore')
print(f"TCP Payload")
except (IndexError, UnicodeDecodeError):
print("Unable to decode TCP payload.")
elif packet.haslayer(scapy.UDP):
try:
payload = packet[scapy.Raw].load
decoded_payload = payload.decode('utf-8', 'ignore')
print(f"UDP Payload")
except (IndexError, UnicodeDecodeError):
print("Unable to decode UDP payload.")
def start_sniffing():
scapy.sniff(store=False, prn=packet_callback)
start_sniffing()