-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
319 lines (268 loc) · 11.3 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
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
315
316
317
318
319
import re
import base64
import datetime
from sqlalchemy.sql import text
from flask import Flask, jsonify, request, make_response, abort, session
from flask_sqlalchemy import SQLAlchemy
from flask_user import current_user, login_required, roles_required, UserManager, UserMixin, user_manager
from flask_cors import CORS
import os
import random
from bs4 import BeautifulSoup as bs
import secrets
from utils import Utils
import subprocess
import pickle
class ConfigClass(object):
SECRET_KEY = "tkeysupersecretkeysupersecretkey"
SQLALCHEMY_DATABASE_URI = 'sqlite:///apisec.sqlite' # File-based SQL database
SQLALCHEMY_TRACK_MODIFICATIONS = False # Avoids SQLAlchemy warning
USER_APP_NAME = "fAilPI" # Shown in and email templates and page footers
USER_ENABLE_EMAIL = False
def create_app():
app = Flask(__name__)
app.config.from_object(__name__+'.ConfigClass')
def check_token(token):
session_found = Sessions.query.filter_by(session = token).first()
if session_found is not None:
return session_found.suser
else:
return False
def isAdmin(sid):
user = Users.query.filter_by(id = sid).first()
return user.isAdmin
""" Flask application factory """
# Create Flask app load app.config
app = Flask(__name__)
app.config.from_object(__name__+'.ConfigClass')
# Initialize Flask-SQLAlchemy
db = SQLAlchemy(app)
#Might not be needed
class Sessions(db.Model):
__tablename__ = 'sessions'
sid = db.Column( db.Integer, primary_key=True)
#timeouts are hard.
expired = db.Column(db.DateTime)
session = db.Column(db.Text)
suser = db.Column(db.Integer)
def __init__( self, **kwargs ):
super( Sessions, self ).__init__( **kwargs )
if 'sid' in kwargs:
self.sid = kwargs[ 'sid' ]
if 'expired' in kwargs:
self.expired = kwargs[ 'expired' ]
if 'session' in kwargs:
self.session = kwargs[ 'session' ]
if 'suser' in kwargs:
self.suser = kwargs[ 'suser' ]
def __repr__( self ):
return '<Session %s>' % self.session
class Users(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255, collation='NOCASE'), nullable=False)
email = db.Column(db.String(255, collation='NOCASE'), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')
notes = db.Column(db.String(255), nullable=True, server_default='')
isAdmin = db.Column(db.Boolean, default=False, nullable=False)
class Widgets(db.Model):
__tablename__ = 'widgets'
id = db.Column(db.Integer, primary_key=True)
wname = db.Column(db.String(255), nullable=False)
wdesc = db.Column(db.String(255), nullable=False)
wprice = db.Column(db.Numeric(), nullable=False)
class Docs(db.Model):
__tablename__ = 'reviews'
id = db.Column(db.Integer, primary_key=True)
did = db.Column(db.Integer, nullable=False)
uid = db.Column(db.Integer, nullable=False)
rcontent = db.Column(db.String(255), nullable=False)
user_manager = UserManager(app, db, Users)
db.create_all()
# If we don't have entries in the database create them
# Create new users
if not Users.query.filter_by(email = '[email protected]').first():
user = Users(
email = '[email protected]',
name = 'admin',
password = Utils.gen_pass(),
notes = 'flag1_be36d730520b209a9a4789be0a5cb66711df2ae64be5034603af852b5d69938b',
isAdmin = True
)
db.session.add(user)
db.session.commit()
if not Users.query.filter_by(email = '[email protected]').first():
user = Users(
email='[email protected]',
name='Generic User',
password= Utils.gen_pass(),
notes='This is just a generic user. Blah blah blah',
isAdmin = False
)
db.session.add(user)
db.session.commit()
if not Users.query.filter_by(email = '[email protected]').first():
user = Users(
email='[email protected]',
name='John Dough',
password= Utils.gen_pass(),
notes='John likes his privacy. As we all should.',
isAdmin = False
)
db.session.add(user)
db.session.commit()
# Populate the Docs table
if not Docs.query.filter_by(did=1035).first():
doc = Docs(
did=1035,
uid=3,
rcontent='flag2_57c45a5211d8327ad201489fff9c4efa889f8bfedc3d7158222ed1575748b73d'
)
db.session.add(doc)
if not Docs.query.filter_by(did=1037).first():
doc = Docs(
did=1037,
uid=2,
rcontent='Some random document here'
)
db.session.add(doc)
if not Docs.query.filter_by(did=1039).first():
doc = Docs(
did=1040,
uid=2,
rcontent='blah balh balh blah blah'
)
db.session.add(doc)
db.session.commit()
##########################################
# Endpoint Definitions
##########################################
#Nothing to see here
@app.route('/')
def home_page():
page = """
<html><head><title>api ctf</title></head><body><p>Hi there, this is a CTF!</p>
<p>Current list of endpoints:<br>
<ul><br><b>Endpoints that do not require authorization:</b><br>
<li>POST /v2/user/register -create new user<br>
Request data: {"email":[string],"password":[string],"name":[string]}<br>
<li>POST /v2/login -login<br>
Request data: {"email":[string],"password":[string]}<br>
<br>
<b>Endpoints requiring authorization:</b><br>
<li>GET /v2/user/[int] -retrieves information for user specified by int<br>
View user information<br>
<li>PUT /v2/user/[int] -updates information for user specified by int<br>
Request data: {"email":[string],"password":[string],"isAdmin":[bool]}
</ul></p>
</body></html>"""
return make_response(page,200)
@app.route('/v2/pickle', methods=['POST'])
def serialize():
if not request.json:
abort(400)
bad_input = base64.urlsafe_b64decode(request.json['input'])
deserialized_data = pickle.loads(bad_input)
return make_response(jsonify({'message':deserialized_data}),200)
@app.route('/v2/domain', methods=['POST'])
def domain():
if not request.json:
abort(400)
domain = request.json['domain']
subprocess.run(["nslookup",domain], shell=True)
return make_response(jsonify({'message':'lookup_done'}),200)
#User Registration
@app.route('/v2/user/register', methods=['POST'])
def register():
if not request.json:
abort(400)
email = request.json['email']
password = request.json['password']
name = request.json['name']
if not request.json['isAdmin']:
isAdmin = False
else:
isAdmin = request.json['isAdmin']
if None in [email, password, name]:
return make_response(jsonify({'message':'Missing required email, password, or name'}),400)
if Users.query.filter_by(email = email).first() is not None:
return make_response(jsonify({'message':'Email already in use.'}),400)
user = Users(
name = name,
email = email,
password = password,
isAdmin = isAdmin
)
try:
db.session.add(user)
db.session.commit()
return make_response(jsonify({'message':'User created successfully'}),200)
except Exception as e:
return make_response(jsonify({'error':str(e)}),500)
#User login API8:2019 Injection
# Solution: SQL injection in the email field
@app.route('/v2/user/login', methods=['POST'])
def login():
if request.method != 'POST':
abort(400, 'Method not allowed')
if not request.json or 'email' not in request.json or 'password' not in request.json:
abort(400, 'Missing things')
username = request.json['email']
username = username.replace('%40','@')
pword = request.json['password']
#hopefully this will allow for SQL injection
q = text("SELECT * FROM users WHERE email='{0}' AND password='{1}'".format(username,pword))
result = db.engine.execute(q).first()
if result is not None:
token = base64.b64encode(secrets.token_bytes(32)).decode()
sess = Sessions(
expired = datetime.datetime.now() + datetime.timedelta(minutes = 30),
suser = result.id,
session = token
)
try:
db.session.add(sess)
db.session.commit()
return make_response(jsonify({'message':'Success','Authorization-Token':token,'User':result.name,'isAdmin':str(result.isAdmin),'Notes':result.notes}),200)
except Exception as e:
return make_response(jsonify({'error':str(e)}),500)
else:
return make_response(jsonify({'error':'nothing returned'}))
@app.route('/v2/user/<int:id>', methods=['GET','PUT'])
def users(id):
if 'Authorization-Token' not in request.headers:
return make_response(jsonify({'Error':'Authorization-Token header is not set'}),403)
token = request.headers.get('Authorization-Token')
sid = check_token(token)
#if we don't have a valid session send 403
if not sid:
return make_response(jsonify({'Error':'Token check failed: {0}'.format(sid)}))
try:
user = Users.query.filter_by(id=id).first()
except Exception as e:
return make_response(jsonify({'error':str(e)}),500)
#
if request.method == 'GET':
if not isAdmin(sid) and sid != id:
return make_response(jsonify({'Error':'You are not an admin. You can only look at yourself.'}),403)
return make_response(jsonify({'id':str(user.id),'name':user.name,'email':user.email,'isAdmin':str(user.isAdmin)}),200)
#API6:2019 Mass Assignment
# Solution 1: include the isAdmin:True element in the request JSON
#API1:2019 Broken Object Level Authorization
# Solution 2: Make the id in the path the admin's id in the database and change the password.
if request.method == 'PUT':
if not request.json:
abort(400)
user = Users.query.filter_by(id=id).first()
for item in request.json:
setattr(user,item,request.json[item])
try:
db.session.commit()
return make_response(jsonify({'message':'User updated successfully'}),200)
except Exception as e:
return make_response(jsonify({'error':str(e)}),500)
#Widgets endpoint
return app
if __name__ == '__main__':
app = create_app()
app.run(host='127.0.0.1', port=5000, debug=True)