|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License.import requests |
| 16 | +# |
| 17 | + |
| 18 | +from datetime import datetime, timedelta |
| 19 | +from cryptography.fernet import Fernet |
| 20 | +from geopy.distance import geodesic |
| 21 | +from flask import Flask, render_template, request |
| 22 | + |
| 23 | +app = Flask(__name__) |
| 24 | + |
| 25 | +STARTING_POINT = (51.6498, 0.0982) |
| 26 | +DESTINATION_POINT = (51.4921, -0.1929) |
| 27 | +MAX_STEP_DISTANCE = 300 # no more than 300 meters per step |
| 28 | +MAX_SPEED_KMPH = 50 # 50 km/h. It will take under an hour to drive from start to destination. |
| 29 | +FINISH_RADIUS = 10 # If the point is within 10m from DESTINATION_POINT then FLAG is shown |
| 30 | +FLAG = ' CTF{Who_is_Tardis_Ormandy}' |
| 31 | + |
| 32 | +f = Fernet(b'l4ydYP_3aH8n7uQR2EIHgt-jfhhL7U6-yurXBeE89Do=') |
| 33 | + |
| 34 | +def parse_state(state): |
| 35 | + lat, lon, time = tuple(map(float, state.split(','))) |
| 36 | + return (lat, lon), datetime.fromtimestamp(time) |
| 37 | + |
| 38 | +@app.route('/') |
| 39 | +def process(): |
| 40 | + try: |
| 41 | + time = datetime.now() |
| 42 | + if 'token' not in request.args: |
| 43 | + token = f.encrypt(('%f,%f,%f' % (STARTING_POINT[0], STARTING_POINT[1],time.timestamp())).encode()).decode() |
| 44 | + return render_template('ui.html', lat=STARTING_POINT[0], lon=STARTING_POINT[1], token=token) |
| 45 | + |
| 46 | + old_token = request.args.get('token') |
| 47 | + pos = float(request.args.get('lat')), float(request.args.get('lon')) |
| 48 | + |
| 49 | + old_pos, old_time = parse_state(f.decrypt(old_token.encode()).decode()) |
| 50 | + distance = geodesic(old_pos, pos) |
| 51 | + hours = (time - old_time) / timedelta(hours=1) |
| 52 | + speed_kmph = distance.kilometers / hours if hours > 0 else 10 * MAX_SPEED_KMPH # if time == old_time, e.g. hours == 0, then we say that player has moved infinitely fast, e.g. 10 max speeds. |
| 53 | + next_pos, next_token = old_pos, old_token |
| 54 | + |
| 55 | + msg = '' |
| 56 | + if old_pos == pos: |
| 57 | + msg = 'If you want to meet your friends, you should move.' |
| 58 | + elif speed_kmph > MAX_SPEED_KMPH: |
| 59 | + msg = 'Woa, were about to move at %dkm/h, this is too fast!' % speed_kmph |
| 60 | + elif distance.meters > MAX_STEP_DISTANCE: |
| 61 | + msg = 'You tried to travel %dm, which too far at once from your current position.' % distance.meters |
| 62 | + else: |
| 63 | + if geodesic(pos, DESTINATION_POINT).meters < FINISH_RADIUS: |
| 64 | + msg = 'Congratulations, you made it, here is the flag: %s' % FLAG |
| 65 | + elif geodesic(pos, DESTINATION_POINT) < geodesic(old_pos, DESTINATION_POINT): |
| 66 | + msg = 'You went %dm at a speed of %dkm/h. You are getting closer…' % (distance.meters, speed_kmph) |
| 67 | + else: |
| 68 | + msg = 'You went %dm at a speed of %dkm/h. You are getting away…' % (distance.meters, speed_kmph) |
| 69 | + next_token = f.encrypt(('%f,%f,%f' % (pos[0], pos[1],time.timestamp())).encode()).decode() |
| 70 | + next_pos = pos |
| 71 | + return render_template('ui.html', lat=next_pos[0], lon=next_pos[1], token=next_token, message=msg) |
| 72 | + except Exception as e: |
| 73 | + print(e) |
| 74 | + return 'Please use roads.' |
0 commit comments