-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
245 lines (193 loc) · 7.22 KB
/
app.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
#!/usr/bin/env python3
from os import getenv
from datetime import datetime, timedelta
from flask import (
current_app,
render_template,
session,
request,
redirect,
url_for,
jsonify,
flash
)
from flask_recaptcha import ReCaptcha
from app.config import (
WEBSITE_TITLE,
CTFD_URL,
CHALLENGES,
MAX_INSTANCE_COUNT,
MAX_INSTANCE_DURATION,
MAX_INSTANCE_PER_TEAM,
ADMIN_ONLY
)
from app.utils import (
get_total_instance_count,
get_challenge_count_per_team,
check_challenge_name,
check_access_key,
get_challenge_info,
remove_all_instances,
remove_container_by_id,
create_instances,
remove_user_running_instance,
remove_old_instances
)
from app.auth import login_required, admin_required
from app.models import Instances
from app.app import create_app
app = create_app()
recaptcha = ReCaptcha(app)
recaptcha.theme = 'dark'
def render(template, **kwargs):
"""
Shortcut for the render_template flask function.
"""
return render_template(template, title=WEBSITE_TITLE, ctfd_url=CTFD_URL,
max_instance_duration=MAX_INSTANCE_DURATION, challenges_option=CHALLENGES,
instances_count=get_total_instance_count(), **kwargs)
@app.route("/admin", methods=['GET'])
@admin_required
def admin():
"""
Admin dashboard with all instances.
"""
return render("admin.html")
@app.route("/login", methods=['GET', 'POST'])
def login():
"""
Handle login process and form.
"""
if session and session["verified"]:
return redirect(url_for("index"))
if request.method == "GET":
return render("login.html")
if request.method == "POST":
access_key = request.form['access_key'] if 'access_key' in request.form else None
if not access_key:
return render('login.html', error=True, message="Please provide an access key.")
success, message, user = check_access_key(access_key)
if not success:
return render('login.html', error=True, message=message)
if ADMIN_ONLY and not user["is_admin"]:
return render('login.html', error=True, message="You need to be an administrator to login.")
session["verified"] = True
session["user_id"] = user["user_id"]
session["user_name"] = user["username"]
session["team_id"] = user["team_id"]
session["team_name"] = user["team_name"]
session["admin"] = user["is_admin"]
return redirect(url_for("index"))
return redirect(url_for("login"))
@app.route("/", methods=['GET'])
@login_required
def index():
"""
Display running instances of your team and allows you to submit new instances.
"""
instances = Instances.query.filter_by(team_id=session["team_id"]).all()
if instances:
challenges_info = {}
for instance in instances:
if instance.network_name not in challenges_info:
challenges_info[instance.network_name] = []
remaining = timedelta(minutes=MAX_INSTANCE_DURATION) - (datetime.utcnow() - instance.creation_date)
if remaining > timedelta(seconds=0):
remaining = f"{remaining.seconds // 60:02d}m{remaining.seconds % 60:02d}s"
else:
remaining = "This instance will be deleted shortly..."
challenges_info[instance.network_name].append({
"name": instance.challenge_name,
"host": instance.host_domain,
"hostname": instance.hostname,
"ip_address": instance.ip_address,
"ports": instance.ports,
"user_name": instance.user_name,
"time_remaining": remaining
})
return render('index.html', challenges=CHALLENGES, captcha=recaptcha, challenges_info=challenges_info)
return render('index.html', challenges=CHALLENGES, captcha=recaptcha)
@app.route("/container/all", methods=['GET'])
@admin_required
def get_all_containers():
"""
Admin restricted function to retrieve all containers.
"""
return jsonify({'success': True, 'data':
[{
"id": instance.id,
"team": instance.team_name,
"username": instance.user_name,
"image": instance.docker_image,
"ports": instance.ports,
"instance_name": instance.instance_name,
"date": instance.creation_date
} for instance in Instances.query.all()]
})
@app.route("/container/all", methods=['DELETE'])
@admin_required
def remove_containers():
"""
Admin restricted function to remove all containers.
"""
remove_all_instances()
return jsonify({'success': True, 'message': 'Instances removed successfully.'})
@app.route("/container/<int:container_id>", methods=['DELETE'])
@admin_required
def remove_container(container_id=None):
"""
Admin restricted function to remove a container with its ID.
"""
remove_container_by_id(container_id)
return jsonify({'success': True, 'message': 'Instances removed successfully.'})
@app.route("/remove/me", methods=['GET'])
@login_required
def remove_me():
"""
Allow a user to remove their current instance.
"""
if remove_user_running_instance(session["user_id"]):
return jsonify({'success': True, 'message': 'Instance removed successfully.'})
return jsonify({'success': False, 'message': 'Unable to find an instance to remove.'})
@app.route("/logout", methods=['GET'])
def logout():
"""
Logout the user.
"""
keys = list(session.keys())
for key in keys:
session.pop(key, None)
return redirect(url_for("login"))
@app.route("/run_instance", methods=['POST'])
@login_required
def run_instance():
"""
Allow a user to create a new instance.
"""
challenge_name = request.form.get("challenge_name", None)
if current_app.config["ENABLE_RECAPTCHA"] and not recaptcha.verify():
flash("Captcha failed.", "red")
return redirect(url_for('index'))
if not challenge_name or challenge_name.strip() == "":
flash("Please provide a challenge name.", "red")
return redirect(url_for('index'))
remove_old_instances()
if not check_challenge_name(challenge_name):
flash("The challenge name is not valid.", "red")
return redirect(url_for('index'))
if get_challenge_count_per_team(session["team_id"]) >= MAX_INSTANCE_PER_TEAM:
flash(f"Your team has reached the maximum number of concurrent running instances ({MAX_INSTANCE_PER_TEAM}).", "red")
return redirect(url_for('index'))
remove_user_running_instance(session["user_id"])
if get_total_instance_count() > MAX_INSTANCE_COUNT:
flash(f"The maximum number of dynamic instances has been reached (max: {MAX_INSTANCE_COUNT}).", "red")
return redirect(url_for('index'))
challenge_info = get_challenge_info(challenge_name)
nb_container = create_instances(session, challenge_info)
if nb_container > 1:
flash(f"{nb_container} containers are starting for {challenge_name}...", "green")
else:
flash(f"{nb_container} container is starting for {challenge_name}...", "green")
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)