-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (73 loc) · 2.52 KB
/
main.py
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
from paho.mqtt import client as mqtt_client
import uuid
from pyfiglet import Figlet
import os,argparse,sys,time,json
############### params #################
broker = "192.168.127.137"
port = 1883
userfile = "./user.txt"
passfile = "./pass.txt"
########################################
usernames = []
passwords = []
output = {"unauth":False,"weakpass":False,"username":"","password":""}
def on_connect_unauth(client, userdata, flags, rc):
if rc == 0:
output["unauth"] = True
print("[√]发现匿名登陆!!")
client.disconnect()
client.loop_stop()
return True
else:
client.disconnect()
client.loop_stop()
return True
def on_connect_brute(client, userdata, flags, rc):
if rc == 0:
output["weakpass"] = True
print("[√]暴力破解连接成功!")
client.disconnect()
client.loop_stop()
return True
else:
client.disconnect()
client.loop_stop()
return True
def unauthaccess(broker, port):
client = mqtt_client.Client(str(uuid.uuid4()))
client.on_connect = on_connect_unauth
client.connect(broker, port)
client.loop_start()
time.sleep(0.5)
def brute(broker, port, userfile, passfile, usernames, passwords):
if((os.path.exists(userfile)!= True) or (os.path.exists(passfile)!= True)):
print("[X]字典路径不存在!!")
return False
user_file = open(userfile,'r')
for user_name in user_file:
usernames.append(user_name[:-1])
pass_file = open(passfile,'r')
for pass_name in pass_file:
passwords.append(pass_name[:-1])
for user in usernames:
for passwd in passwords:
client = mqtt_client.Client(str(uuid.uuid4()))
client.on_connect = on_connect_brute
print("[+]正在测试: username:"+user+" "+"password:"+passwd)
client.username_pw_set(username=user, password=passwd)
client.connect(broker, port)
client.loop_start()
time.sleep(0.5)
if output["weakpass"] == True:
output["username"] = user
output["password"] = passwd
return
f = Figlet(font="slant", width=100)
print(f.renderText("MQTT Security Tool"))
print("[+]正在测试匿名登陆...")
unauthaccess(broker, port)
if (output["unauth"] == False):
print("[X]不存在匿名登陆,正在暴力破解用户名和密码...")
brute(broker, port, userfile, passfile, usernames, passwords)
data = json.dumps({'result':output})
print(data)