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