To extract saved Wi-Fi passwords in Linux using Python

Published on Aug. 22, 2023, 12:16 p.m.

To extract saved Wi-Fi passwords in Linux using Python, you can use the subprocess module to run the cat command in the terminal, which displays the contents of the Wi-Fi password file on the system.

Here’s an example Python code that extracts the Wi-Fi passwords using the subprocess module:

import subprocess

# Run the command to get the names of the Wi-Fi profiles
command_output = subprocess.check_output(['ls', '/etc/NetworkManager/system-connections'], shell=True)
profiles_list = [i.split(".")[0] for i in command_output.decode('utf-8').split("\n") if i]

# Loop through each profile and get its password
for profile in profiles_list:
    try:
        profile_info = subprocess.check_output(['sudo', 'cat', f'/etc/NetworkManager/system-connections/{profile}'], shell=True)
        password = [line.split("psk=")[1][1:-2] for line in profile_info.decode('utf-8').split("\n") if "psk=" in line][0]
        print(f'Wi-Fi Name: {profile}, Password: {password}')
    except subprocess.CalledProcessError as e:
        print(f'Failed to extract Wi-Fi password for profile {profile}. Error: {e}')

This code first runs the ls command to get the Wi-Fi profiles stored on the system, and then loops through each profile to get its password using a cat command. The Wi-Fi name and password are then printed to the console.

Note that this code requires that Python is run with administrative privileges to access the password files. Additionally, please note that extracting a Wi-Fi password that belongs to another user may violate their privacy and may even be illegal in certain jurisdictions.

Tags: