-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_5
More file actions
31 lines (25 loc) · 1.06 KB
/
Task_5
File metadata and controls
31 lines (25 loc) · 1.06 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
from scapy.all import sniff, Ether, IP, TCP, UDP
def process_packet(packet):
"""Process and display packet details."""
print("\n=== Packet Captured ===")
if packet.haslayer(Ether):
eth = packet[Ether]
print(f"Ethernet Frame:\n Source MAC: {eth.src}\n Destination MAC: {eth.dst}")
if packet.haslayer(IP):
ip = packet[IP]
print(f"IP Packet:\n Source IP: {ip.src}\n Destination IP: {ip.dst}\n Protocol: {ip.proto}")
if packet.haslayer(TCP):
tcp = packet[TCP]
print(f"TCP Segment:\n Source Port: {tcp.sport}\n Destination Port: {tcp.dport}")
if packet.haslayer(UDP):
udp = packet[UDP]
print(f"UDP Segment:\n Source Port: {udp.sport}\n Destination Port: {udp.dport}")
def main():
print("Starting packet sniffer... Press Ctrl+C to stop.")
try:
# Sniff packets on the default network interface
sniff(prn=process_packet, filter="ip", store=0)
except KeyboardInterrupt:
print("\nPacket sniffer stopped.")
if __name__ == "__main__":
main()