forked from TowerofHanoi/CTFsubmitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.py
69 lines (51 loc) · 1.61 KB
/
webservice.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
from __future__ import absolute_import, print_function, unicode_literals
from bottle import (
post, get, run, request, abort,
template, static_file, route, error)
from config import config
import re
import os
from ipaddress import ip_address
from backend.mongodb import MongoBackend
from logger import log
# define a regex for flags
flag_regex = config.get("flag_regex", "^\w{31}=$")
service_regex = "^\w{0,32}$"
backend = MongoBackend()
@route('/static/<path:path>')
def callback(path):
return static_file(path, './static')
@get('/stats')
def stats():
return template('templates/stats.html')
# web interface here
@post('/submit')
def submit_flag():
name = request.forms.get('name')
team = request.forms.get('team')
service = request.forms.get('service')
flags = request.forms.getall('flags')
ip = request.environ.get('REMOTE_ADDR').decode('utf-8')
ip = int(ip_address(ip))
if not flags or not team or not service or not name:
# bad request
abort(400)
if not re.match(service_regex, service):
abort(400, "wrong format for service \w{32}")
backend.insert_flags(
team, service, flags,
name, ip)
@error(500)
def handle_500_error(err):
log.exception(err.exception)
return "500 - Internal server error"
if __name__ == "__main__":
if 'BOTTLE_CHILD' not in os.environ:
log.info("Submitter service started")
backend.cold_restart()
# try to set all the pending task to unsubmitted (retry)
run(
host='localhost',
port=8080,
reloader=True,
debug=config.get("debug", False))