Extracting Saved WiFi Passwords in Windows Using Python

In our digitally connected world, access to the internet has become indispensable. Often, we connect to various WiFi networks, both at home and in public spaces, and Windows conveniently saves these network profiles along with their passwords for automatic connection in the future. However, there might be occasions when you need to retrieve a saved WiFi password, maybe because you’ve forgotten it or need to share it with someone else.

While Windows provides a graphical user interface (GUI) method to view WiFi passwords, it requires navigating through several menus. An alternative approach is to use Python scripting to automate the process. In this article, we’ll explore how to retrieve WiFi passwords in Windows using a Python script.

Prerequisites:

Before we delve into the code, ensure you have the following:

  1. Python installed on your Windows system.
  2. Basic understanding of Python programming.
  3. Access to a command-line interface.

Python Script:

Below is a Python script that retrieves saved WiFi passwords in Windows:

import subprocess
import re

def retrieve_wifi_passwords():
    try:
        # Execute the command to list saved WiFi profiles
        output = subprocess.check_output(["netsh", "wlan", "show", "profiles"], shell=True).decode("latin-1")
        # Extract profile names using regex, handling special characters
        profiles = re.findall(r"Profile\s*:\s(.*)", output)
        passwords = {}

        for profile in profiles:
            # Execute the command to retrieve password for each profile
            results = subprocess.check_output(["netsh", "wlan", "show", "profile", profile.strip(), "key=clear"], shell=True).decode("latin-1")
            password = re.search(r"Key Content\s*: (.*)", results)

            if password is not None:
                passwords[profile] = password.group(1)

        return passwords
    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")
        return None
    except UnicodeDecodeError as e:
        print(f"Error decoding output: {e}")
        return None

def main():
    print("Retrieving WiFi passwords...\n")
    passwords = retrieve_wifi_passwords()

    if passwords:
        print("Saved WiFi Passwords:")
        for profile, password in passwords.items():
            print(f"{profile}: {password}")
    else:
        print("No saved WiFi passwords found.")


if __name__ == "__main__":
    main()

Understanding the Script:

Let’s break down the script:

  1. The script utilizes the subprocess module to execute command-line instructions within Python.
  2. The retrieve_wifi_passwords() function retrieves saved WiFi profiles and their passwords.
  3. It first lists all WiFi profiles using netsh wlan show profiles.
  4. Then, for each profile, it uses netsh wlan show profile [profile_name] key=clear to extract the password.
  5. The extracted passwords are stored in a dictionary with the profile names as keys.
  6. Finally, the main() function orchestrates the retrieval process and displays the results.

Running the Script:

To retrieve WiFi passwords using this script, follow these steps:

  1. Save the script in a file with a .py extension, for example, retrieve_wifi_passwords.py.
  2. Open a command prompt with administrative privileges.
  3. Navigate to the directory containing the script.
  4. Run the script by typing python retrieve_wifi_passwords.py and press Enter.

The script will then display the saved WiFi profiles along with their respective passwords, if available.

Conclusion:

Using Python, we’ve created a simple yet effective script to retrieve saved WiFi passwords in Windows. This method offers convenience and automation, especially when dealing with multiple networks or when the passwords are forgotten. However, it’s important to note that administrative privileges are required to execute the script successfully. Always exercise caution when handling sensitive information such as passwords and ensure compliance with local regulations and policies.


Leave a Reply

Your email address will not be published. Required fields are marked *

DarkLight
×