Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 3a1c1bc

Browse files
authored
Merge pull request #2 from avipars/python-code-format-patches
Fixes by format action
2 parents 912f716 + cf6fbac commit 3a1c1bc

File tree

4 files changed

+108
-74
lines changed

4 files changed

+108
-74
lines changed

flags.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
stages = { # Sample dictionary with flags and multiple hints
1+
stages = { # Sample dictionary with flags and multiple hints
22
1: {
33
"flag": "flag{stage1}",
44
"hints": [
55
"This is the least revealing hint for stage 1.",
66
"This is a more revealing hint for stage 1.",
7-
"This is the most revealing hint for stage 1."
8-
]
7+
"This is the most revealing hint for stage 1.",
8+
],
99
},
1010
2: {
1111
"flag": "flag{stage2}",
1212
"hints": [
1313
"This is the least revealing hint for stage 2.",
1414
"This is a more revealing hint for stage 2.",
15-
"This is the most revealing hint for stage 2."
16-
]
15+
"This is the most revealing hint for stage 2.",
16+
],
1717
},
18-
3: {
19-
"flag": "flag{stage3}",
20-
"hints": [
21-
"Almost there"
22-
]
23-
}
18+
3: {"flag": "flag{stage3}", "hints": ["Almost there"]},
2419
}
2520

2621
# if __name__ == "__main__":
27-
# print(stages)
22+
# print(stages)

hints/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from flask import Flask
2-
# from ctf.error_handlers import register_error_handlers # Adjust the import based on your project structure
2+
3+
4+
# from ctf.error_handlers import register_error_handlers # Adjust the
5+
# import based on your project structure
6+
37

48
def create_app():
59
app = Flask(
@@ -9,19 +13,24 @@ def create_app():
913
template_folder="templates",
1014
)
1115
app.config["SECRET_KEY"] = "CTF_K3YF0RS355i0n5!"
12-
# ensure session cookie is httponly
16+
# ensure session cookie is httponly
1317
app.config["SESSION_COOKIE_HTTPONLY"] = True
1418
# ensure session cookie is same site
1519
app.config["SESSION_COOKIE_SAMESITE"] = "Strict"
16-
app.config["REMEMBER_COOKIE_SECURE"] = True # ensure remember cookie is secure
17-
app.config["SESSION_COOKIE_SECURE"] = True # ensure session cookie is secure
20+
# ensure remember cookie is secure
21+
app.config["REMEMBER_COOKIE_SECURE"] = True
22+
# ensure session cookie is secure
23+
app.config["SESSION_COOKIE_SECURE"] = True
1824

1925
with app.app_context():
2026
from . import routes
27+
2128
app.register_blueprint(routes.bp)
2229
# register_error_handlers(app)
2330
# app.config['referrer_policy'] = 'strict-origin-when-cross-origin'
2431
return app
32+
33+
2534
# app = Flask(
2635
# __name__,
2736
# static_folder="static",
@@ -32,5 +41,4 @@ def create_app():
3241

3342
# register_error_handlers(app)
3443
# app.config['referrer_policy'] = 'strict-origin-when-cross-origin'
35-
3644
from hints import routes

hints/routes.py

Lines changed: 87 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,147 @@
1-
from flask import Blueprint, render_template, request, redirect, url_for, flash, make_response, session
21
import datetime
2+
3+
from flask import (Blueprint, flash, make_response, redirect, render_template,
4+
request, session, url_for)
5+
36
from flags import stages # import everything from flags.py
4-
bp = Blueprint('ctf', __name__)
7+
8+
bp = Blueprint("ctf", __name__)
59

610
current_stage = 1
711
hint_index = 0
812

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"])
1116
def flags():
1217
global current_stage, hint_index
1318
# initialize session variables
1419
if "submitted_flags" not in session:
15-
session['submitted_flags'] = []
20+
session["submitted_flags"] = []
1621
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")
2834
submitted_flags.append(submitted_flag)
29-
session['submitted_flags'] = submitted_flags
35+
session["submitted_flags"] = submitted_flags
3036
if current_stage < len(stages):
3137
current_stage += 1
3238
hint_index = 0
3339
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
3746
else:
3847
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
4050
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", )
4354
current_stage = stage
4455
hint_index = 0
4556
found = True
4657
break
4758
# else:
4859
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
5366
hint_index += 1
5467
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
5670
hint_index = 0
5771
# flash a message to the user
58-
59-
flash("No new hints :( Try harder!", 'info')
72+
73+
flash("No new hints :( Try harder!", "info")
6074
# 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']))
6475

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+
)
6585

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"])
7091
def index():
71-
# initialize session variables
92+
# initialize session variables
7293
if "submitted_flags" not in session:
73-
session['submitted_flags'] = []
94+
session["submitted_flags"] = []
7495
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")
7899
brief = """
79100
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!
80101
81102
\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.
83104
\n
84105
"""
85106
# 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"])
94116
def restart():
95117
session.clear()
96-
flash("Progress reset. You are back to Stage 1.", 'info')
118+
flash("Progress reset. You are back to Stage 1.", "info")
97119
# reroute to index
98-
return redirect(url_for('ctf.index'))
120+
return redirect(url_for("ctf.index"))
121+
99122

100123
# error pages
101124
@bp.app_errorhandler(404)
102125
def page_not_found(error):
103-
return render_template('404.html'), 404
126+
return render_template("404.html"), 404
127+
104128

105129
# nice page for anything else using message.html and return 500
106130
@bp.app_errorhandler(500)
107131
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+
)
109140

110141

111142
@bp.context_processor
112143
def inject_today_date():
113144
"""
114145
used for the footer to display the current year
115146
"""
116-
return {'year': datetime.date.today().year}
147+
return {"year": datetime.date.today().year}

run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
if __name__ == "__main__":
88
# app.run(debug=True, port="5000") #debug ,
9-
9+
1010
app.run(debug=True, host="0.0.0.0", port=8080) # production

0 commit comments

Comments
 (0)