1
- from flask import Blueprint , render_template , request , redirect , url_for , flash , make_response , session
2
1
import datetime
2
+
3
+ from flask import (Blueprint , flash , make_response , redirect , render_template ,
4
+ request , session , url_for )
5
+
3
6
from flags import stages # import everything from flags.py
4
- bp = Blueprint ('ctf' , __name__ )
7
+
8
+ bp = Blueprint ("ctf" , __name__ )
5
9
6
10
current_stage = 1
7
11
hint_index = 0
8
12
9
- @bp .route ('/flags' , methods = ['GET' , 'POST' ])
10
- @bp .route ('/flags.html' , methods = ['GET' , 'POST' ])
13
+
14
+ @bp .route ("/flags" , methods = ["GET" , "POST" ])
15
+ @bp .route ("/flags.html" , methods = ["GET" , "POST" ])
11
16
def flags ():
12
17
global current_stage , hint_index
13
18
# initialize session variables
14
19
if "submitted_flags" not in session :
15
- session [' submitted_flags' ] = []
20
+ session [" submitted_flags" ] = []
16
21
if "current_stage" not in session :
17
- session ['current_stage' ] = 1
18
-
19
- current_stage = session ['current_stage' ]
20
- submitted_flags = session ['submitted_flags' ]
21
- # need to verify that the user didn't try to skip stages or trick the system
22
-
23
- if request .method == 'POST' :
24
- if 'submit_flag' in request .form :
25
- submitted_flag = request .form .get ('flag' )
26
- if submitted_flag == stages [current_stage ]['flag' ]:
27
- flash (f"Correct flag for Stage { current_stage } !" , 'success' )
22
+ session ["current_stage" ] = 1
23
+
24
+ current_stage = session ["current_stage" ]
25
+ submitted_flags = session ["submitted_flags" ]
26
+ # need to verify that the user didn't try to skip stages or trick the
27
+ # system
28
+
29
+ if request .method == "POST" :
30
+ if "submit_flag" in request .form :
31
+ submitted_flag = request .form .get ("flag" )
32
+ if submitted_flag == stages [current_stage ]["flag" ]:
33
+ flash (f"Correct flag for Stage { current_stage } !" , "success" )
28
34
submitted_flags .append (submitted_flag )
29
- session [' submitted_flags' ] = submitted_flags
35
+ session [" submitted_flags" ] = submitted_flags
30
36
if current_stage < len (stages ):
31
37
current_stage += 1
32
38
hint_index = 0
33
39
elif current_stage == len (stages ):
34
- flash ("You have completed all the stages. Congratulations!" , 'success' )
35
- # if the user has completed all the stages, then flash a message
36
- session ['current_stage' ] = current_stage
40
+ flash (
41
+ "You have completed all the stages. Congratulations!" ,
42
+ "success" )
43
+ # if the user has completed all the stages, then flash a
44
+ # message
45
+ session ["current_stage" ] = current_stage
37
46
else :
38
47
found = False
39
- # if the flag is for a different stage, then put them on their stage
48
+ # if the flag is for a different stage, then put them on their
49
+ # stage
40
50
for stage , stage_data in stages .items ():
41
- if submitted_flag == stage_data ['flag' ]:
42
- flash (f"That's the flag for stage { stage } , but in the wrong order" , 'info' )
51
+ if submitted_flag == stage_data ["flag" ]:
52
+ flash (
53
+ f"That's the flag for stage { stage } , but in the wrong order" , "info" , )
43
54
current_stage = stage
44
55
hint_index = 0
45
56
found = True
46
57
break
47
58
# else:
48
59
if not found :
49
- flash ("Incorrect flag. Try again." , 'danger' )
50
-
51
- elif 'reveal_hint' in request .form :
52
- if hint_index < len (stages [current_stage ]['hints' ]) - 1 : # if there are more hints to reveal
60
+ flash ("Incorrect flag. Try again." , "danger" )
61
+
62
+ elif "reveal_hint" in request .form :
63
+ if (
64
+ hint_index < len (stages [current_stage ]["hints" ]) - 1
65
+ ): # if there are more hints to reveal
53
66
hint_index += 1
54
67
else :
55
- # if the user exhausted all the hints, have it show from the beginning
68
+ # if the user exhausted all the hints, have it show from the
69
+ # beginning
56
70
hint_index = 0
57
71
# flash a message to the user
58
-
59
- flash ("No new hints :( Try harder!" , ' info' )
72
+
73
+ flash ("No new hints :( Try harder!" , " info" )
60
74
# hide the button till they get to next stage
61
-
62
- hints = stages [current_stage ]['hints' ][:hint_index + 1 ]
63
- return render_template ('flags.html' , stage = current_stage , hints = hints , hint_index = hint_index , submitted_flags = submitted_flags , num_hints = len (stages [current_stage ]['hints' ]))
64
75
76
+ hints = stages [current_stage ]["hints" ][: hint_index + 1 ]
77
+ return render_template (
78
+ "flags.html" ,
79
+ stage = current_stage ,
80
+ hints = hints ,
81
+ hint_index = hint_index ,
82
+ submitted_flags = submitted_flags ,
83
+ num_hints = len (stages [current_stage ]["hints" ]),
84
+ )
65
85
66
- @bp .route ('/' , methods = ['GET' ]) # also for index
67
- @bp .route ('/index' , methods = ['GET' ])
68
- @bp .route ('/home' , methods = ['GET' ])
69
- @bp .route ('/index.html' , methods = ['GET' ])
86
+
87
+ @bp .route ("/" , methods = ["GET" ]) # also for index
88
+ @bp .route ("/index" , methods = ["GET" ])
89
+ @bp .route ("/home" , methods = ["GET" ])
90
+ @bp .route ("/index.html" , methods = ["GET" ])
70
91
def index ():
71
- # initialize session variables
92
+ # initialize session variables
72
93
if "submitted_flags" not in session :
73
- session [' submitted_flags' ] = []
94
+ session [" submitted_flags" ] = []
74
95
if "current_stage" not in session :
75
- session [' current_stage' ] = 1
76
-
77
- flash ("Welcome to the CTF, please read the following:" , ' info' )
96
+ session [" current_stage" ] = 1
97
+
98
+ flash ("Welcome to the CTF, please read the following:" , " info" )
78
99
brief = """
79
100
This site is not required to solve the CTF challenge and is not a part of the CTF challenge itself, but a tool to help you keep track of your progress. The flags are not hidden on this site. You need to find them on your own. Good luck!
80
101
81
102
\n
82
- Do not use this site for any illegal activities, please do not attack it in any way as it harms other users who are solving the CTF. The site collects logs for security purposes.
103
+ Do not use this site for any illegal activities, please do not attack it in any way as it harms other users who are solving the CTF. The site collects logs for security purposes.
83
104
\n
84
105
"""
85
106
# everwhere where is /n, replace with <br> for html
86
- brief = brief .split ('\n ' )
87
-
88
- return render_template ('index.html' , summary = brief )
89
-
90
- @bp .route ('/restart' , methods = ['GET' ])
91
- @bp .route ('/restart.html' , methods = ['GET' ])
92
- @bp .route ('/reset' , methods = ['GET' ])
93
- @bp .route ('/reset.html' , methods = ['GET' ])
107
+ brief = brief .split ("\n " )
108
+
109
+ return render_template ("index.html" , summary = brief )
110
+
111
+
112
+ @bp .route ("/restart" , methods = ["GET" ])
113
+ @bp .route ("/restart.html" , methods = ["GET" ])
114
+ @bp .route ("/reset" , methods = ["GET" ])
115
+ @bp .route ("/reset.html" , methods = ["GET" ])
94
116
def restart ():
95
117
session .clear ()
96
- flash ("Progress reset. You are back to Stage 1." , ' info' )
118
+ flash ("Progress reset. You are back to Stage 1." , " info" )
97
119
# reroute to index
98
- return redirect (url_for ('ctf.index' ))
120
+ return redirect (url_for ("ctf.index" ))
121
+
99
122
100
123
# error pages
101
124
@bp .app_errorhandler (404 )
102
125
def page_not_found (error ):
103
- return render_template ('404.html' ), 404
126
+ return render_template ("404.html" ), 404
127
+
104
128
105
129
# nice page for anything else using message.html and return 500
106
130
@bp .app_errorhandler (500 )
107
131
def internal_server_error (error ):
108
- return render_template ('message.html' , title = "500 Internal Server Error" , message = "Please try again later" ), 500
132
+ return (
133
+ render_template (
134
+ "message.html" ,
135
+ title = "500 Internal Server Error" ,
136
+ message = "Please try again later" ,
137
+ ),
138
+ 500 ,
139
+ )
109
140
110
141
111
142
@bp .context_processor
112
143
def inject_today_date ():
113
144
"""
114
145
used for the footer to display the current year
115
146
"""
116
- return {' year' : datetime .date .today ().year }
147
+ return {" year" : datetime .date .today ().year }
0 commit comments