forked from n0psn0ps/FernbachAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
298 lines (230 loc) · 9.32 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
from flask import Flask, request, jsonify, make_response, render_template_string, url_for, render_template, redirect
from flask_sqlalchemy import SQLAlchemy
import uuid
from werkzeug.security import generate_password_hash, check_password_hash
### PyJWT-1.7.1 works with the JWT EXP ###
import jwt
import datetime
from functools import wraps
import os
from os import popen
import urllib
app = Flask(__name__)
app.config['SECRET_KEY'] = "supersecretkey"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
password = db.Column(db.String(80))
admin = db.Column(db.Boolean)
class Ship(db.Model):
id = db.Column(db.Integer, primary_key=True)
product = db.Column(db.String(50))
cc = db.Column(db.String(50))
add = db.Column(db.String(50))
complete = db.Column(db.Boolean)
user_id = db.Column(db.Integer)
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if 'x-access-token' in request.headers:
token = request.headers['x-access-token']
if not token:
return jsonify({'message' : 'Token is missing.'}), 401
try:
data = jwt.decode(token, app.config['SECRET_KEY'])
current_user = User.query.filter_by(public_id=data['public_id']).first()
except:
return jsonify({'message' : 'Token is invald.'}), 401
return f(current_user, *args, **kwargs)
return decorated
### SSTI Cloaked into the status page ####
@app.route("/status") ### user string:name + apply to response in resp.header with an if else
def home2():
#if request.args.get('user'):
return jsonify({"Welcome to" : "Fernbach's greatest invention!", "status of API" : "up and running"})
@app.route("/status/<string:a>") ### user string:name + apply to response in resp.header with an if else
def home(a):
#if request.args.get('user'):
return jsonify({"Welcome to" : "Fernbach's greatest invention!", "status of API" : "up and running" + render_template_string(a)})
#### MFLAC for depreciated ADMIN Panel ####
@app.route('/api/v0/admin', methods=['GET'])
@token_required
def all_users(current_user):
users = User.query.all()
output = []
for user in users:
user_data = {}
user_data ['public_id'] = user.public_id
user_data ['name'] = user.name
user_data ['password'] = user.password
user_data ['admin'] = user.admin
output.append(user_data)
return jsonify({'users' : output})
####
@app.route('/api/v1/user', methods=['GET'])
@token_required
def get_all_users(current_user):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that action.'})
users = User.query.all()
output = []
for user in users:
user_data = {}
user_data ['public_id'] = user.public_id
user_data ['name'] = user.name
user_data ['password'] = user.password
user_data ['admin'] = user.admin
output.append(user_data)
return jsonify({'users' : output})
### Fix this Endpoint No Longer Allowing a single user to run IDOR####
@app.route('/api/v1/user/<public_id>', methods=['GET'])
@token_required
def get_one_user(current_user, public_id):
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
user_data = {}
user_data ['public_id'] = user.public_id
user_data ['name'] = user.name
user_data ['password'] = user.password
user_data ['admin'] = user.admin
return jsonify({'user' : user_data})
@app.route('/api/v1/user', methods=['POST'])
@token_required
def create_user():
"""
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that action. Need username and password parameters.'})
"""
data = request.get_json()
### Change the hashing type? MD5 ####
hashed_password = generate_password_hash(data['password'], method='md5')
### Use a guessable UUID that is not random but auto increments ####
new_user = User(public_id=str(uuid.uuid4()), name=data['name'], password=hashed_password, admin=False)
db.session.add(new_user)
db.session.commit()
return jsonify({'message' : 'New user created!'})
### Entry Point into admin functionality - MFLAC on endpoint ####
@app.route('/api/v1/user/<public_id>', methods=['PUT'])
@token_required
def promote_user(current_user, public_id):
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
user.admin = True
db.session.commit()
return jsonify({'message' : 'The user has been promoted'})
"""
@app.route('/api/v1/user/<public_id>', methods=['DELETE'])
@token_required
def delete_user(current_user, public_id):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that action.'})
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
db.session.delete(user)
db.session.commit()
return jsonify({'message' : 'User has been deleted.'})
"""
### SQL injection vulnerability will live here ####
@app.route('/api/v1/login')
def login():
auth = request.authorization
if not auth or not auth.username or not auth.password:
return make_response('No credentials supplied', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
user = User.query.filter_by(name=auth.username).first()
if not user:
return make_response('Could not verify username or password', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
if check_password_hash(user.password, auth.password):
token = jwt.encode({'public_id' : user.public_id, 'name' : user.name, 'password' : user.password, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=800)}, app.config['SECRET_KEY'])
return jsonify({'token' : token.decode('UTF-8')})
return make_response('Could not verify password', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
### Package Shipments API endpoints ###
@app.route('/shipments', methods=['GET'])
@token_required
def get_all_shipments(current_user):
if not current_user.admin:
return jsonify({'message' : 'Admin privileges required. Cannot perform that action.'})
shipments = Ship.query.filter_by(user_id=current_user.id).all()
output = []
for ship in shipments:
ship_data = {}
ship_data['id'] = ship.id
ship_data['product'] = ship.product
ship_data['cc'] = ship.cc
ship_data['add'] = ship.add
ship_data['complete'] = ship.complete
ship_data['user_id'] = ship.user_id
output.append(ship_data)
return jsonify({'shipments' : output})
### SSRF ####
### fix the blank issue with error log ###
@app.route('/api/v0/server', methods = ['GET'])
def ssrf():
url = request.args.get("url")
comp = ''
cond1 = 'http://localhost'
cond2 = 'http://localhost:80'
cond3 = 'file:///etc/passwd'
cond4 = 'http://google.com'
if request.method == 'GET' and url == comp:
return jsonify({'message' : 'No shipment found include a valid url.'})
elif request.method == 'GET' and url == cond1 or url == cond2 or url == cond3 or url == cond4:
return urllib.request.urlopen(url).read()
else:
return jsonify({'message' : 'No shipment found include a valid url.'})
### IDOR for todo > soon to be reciept items ####
@app.route('/shipment/<ship_id>', methods=['GET'])
def get_one_todo(ship_id):
ship = Ship.query.filter_by(id=ship_id).first()
if not ship:
return jsonify({'message' : 'No shipment found!'})
ship_data = {}
ship_data['id'] = ship.id
ship_data['product'] = ship.product
ship_data['cc'] = ship.cc
ship_data['add'] = ship.add
ship_data['complete'] = ship.complete
ship_data['user_id'] = ship.user_id
return (ship_data)
"""
@app.route('/shipment', methods=['POST'])
@token_required
def create_shipment(current_user):
data = request.get_json()
if not current_user.admin:
return jsonify({'message' : 'Admin privileges required. Cannot perform that action.'})
new_ship = Ship(product=data['product'], cc=data['cc'], add=data['add'], complete=False, user_id=current_user.id)
db.session.add(new_ship)
db.session.commit()
return jsonify({'message' : "Shipment created!"})
@app.route('/shipment/<ship_id>', methods=['DELETE'])
@token_required
def delete_shipment(current_user, ship_id):
ship = Ship.query.filter_by(id=ship_id, user_id=current_user.id).first()
if not current_user.admin:
return jsonify({'message' : 'Admin privileges required. Cannot perform that action.'})
if not todo:
return jsonify({'message' : 'No shipment found!'})
db.session.delete(ship)
db.session.commit()
return jsonify({'message' : 'Shipment deleted.'})
"""
### Command Injection ###
@app.route('/api/v0/usage')
def ping():
a = request.args.get("a")
cmd = "uptime %s" % a
os.popen(cmd)
return popen(cmd).read()
### File Include and XXE? ###
@app.route("/api/v0/search")
def start():
return render_template("warehouse.xml")
if __name__ == '__main__':
app.run(debug=True)