-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hazprone.py
306 lines (290 loc) · 12.1 KB
/
Hazprone.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
import sys
import os
import re
import texts
import shutil
import datetime
import subprocess
from python_terraform import *
from functions import (
gen_whitelist
)
# Global Variables------------------------------------------------------------------------------------
opr = ""
quest_id = ""
user_prof = ""
ip_addr = ""
quest_folder = ""
quest_folder_final = ""
quest_game_env = ""
quest_game_env_del = ""
dt = datetime.datetime.now()
day = dt.strftime("%d")
month = dt.strftime("%b")
year = dt.strftime("%Y")
hour = dt.strftime("%I")
minute = dt.strftime("%M")
seconds = dt.strftime("%S")
time = dt.strftime("%p")
rand = f'{day}-{month}-{year}-{hour}-{minute}-{seconds}-{time}'
# Beginning of Functions-----------------------------------------------------
if sys.argv[1].casefold() == "help":
print(texts.HAZPRONE)
sys.exit()
elif sys.argv[1].casefold() == "quest":
if len(sys.argv) == 3:
if sys.argv[2].casefold() == "q1-openworld":
print(texts.OPENWORLD)
sys.exit()
elif sys.argv[2].casefold() == "q2-up":
print(texts.UP)
sys.exit()
elif sys.argv[2].casefold() == "q3-infiltrate":
print(texts.INFILTRATE)
sys.exit()
elif sys.argv[2].casefold() == "q4-finals":
print(texts.FINALS)
sys.exit()
else:
print(texts.QUESTS)
sys.exit()
else:
print(texts.QUESTS)
sys.exit()
elif len(sys.argv) < 4:
print("Atleast Three Arguments are required!!")
sys.exit()
elif len(sys.argv) > 4:
print("Only Three arguments are required!!")
sys.exit()
else:
operation = sys.argv[1].lower()
quest = sys.argv[2].lower()
profile = sys.argv[3].lower()
print('''
*******************************************
===========================================
Initializing HAZPRONE!!!
+++++++++++++++++++++++++++++++++++++++++++
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
''')
if operation != "start" and operation != "stop":
print("Operation must be either start or stop only!!")
sys.exit()
elif quest != "q1-openworld" and quest != "q2-up" and quest != "q3-infiltrate" and quest != "q4-finals":
print(texts.QUESTS_WRONG)
sys.exit()
else:
profile_check = subprocess.Popen(["aws", "--profile", profile, "sts", "get-caller-identity"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
profile_check.wait()
profile_out = profile_check.stdout.read().decode("utf-8").strip()
profile_err = profile_check.stderr.read().decode("utf-8").strip()
if profile_err:
print(profile_err)
print("\n HAZPRONE :: Enter the correct Profile name!!\n")
sys.exit()
else:
print(f'\nProfile {profile} is Valid!!\n')
if operation == "stop":
print("\nHAZPRONE :: Enter yes to confirm stopping the quest!!!\n")
stop_op = input("Your Entry : ")
if stop_op.casefold() != "yes":
print("\nQuiting Hazprone!!!\n")
sys.exit()
else:
opr = "stop"
quest_id = quest
user_prof = profile
elif operation == "start":
print("Checking for whitelist.txt!!\n")
if not os.path.exists("./whitelist.txt"):
print('''
HAZPRONE :: whitelist.txt file does not exist. Hazprone will
create a new file by curling your public ip from
ifconfig.me if you type yes. If you would rather
configure the file on your own type no to exit the
framework.\n
''')
whitelist_conf_1 = input("Your Entry : ")
if whitelist_conf_1 == "yes":
print("\nHAZPRONE :: Generating new whitelist.txt file!\n")
ip = gen_whitelist()
print("Your IP Address is: "+ip)
if not ip:
print("\nHAZPRONE :: Unknown Error! Manually add IP to whitelist.txt!")
sys.exit()
if not re.findall(r".*\/(\d+)", ip):
ip = ip.split("/")[0] + "/32"
with open("./whitelist.txt", "w") as ip_file:
ip_file.write(ip)
print("Written Succesfully")
else:
print("\nHAZPRONE :: Quiting Hazprone!\n")
sys.exit()
else:
print('''
HAZPRONE :: whitelist.txt file exists! Type yes if you would like
Hazprone to curl your IP Adress from ifconfig.me and
overwrite the existing whitelist.txt file, otherwise
type no to continue with existing file.\n
''')
whitelist_conf = input("Your Entry : ")
if whitelist_conf == "yes":
print("\nHAZPRONE :: Generating new whitelist.txt file!\n")
ip = gen_whitelist()
print("Your IP Adress is: "+ip)
if not ip:
print("\nHAZPRONE :: Unknown Error! Manually add IP to whitelist.txt!")
sys.exit()
if not re.findall(r".*\/(\d+)", ip):
ip = ip.split("/")[0] + "/32"
with open("./whitelist.txt", "w") as ip_file:
ip_file.write(ip)
print("Written Succesfully")
else:
print("\nHAZPRONE :: Continuing with existing whitelist.txt file!\n")
with open("./whitelist.txt", "r") as ip_file:
ip = ip_file.read()
print(ip)
print("\nHAZPRONE :: Enter yes to confirm starting the quest!!\n")
start_op = input("Your Entry : ")
if start_op.casefold() != "yes":
print("\nQuiting Hazprone!!!\n")
sys.exit()
else:
opr = "start"
quest_id = quest
user_prof = profile
ip_addr = ip
print("The operation is : "+opr)
print("The quest is : "+quest_id)
print("The profile is : "+user_prof)
print("The ip address is : "+ip_addr)
if quest_id == "q1-openworld":
quest_folder = "./Quests/q1-openworld/terraform"
quest_game_env = f'./q1-openworld-env-{rand}'
elif quest_id == "q2-up":
quest_folder = "./Quests/q2-up/terraform"
quest_game_env = f'./q2-up-env-{rand}'
elif quest_id == "q3-infiltrate":
quest_folder = "./Quests/q3-infiltrate/terraform"
quest_game_env = f'./q3-infiltrate-env-{rand}'
elif quest_id == "q4-finals":
quest_folder = "./Quests/q4-finals/terraform"
quest_game_env = f'./q4-finals-env-{rand}'
else:
print("\nHAZPRONE :: Internal Error! Please try again!!\n")
sys.exit()
region = "us-east-1"
tf = Terraform(working_dir=f'{quest_game_env}/terraform')
if opr == "start" and user_prof:
if os.path.isfile("./hazprone_state.txt") and os.path.exists("./hazprone_state.txt"):
state_file_check = open("hazprone_state.txt", "r")
else:
print('''
HAZPRONE :: Hazprone's state file is missing!!! Please create a
hazprone_state.txt file in the directory where Hazprone's main
files are present.\n
''')
sys.exit()
quest_game_env_del = state_file_check.readline()
state_file_check.close()
if quest_game_env_del != "":
print('''
HAZPRONE :: The hazprone state file is not empty!! Either a Quest has
already been started or some Internal Error has occured.
If a Quest is currently running, please stop it, inorder to
start another quest!\n
''')
sys.exit()
if not os.path.exists(quest_game_env):
print("\nHAZPRONE :: Initializing Quest Environment!!\n")
src = quest_folder.replace('/terraform', '')
dest = quest_game_env
print("\nHAZPRONE :: Copying files!\n")
quest_folder_final = shutil.copytree(src, dest)
if os.path.exists(f'{quest_game_env}/terraform/start.sh'):
start_process = subprocess.Popen(["sh", "start.sh"], cwd=f'{quest_game_env}/terraform')
start_process.wait()
else:
print("\nCLOUDPRONE :: No intermediate start script! Directly starting the Quest!\n")
print("\nHAZPRONE :: Quest Environment is ready!!\n")
elif os.path.exists(quest_game_env):
print("\nHAZPRONE :: Quest Folder already exists!! First Destroy it to start another Session!\n")
print("\nCLOUPRONE :: You could manually remove the folder if nothing else works!!\n")
sys.exit()
if not os.path.exists(quest_game_env):
print("\nHAZPRONE :: There has been an error in creating Quest folder. Make sure Hazprone has all the required permissions!\n")
return_code, stdout, stderr = tf.init(capture_output=False)
return_code, stdout, stderr = tf.plan(capture_output=False, var={'profile': user_prof, 'region': region, 'userIP': ip_addr})
return_code, stdout, stderr = tf.apply(capture_output=False, var={'profile': user_prof, 'region': region, 'userIP': ip_addr})
if return_code != 0:
print("\n HAZPRONE :: Error Code returned by Terraform. Please try again!!\n")
if not os.path.exists("./trash"):
os.makedirs("./trash")
print("\nHAZPRONE :: Deleting Quest Folder!!\n")
shutil.move(quest_game_env, './trash')
elif os.path.exists("./trash"):
print("\nHAZPRONE :: Deleting Quest Folder!!\n")
shutil.move(quest_game_env, './trash')
else:
print("\nHAZPRONE :: Fatal Internal Error has Occured!!\n")
sys.exit()
else:
state_file = open("hazprone_state.txt", "w")
n = state_file.write(quest_game_env)
state_file.close()
print(f'\nHAZPRONE QUEST {quest_id} HAS BEEN INITIATED!!!\n')
sys.exit()
elif opr == "stop" and user_prof:
if os.path.isfile("./hazprone_state.txt") and os.path.exists("./hazprone_state.txt"):
state_file = open("hazprone_state.txt", "r")
else:
print('''
HAZPRONE :: Hazprone's state file is missing!!! Please create a
hazprone_state.txt file in the directory where Hazprone's main
files are present.\n
''')
quest_game_env_del = state_file.readline()
if quest_game_env_del == "":
print('''
HAZPRONE :: The hazprone state file is empty!! Either no Quest has been started or
an Internal Error has occured. If resources have been deployed please
delete them manually!!\n
''')
sys.exit()
state_file.close()
tf = Terraform(working_dir=f'{quest_game_env_del}/terraform')
if not os.path.exists(quest_game_env_del):
print('''
HAZPRONE :: Internal Error!! The Quest folder can't be found!!
Manually destroy the AWS Resources and move the
quest folder to ./trash\n
''')
sys.exit()
return_code, stdout, stderr = tf.destroy(capture_output=False, var={'profile': user_prof, 'region': region, 'userIP': ip_addr})
if return_code != 0:
print("\nHAZPRONE :: Terraform has returned an Error Code. Please try again or manually destroy the resource!\n")
sys.exit()
else:
open("hazprone_state.txt", "w").close()
if os.path.exists(quest_game_env_del) and os.path.isdir(quest_game_env_del):
if not os.path.exists("./trash"):
os.makedirs("./trash")
print("\nHAZPRONE :: Deleting Quest Folder!!\n")
shutil.move(quest_game_env_del, './trash')
elif os.path.exists("./trash"):
print("\nHAZPRONE :: Deleting Quest Folder!!\n")
shutil.move(quest_game_env_del, './trash')
else:
print("\nHAZPRONE :: Fatal Internal Error has Occured!!\n")
sys.exit()
else:
print("\nHAZPRONE :: Quest Folder does not Exist!! Please delete the resources manually!\n")
print(f'\nHAZPRONE QUEST {quest_id} HAS BEEN TERMINATED!!!\n')
sys.exit()
else:
print("\nHAZPRONE :: Unknow Error has occurred!!! Please retry!!\n")
sys.exit()
# The End-------------------------------------------------------------------------------