View Saved WiFi Passwords Programmatically (Java, Python)

Link Copied To Clipboard !

view-wifi-password-programmatically Java

Checking saved WiFi password is fairly an easy task. However, running a simple program and getting result instantly saves lot of time. It just takes a command or a mouse click to run the program whereas you might have to go through several steps or even several windows if you do it manually. Also, its fun exploring options and its so much fun especially if it involves coding.

WiFi is a wireless technology that connects devices to the internet. We see it almost every devices from mobile phones, laptops, pcs, fridges to even CCTV cameras, cars etc.

Programming languages contain modules for making OS calls and networking packages. We can utilize these to view saved passwords. The basic idea is to execute relevant command and displaying obtained result. The command, however differs from one operation system to another. So, although the same program works for different machines, we have to make it run different commands depending on machines or operating system.

Therefore, first lets see how we can get saved passwords using commands.

How to get saved password using command in windows ?

Following command displays all saved passwords with their SSIDs.

netsh wlan show profiles

Now, we note the SSID whose password is to be displayed (lets say its xyz) and run the following command. It will show all profile information of xyz wifi except the password.

netsh wlan show profiles xyz

The final step is to append key=clear at the end of above command. We will now be able to look at the security key or password. You can simple run the whole command at once or if you want to differentiate between commands, you can also run separately.

netsh wlan show profiles xyz key=clear

How to get saved password using command in linux(ubuntu) ?

Open a terminal and type the following(you need to replace <your-SSID> with your network’s name) :

sudo cat /etc/NetworkManager/system-connections/<your-SSID>

Go to the line containing psk . The content at right side of it is the password. If you do not feel like scrolling, simply run the following command and it will show just the password.

sudo grep '^psk=' /etc/NetworkManager/system-connections/<your-SSID>

Now that we know what command is to use in what operating system, we need to write a program that will find out what operating system our device is running and execute suitable command and provide that output. Lets see how we can achieve this in various programming languages.

How to view saved password using Java ?

package np.com.bsubash.others;

import java.io.IOException;
import java.util.Scanner;

/**
 * @author subash
 * @since Oct 27, 2021
 */
public class WifiPassword {

	public static void main(String[] args) {
		System.out.println("checking wifi password...");
		String networkName = "bhattaraisubash";
		String os = System.getProperty("os.name").toLowerCase();
		String command = "netsh wlan show profiles " + networkName + " key=clear";

		// check if os is unix and replace command if yes
		if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0) {
			command = "sudo grep '^psk=' /etc/NetworkManager/system-connections/" + networkName;
		}
		System.out.println("executing " + command + " ...");

		/*
		 * Note that the command for linux requires sudo previleges so you might have to
		 * find a way to provide a sudo password or grant an access. The windows command
		 * however works without needing to change anything.
		 */
		try {
			Process process = Runtime.getRuntime().exec(command);
			System.out.println(process.info());
			Scanner scanner = new Scanner(process.getInputStream());
			while (scanner.hasNextLine()) {
				System.out.println(scanner.nextLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

How to view saved wifi password using Python ?

import subprocess

data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8', errors="backslashreplace").split('\n')
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
for i in profiles:
    try:
        results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8', errors="backslashreplace").split('\n')
        results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
        try:
            print ("{:<30}|  {:<}".format(i, results[0]))
        except IndexError:
            print ("{:<30}|  {:<}".format(i, ""))
    except subprocess.CalledProcessError:
        print ("{:<30}|  {:<}".format(i, "ENCODING ERROR"))
input("")

The programs are basic and can be improved. They were just for showing what could be done. So, feel free to tweak around.

HAPPY CODING !


You May Also Like

News Letter