|
| 1 | + |
| 2 | +from gevent import monkey; monkey.patch_all() |
| 3 | + |
| 4 | +import bottle |
| 5 | +import json |
| 6 | +import jwt |
| 7 | +from bottle_redis import RedisPlugin |
| 8 | +from uuid import uuid4 |
| 9 | +from random import randint |
| 10 | + |
| 11 | +app = bottle.Bottle() |
| 12 | +FLAG = "flag{Y-ARHq29rhchpFJjyJyr}" |
| 13 | + |
| 14 | +@app.get('/api/users/<username>') |
| 15 | +def callback(username, rdb): |
| 16 | + "Returns if an username exists" |
| 17 | + user = rdb.get("user:%s" % username) |
| 18 | + |
| 19 | + if user is None: |
| 20 | + bottle.abort(404, "The user does not exist") |
| 21 | + user = json.loads(user) |
| 22 | + |
| 23 | + auth_header = bottle.request.headers.get('Authorization') |
| 24 | + auth = False |
| 25 | + if auth_header is not None and auth_header.startswith("Bearer "): |
| 26 | + jwt_header = auth_header.split(" ")[1] |
| 27 | + try: |
| 28 | + user_jwt = jwt.decode(jwt_header, user.get('token'), algorithms=['HS256']) |
| 29 | + if user_jwt.get('username') == username and \ |
| 30 | + user_jwt.get('timestamp', 0) > user.get('last_timestamp', 0): |
| 31 | + |
| 32 | + user['last_timestamp'] = max(user.get('timestamp', 0), user_jwt.get('timestamp', 0)) |
| 33 | + rdb.set("user:%s" % user.get('username'), json.dumps(user)) |
| 34 | + |
| 35 | + auth = True |
| 36 | + print(repr(user_jwt)) |
| 37 | + except jwt.exceptions.DecodeError as err: |
| 38 | + print(repr(err)) |
| 39 | + |
| 40 | + if auth and user is not None: |
| 41 | + |
| 42 | + if user.get('show_flag', False): |
| 43 | + user['locked'] = False |
| 44 | + rdb.set("user:%s" % user.get('username'), json.dumps(user)) |
| 45 | + |
| 46 | + return { |
| 47 | + "name": user.get('name'), |
| 48 | + "username": user.get('username'), |
| 49 | + "token": user.get('token') |
| 50 | + } |
| 51 | + else: |
| 52 | + return { |
| 53 | + 'exists': user is not None |
| 54 | + } |
| 55 | + |
| 56 | +@app.get('/api/usert/<token>') |
| 57 | +def callback(token, rdb): |
| 58 | + "Returns the username that belongs to the token" |
| 59 | + username = rdb.get("token:%s" % token) |
| 60 | + if username is None: |
| 61 | + bottle.abort(404, "The user does not exist.") |
| 62 | + return { |
| 63 | + 'username': username.decode('utf-8') |
| 64 | + } |
| 65 | + |
| 66 | +@app.post('/api/users') |
| 67 | +def callback(rdb): |
| 68 | + "Creates a new user" |
| 69 | + |
| 70 | + data = bottle.request.json |
| 71 | + |
| 72 | + fields = ["name", "username", "password"] |
| 73 | + missing_fields = [] |
| 74 | + for field in fields: |
| 75 | + if field not in data or len(data.get(field)) <= 0: |
| 76 | + missing_fields.append(field) |
| 77 | + |
| 78 | + if len(missing_fields) > 0: |
| 79 | + return {"success": False, "message": "All fields are required. Missing fields: %s" % ', '.join(missing_fields)} |
| 80 | + |
| 81 | + user = rdb.get("user:%s" % data.get('username')) |
| 82 | + if user is not None: |
| 83 | + return {"success": False, "message": "This username already exists."} |
| 84 | + |
| 85 | + token_generated = False |
| 86 | + while not token_generated: |
| 87 | + token = uuid4().hex |
| 88 | + rtoken = rdb.get("token:%s" % token) |
| 89 | + if rtoken is None: |
| 90 | + token_generated = True |
| 91 | + |
| 92 | + rdb.set("user:%s" % data.get('username'), json.dumps({ |
| 93 | + "name": data.get("name"), |
| 94 | + "username": data.get("username"), |
| 95 | + "password": data.get("password"), |
| 96 | + "token": token, |
| 97 | + "metric_count": 0, |
| 98 | + "locked": False, |
| 99 | + "show_flag": False, |
| 100 | + "last_timestamp": 0 |
| 101 | + })) |
| 102 | + rdb.set("token:%s" % token, data.get("username")) |
| 103 | + |
| 104 | + return {"success": True} |
| 105 | + |
| 106 | + |
| 107 | +@app.post('/api/authenticate') |
| 108 | +def callback(rdb): |
| 109 | + "Logins a user" |
| 110 | + |
| 111 | + data = bottle.request.json |
| 112 | + if 'username' not in data or 'password' not in data: |
| 113 | + return {"success": False, "message": "Username and Password required"} |
| 114 | + |
| 115 | + user = rdb.get("user:%s" % data.get('username')) |
| 116 | + |
| 117 | + success = False |
| 118 | + if user is not None: |
| 119 | + user = json.loads(user) |
| 120 | + if user.get('password') == data.get('password'): |
| 121 | + success = True |
| 122 | + |
| 123 | + if success: |
| 124 | + return { |
| 125 | + "success": True, "data": { |
| 126 | + "name": user.get('name'), |
| 127 | + "username": user.get('username'), |
| 128 | + "token": user.get('token') |
| 129 | + } |
| 130 | + } |
| 131 | + else: |
| 132 | + return {"success": False, "message": "Invalid username or password"} |
| 133 | + |
| 134 | +@app.get('/api/metrics') |
| 135 | +def callback(rdb): |
| 136 | + |
| 137 | + auth_header = bottle.request.headers.get('Authorization') |
| 138 | + |
| 139 | + if auth_header is None or not auth_header.startswith("Bearer "): |
| 140 | + bottle.abort(403, "Forbidden") |
| 141 | + |
| 142 | + jwt_header = auth_header.split(" ")[1] |
| 143 | + try: |
| 144 | + unvalidated_jwt = jwt.decode(jwt_header, verify=False) |
| 145 | + if unvalidated_jwt is None or 'username' not in unvalidated_jwt: |
| 146 | + bottle.abort(403, "Forbidden") |
| 147 | + |
| 148 | + user = rdb.get("user:%s" % unvalidated_jwt.get('username')) |
| 149 | + if user is None: |
| 150 | + bottle.abort(403, "Forbidden") |
| 151 | + |
| 152 | + user = json.loads(user) |
| 153 | + user_jwt = jwt.decode(jwt_header, user.get('token'), algorithms=['HS256']) |
| 154 | + |
| 155 | + if user.get('last_timestamp', 0) >= user_jwt.get('timestamp', 0): |
| 156 | + bottle.abort(403, "Forbidden") |
| 157 | + |
| 158 | + user['last_timestamp'] = max(user.get('timestamp', 0), user_jwt.get('timestamp', 0)) |
| 159 | + user['metric_count'] = user.get('metric_count', 0) + 1 |
| 160 | + if user['metric_count'] >= 3 and not user.get('show_flag', False): |
| 161 | + user['locked'] = True |
| 162 | + user['show_flag'] = True |
| 163 | + user['password'] = "9X%DuAHDj!!PjhQK%p^gPjSgG9" |
| 164 | + |
| 165 | + rdb.set("user:%s" % user.get('username'), json.dumps(user)) |
| 166 | + |
| 167 | + print(repr(user_jwt)) |
| 168 | + print(repr(user)) |
| 169 | + except jwt.exceptions.DecodeError as e: |
| 170 | + bottle.abort(403, "Forbidden") |
| 171 | + |
| 172 | + if user.get('locked', False): |
| 173 | + return {"success": False, "message": "Detected an unknown access location. For your security we changed your password and logged you out."} |
| 174 | + |
| 175 | + metrics = { |
| 176 | + 'metrics': [ |
| 177 | + {'name': 'Current Users', 'value': randint(52, 57)}, |
| 178 | + {'name': 'Max Users', 'value': 133}, |
| 179 | + {'name': 'Current Ping', 'value': randint(52, 2333)}, |
| 180 | + {'name': 'Max Ping', 'value': 80082} |
| 181 | + ] |
| 182 | + } |
| 183 | + |
| 184 | + if user.get('show_flag', False): |
| 185 | + metrics['metrics'].append({'name': 'Flag', 'value': 'flag{Y-ARHq29rhchpFJjyJyr}'}) |
| 186 | + |
| 187 | + return metrics |
| 188 | + |
| 189 | +@app.get('/') |
| 190 | +def callback(): |
| 191 | + response = bottle.static_file("index.html", "./static") |
| 192 | + response.set_header("Cache-Control", "public, max-age=1") |
| 193 | + return response |
| 194 | + |
| 195 | +@app.get('/<path:path>') |
| 196 | +def callback(path): |
| 197 | + response = bottle.static_file(path, "./static") |
| 198 | + response.set_header("Cache-Control", "public, max-age=1") |
| 199 | + return response |
| 200 | + |
| 201 | + |
| 202 | +if __name__ == '__main__': |
| 203 | + app.install(RedisPlugin(host="localhost")) |
| 204 | + app.run( |
| 205 | + host='localhost', |
| 206 | + port=8080, |
| 207 | + debug=True, |
| 208 | + reloader=True |
| 209 | + ) |
| 210 | +else: |
| 211 | + app.install(RedisPlugin(host="redis")) |
| 212 | + application = app |
| 213 | + |
| 214 | + |
0 commit comments