利用Python提取无线网络密码信息
作者:互联网
import subprocess import sys import json import os import optparse import re class WiFiNetworksCredentials: def __init__(self) -> None: self.filename = self.get_params() def get_params(self): parser = optparse.OptionParser('Usage: <Program> -o output fielname') parser.add_option('-o', '--output', dest='output', type='string', help='Specify filename to output') options, args = parser.parse_args() if options.output is None: print(parser.usage) sys.exit() if os.path.exists(options.output): print("[-] The file exists") sys.exit() return options.output def retrieve_all_wifi_networks(self): try: command = 'netsh wlan show profiles' result = subprocess.check_output(command, shell=True, encoding='gbk') #这里需要注意,加上gbk编码,否则会报错 pattern = r'(?:所有用户配置文件\s*:\s*)(.*)' wifi_networks_list = re.findall(pattern, result) if len(wifi_networks_list)>0: return wifi_networks_list else: return False except Exception as e: print(e) sys.exit() def retrieve_password(self, wifi_name): try: command = 'netsh wlan show profiles "%s" key=clear' % wifi_name #这里的字符串需要用双引号包裹,否则因为在windows中有些wifi名称很奇葩,有各种符号,会导致命令执行失败 result = subprocess.check_output(command, shell=True, encoding='gbk') pattern = r'(?:关键内容\s*:\s*)(.*)' search_result = re.search(pattern, result) if search_result: print(search_result.group(1)) return wifi_name, search_result.group(1) except: pass def run(self): wifi_networks_list = self.retrieve_all_wifi_networks() wifi_credentials = {} if wifi_networks_list: for wifi in wifi_networks_list: res = self.retrieve_password(wifi) if res: wifi_name, key_info = res wifi_credentials[wifi_name] = key_info print(wifi_credentials) with open(self.filename, 'w') as f: f.write(json.dumps(wifi_credentials)) if __name__ == '__main__': wifiobj = WiFiNetworksCredentials() wifiobj.run()
标签:__,Python,self,wifi,密码,result,output,无线网络,networks 来源: https://www.cnblogs.com/jason-huawen/p/16361862.html