-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
314 lines (281 loc) · 10.7 KB
/
server.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
from flask import Flask, request, redirect, url_for, session
from flask_session import Session # server-side sessions
import subprocess
import random
import os
# shitfing process to another user
def demote(user_gid, user_uid):
def set_ids():
os.setgid(user_gid)
os.setuid(user_uid)
return set_ids
# Configure Flask app
app = Flask(__name__)
app.secret_key = '964bf4687ccd537fe549fe9c7d07c5af4da9bd5acb3cbd98b7681553f8a10fcb'
# Configure server-side session
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
MENU = {
"Silo Doors": {
"Open Doors": "Opening Silo Doors...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"Close Doors": "Closing Silo Doors...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"Door Status": "Checking Door Status...\n...ERROR: Operation failed. SYSTEM CORRUPTED!"
},
"Security System": {
"Activate System": "Activating Security System...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"Deactivate System": "Deactivating Security System...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"System Status": "Checking Security System Status...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
},
"Maintenance": {
"Perform Routine Maintenance": "Performing Routine Maintenance...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"System Diagnostics": "Running System Diagnostics...\n...ERROR: Operation failed. SYSTEM CORRUPTED!",
"Help": """
Ah ah ah, you didn't say the magic word.
Memory Dump: [73, 110, 105, 116, 105, 97, 116, 105, 110, 103, 32, 115, 101, 99, 117, 114, 105, 116, 121, 32, 112, 114, 111, 116, 111, 99, 111, 108, 46, 46, 46, 32, 82, 101, 109, 98, 101, 114, 44, 32, 39, 112, 105, 112, 39, 32, 105, 115, 32, 116, 104, 101, 32, 98, 111, 121, 32, 102, 111, 114, 32, 39, 108, 105, 102, 101, 39, 46, 46, 46]
"""
},
"Missile Controls": {
"Enter Key": "Please enter launch key:"
}
}
hint_messages = [
"System malfunction. Unexpected error at address 0x7f8b86d3. Pip... Err...",
"Terminal failure. Code - 0x4c494645. Memory dump: [86, 97, 117, 108, 116, 101, 99, 104, 32, 115, 101, 99, 117, 114, 105, 116, 121, 32, 101, 110, 104, 97, 110, 99, 101, 109, 101, 110, 116, 32, 112, 114, 111, 99, 101, 100, 117, 114, 101, 32, 105, 110, 105, 116, 105, 97, 116, 101, 100...]",
"Error 404: 'pipboy' object not found.",
"Caution! Password protection active. Remember: 'life' is key...",
"Exception! Address <'vault'> not found in memory.",
"Terminal needs valid credentials. Run 'Enter' for user authentication.",
"Terminal failure. Code - 0xD3FC0N31. Memory dump: [73, 110, 105, 116, 105, 97, 116, 105, 110, 103, 32, 115, 101, 99, 117, 114, 105, 116, 121, 32, 112, 114, 111, 116, 111, 99, 111, 108, 46, 46, 46, 32, 82, 101, 109, 101, 109, 98, 101, 114, 44, 32, 39, 112, 105, 112, 39, 32, 105, 115, 32, 116, 104, 101, 32, 98, 111, 121, 32, 102, 111, 114, 32, 39, 108, 105, 102, 101, 39, 46, 46, 46]",
"Error 421: '/login' not found",
"password format failure = ******#****"
]
# The secret password
password = "Death is a preferable alternative to communism"
flag = "flag{584d0218c9c70bfebb5416967f5a35a1}"
def render_menu(menu):
options = ['0: Back'] if menu != MENU else []
options.extend(f"{i}: {option}" for i, option in enumerate(menu, start=1) if option != "Missile Controls")
return '\n'.join(options)
@app.route('/robots.txt', methods=['GET'])
def robots():
return '''
User-agent: *
Disallow: /login
'''
@app.route('/', methods=['GET', 'POST'])
def landing():
menu_stack = session.get('menu_stack', [MENU])
current_menu = menu_stack[-1]
if request.method == 'POST':
command = request.form['command']
if command == '0':
menu_stack = [MENU]
session['menu_stack'] = menu_stack
return render_landing_page(render_menu(menu_stack[-1]))
elif command.lower() == 'missile controls':
menu_stack.append(MENU['Missile Controls'])
session['menu_stack'] = menu_stack
return render_landing_page(render_menu(menu_stack[-1]))
elif command == password and menu_stack[-1] == MENU['Missile Controls']:
return render_landing_page(flag)
else:
try:
selected_option = list(current_menu.keys())[int(command) - 1]
if isinstance(current_menu[selected_option], dict):
menu_stack.append(current_menu[selected_option])
session['menu_stack'] = menu_stack
return render_landing_page(render_menu(menu_stack[-1]))
else:
return render_landing_page(current_menu[selected_option])
except (IndexError, ValueError):
hint_message = random.choice(hint_messages)
return render_landing_page(f"Invalid command!\n{hint_message}\n\n" + render_menu(current_menu))
return render_landing_page(render_menu(current_menu))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
password = request.form['password']
if password == 'pipboy4life':
session['authenticated'] = True
return redirect(url_for('terminal'))
else:
return render_login_page(error=True)
return render_login_page()
@app.route('/terminal', methods=['GET', 'POST'])
def terminal():
if not session.get('authenticated', False):
return redirect(url_for('login'))
os.chdir('/app/data') # change current working directory to /app/data
if request.method == 'POST':
command = request.form['command']
try:
result = subprocess.run(
command,
shell=True,
timeout=0.5,
capture_output=True,
preexec_fn=demote(33, 33),
text=True,
).stdout
except Exception:
result = "ERROR: Operation failed."
return render_terminal_page(result=result)
return render_terminal_page()
def render_landing_page(message=''):
return f'''
<!doctype html>
<html>
<head>
<title>Vault-Tec Terminal</title>
<style>
body {{
background-color: black;
color: lime;
font-family: 'Courier New', monospace;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}}
#command {{
background-color: black;
color: lime;
border: 2px solid lime;
width: 85%;
height: 2em;
padding: 5px;
}}
pre {{
color: white;
}}
</style>
<!-- SGludDogTG9vayBmb3Igc2VjcmV0IG1lbnUgb3B0aW9ucw== -->
<script>
document.addEventListener("DOMContentLoaded", function() {{
var input = document.getElementById("command");
input.addEventListener("keyup", function(event) {{
if (event.keyCode === 13) {{
event.preventDefault();
document.getElementById("landing-form").submit();
}}
}});
}});
</script>
</head>
<body>
<h1>Vault-Tec Missile Silo Terminal</h1>
<h3>Silo Status: ERR-0R-404</h3>
<pre>{message}</pre>
<form id="landing-form" method=post>
><input id="command" type=text name=command>
</form>
</body>
</html>
'''
def render_login_page(error=False):
error_msg = "<p>Incorrect password, please try again.</p>" if error else ""
return f'''
<!doctype html>
<html>
<head>
<title>Vault-Tec Terminal - Login</title>
<style>
body {{
background-color: black;
color: lime;
font-family: 'Courier New', monospace;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}}
.input-container {{
display: flex;
flex-direction: column;
align-items: center;
width: 80%;
}}
input {{
background-color: black;
color: lime;
border: 2px solid lime;
width: 100%;
height: 2em;
margin-bottom: 10px;
padding: 5px;
}}
p {{
color: red;
}}
</style>
</head>
<body>
<h1>Vault-Tec Secure Terminal</h1>
<h2>Enter your password</h2>
<form method=post>
<div class="input-container">
<input type=password name=password>
<input type=submit value=Enter>
</div>
</form>
{error_msg}
</body>
</html>
'''
def render_terminal_page(result=''):
return f'''
<!doctype html>
<html>
<head>
<title>Vault-Tec Terminal</title>
<style>
body {{
background-color: black;
color: lime;
font-family: 'Courier New', monospace;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}}
#command {{
background-color: black;
color: lime;
border: 2px solid lime;
width: 85%;
height: 2em;
padding: 5px;
}}
pre {{
color: white;
}}
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {{
var input = document.getElementById("command");
input.addEventListener("keyup", function(event) {{
if (event.keyCode === 13) {{
event.preventDefault();
document.getElementById("terminal-form").submit();
}}
}});
}});
</script>
</head>
<body>
<h1>Vault-Tec Secure Terminal</h1>
<pre>{result}</pre>
<form id="terminal-form" method=post>
><input id="command" type=text name=command>
</form>
</body>
</html>
'''
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8045, debug=True)