-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifipasswords_exe.py
194 lines (167 loc) · 5.93 KB
/
wifipasswords_exe.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
__copyright__ = "Copyright (C) 2019-2021 Joe Campbell"
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY
# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see < https: // www.gnu.org/licenses/>.
import os
import sys
from argparse import ArgumentParser, RawTextHelpFormatter
from colorama import init, Fore, Back
from wifipasswords import WifiPasswords, __version__, __licence__
def get_command_line_arguments() -> dict:
"""
Get Command line arguments passed to script.\n
Returns - args as dictionary.
"""
helpstring = """
Show all wifi passwords stored on windows and linux. MacOS to be added.
For all commands below PATH is optional for saving files.
If no path is specified, will default the current working directory.
wifipasswords version {}
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions, see the GPLv3 Licence file attached.
{} - Licence: {} """.format(
__version__, __copyright__, __licence__
)
parser = ArgumentParser(description=helpstring, formatter_class=RawTextHelpFormatter)
parser.add_argument(
"-a",
"--all",
help="Show DNS, current visible networks, and save as JSON and wpa_supplicant.conf in given path",
nargs="?",
const=".",
metavar="PATH",
)
parser.add_argument(
"-c",
"--current",
help="show currently visible networks",
nargs="?",
const=".",
metavar="",
)
parser.add_argument(
"-d",
"--dns",
help="Show DNS configurations",
nargs="?",
const=".",
metavar="",
)
parser.add_argument(
"-j",
"--json",
help="output JSON of networks in given directory",
nargs="?",
const=".",
metavar="PATH",
)
parser.add_argument(
"-v",
"-V",
"--version",
action="version",
version=__version__,
)
parser.add_argument(
"-w",
"--wpasupplicant",
help="create a wpa_supplicant.conf for all networks.",
nargs="?",
const=".",
metavar="PATH",
)
args = vars(parser.parse_args())
return args
def print_output_heading() -> None:
"""
Prints the static header for the command line output.\n
"""
print(Fore.BLACK + Back.WHITE + "{:^92}".format("WIFI PASSWORDS " + __version__))
print("{:^92}".format("Lists known wifi networks and passwords."))
print("{:^92}".format("'>' before SSID denotes the currently connected network."))
print("{:^92}".format("(M) denotes metered connection. --help to show more options."))
print("*" * 92)
print(
Back.WHITE
+ Fore.BLACK
+ "{:^33} | {:^13} | {:^40}".format("NETWORK", "AUTH", "PASSWORD")
)
def print_network_data(networks, connected_ssids) -> None:
"""
Print data from the network dictionary.
"""
for key, n in networks.items():
if key in connected_ssids:
connected = ">"
else:
connected = " "
if n["metered"]:
metered = Fore.LIGHTBLACK_EX + "(M)"
else:
metered = ""
print(
"{:<1} {:<31} | {:<13} | {:<36} {}".format(
connected, key, n["auth"], n["psk"], metered
)
)
def print_output_footer() -> None:
"""
Print static footer.
"""
print("\r\n" + "*" * 92)
print("{:>92}".format("JC 2019-2021"))
def print_visible_networks(current_networks) -> None:
"""
Output for visible networks.
"""
print(Fore.BLACK + Back.WHITE + "{:^92}".format("Currently Visible Networks"))
print(current_networks)
print("*" * 92)
def print_current_dns_config(dns_config) -> None:
"""
output for passed DNS config.
"""
print(Fore.BLACK + Back.WHITE + "{:^92}".format("Currently DNS Configuration"))
print(dns_config)
print("*" * 92)
def cli():
init(autoreset=True)
pw = WifiPasswords()
args = get_command_line_arguments()
print_output_heading()
data = pw.get_passwords()
active_ssids = pw.get_currently_connected_ssids()
print_network_data(data, active_ssids)
print_output_footer()
if not args["current"] is None or not args["all"] is None:
print_visible_networks(pw.get_visible_networks())
if not args["dns"] is None or not args["all"] is None:
print_current_dns_config(pw.get_dns_config())
if not args["wpasupplicant"] is None or not args["all"] is None:
if args["wpasupplicant"] is None:
args["wpasupplicant"] = args["all"]
print()
pw.save_wpa_supplicant(
os.path.join(args["wpasupplicant"], "wpa_supplicant.conf"), data, True, "GB"
)
print(
f"wpa_supplicant.conf written to {os.path.join(args['wpasupplicant'],'wpa_supplicant.conf')}"
)
if not args["json"] is None or not args["all"] is None:
if args["json"] is None:
args["json"] = args["all"]
print()
pw.save_json(os.path.join(args["json"], "networks_data.json"), data)
print("JSON saved >> {}".format(os.path.join(args["json"], "networks_data.json")))
os.system("pause")
if __name__ == "__main__":
sys.exit(cli())