Code:
import threading
from [Link] import sniff, IP, TCP, UDP, ARP
import tkinter as tk
from tkinter import ttk, messagebox
from collections import Counter
import datetime
class PacketSnifferApp:
def __init__(self, root):
[Link] = root
[Link]("Packet Sniffer with Report")
# Setup Treeview (table) columns
columns = ("src_ip", "dst_ip", "protocol")
[Link] = [Link](root, columns=columns, show="headings")
[Link]("src_ip", text="Source")
[Link]("dst_ip", text="Destination")
[Link]("protocol", text="Protocol")
[Link]("src_ip", width=200)
[Link]("dst_ip", width=200)
[Link]("protocol", width=100)
[Link](fill=[Link], expand=True)
# Add report button
btn_frame = [Link](root)
btn_frame.pack(fill=tk.X, pady=5)
report_btn = [Link](btn_frame, text="Generate Report",
command=self.generate_report)
report_btn.pack(side=[Link], padx=10)
# Packet statistics storage
self.packet_count = 0
self.protocol_counter = Counter()
self.source_counter = Counter()
# Start sniffing thread
self.sniff_thread = [Link](target=self.start_sniffing, daemon=True)
self.sniff_thread.start()
# Capture start time
self.start_time = [Link]()
def start_sniffing(self):
sniff(prn=self.packet_callback, store=False)
def packet_callback(self, packet):
proto_name = "Unknown"
src = ""
dst = ""
if ARP in packet:
proto_name = "ARP"
src = packet[ARP].psrc
dst = packet[ARP].pdst
elif IP in packet:
ip_layer = packet[IP]
src = ip_layer.src
dst = ip_layer.dst
if TCP in packet:
tcp_layer = packet[TCP]
sport = tcp_layer.sport
dport = tcp_layer.dport
if sport in (80, 8080) or dport in (80, 8080):
proto_name = "HTTP"
elif sport == 443 or dport == 443:
proto_name = "TLS"
else:
proto_name = "TCP"
elif UDP in packet:
proto_name = "UDP"
else:
proto_name = f"IP Proto {ip_layer.proto}"
# Update stats
self.packet_count += 1
self.protocol_counter[proto_name] += 1
if src:
self.source_counter[src] += 1
# Insert packet info into UI
[Link](0, lambda: [Link]("", "end", values=(src, dst, proto_name)))
def generate_report(self):
duration = [Link]() - self.start_time
report_lines = []
report_lines.append("==== Packet Sniffer Report ====\n")
report_lines.append(f"Capture Start Time: {self.start_time.strftime('%Y-%m-%d
%H:%M:%S')}")
report_lines.append(f"Capture Duration: {str(duration)}")
report_lines.append(f"Total Packets Captured: {self.packet_count}\n")
report_lines.append("Packets by Protocol:")
for proto, count in self.protocol_counter.most_common():
report_lines.append(f" {proto}: {count}")
report_lines.append("\nTop 10 Source IPs:")
for ip, count in self.source_counter.most_common(10):
report_lines.append(f" {ip}: {count}")
report_text = "\n".join(report_lines)
# Save report to file
filename = f"packet_report_{self.start_time.strftime('%Y%m%d_%H%M%S')}.txt"
try:
with open(filename, "w") as f:
[Link](report_text)
[Link]("Report Generated", f"Report saved as:\n{filename}")
except Exception as e:
[Link]("Error", f"Failed to save report:\n{e}")
if __name__ == "__main__":
root = [Link]()
app = PacketSnifferApp(root)
[Link]()
Description
A packet tracer similar to wireshark made in python using scapy
and tkinter library for gui.
It tracks source address , destination address along with the
protocol being used.