from flask import Flask, request, abort from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from Crypto.Util.Padding import pad, unpad from flask import Flask, request, Response from base64 import b64encode, b64decode
@app.route("/") defindex(): session = request.cookies.get('session') if session isNone: res = Response( "welcome to the FlipPIN server try request /hint to get the hint") res.set_cookie('session', encrypt(default_session).decode()) return res else: return'have a fun'
@app.route("/hint") defhint(): res = Response(open(__file__).read(), mimetype='text/plain') return res
@app.route("/read") deffile():
session = request.cookies.get('session') if session isNone: res = Response("you are not logged in") res.set_cookie('session', encrypt(default_session)) return res else: plain_session = decrypt(session) if plain_session isNone: return'don\'t hack me'
session_data = json.loads(plain_session)
if session_data['admin'] : filename = request.args.get('filename')
ifany(blacklist_str in filename for blacklist_str in filename_blacklist): abort(403, description='Access to this file is forbidden.')
try: withopen(filename, 'r') as f: return f.read() except FileNotFoundError: abort(404, description='File not found.') except Exception as e: abort(500, description=f'An error occurred: {str(e)}') else: return'You are not an administrator'
if __name__ == "__main__": app.run(host="0.0.0.0", port=9091, debug=True)
h = hashlib.sha1() for bit in chain(probably_public_bits, private_bits): ifnot bit: continue ifisinstance(bit, str): bit = bit.encode('utf-8') h.update(bit) h.update(b'cookiesalt')
cookie_name = '__wzd' + h.hexdigest()[:20]
num = None if num isNone: h.update(b'pinsalt') num = ('%09d' % int(h.hexdigest(), 16))[:9]
rv =None if rv isNone: for group_size in5, 4, 3: iflen(num) % group_size == 0: rv = '-'.join(num[x:x + group_size].rjust(group_size, '0') for x inrange(0, len(num), group_size)) break else: rv = num