-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge-simulator.py
124 lines (99 loc) · 3.49 KB
/
challenge-simulator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import time
import paho.mqtt.client as mqtt
#############################
### Configuration Section ###
#############################
ChallengeReference = "0d3d6876-c8c5-4f89-af4d-057c6ae9dcbc"
MqttHost = "127.0.0.1"
SimulatorClientId = "ChallengeSimulator"
##########################################################################################################################################################
#########################################
### Topic configration - don't change ###
#########################################
ChallengeEventTopic = "challenges/event"
RegisterTopic = "challenges/registry"
ResetChallengeTopic = "challenges/" + ChallengeReference + "/reset"
RequestToRegisterTopic = "pd/requestregister"
########################
### Global variables ###
########################
Client: mqtt.Client = None
LastIncommingMessage: str = None
######################
### Helper methods ###
######################
def RegisterMqtt():
global Client
global ChallengeEventTopic
global RegisterTopic
global SimulatorClientId
print("Registering to MQTT Broker...")
Client = mqtt.Client(SimulatorClientId, clean_session=False)
Client.message_callback_add(RegisterTopic, OnMessage)
Client.message_callback_add(ChallengeEventTopic, OnMessage)
Client.connect(MqttHost, port=1883, keepalive=60)
Client.subscribe([(RegisterTopic, 2)], [(ChallengeEventTopic, 2)])
Client.loop_start()
print("MQTT Ready!")
def OnMessage(client, userdata, message: mqtt.MQTTMessage):
global LastIncommingMessage
payload = message.payload.decode("utf-8")
print(
"\n[!] Received message on topic"
+ message.topic
+ ", with payload: "
+ payload
+ "\n"
)
def ShowMenu():
print()
print("Tobania.EscapeContainer Challenge Simulator v1.0")
print(
"This tool will keep listening on the MQTT Topics ChallengeEventTopic and RegisterTopic and print the output of the messages.\n"
+ " You can then submit messages to other topics, using the following menu"
)
print("Options:")
print("1: Send RequestToRegister")
print("2: Send ResetChallenge")
print("0: Refresh this menu")
print("hit any other key to terminate the simulator")
def SendToTopic(topicName: str, payload: str):
global Client
if Client.is_connected:
Client.publish(topicName, payload)
def SendRequestToRegister():
global RequestToRegisterTopic
print("Sending Request to register...")
SendToTopic(RequestToRegisterTopic, "RequestToRegister")
def SendResetChallenge():
global ResetChallengeTopic
resetPayload = input("[?] Please enter the reset payload to send: ")
if resetPayload is not None and resetPayload != "":
SendToTopic(ResetChallengeTopic, resetPayload)
# Main method
def main():
global ChallengeReference
global Client
global LastIncommingMessage
print("Starting Challenge Simulator with parameters:")
print("\t- " + ChallengeReference)
RegisterMqtt()
while True:
ShowMenu()
command = input("[?] What do you want to do? ")
if command == "1":
SendRequestToRegister()
print("-----------")
elif command == "2":
SendResetChallenge()
print("-----------")
elif command == "0":
print("-----------")
else:
print("Terminating simulator")
break
Client.loop_stop()
Client.disconnect()
# Start logic
if __name__ == "__main__":
main()