|
| 1 | +###################################### |
| 2 | +#Copyright of David Bombal, 2021 # |
| 3 | +#https://www.davidbombal.com # |
| 4 | +#https://www.youtube.com/davidbombal # |
| 5 | +###################################### |
| 6 | + |
| 7 | +# Import subprocess so we can use system commands |
| 8 | +import subprocess |
| 9 | + |
| 10 | +# Import the re module so that we can make use of regular expressions. |
| 11 | +import re |
| 12 | + |
| 13 | +# Python allows us to run system commands by using a function provided by the subprocess module |
| 14 | +# (subprocess.run(<list of command line arguments goes here>, <specify the second argument if you want to capture the output>)) |
| 15 | +# The script is a parent process and creates a child process which runs the system command, |
| 16 | +# and will only continue once the child process has completed. |
| 17 | +# To save the contents that gets sent to the standard output stream (the terminal) |
| 18 | +# we have to specify that we want to capture the output, |
| 19 | +# so we specify the second argument as capture_output = True. |
| 20 | +# This information gets stored in the stdout attribute. |
| 21 | +# The information is stored in bytes and we need to decode it to Unicode |
| 22 | +# before we use it as a String in Python. |
| 23 | +command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode() |
| 24 | + |
| 25 | +# We imported the re module so that we can make use of regular expressions. |
| 26 | +# We want to find all the Wifi names which is always listed after "ALL User Profile :". |
| 27 | +# In the regular expression we create a group of all characters until the return escape sequence (\r) appears. |
| 28 | +profile_names = (re.findall("All User Profile : (.*)\r", command_output)) |
| 29 | + |
| 30 | +# We create an empty list outside of the loop where dictionaries with all the wifi |
| 31 | +# username and passwords will be saved. |
| 32 | +wifi_list = list() |
| 33 | + |
| 34 | +# If we didn't find profile names we didn't have any wifi connections, |
| 35 | +# so we only run the part to check for the details of the wifi and |
| 36 | +# whether we can get their passwords in this part. |
| 37 | +if len(profile_names) != 0: |
| 38 | + for name in profile_names: |
| 39 | + # Every wifi connection will need its own dictionary which will be appended to the wifi_list |
| 40 | + wifi_profile = dict() |
| 41 | + # We now run a more specific command to see the information about the specific wifi connection |
| 42 | + # and if the Security key is not absent we can possibly get the password. |
| 43 | + profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode() |
| 44 | + # We use a regular expression to only look for the absent cases so we can ignore them. |
| 45 | + if re.search("Security key : Absent", profile_info): |
| 46 | + continue |
| 47 | + else: |
| 48 | + # Assign the ssid of the wifi profile to the dictionary |
| 49 | + wifi_profile["ssid"] = name |
| 50 | + # These cases aren't absent and we should run them "key=clear" command part to get the password |
| 51 | + profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode() |
| 52 | + # Again run the regular expressions to capture the group after the : which is the password |
| 53 | + password = re.search("Key Content : (.*)\r", profile_info_pass) |
| 54 | + # Check if we found a password in the regular expression. All wifi connections will not have passwords. |
| 55 | + if password == None: |
| 56 | + wifi_profile["password"] = None |
| 57 | + else: |
| 58 | + # We assign the grouping (Where the password is contained) we are interested to the password key in the dictionary. |
| 59 | + wifi_profile["password"] = password[1] |
| 60 | + # We append the wifi information to the wifi_list |
| 61 | + wifi_list.append(wifi_profile) |
| 62 | + |
| 63 | +for x in range(len(wifi_list)): |
| 64 | + print(wifi_list[x]) |
| 65 | + |
0 commit comments