from tkinter import *
from tkinter import scrolledtext
from tkinter import filedialog
from scapy.all import *
import dpkt
import sys
import datetime
import socket
def clicked():
window.quit()
def mac_addr(address):
"""Convert a MAC address to a readable/printable string
Args:
address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06')
Returns:
str: Printable/readable MAC address
"""
return ':'.join('%02x' % ord(b) for b in address)
def inet_to_str(inet):
"""Convert inet object to a string
Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address
"""
# First try ipv4 and then ipv6
try:
return socket.inet_ntop(socket.AF_INET, inet)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, inet)
def print_http_requests(pcap):
for timestamp, buf in pcap:
# Unpack the Ethernet frame (mac src/dst, ethertype)
eth = dpkt.ethernet.Ethernet(buf)
# Make sure the Ethernet data contains an IP packet
if not isinstance(eth.data, dpkt.ip.IP):
print( 'Non IP Packet type not supported %s\n' % eth.data.__class__.__name__)
continue
# Now grab the data within the Ethernet frame (the IP packet)
ip = eth.data
# Check for TCP in the transport layer
if isinstance(ip.data, dpkt.tcp.TCP):
# Set the TCP data
tcp = ip.data
# Now see if we can parse the contents as a HTTP request
try:
request = dpkt.http.Request(tcp.data)
except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
continue
# Pull out fragment information (flags and offset all packed into off field, so use bitmasks)
do_not_fragment = bool(ip.off & dpkt.ip.IP_DF)
more_fragments = bool(ip.off & dpkt.ip.IP_MF)
fragment_offset = ip.off & dpkt.ip.IP_OFFMASK
# Print out the info
print ('HTTP request: %s\n' % repr(request))
# Check for Header spanning acrossed TCP segments
if not tcp.data.endswith('\r\n'):
print ('\nHEADER TRUNCATED! Reassemble TCP segments!\n')
def download():
file = filedialog.askopenfilename(filetypes = (("Trafic files","*.pcap"),))
with open(file,'rb') as f:
pcap = dpkt.pcap.Reader(f)
print_http_requests(pcap)
#packs = rdpcap(f)
#txt_log.insert(INSERT,"Протоколы и их количество в файле:\n")
#txt_log.insert(INSERT,packs)
#ГРАФИКА
window = Tk()
#Окно
window.geometry('1015x630')
window.title("SniffPcap")
#Кнопки
btn = Button(window, text="Выход", command=clicked)
btn.place(x=960, y=600)
#Меню
menu = Menu(window)
new_item = Menu(menu, tearoff=0)
new_item.add_command(label='Загрузить' , command=download)
new_item.add_separator()
new_item.add_command(label='Авторы')
new_item.add_separator()
new_item.add_command(label='О программе')
#new_item.add_command(label='О программе', command=имя функции для выполнения команды)
menu.add_cascade(label='Файл', menu=new_item)
window.config(menu=menu)
#Поле ввода
txt_in = scrolledtext.ScrolledText(window, width=60, height=25)
txt_out = scrolledtext.ScrolledText(window, width=60, height=25)
txt_log = scrolledtext.ScrolledText(window, width=123, height=10)
txt_in.place(x=5, y=5)
txt_out.place(x=510, y=5)
txt_log.place(x=5, y=420)
window.mainloop()