|
| 1 | +''' |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2018 Fabricio Roberto Reinert |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +''' |
| 24 | + |
| 25 | +import sys |
| 26 | +import subprocess |
| 27 | +import argparse |
| 28 | +import locale |
| 29 | +import os |
| 30 | +import json |
| 31 | + |
| 32 | + |
| 33 | +class Storage: |
| 34 | + """WLAN Profile Storage""" |
| 35 | + |
| 36 | + storage = dict() |
| 37 | + |
| 38 | + def add_to_storage(self, key, val): |
| 39 | + """Update a single key and value""" |
| 40 | + self.storage[key] = val |
| 41 | + return self.storage[key] |
| 42 | + |
| 43 | + def bulk_storage_update(self, profiles): |
| 44 | + """Update the storage by a dictionary""" |
| 45 | + self.storage.update(profiles) |
| 46 | + return self.storage |
| 47 | + |
| 48 | + def get_by_key(self, key): |
| 49 | + """Keyfind""" |
| 50 | + return self.storage[key] |
| 51 | + |
| 52 | + def export(self, output): |
| 53 | + if output in ["txt", "json"]: |
| 54 | + filename = "wlan_profiles." + output |
| 55 | + |
| 56 | + # Json Export |
| 57 | + if output == "json": |
| 58 | + try: |
| 59 | + with open(filename, "w") as fopen: |
| 60 | + json.dump(self.storage, fopen) |
| 61 | + return filename |
| 62 | + except: |
| 63 | + return "Operation Error" |
| 64 | + |
| 65 | + # Text File Export |
| 66 | + if output == "txt": |
| 67 | + try: |
| 68 | + with open(filename, "w") as fopen: |
| 69 | + for k, v in self.storage.items(): # iterating freqa dictionary |
| 70 | + fopen.write(k + "\t" + v) |
| 71 | + return filename |
| 72 | + except: |
| 73 | + return "Operation Error" |
| 74 | + |
| 75 | + def __repr__(self): |
| 76 | + return self.storage.__str__() |
| 77 | + |
| 78 | + |
| 79 | +class Profiler: |
| 80 | + """Windows WLAN Profiler""" |
| 81 | + |
| 82 | + def __init__(self, lang): |
| 83 | + self.lang = lang |
| 84 | + self.lang_vars = { |
| 85 | + "pt_BR": { |
| 86 | + "profiles": "Todos os Perfis de Usu\\xa0rios", |
| 87 | + "profile": "Conte\\xa3do da Chave", |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def get_profiles(self): |
| 92 | + """Get all profiles""" |
| 93 | + data = ( |
| 94 | + subprocess.check_output(["netsh", "wlan", "show", "profiles"]) |
| 95 | + .decode("utf-8", errors="backslashreplace") |
| 96 | + .split("\r\n") |
| 97 | + ) |
| 98 | + profiles = [ |
| 99 | + i.split(":")[1][1:] |
| 100 | + for i in data |
| 101 | + if self.lang_vars[self.lang]["profiles"] in i |
| 102 | + ] |
| 103 | + return profiles |
| 104 | + |
| 105 | + def get_passwords_from_profiles(self): |
| 106 | + """Get all passwords from profiles""" |
| 107 | + memchache = dict() |
| 108 | + for i in self.get_profiles(): |
| 109 | + try: |
| 110 | + results = ( |
| 111 | + subprocess.check_output( |
| 112 | + ["netsh", "wlan", "show", "profile", i, "key=clear"] |
| 113 | + ) |
| 114 | + .decode("utf-8", errors="backslashreplace") |
| 115 | + .split("\r\n") |
| 116 | + ) |
| 117 | + results = [ |
| 118 | + b.split(":")[1][1:] |
| 119 | + for b in results |
| 120 | + if self.lang_vars[self.lang]["profile"] in b |
| 121 | + ] |
| 122 | + try: |
| 123 | + memchache[i] = "{:<}".format(results[0]) |
| 124 | + except IndexError: |
| 125 | + memchache[i] = "" |
| 126 | + except subprocess.CalledProcessError: |
| 127 | + print("{:<30}| {:<}".format(i, "ENCODING ERROR")) |
| 128 | + return memchache |
| 129 | + |
| 130 | + |
| 131 | +class Program: |
| 132 | + def __init__(self, lang): |
| 133 | + self.storage = Storage() |
| 134 | + self.profiler = Profiler(lang) |
| 135 | + |
| 136 | + def run(self): |
| 137 | + self.storage.bulk_storage_update(self.profiler.get_passwords_from_profiles()) |
| 138 | + return self.storage |
| 139 | + |
| 140 | + def export(self, type): |
| 141 | + if type == "cli": |
| 142 | + return str(self.storage) |
| 143 | + else: |
| 144 | + return program.storage.export(type) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + |
| 149 | + # Available locations |
| 150 | + locations = ["pt_BR"] |
| 151 | + language = locale.getdefaultlocale()[0] |
| 152 | + |
| 153 | + # Check OS |
| 154 | + if os.name != "nt": |
| 155 | + sys.exit(">> Your OS is not supported. Windows only. (%s)" % os.name) |
| 156 | + |
| 157 | + # Check Location |
| 158 | + if not language in locations: |
| 159 | + sys.exit(">> Your OS Language is not supported (%s)" % language) |
| 160 | + |
| 161 | + # CLI |
| 162 | + parser = argparse.ArgumentParser( |
| 163 | + prog="WLAN PROFILES", |
| 164 | + description="Get all WLAN Profiles and passwords stored on OS (MS Windows only)", |
| 165 | + ) |
| 166 | + parser.add_argument( |
| 167 | + "-o", |
| 168 | + "--output", |
| 169 | + help="Output options", |
| 170 | + type=str, |
| 171 | + choices=["cli", "json", "txt"], |
| 172 | + default="cli", |
| 173 | + ) |
| 174 | + args = parser.parse_args() |
| 175 | + |
| 176 | + # Main program |
| 177 | + program = Program(language) |
| 178 | + program.run() |
| 179 | + sys.exit(">> " + program.export(args.output)) |
0 commit comments