-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
480 lines (404 loc) · 17.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import os
import sys
import time
import pickle
import base64
from io import BytesIO
# import timeit
import pyqrcode
import onetimepass
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import secure_filename
from flask_paginate import Pagination, get_page_args
from wtforms.validators import Required, Length, EqualTo
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, logout_user, current_user
from flask import Flask, render_template, redirect, url_for, flash, session, abort, flash, request, Response, send_file
from Main import *
import background_tasks
# create application instance
app = Flask(__name__)
app.config.from_object('config')
UPLOAD_FOLDER = app.config['UPLOAD_FOLDER']
ALLOWED_EXTENSIONS = app.config['ALLOWED_EXTENSIONS']
# initialize extensions
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
lm = LoginManager(app)
TABLE = "PasSkull"
KEYSPACE = "passkullspace"
search_results_by_username = {}
def get_results(results, offset=0, per_page=100):
return results[offset: offset + per_page]
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def remove_password(username):
User.query.filter_by(username=username).delete()
db.session.commit()
def change_password(username, password, adminb):
admin = User.query.filter_by(username=username).first()
admin.password = password
admin.admin = adminb
db.session.commit()
class User(UserMixin, db.Model):
"""User model."""
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True)
password_hash = db.Column(db.String(128))
otp_secret = db.Column(db.String(16))
admin = db.Column(db.Boolean())
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
if self.otp_secret is None:
# generate a random secret
self.otp_secret = base64.b32encode(os.urandom(10)).decode('utf-8')
@property
def password(self):
raise AttributeError('password is not a readable attribute')
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)
def get_totp_uri(self):
return f'otpauth://totp/PasSkull:{self.username}?secret={self.otp_secret}&issuer=PasSkull'
def verify_totp(self, token):
return onetimepass.valid_totp(token, self.otp_secret)
@lm.user_loader
def load_user(user_id):
"""User loader callback for Flask-Login."""
return User.query.get(int(user_id))
class AddUserForm(FlaskForm):
"""Add User Form."""
username = StringField('Username', validators=[Required(), Length(1, 64)])
password = PasswordField('Password', validators=[Required(), Length(8, 15)])
password_again = PasswordField('Password again', validators=[Required(), EqualTo('password', message='Passwords must match')])
admin = BooleanField('Admin')
submit = SubmitField('Add')
class ChangePasswordForm(FlaskForm):
"""Change Password Form."""
username = StringField('Username', validators=[Required(), Length(1, 64)])
password = PasswordField('Password', validators=[Length(8, 15)])
password_again = PasswordField('Password again', validators=[EqualTo('password', message='Passwords must match')])
admin = BooleanField('Admin')
Remove = BooleanField('Remove')
submit = SubmitField('Change')
class LoginForm(FlaskForm):
"""Login form."""
username = StringField('Username', validators=[Required(), Length(1, 64)])
password = PasswordField('Password', validators=[Required(), Length(8, 15)])
token = StringField('Token', validators=[Required(), Length(6, 6)])
submit = SubmitField('Login')
@app.errorhandler(404)
def error404(error):
if current_user.is_authenticated:
return render_template("404.html"), 404
return redirect(url_for('login'))
@app.errorhandler(500)
def error500(error):
number = 500
msg1 = 'Oops! Server Error!'
msg2 = 'Sorry We Have problem inside the Server.'
return render_template("error_page.html", number=number, msg1=msg1, msg2=msg2), 500
@app.errorhandler(405)
def error405(error):
number = 405
msg1 = 'Oops! Method Not Allowed!'
msg2 = 'WHAT THE HELL ARE YOU DOING?!'
return render_template("error_page.html", number=number, msg1=msg1, msg2=msg2), 405
@app.errorhandler(403)
def error403(error):
number = 403
msg1 = 'Forbidden!'
msg2 = 'Can\'t access? Good!'
return render_template("error_page.html", number=number,msg1=msg1, msg2=msg2), 403
@app.errorhandler(401)
def error401(error):
number = 401
msg1 = 'Unauthorized!'
msg2 = 'What about login?! try it maybe its magic...'
return render_template("error_page.html", number=number,msg1=msg1, msg2=msg2), 401
@app.route('/')
def index():
count_users = len(User.query.limit(10).all())
return render_template('index.html', ucount=count_users)
@app.route('/adduser', methods=['GET', 'POST'])
def adduser():
try:
if User.query.filter_by(username=current_user.username, admin=True).first():
admin = True
else:
admin = False
login_state = True
except:
admin = False
login_state = False
if current_user.is_authenticated and admin or len(User.query.limit(10).all()) == 0:
form = AddUserForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is not None:
flash('Username already exists.')
return redirect(url_for('register'))
# add new user to the database
user = User(username=form.username.data, password=form.password.data, admin=form.admin.data)
db.session.add(user)
db.session.commit()
# redirect to the two-factor auth page, passing username in session
session['username'] = user.username
return redirect(url_for('two_factor_setup'))
return render_template('adduser.html', form=form)
if login_state == False:
return redirect(url_for('login'))
else:
flash('Not Admin user. Please contact the administrative user.')
return redirect(url_for('index'))
@app.route('/passwordchange', methods=['GET', 'POST'])
def passwordchange():
try:
if User.query.filter_by(username=current_user.username, admin=True).first():
admin = True
else:
admin = False
login_state = True
except:
admin = False
login_state = False
if current_user.is_authenticated and admin:
form = ChangePasswordForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None:
flash('User Dose\'t exists.')
return redirect(url_for('passwordchange'))
# change new user to the database
if not form.remove.data:
change_password(username=form.username.data, password=form.password.data, adminb=form.admin.data)
else:
remove_user(username=form.username.data)
flash('User Details Change Successfully.')
return render_template('passwordchange.html', form=form)
if login_state == False:
return redirect(url_for('login'))
else:
flash('Not Admin user. Please contact the administrative user.')
return redirect(url_for('index'))
@app.route('/twofactor')
def two_factor_setup(): #todo: first validation
if 'username' not in session:
return redirect(url_for('index'))
user = User.query.filter_by(username=session['username']).first()
if user is None:
return redirect(url_for('index'))
# since this page contains the sensitive qrcode, make sure the browser
# does not cache it
return render_template('two-factor-setup.html'), 200, {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
@app.route('/qrcode')
def qrcode():
if 'username' not in session:
abort(404)
user = User.query.filter_by(username=session['username']).first()
if user is None:
abort(404)
# for added security, remove username from session
del session['username']
# render qrcode for FreeTOTP
url = pyqrcode.create(user.get_totp_uri())
stream = BytesIO()
url.svg(stream, scale=3)
return stream.getvalue(), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
@app.route('/login', methods=['GET', 'POST'])
def login():
"""User login route."""
if current_user.is_authenticated:
# if user is logged in we get out of here
return redirect(url_for('login'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.verify_password(form.password.data) or \
not user.verify_totp(form.token.data):
flash('Invalid username, password or token.')
return redirect(url_for('login'))
# log user in
login_user(user)
# flash('You are now logged in!')
return redirect(url_for('search'))
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
"""User logout route."""
logout_user()
session.clear()
return redirect(url_for('index'))
@app.route('/random')
def random():
if current_user.is_authenticated:
while True:
try:
with open("/data/Passkull/Web_Data/Password_hour", "rb") as f:
username, password = pickle.load(f)
break
except Exception as e:
print("Password_hour file not exist, trying again in 5 second")
time.sleep(5)
return render_template('password_of_the_Day.html', username=username, password=password)
return redirect(url_for('login'))
@app.route('/search', methods=['GET', 'POST'])
def search():
if current_user.is_authenticated:
try:
key = session['key']
except:
key = None
if request.method == 'POST' or request.args.get('page') or key or current_user.username in search_results_by_username.keys():
print(key)
if key and current_user.username in search_results_by_username.keys():
search_results_by_username.pop(current_user.username)
try:
session['key'] = (request.form['key'].strip()).lower()
session['value'] = request.form['value'].strip()
except:
pass
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
per_page = 100
if current_user.username in search_results_by_username.keys():
results = search_results_by_username[current_user.username]
else:
initilize_export_file(username=current_user.username, hashsearch=False)
results = search_in_database_regex(key=session['key'], value=session['value'], keyspace=KEYSPACE, table=TABLE, username=current_user.username)
len_results = len(results)
pagination_results = get_results(results=results, offset=offset, per_page=per_page)
pagination = Pagination(page=page, per_page=per_page, total=len_results, css_framework='bootstrap4')
return render_template('search.html', results=pagination_results, page=page, per_page=per_page, pagination=pagination)
else:
return render_template('search.html', results=[])
return redirect(url_for('login'))
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if current_user.is_authenticated:
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('upload'))
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(url_for('upload'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{current_user.username}_PassDump.txt')
file.save(file_path)
read_file_and_upload(file_name=file_path, delimiter=',', keyspace=KEYSPACE, table=TABLE)
os.remove(file_path)
return redirect(url_for('search'))
return render_template('upload.html')
return redirect(url_for('login'))
@app.route('/userslist', methods=['GET', 'POST'])
def userslist():
if current_user.is_authenticated:
session['key'] = None
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('userslist'))
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(url_for('userslist'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{current_user.username}_PassDump_Search.txt')
file.save(file_path)
results = search_list_in_db(file_list=file_path, keyspace=KEYSPACE, table=TABLE, username=current_user.username, hashsearch=False, key='mail')
search_results_by_username[current_user.username] = results
os.remove(file_path)
print('Done')
return redirect(url_for('search'))
return render_template('userslist.html')
return redirect(url_for('login'))
@app.route('/hashlist', methods=['GET', 'POST'])
def hashlist():
if current_user.is_authenticated:
session['key'] = None
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('hashlist'))
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(url_for('hashlist'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{current_user.username}_PassDump_Search.txt')
file.save(file_path)
results = search_list_in_db(file_list=file_path, keyspace=KEYSPACE, table=TABLE, username=current_user.username, hashsearch=True, key='hash')
search_results_by_username[current_user.username] = results
os.remove(file_path)
print('Done')
return redirect(url_for('search'))
return render_template('hashlist.html')
return redirect(url_for('login'))
# create database tables if they don't exist yet
db.create_all()
@app.route('/removerow')
def remove_row():
if current_user.is_authenticated:
delete_row(keyspace=KEYSPACE, table=TABLE, id=request.args.get('id'))
return redirect(url_for('search'))
return redirect(url_for('login'))
@app.route('/export')
def export():
if current_user.is_authenticated:
file_path = f"/tmp/{current_user.username}_export.csv"
try:
with open(file_path) as fp:
csv = fp.read()
os.remove(file_path)
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=Dump_Export.csv"})
except:
flash('There is not last Export file to Downalod.')
return redirect(url_for('search'))
return redirect(url_for('login'))
@app.route('/status')
def status():
if current_user.is_authenticated:
while True:
try:
with open("/data/Passkull/Web_Data/DB_status", "rb") as f:
results = pickle.load(f)
break
except Exception as e:
print("DB_status file not exist, trying again in 5 second")
time.sleep(5)
return render_template('count.html', results=results)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80, debug=True)