-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.py
More file actions
46 lines (34 loc) · 1.16 KB
/
Copy pathTask1.py
File metadata and controls
46 lines (34 loc) · 1.16 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
from scapy.all import sniff, IP, TCP, UDP
def packet_callback(packet):
if packet.haslayer(IP):
ip_layer = packet[IP]
src_ip = ip_layer.src
dst_ip = ip_layer.dst
proto = ip_layer.proto
if proto == 6:
protocol_name = "TCP"
elif proto == 17:
protocol_name = "UDP"
elif proto == 1:
protocol_name = "ICMP"
else:
protocol_name = str(proto)
print("="*60)
print(f"[+] Packet Captured!")
print(f"Source IP : {src_ip}")
print(f"Destination IP : {dst_ip}")
print(f"Protocol : {protocol_name}")
if packet.haslayer(TCP) or packet.haslayer(UDP):
payload = bytes(packet.payload)
print(f"Payload : {payload[:50]}")
print("="*60 + "\n")
def main():
"""
Main function to start packet sniffing
"""
print("=== Basic Network Sniffer ===")
print("Press CTRL+C to stop capturing.\n")
packet_count = 10
sniff(count=packet_count, prn=packet_callback, store=False)
if __name__ == "__main__":
main()