# 可以用80端口进行试验,只要打开浏览器浏览网页即可捕获数据包
import os
######################################
##########
########## 定义结构体数组,保存源IP、目的IP、协议类型
##########
######################################
class inf:
def __init__(self):
self.SrcIP = '' # 源IP
self.DesIP = '' # 目的IP
self.Protocol = '' # 协议类型
######################################
##########
########## 指定端口,获取数据包并保存
##########
######################################
from scapy.all import *
def get_pacp(port):
filter = 'dst port ' + str(port) # 目的端口
ifs = 'Realtek 8822CE Wireless LAN 802.11ac PCI-E NIC' # 本机网卡
#ip = "116.4.8.127" # 该选项可以用来抓取指定地址的数据包,也可写域名,如:www.baidu.com
dpkt = sniff(iface=ifs,filter=filter,count=1) # 使用sniff函数抓包
path = 'desktop/demo.pacp' # 文件的保存路径
wrpcap(path,dpkt) # 将抓取到的包保存为pcap文件
return path
######################################
##########
########## 解析pcap文件,获取信息
##########
######################################
import struct
import binascii
def parse(path) :
print("Get in parse()")
fpcap = open(path,'rb') # 读取.pcap文件
data = fpcap.read() # 读取到data缓冲中
# print(type(data)) # 输出数据类型 data为bytes类型的数据
lst = inf()
# 获取Protocol,1=ICMP,2=IGMP,6=TCP,17=UDP, 63字节为Protocol
if(data[63:64] == b'\x06') :
lst.protocol = 'TCP'
elif(data[63:64] == b'\x01') :
lst.protocol = 'ICMP'
elif(data[63:64] == b'\x02') :
lst.protocol = 'IGMP'
elif(data[63:64] == b'\x11') :
lst.protocol = 'UDP'
# 获取源IP,66,67,68,69字节为源IP
Sip1 = int.from_bytes(data[66:67], byteorder='big', signed=False)
Sip2 = int.from_bytes(data[67:68], byteorder='big', signed=False)
Sip3 = int.from_bytes(data[68:69], byteorder='big', signed=False)
Sip4 = int.from_bytes(data[69:70], byteorder='big', signed=False)
lst.SrcIP = '%s.%s.%s.%s' % (Sip1,Sip2,Sip3,Sip4)
# 获取目的IP,70,71,72,73字节为目的IP
Dip1 = int.from_bytes(data[70:71], byteorder='big', signed=False)
Dip2 = int.from_bytes(data[71:72], byteorder='big', signed=False)
Dip3 = int.from_bytes(data[72:73], byteorder='big', signed=False)
Dip4 = int.from_bytes(data[73:74], byteorder='big', signed=False)
lst.DesIP = '%s.%s.%s.%s' % (Dip1,Dip2,Dip3,Dip4)
return lst
######################################
##########
########## 通过文本框输入获取指定端口号
##########
######################################
def get_port():
lst = inf() # 定义结构体,用于保存获取的数据
port = PortValue.get() # 通过文本框输入获取指定端口号
path = get_pacp(port) # 抓取指定端口的数据包,保存在path路径下
lst = parse(path) # 解析数据包,返回lst结构体
print("Protocol: ", lst.protocol)
print("Source IP:",lst.SrcIP)
print("Destination IP:",lst.DesIP)
################ 在UI显示信息 ##################
################ 1.显示协议类型 ##################
Protocol = Label(root, text="Protocol", bg="white",bd=5, font=("Arial",12), width=15, height=2)
Protocol.place(x=100, y=70)
Pvalue = Label(root, text=lst.protocol, bg="white",bd=5, font=("Arial",12), width=15, height=2)
Pvalue.place(x=250, y=70)
################ 2.显示源IP ##################
SrcIP = Label(root, text="Source IP", bg="white",bd=5, font=("Arial",12), width=15, height=2)
SrcIP.place(x=100, y=120)
Svalue = Label(root, text=lst.SrcIP, bg="white",bd=5, font=("Arial",12), width=15, height=2)
Svalue.place(x=250, y=120)
################ 3.显示目的IP ##################
DesIP = Label(root, text="Destination IP", bg="white",bd=5, font=("Arial",12), width=15, height=2, relief=FLAT)
DesIP.place(x=100, y=170)
Dvalue = Label(root, text=lst.DesIP, bg="white",bd=5, font=("Arial",12), width=15, height=2, relief=FLAT)
Dvalue.place(x=250, y=170)
######################################
##########
########## 主函数,编写界面
##########
######################################
from tkinter import *
from tkinter.tix import Tk, Control, ComboBox #升级的组合控件包
from tkinter.messagebox import showinfo, showwarning, showerror #各种类型的提示框
################ 界面的初始化 ################
root = Tk() # 初始化Tk()
root.title("Information From Package") # 设置窗口标题
root.geometry("500x250") # 设置窗口大小 注意:是x 不是*
root.resizable(width=True, height=True) # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
root.tk.eval('package require Tix') #引入升级包,这样才能使用升级的组合控件
############# 端口输入文本框 #############
Port = Label(root, text="PORT", bd=5, font=("Arial",12), width=5, height=1)
Port.place(x=100, y=25, relheight=0.1)
PortValue = Entry(root)
PortValue.place(x=160, y=25, relwidth=0.2, relheight=0.1)
############# 输入完成后,点击Finis按钮结束输入 #############
btn1 = Button(root, text='Finish', command=get_port ) # 进入get_port函数,进行抓包解包显示
btn1.place(x=270, y=25, relwidth=0.2, relheight=0.1)
root.mainloop()