Retrieving and Displaying Wi-Fi SSIDs and Passwords on Windows Using Go

Javelin
2 min readAug 25, 2024

--

Download Release

In this tutorial, you’ll learn how to write a Go program that fetches and displays saved Wi-Fi SSIDs and their corresponding passwords on a Windows machine. We’ll dive into executing system commands, using regular expressions to parse command output, and handling string manipulation in Go.

Prerequisites

Before you begin, make sure you have the following:

  • A Windows machine
  • Go installed on your system (You can download it from here)
  • Basic knowledge of Go programming

Source code:

https://github.com/javelinsoft/Windows-WiFi-Password-Viewer

Understanding the Code

1. Retrieving Wi-Fi Profiles:

  • The program starts by running the netsh wlan show profiles command to get a list of all saved Wi-Fi profiles on the machine. This is done using the exec.Command function.
  • The output of the command is captured in a bytes.Buffer.

2. Extracting SSIDs:

  • The program uses a regular expression to match the lines containing the SSIDs. The regular expression \s{4}All User Profile\s{5}:\s(.*) is used to match lines with four leading spaces, the exact string "All User Profile", five spaces, a colon, and then the SSID.

3. Displaying SSIDs and Passwords:

  • For each SSID found, the program prints the SSID and then runs another netsh command (netsh wlan show profile name=<SSID> key=clear) to retrieve the password for that SSID.
  • The password is extracted using another regular expression Key Content\s*:\s*(.*), which matches the "Key Content" line and captures the password.

Run the Program

Once you have written the code, you can build the program:

Build in linux for windows:

GOOS=windows GOARCH=386 go build -o Windows-WiFi-Password-Viewer.exe Windows-WiFi-Password-Viewer.go

Build in windows:

go build -o Windows-WiFi-Password-Viewer.exe Windows-WiFi-Password-Viewer.go

This compiled program displays all the saved Wi-Fi SSIDs on your machine along with their passwords.

Handle Errors and Edge Cases

  • No profiles found: If no Wi-Fi profiles are found, the program will not print any SSIDs.
  • Error in command execution: If there is an error in running any of the netsh commands, the program will display an error message.

Conclusion

This Go program is a practical example of how to interact with system commands and extract useful information using regular expressions. The key challenge was to adjust the regular expression to handle the specific spacing requirements of the netsh command output.

--

--

No responses yet