-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
469 lines (426 loc) · 17.7 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import uuid, json, flask, time, sys
from flask import Flask, request, redirect
from monster import render, init
from database import get, set, get_All, delete
from mailer import mail
import re, hashlib, random
from secrets_parser import parse
import hashlib
import requests
from urllib.parse import quote
from datetime import datetime, timedelta
salt = parse("variables.txt")["salt"]
botapi=parse("variables.txt")["botapi"]
startTime=int(str(parse("variables.txt")["startTime"]))
endTime=int(str(parse("variables.txt")["endTime"]))
def during_event():
return startTime<=time.time() and time.time()<=endTime
try:
profanity=open("profanity.txt").read()
except:
profanity=""
app = Flask(__name__)
init(app)
def is_valid_email(email):
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
return re.match(pattern, email) is not None
def User():
return {
"name": "",
"level": {
"ctf":0,
"cryptic":0
},
"logs": "",
"email": "",
"password": "",
"phonenumber": ""
}
@app.errorhandler(404)
def on_404(_):
backlink=get("backlinks", request.path.split("/", 1)[1])
if backlink["Ok"]:
return flask.Response(backlink["Value"])
return render("404", locals())
def auth(cookies):
if "email" in cookies:
account = get("accounts", cookies["email"])
if account["Ok"] == False:
return account
if (
account["Value"]["password"]
== hashlib.sha256(cookies["password"].encode()).hexdigest()
):
return account
else:
return {"Ok": False}
else:
return {"Ok": False}
@app.get("/")
def home():
loggedIn = auth(dict(request.cookies))
if loggedIn["Ok"]:
status = "Logout"
status_url = "/logout"
else:
status = "Log In"
status_url = "/auth"
announcements = ""
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
confetti = render("components/confetti.html", locals())
if loggedIn["Ok"]:
if during_event():
button_text="PLAY NOW"
redirect_url="/play"
countdown = render("buttons/home", locals())
else:
button_text="SIGN IN WITH EMAIL"
redirect_url="/auth"
if loggedIn["Value"]["email"] in admin:
countdown = render("buttons/home", locals())
else:
starttime=startTime*1000
countdown = render("countdown", locals())
else:
button_text="SIGN IN WITH EMAIL"
redirect_url="/auth"
countdown = render("buttons/home", locals())
return render("components/index.html", locals())
@app.get("/leaderboard")
def leaderboard():
loggedIn = auth(dict(request.cookies))
fetchedData = get_All("leaderboard")
levels={}
for x in fetchedData["Value"]:
if x["points"] not in levels:
levels[x["points"]]=[x]
else:
levels[x["points"]].append({"time":x["time"], "name":x["name"], "email":x["email"], "points":x["points"]})
leaderboard_data=[]
players_added=[]
for level in sorted(levels)[::-1]:
level=levels[level]
level.sort(key=lambda data: data["time"])
for player in level:
if player["email"] not in players_added:
leaderboard_item={"time":player["time"], "name":player["name"], "points":player["points"]}
if request.cookies.get("email") in admin:
leaderboard_item["email"]=player["email"]
leaderboard_data.append(leaderboard_item)
players_added.append(player["email"])
leaderboard = []
for i in range(len(leaderboard_data)):
name = leaderboard_data[i]["name"]
level = leaderboard_data[i]["points"]
email_text = ""
if request.cookies.get("email") in admin:
email_text=" • Email: "+leaderboard_data[i]["email"]
rank = i + 1
leaderboard.append(render("components/leaderboard/card.html", locals()))
if loggedIn["Ok"]:
status = "Logout"
status_url = "/logout"
else:
status = "Log In"
status_url = "/auth"
announcements = ""
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
return render("components/leaderboard/leaderboard.html", locals())
@app.get("/logout")
def logout():
return render("logout", locals())
@app.get("/auth")
def auth_page():
loggedIn = auth(dict(request.cookies))
if loggedIn["Ok"]:
status = "Logout"
status_url = "/logout"
else:
status = "Log In"
status_url = "/auth"
announcements = ""
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
return render("components/auth.html", locals())
@app.get("/api/auth")
def auth_api():
args = dict(request.args)
if "password" not in args or "email" not in args or "method" not in args or "phonenumber" not in args:
return json.dumps({"error": "Missing Fields", "args": args})
if is_valid_email(args["email"]):
if get("emails", args["email"])["Ok"]:
account = get("accounts", args["email"])["Value"]
if (account["password"]== hashlib.sha256(args["password"].encode()).hexdigest()):
return json.dumps({"success": True})
else:
return json.dumps({"error": "Incorrect Password"})
else:
if "name" not in args or "otp" not in args:
return json.dumps({"error": "Missing Fields", "args": args})
args["name"]=args["name"].replace("\t", "").replace(" ", " ").strip()
if args["method"]=="signup":
if len(args["phonenumber"])!=10:
return json.dumps({"error":"Phone Number must be 10 digits long"})
if len(args["name"])>50:
return json.dumps({"error":"Name cannot exceed 50 characters in total"})
for x in args["name"].replace(" ", ""):
if x not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
return json.dumps({"error":"Name can only contain alphabets"})
for x in args["name"].split(" "):
if x in profanity:
return json.dumps({"error":"Profanity Detected"})
if args["otp"] == get_otp(args["email"]):
set("emails", args["email"], {"email":args["email"], "time":time.time()})
if "source" in args:
set("trackers", args["email"], args["source"])
user = User()
user["email"] = args["email"]
user["name"] = args["name"].title()
user["password"] = hashlib.sha256(args["password"].encode()).hexdigest()
user["phonenumber"] = args["phonenumber"]
set("accounts", args["email"], user)
set("leaderboard", args["email"], {"email":args["email"], "time":time.time(), "points":0, "name":args["name"]})
return json.dumps({"success": True})
else:
if args["method"]=="login":
return json.dumps({"error":"Account does not exist"})
else:
return json.dumps({"error": "Wrong OTP"})
return json.dumps({"error": "Invalid Email"})
def get_otp(email):
digest = hashlib.sha256((email+salt).encode()).digest()
random.seed(int.from_bytes(digest, "big"))
otp = str(random.randint(0, 999999))
return "0" * (6 - len(otp)) + otp
@app.get("/send_otp")
def sendotp():
args = dict(request.args)
if "email" not in args:
return json.dumps({"error": "Missing Fields"})
if is_valid_email(args["email"]):
otp=get_otp(args["email"])
digit1=otp[0]
digit2=otp[1]
digit3=otp[2]
digit4=otp[3]
digit5=otp[4]
digit6=otp[5]
mail(
args["email"],
"Sudocrypt 2024 OTP Verification",
render("mail/otp", locals()),
)
return json.dumps({"otp": "success"})
else:
return json.dumps({"error": "Invalid Email"})
@app.get("/set_level")
def set_level():
loggedIn = auth(dict(request.cookies))
if loggedIn["Ok"] and request.cookies.get("email") in admin:
args = dict(request.args)
if "source" not in args or "answer" not in args and "markup" not in args or "levelid" not in args:
return {"error":"Missing Fields"}
if (not args["levelid"].startswith("ctf-") and not args["levelid"].startswith("cryptic-")) or not args["levelid"].split("-", 1)[1].isdigit():
return {"error":"Level Id format must be {ctf or cryptic}-{number}"}
set("levels", args["levelid"], {"id":args["levelid"], "answer":args["answer"], "markup":args["markup"], "sourcehint":args["source"]})
requests.get(botapi+"/create_level?level="+args["levelid"])
return {"level":"success"}
return ""
@app.get("/delete_level")
def delete_level():
loggedIn = auth(dict(request.cookies))
if loggedIn["Ok"] and request.cookies.get("email") in admin:
args = dict(request.args)
if "level" not in args:
return {"error":"Missing Fields"}
delete("levels", args["level"])
return {"level":"success"}
return ""
@app.get("/admin")
def admin_page():
loggedIn = auth(dict(request.cookies))
if request.cookies.get("email") in admin and loggedIn["Ok"]:
levels=[]
levels_db=get_All("levels")
levelsData=json.dumps(dict([[x["id"], x] for x in levels_db["Value"]]))
for x in levels_db["Value"]:
levelid=x["id"]
levels.append(render("admin/level", locals()))
if loggedIn["Ok"]:
status = "Logout"
status_url = "/logout"
else:
status = "Log In"
status_url = "/auth"
announcements = ""
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
return render("admin/admin", locals())
return render("404")
@app.get("/play")
def play():
args=dict(request.args)
type="cryptic"
if "type" in args and args["type"]=="ctf":
type="ctf"
loggedIn = auth(dict(request.cookies))
if loggedIn["Ok"] and (request.cookies.get("email") in admin or during_event()):
if loggedIn["Ok"]:
status = "Logout"
status_url = "/logout"
else:
status = "Log In"
status_url = "/auth"
announcements = render("components/announcements.html", locals())
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
level=type+"-"+str(loggedIn["Value"]["level"][type])
level_Details=get("levels", str(level))
markup=level_Details["Value"]["markup"]
level_Answer_Hash=hashlib.sha256(("public_salt_to_prevent_rainbow_tables"+level_Details["Value"]["answer"]).encode()).hexdigest()
chats=[]
avatar="https://api.dicebear.com/9.x/big-smile/svg?seed="+quote(loggedIn["Value"]["name"])
chat_btn = render("chat/chat", locals())
sourcehint=level_Details["Value"]["sourcehint"]
return render("play", locals())
return render("redirect")
@app.get("/chats_checksum")
def chat_checksum():
loggedIn = auth(dict(request.cookies))
args=dict(request.args)
if "type" not in args:
return {"error":"Missing Fields"}
if loggedIn["Ok"]:
all_messages=get_All("messages/"+request.cookies.get("email"))
hints=get_All("hints/"+args["type"]+"-"+str(loggedIn["Value"]["level"][args["type"]]))
leads=get("status", "leads-"+args["type"])
return {"checksum":hashlib.sha256(json.dumps(all_messages).encode()+json.dumps(hints).encode()).hexdigest(), "leads":leads["Value"], "announcements":hashlib.sha256(json.dumps(get_All("announcements")).encode()).hexdigest()}
else:
return {"error":"Not LoggedIn"}
@app.get("/chats")
def chats():
loggedIn = auth(dict(request.cookies))
args=dict(request.args)
if "type" not in args:
return {"error":"Missing Fields"}
if loggedIn["Ok"]:
all_messages=get_All("messages/"+request.cookies.get("email"))
hints=get_All("hints/"+args["type"]+"-"+str(loggedIn["Value"]["level"][args["type"]]))
return {"chats":all_messages["Value"], "hints":hints["Value"]}
else:
return {"error":"Not LoggedIn"}
@app.get("/announcements")
def announcements():
loggedIn = auth(dict(request.cookies))
args=dict(request.args)
if loggedIn["Ok"]:
all_announcements=get_All("announcements")["Value"]
return {"announcements":all_announcements}
else:
return {"error":"Not LoggedIn"}
@app.get("/submit_message")
def submit_message():
loggedIn = auth(dict(request.cookies))
args=dict(request.args)
type="cryptic"
if "type" in args and args["type"]=="ctf":
type="ctf"
if "content" not in args:
return {"error":"Missing Fields"}
if loggedIn["Ok"]:
if not (request.cookies.get("email") in admin or during_event()):
if time.time()<startTime:
return {"error":"The event has not commenced yet"}
if time.time()>=endTime:
return {"error":"The event has concluded"}
leads=get("status", "leads-"+type)
if not leads["Value"]:
return {"error":"Leads are unavailable at this moment"}
last_Time=get("messagetimes", loggedIn["Value"]["email"])
disqualified=get("disqualified", loggedIn["Value"]["email"])
if not disqualified["Ok"]:
disqualified["Value"]=False
set("disqualified", loggedIn["Value"]["email"], False)
else:
if disqualified["Value"]:
return {"error":"You have been disqualified. Kindly contact us at [email protected] for clarification."}
if not last_Time["Ok"]:
last_Time["Value"]=0
if time.time()-last_Time["Value"]<4:
return {"error":"Messaging Rate Limit Exceeded"}
if len(args["content"])>=512:
return {"error":"Message is too long"}
player=loggedIn["Value"]
id=str(time.time())
set("messagetimes", player["email"], time.time())
set("messages/"+player["email"], id, {"author":loggedIn["Value"]["email"], "content":args["content"], "time":id, "id":id, "type":type})
level=type+"-"+str(player["level"][type])
requests.get(botapi+"/send_message?level="+str(level)+"&name="+quote(player["name"])+"&email="+quote(player["email"])+"&content="+quote(args["content"]))
return {"success":True}
else:
return {"error":"Not LoggedIn"}
def unix_to_ist(unix_time):
dt = datetime.fromtimestamp(unix_time)
ist_offset = timedelta(hours=5, minutes=30)
ist_time = dt + ist_offset
return ist_time.strftime('%Y-%m-%d %H:%M:%S')
@app.get("/submit")
def submit():
loggedIn = auth(dict(request.cookies))
args=dict(request.args)
type="cryptic"
if "type" in args and args["type"]=="ctf":
type="ctf"
if "answer" not in args:
return {"error":"Missing Fields"}
if loggedIn["Ok"]:
if not (request.cookies.get("email") in admin or during_event()):
if time.time()<startTime:
return {"error":"The event has not commenced yet"}
if time.time()>=endTime:
return {"error":"The event has concluded"}
last_Time=get("submittimeout", loggedIn["Value"]["email"])
disqualified=get("disqualified", loggedIn["Value"]["email"])
if not disqualified["Ok"]:
disqualified["Value"]=False
set("disqualified", loggedIn["Value"]["email"], False)
else:
if disqualified["Value"]:
return {"error":"You have been disqualified. Kindly contact us at [email protected] for clarification."}
if not last_Time["Ok"]:
last_Time["Value"]=0
if time.time()-last_Time["Value"]<=1:
return {"error":"Submit Rate Limit Exceeded"}
set("submittimeout", loggedIn["Value"]["email"], time.time())
status = "Logout"
status_url = "/logout"
announcements = ""
header = render("components/header.html", locals())
footer = render("components/footer.html", locals())
level=type+"-"+str(loggedIn["Value"]["level"][type])
level_Details=get("levels", str(level))
player=loggedIn["Value"]
playerLogs=get("logs", player["email"])
if not playerLogs["Ok"]:
set("logs", player["email"], str(unix_to_ist(int(time.time())))+" : "+args["answer"]+"\n")
else:
newLog=playerLogs["Value"]+str(unix_to_ist(int(time.time())))+" : "+args["answer"]+"\n"
if len(newLog)>10240:
newLog=newLog[len(newLog)-10240:]
set("logs", player["email"], newLog)
if level_Details["Value"]["answer"]==args["answer"]:
player["level"][type]+=1
set("accounts", player["email"], player)
set("leaderboard", player["email"], {"email":player["email"], "time":time.time(), "points":player["level"]["ctf"]+player["level"]["cryptic"], "name":player["name"]})
for x in get_All("messages/"+player["email"])["Value"]:
if x["type"]==type:
delete("messages/"+player["email"], x["id"])
return {"success":True}
else:
return {"success":False}
else:
return {"error":"Not LoggedIn"}
app.run(host="127.0.0.1", port=int(sys.argv[1]))