-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
230 lines (204 loc) · 7.76 KB
/
routes.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
from flask import Flask, request, render_template, redirect, url_for, session
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from flask_bootstrap import Bootstrap
from sys import exit
from os import environ
from subprocess import check_output, run
from string import ascii_uppercase as letters
from random import choice
from tabulate import tabulate
import sqlite3
def get_random_string(n):
return ''.join(choice(letters) for i in range(n))
DB_NAME = "./db.sqlite"
TUTORIAL_DB_NAME = "./animals.sqlite"
app = Flask(__name__)
Bootstrap(app)
app.config["SECRET_KEY"] = environ.get('SECRET_KEY') or get_random_string(32)
app.config["WTF_CSRF_ENABLED"] = False
class BashForm(FlaskForm):
command = StringField('Command')
submit = SubmitField("Run Command")
class SqlForm(FlaskForm):
query = StringField('Query')
submit = SubmitField("Execute query")
class TestForm(FlaskForm):
address = StringField('IP Address or Domain Name')
submit = SubmitField('Perform Test')
class LogHistory(FlaskForm):
file = SubmitField('login_history.log')
class LogCalls(FlaskForm):
file = SubmitField('call_history.log')
class LoginForm(FlaskForm):
username = StringField('Username')
password = PasswordField('Password')
submit = SubmitField('Sign In')
@app.route('/')
def root():
print("[+] Executing root route")
message = "Please login to continue"
return render_template("./index.j2", message=message, form=LoginForm())
@app.route('/login', methods=["POST"])
def login():
print(f"[+] Executing login route")
user = request.form.get("username")
password = request.form.get("password")
sql_stmt = f"SELECT * FROM Users WHERE username='{user}' and password='{password}'"
print(f"[+] Executing SQL Statement:\n\t{sql_stmt}")
with sqlite3.connect(DB_NAME) as db:
c = db.cursor()
c.execute(sql_stmt)
row = c.fetchone()
if row:
print(f"[+] Logged in as {row[0]} succesefully!")
session['username'] = row[0]
return redirect(url_for("control_panel"))
else:
print(f"[!] Login failed!")
message = "Login failed, please try again"
return render_template("./index.j2", message=message, form=LoginForm())
@app.route('/control_panel')
def control_panel():
print(f"[+] Executing control_panel route")
if 'username' in session and session['username'] == 'Admin':
return render_template("control_panel.j2",
username=session['username'],
testForm=TestForm(),
logHistory=LogHistory(),
logCalls=LogCalls())
else:
print(f"[!] User is not logged in, redirecting...")
return redirect(url_for("root"))
@app.route('/do_test')
def do_test():
print(f"[+] Executing do_test route")
if 'username' in session and session['username'] == 'Admin':
command_string = f"ping -c 1 -t 100 {request.args.get('address')}"
try:
command_output = check_output([command_string], shell=True).decode().strip()
except Exception:
command_output = f"Failed to execute {command_string}"
return render_template("control_panel.j2",
username=session['username'],
testForm=TestForm(),
logHistory=LogHistory(),
logCalls=LogCalls(),
command_output=command_output)
else:
print(f"[!] User is not logged in, redirecting...")
return redirect(url_for("root"))
@app.route('/get_file')
def get_file():
print(f"[+] Executing get_file route")
if 'username' in session and session['username'] == 'Admin':
file_name = request.args.get('file')
print(f"[+] Opening {file_name}")
try:
with open(file_name) as f:
data = f.read()
print("[+] File read successeful!")
except Exception:
print(f"[!] failed to open {file_name}")
data = f"Couldn't open file {file_name}"
return render_template("control_panel.j2",
username=session['username'],
testForm=TestForm(),
logHistory=LogHistory(),
logCalls=LogCalls(),
file_data=data)
else:
print(f"[!] User is not logged in, redirecting...")
return redirect(url_for("root"))
@app.route('/learn_sql')
def learn_sql():
if "sql_output" in session:
sql_output = session["sql_output"]
del session["sql_output"]
else:
sql_output = None
with sqlite3.connect(TUTORIAL_DB_NAME) as db:
try:
c = db.cursor()
c.execute("SELECT * FROM Animals")
rows = c.fetchall()
headers = [x[0] for x in c.description]
table = tabulate(rows, headers, tablefmt="psql")
except Exception as e:
print(f"[!] Someone broke the database...{e}")
table = f"[!]Table is broken:\n{e}"
return render_template("learn_sql.j2",
sqlForm=SqlForm(),
table=table,
sql_output=sql_output)
@app.route("/execute_sql", methods=["POST"])
def run_sql():
with sqlite3.connect(TUTORIAL_DB_NAME) as db:
try:
c = db.cursor()
c.execute(request.form.get("query"))
rows = c.fetchall()
rows = '\n'.join([str(x) for x in rows])
except Exception as e:
rows = e
session["sql_output"] = rows
return redirect(url_for("learn_sql"))
@app.route('/learn_bash')
def learn_bash():
if "bash_output" in session:
bash_output = session["bash_output"]
del session["bash_output"]
else:
bash_output = None
return render_template("learn_bash.j2",
bashForm=BashForm(),
bash_output=bash_output)
@app.route('/run_bash_command', methods=["POST"])
def run_bash():
try:
completed_process = run([request.form.get("command")], shell=True, capture_output=True)
command_output = completed_process.stdout.decode()
command_output += completed_process.stderr.decode()
except Exception as e:
command_output = f"Failed to execute {request.form.get('command')}\n{e}"
session['bash_output'] = command_output
return redirect(url_for("learn_bash"))
def setup_db():
try:
password = get_random_string(32)
db = sqlite3.connect(DB_NAME)
cursor = db.cursor()
try:
cursor.execute("DROP TABLE Users")
db.commit()
except Exception as e:
print("[+] Looks like this is a first run :)")
cursor.execute("CREATE TABLE Users (username text, password text)")
cursor.execute(f"INSERT INTO Users VALUES ('Admin', '{password}')")
db.commit()
except Exception as e:
print(f"Failed to setup database...\n{e}")
exit(-1)
def setup_test_db():
try:
db = sqlite3.connect(TUTORIAL_DB_NAME)
cursor = db.cursor()
try:
cursor.execute("DROP TABLE Animals")
db.commit()
except Exception as e:
pass
cursor.execute("CREATE TABLE Animals (name text, legs int, class text)")
cursor.execute("INSERT INTO Animals VALUES ('Dog', 4, 'Mammal')")
cursor.execute("INSERT INTO Animals VALUES ('Spider', 8, 'Arachnid')")
cursor.execute("INSERT INTO Animals VALUES ('Snake', 0, 'Reptile')")
cursor.execute("INSERT INTO Animals VALUES ('Gorilla', 2, 'Mammal')")
cursor.execute("INSERT INTO Animals VALUES ('Robin', 2, 'Reptile')")
db.commit()
except Exception as e:
print(f"Failed to setup database...\n{e}")
exit(-1)
if __name__ == "__main__":
setup_db()
setup_test_db()
app.run(debug=True, host="0.0.0.0")