-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsniffer.py
More file actions
72 lines (57 loc) · 2.45 KB
/
Copy pathsniffer.py
File metadata and controls
72 lines (57 loc) · 2.45 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
from scapy.all import sniff, IP, TCP, UDP, ICMP
def packet_callback(packet):
"""
Callback function that processes each captured network packet.
Extracts Layer 3 (IP) and Layer 4 (TCP/UDP/ICMP) structural details.
"""
if packet.haslayer(IP):
ip_layer = packet[IP]
source_ip = ip_layer.src
destination_ip = ip_layer.dst
protocol_num = ip_layer.proto
protocol_name = "Unknown"
if protocol_num == 6:
protocol_name = "TCP"
elif protocol_num == 17:
protocol_name = "UDP"
elif protocol_num == 1:
protocol_name = "ICMP"
print("-" * 60)
print(f"[+] Packet Captured:")
print(f" Source IP: {source_ip}")
print(f" Destination IP: {destination_ip}")
print(f" Protocol: {protocol_name} (Number: {protocol_num})")
if packet.haslayer(TCP):
tcp_layer = packet[TCP]
print(f" Source Port: {tcp_layer.sport}")
print(f" Dest Port: {tcp_layer.dport}")
# If there is raw data payload, display it
if packet.haslayer('Raw'):
payload = packet['Raw'].load
print(f" Payload (Raw): {payload[:100]}...") # Shows first 100 bytes
elif packet.haslayer(UDP):
udp_layer = packet[UDP]
print(f" Source Port: {udp_layer.sport}")
print(f" Dest Port: {udp_layer.dport}")
if packet.haslayer('Raw'):
payload = packet['Raw'].load
print(f" Payload (Raw): {payload[:100]}...")
elif packet.haslayer(ICMP):
print(f" ICMP Type: {packet[ICMP].type}")
def main():
print("=" * 60)
print(" CODEALPHA BASIC NETWORK SNIFFER ")
print("=" * 60)
print("[*] Initializing packet capture loop...")
print("[*] Sniffing live traffic. Press 'Ctrl + C' to terminate safely.\n")
try:
sniff(prn=packet_callback, store=0)
except PermissionError:
print("\n[!] ERROR: Insufficient privileges. You MUST run this script as an Administrator / Root user.")
sys.exit(1)
except KeyboardInterrupt:
print("\n[-] Sniffer halted successfully. Exiting clean.")
sys.exit(0)
if __name__ == "__main__":
main()