资源简介
基于python+scapy设计协议编辑器,基于Tkinter的Python GUI界面设计。实现了MAC、ARP、IP 、TCP、UDP协议的编辑与发送,并且支持生成协议字段的默认值,支持用户输入协议字段值,发包前对协议字段的合理性进行检查,发包前自动计算并显示校验和,支持单次发包、多次发包,支持连续发包,连续发包时可随时停止和计算并显示数据包发送速度的功能。
代码片段和文件信息
# coding=utf-8
import datetime
import tkinter
from tkinter import *
from tkinter.constants import *
from tkinter.ttk import Treeview style
from scapy.all import *
from scapy.layers.inet import *
from scapy.layers.l2 import *
tk = tkinter.Tk()
tk.title(“协议编辑器“)
# tk.geometry(“1000x700“)
# 使窗体最大化
tk.state(“zoomed“)
# 左右分隔窗体
main_panedwindow = PanedWindow(tk sashrelief=RAISED sashwidth=5)
# 协议编辑区窗体
protocol_editor_panedwindow = PanedWindow(orient=VERTICAL sashrelief=RAISED sashwidth=5)
# 协议导航树
protocols_tree = Treeview()
# 当前网卡的默认网关
default_gateway = [a for a in os.popen(‘route print‘).readlines() if ‘ 0.0.0.0 ‘ in a][0].split()[-3]
# 用来终止数据包发送线程的线程事件
stop_sending = threading.Event()
# 状态栏类
class StatusBar(frame):
def __init__(self master):
frame.__init__(self master)
self.label = Label(self bd=1 relief=SUNKEN anchor=W)
self.label.pack(fill=X)
def set(self fmt *args):
self.label.config(text=fmt % args)
self.label.update_idletasks()
def clear(self):
self.label.config(text=““)
self.label.update_idletasks()
# 状态栏
status_bar = StatusBar(tk)
status_bar.pack(side=BOTTOM fill=X)
status_bar.set(“%s“ ‘开始‘)
def create_protocols_tree():
“““
创建协议导航树
:return: 协议导航树
“““
protocols_tree.heading(‘#0‘ text=‘选择网络协议‘ anchor=‘w‘)
# 参数:parent index iid=None **kw (父节点,插入的位置,id,显示出的文本)
# 应用层
applicatoin_layer_tree_entry = protocols_tree.insert(““ 0 “应用层“ text=“应用层“) # ““表示父节点是根
http_packet_tree_entry = protocols_tree.insert(applicatoin_layer_tree_entry 1 “HTTP包“ text=“HTTP包“)
dns_packet_tree_entry = protocols_tree.insert(applicatoin_layer_tree_entry 1 “DNS包“ text=“DNS包“)
# 传输层
transfer_layer_tree_entry = protocols_tree.insert(““ 1 “传输层“ text=“传输层“)
tcp_packet_tree_entry = protocols_tree.insert(transfer_layer_tree_entry 0 “TCP包“ text=“TCP包“)
upd_packet_tree_entry = protocols_tree.insert(transfer_layer_tree_entry 1 “UDP包“ text=“UDP包“)
# 网络层
ip_layer_tree_entry = protocols_tree.insert(““ 2 “网络层“ text=“网络层“)
ip_packet_tree_entry = protocols_tree.insert(ip_layer_tree_entry 0 “IP包“ text=“IP包“)
icmp_packet_tree_entry = protocols_tree.insert(ip_layer_tree_entry 1 “ICMP包“ text=“ICMP包“)
arp_packet_tree_entry = protocols_tree.insert(ip_layer_tree_entry 2 “ARP包“ text=“ARP包“)
# 网络接入层
ether_layer_tree_entry = protocols_tree.insert(““ 3 “网络接入层“ text=“网络接入层“)
mac_frame_tree_entry = protocols_tree.insert(ether_layer_tree_entry 1 “MAC帧“ text=“MAC帧“)
protocols_tree.bind(‘<>‘ on_click_protocols_tree)
style = style(tk)
# get disabled entry colors
disabled_bg = style.lookup(“TEntry“ “fieldbackground“ (“disabled“))
style.map(“Treeview“
fieldbackground=[(“disabled“ disabled_bg)]
foreground=[(“disabled“ “gray“)]
bac
评论
共有 条评论