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

Commit 011ae18

Browse files
committed
improvements
1 parent abdeedc commit 011ae18

File tree

10 files changed

+96
-37
lines changed

10 files changed

+96
-37
lines changed

flags.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"This is a more revealing hint for stage 1.",
77
"This is the most revealing hint for stage 1.",
88
],
9+
"notes": "This is a note for stage 1",
910
},
1011
2: {
1112
"flag": "flag{stage2}",
@@ -14,6 +15,8 @@
1415
"This is a more revealing hint for stage 2.",
1516
"This is the most revealing hint for stage 2.",
1617
],
18+
"notes": "This is a note for stage 2",
19+
1720
},
1821
3: {"flag": "flag{stage3}", "hints": ["Almost there"]},
1922
}

hints/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
def create_app():
9+
print("Creating app")
910
app = Flask(
1011
__name__,
1112
static_folder="static",
@@ -21,7 +22,8 @@ def create_app():
2122
app.config["REMEMBER_COOKIE_SECURE"] = True
2223
# ensure session cookie is secure
2324
app.config["SESSION_COOKIE_SECURE"] = True
24-
25+
app.config["SESSION_PERMANENT"] = True
26+
app.config['PERMANENT_SESSION_LIFETIME'] = 60 #in seconds
2527
with app.app_context():
2628
from . import routes
2729

hints/routes.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from flask import (Blueprint, flash, redirect, render_template,
44
request, session, url_for)
5+
from markupsafe import escape
56

67
from flags import stages # import everything from flags.py
78

@@ -20,26 +21,41 @@ def flags():
2021
session["submitted_flags"] = []
2122
if "current_stage" not in session:
2223
session["current_stage"] = 1
24+
if "hint_index" not in session:
25+
session["hint_index"] = 0
2326

2427
current_stage = session["current_stage"]
2528
submitted_flags = session["submitted_flags"]
26-
# need to verify that the user didn't try to skip stages or trick the
27-
# system
29+
hint_index = session["hint_index"]
30+
31+
32+
# need to verify that the user didn't try to skip stages or trick the system
2833

2934
if request.method == "POST":
3035
if "submit_flag" in request.form:
3136
submitted_flag = request.form.get("flag")
32-
if submitted_flag == stages[current_stage]["flag"]:
37+
submitted_flag = escape(submitted_flag.strip())
38+
# make sure its ascii only and not invalid
39+
if not submitted_flag.isascii():
40+
flash("Invalid flag. Please try again.", "danger")
41+
return redirect(url_for("ctf.flags"))
42+
43+
if submitted_flag in submitted_flags:
44+
flash("You already submitted this flag.", "info")
45+
elif submitted_flag == stages[current_stage]["flag"]:
3346
flash(f"Correct flag for Stage {current_stage}!", "success")
34-
submitted_flags.append(submitted_flag)
35-
session["submitted_flags"] = submitted_flags
47+
if submitted_flag not in submitted_flags:
48+
submitted_flags.append(submitted_flag)
49+
session["submitted_flags"] = submitted_flags
50+
3651
if current_stage < len(stages):
3752
current_stage += 1
3853
hint_index = 0
3954
elif current_stage == len(stages):
4055
flash(
4156
"You have completed all the stages. Congratulations!",
4257
"success")
58+
return render_template("message.html", title="Congratulations!", message="You have completed all the stages of the CTF. ")
4359
# if the user has completed all the stages, then flash a
4460
# message
4561
session["current_stage"] = current_stage
@@ -54,34 +70,51 @@ def flags():
5470
current_stage = stage
5571
hint_index = 0
5672
found = True
73+
74+
if submitted_flag not in submitted_flags:
75+
submitted_flags.append(submitted_flag)
76+
session["submitted_flags"] = submitted_flags
77+
78+
# check if they got all the flags (even if they are out of order)
79+
if sorted(submitted_flags) == sorted([stage_data["flag"] for stage_data in stages.values()]):
80+
flash(
81+
"You have completed all the stages. Congratulations!",
82+
"success",
83+
)
84+
return render_template("message.html", title="Congratulations!", message="You have completed all the stages of the CTF. ")
85+
# if the user has completed all the stages, then flash a
86+
# message
5787
break
5888
# else:
5989
if not found:
6090
flash("Incorrect flag. Try again.", "danger")
6191

6292
elif "reveal_hint" in request.form:
93+
# hint_num = request.form.get("reveal_hint")
6394
if (
64-
hint_index < len(stages[current_stage]["hints"]) - 1
95+
hint_index < len(stages[current_stage]["hints"])
6596
): # if there are more hints to reveal
6697
hint_index += 1
6798
else:
6899
# if the user exhausted all the hints, have it show from the
69100
# beginning
70-
hint_index = 0
101+
# hint_index = 0
71102
# flash a message to the user
72-
flash("No new hints :( Try harder!", "info")
103+
flash("Exhausted all hints for this stage :( Try harder!", "warning")
73104
# hide the button till they get to next stage
74-
75-
hints = stages[current_stage]["hints"][: hint_index + 1]
105+
session["hint_index"] = hint_index
106+
hints = stages[current_stage]["hints"][: hint_index]
107+
notes = stages[current_stage].get("notes")
76108
return render_template(
77-
"flags.html",
109+
"flags.html",title=f"CTFlask - Stage {current_stage}",
78110
stage=current_stage,
79111
hints=hints,
80112
hint_index=hint_index,
81113
submitted_flags=submitted_flags,
82-
num_hints=len(stages[current_stage]["hints"]),
114+
num_hints=len(stages[current_stage]["hints"]),notes=notes
83115
)
84116

117+
85118
@bp.route("/", methods=["GET"]) # also for index
86119
@bp.route("/index", methods=["GET"])
87120
@bp.route("/home", methods=["GET"])
@@ -92,19 +125,20 @@ def index():
92125
session["submitted_flags"] = []
93126
if "current_stage" not in session:
94127
session["current_stage"] = 1
95-
128+
if "hint_index" not in session:
129+
session["hint_index"] = 0
130+
96131
flash("Welcome to the CTF, please read the following:", "info")
97132
brief = """
98-
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!
99-
133+
Using 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. You need to find the flags on your own and not via this site itself. Good luck!
100134
\n
101135
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.
102136
\n
103137
"""
104-
# everwhere where is /n, replace with <br> for html
138+
# everywhere where is /n, replace with <br> for html
105139
brief = brief.split("\n")
106140

107-
return render_template("index.html", summary=brief)
141+
return render_template("index.html", summary=brief, title="CTFlask - Home")
108142

109143

110144
@bp.route("/restart", methods=["GET"])

hints/static/js/code.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (window.history.replaceState) {
2+
3+
window.history.replaceState(null, null, window.location.href);
4+
5+
}

hints/templates/404.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
{% endblock %}
1111

1212
{% block content %}
13-
<h1 class="error">
13+
<h1 class="error col-md-12 text-center">
1414
{{ "404 Page Not Found" }}
1515
</h1>
1616

1717
{% block body %}
18-
<h2>Oops! Looks like the page doesn't exist anymore</h2>
19-
<a href="{{ url_for('ctf.index') }}">Click Here</a> to return to the Home Page
18+
<div class="col-md-12 text-center">
19+
<h4 class="h4 mb-2">Oops! Looks like this page doesn't exist</h4>
20+
<p><a href="{{ url_for('ctf.index') }}">Click Here</a> to return to the Home Page</p>
21+
</div>
22+
2023
{% endblock %}
2124

2225
{% endblock %}

hints/templates/flags.html

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
<h1 class="text-center">CTF Platform</h1>
1313
<div class="mt-4">
1414
<h3>Stage {{ stage }}</h3>
15+
<h5>Notes</h5>
16+
{% if notes is defined and notes %}
17+
<p>{{ notes }}</p>
18+
{% endif %}
19+
1520
<form method="POST">
1621
<div class="form-group">
1722
<label for="flag">Enter Flag:</label>
@@ -21,7 +26,7 @@ <h3>Stage {{ stage }}</h3>
2126
</form>
2227
</div>
2328
{% with messages = get_flashed_messages(with_categories=true) %}
24-
{% if messages %}
29+
{% if messages is defined and messages %}
2530
<div class="mt-4">
2631
{% for category, message in messages %}
2732
<div class="alert alert-{{ category }}" >{{ message }}</div>
@@ -30,8 +35,8 @@ <h3>Stage {{ stage }}</h3>
3035
{% endif %}
3136
{% endwith %}
3237
<div class="mt-4">
33-
<h4>Hints:</h4>
34-
{% if hints %}
38+
<h4>Hints</h4>
39+
{% if hints is defined and hints %}
3540
<ul>
3641
{% for hint in hints %}
3742
<li>{{ hint }}</li>
@@ -42,7 +47,7 @@ <h4>Hints:</h4>
4247
{% endif %}
4348

4449
<form method="POST">
45-
<button type="submit" name="reveal_hint" class="btn btn-secondary">Reveal Next Hint</button>
50+
<button type="input" name="reveal_hint" class="btn btn-secondary" value="d">Reveal Next Hint</button>
4651
</form>
4752

4853
{#<!-- {% if hint_index < num_hints-1 %}
@@ -52,22 +57,23 @@ <h4>Hints:</h4>
5257
</form>
5358
<!-- {% endif %} -->#}
5459
</div>
60+
5561
<div class="mt-4">
56-
<a href="{{ url_for('ctf.restart') }}" class="btn btn-danger">Restart Challenge</a>
57-
</div>
58-
<div class="sidebar">
59-
<h4>Previously Entered Flags</h4>
60-
{% if submitted_flags %}
62+
<h4>Previously Submitted Flags</h4>
63+
{% if submitted_flags is defined and submitted_flags %}
6164
<ul>
6265
{% for flag in submitted_flags %}
6366
<li>{{ flag }}</li>
6467
{% endfor %}
6568
</ul>
6669
{% else %}
67-
<p>No flags entered yet.</p>
70+
<p>No flags submitted yet.</p>
6871
{% endif %}
6972
</div>
70-
73+
<div class="mt-4">
74+
<a href="{{ url_for('ctf.restart') }}" class="btn btn-danger">Restart Challenge</a>
75+
</div>
7176
</div>
77+
7278
{% endblock %}
7379

hints/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{% endblock %}
1010
{% block content %}
1111
<div class="container mt-5">
12-
<h1 class="text-center">CTF Platform</h1>
12+
<h1 class="text-center">CTFlask</h1>
1313

1414
{% with messages = get_flashed_messages(with_categories=true) %}
1515
{% if messages %}
@@ -23,7 +23,7 @@ <h1 class="text-center">CTF Platform</h1>
2323

2424
<!-- give a brief summary of the challenge -->
2525
<div class="mt-4">
26-
<h3>Challenge Summary</h3>
26+
<h3>Rules and Information</h3>
2727
{% for para in summary %}
2828
<p>{{para}}</p>
2929
{% endfor %}

hints/templates/layout.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
<link rel="shortcut icon" href="{{ url_for('static', filename='favicons/favicon.ico') }}">
1919
<meta name="description" content="CTF Hints and Flag Site for the ColaCo Challenge">
2020
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
21-
21+
<!-- js -->
22+
<script src="{{ url_for('static', filename='js/code.js') }}"></script>
2223
{% endblock %}
2324
</head>
2425

hints/templates/message.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010

1111
{% block content %}
1212

13-
<h1 class="error">
13+
<h1 class="error col-md-12 text-center">
1414
{{ title }}
1515
</h1>
1616

1717
{% block body %}
18-
<h2> {{ message }}</h2>
18+
<div class="col-md-12 text-center">
19+
20+
<h2 class="h2 mb-2"> {{ message }}</h2>
21+
</div>
22+
1923
{% endblock %}
2024

2125
{% endblock %}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ itsdangerous==2.2.0
66
Jinja2==3.1.4
77
MarkupSafe==2.1.5
88
Werkzeug==3.0.3
9+
markupsafe
910
gunicorn

0 commit comments

Comments
 (0)