Repository navigation
Expand file tree
/
Copy pathparseconfig.py
More file actions
executable file
·272 lines (253 loc) · 9.55 KB
/
Copy pathparseconfig.py
File metadata and controls
executable file
·272 lines (253 loc) · 9.55 KB
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
#!/usr/bin/python
import getopt
import json
import Queue
import time
import re
import sys
import globalvars
import threading
import traceback
from FlagServer import FlagServer
from MessageStore import MessageStore
from Scoreboard import Scoreboard
from Injects import Injects
from BlueTeam import BlueTeam
from FlagStore import FlagStore
from Logger import Logger
import pymongo
# from pprint import pprint
shortargs = "hbc:rvdqn"
longargs = ["help", "verbose", "debug", "quick", "binjitsu", "config=",
"nomovie", "resume"]
globalvars.verbose = False
globalvars.binjitsu = False
globalvars.nomovie = False
usage_str = """
Usage: %s [options]
Options:
--help
--quick
--binjitsu
--verbose
--debug
--config=<filename>
"""
filename = "scorebotcfg.json"
config = open(filename)
blueteams_cfg, injects_cfg = config.read().split("\n\n\n")
inject_header_re = re.compile("INJECT (\S+):(\d+):(\d+) HEAD")
inject_subject_re = re.compile("INJECT SUBJECT: (.*)$")
inject_footer_re = re.compile("INJECT FOOTER")
injects = Injects()
dbname = "scorebot-%s-%s".replace(" ", "") % (time.strftime("%X"),
time.strftime("%x").replace("/", ""))
def make_start_time():
rightnow = time.time()
rightnow_lt = time.localtime(rightnow)
ones = rightnow_lt.tm_min % 10
tens = rightnow_lt.tm_min - ones
if ones < 5:
next_tens = tens + 5
else:
next_tens = tens + 10
time_tuple = (rightnow_lt.tm_year, rightnow_lt.tm_mon,
rightnow_lt.tm_mday, rightnow_lt.tm_hour, next_tens,
rightnow_lt.tm_sec, rightnow_lt.tm_wday, rightnow_lt.tm_yday,
rightnow_lt.tm_isdst)
# Return the time when the clock starts
return time.mktime(time_tuple)
def usage():
sys.stdout.write(usage_str % sys.argv[0])
sys.exit(2)
def get_blueTeams(config, flag_store):
conn = pymongo.MongoClient()
db = conn[dbname]
blues = {}
start_time = make_start_time()
this_team = None
raw_blueteams = config.split(",\n\n")
for b in raw_blueteams:
blueteam_json = json.loads(b)
team_doc_id = db.game.insert(blueteam_json)
this_team = str(blueteam_json['name'])
blues[this_team] = BlueTeam(this_team, start_time, flag_store, team_doc_id, db.game)
blues[this_team].add_dns(str(blueteam_json['dns']))
for host in blueteam_json['hosts'].keys():
host_dict = blueteam_json['hosts'][host]
hostname = str(host_dict['hostname'])
score = str(host_dict['score'])
services = host_dict['services']
blues[this_team].add_host(hostname, score)
for service in services:
try:
content = str(service['content'])
except:
content = None
try:
uri = str(service['uri'])
except:
uri = None
try:
password = str(service['password'])
except:
password = None
try:
username = str(service['username'])
except:
username = None
port = str(service['port'])
protocol = str(service['protocol'])
service_score = str(service['service_score'])
blues[this_team].add_service(hostname, port, protocol,
service_score, uri, content,
username, password)
flags = blueteam_json['flags']
for flagname in blueteam_json['flags'].keys():
flagvalue = str(flags[flagname])
blues[this_team].add_flag(str(flagname), flagvalue)
return blues
def resume_blueTeams(flag_store):
conn = pymongo.MongoClient()
db = conn[dbname]
blues = {}
start_time = make_start_time()
this_team = None
for b in db.game.find():
blueteam_json = b
team_doc_id = blueteam_json['_id']
this_team = str(blueteam_json['name'])
current_round = int(blueteam_json['current_round'])
team_score = int(blueteam_json['team_score'])
blues[this_team] = BlueTeam(this_team, start_time, flag_store, current_round, team_score, team_doc_id, db.game)
blues[this_team].add_dns(str(blueteam_json['dns']))
for host in blueteam_json['hosts'].keys():
host_dict = blueteam_json['hosts'][host]
hostname = str(host_dict['hostname'])
score = str(host_dict['score'])
services = host_dict['services']
blues[this_team].add_host(hostname, score)
for service in services:
try:
content = str(service['content'])
except:
content = None
try:
uri = str(service['uri'])
except:
uri = None
try:
password = str(service['password'])
except:
password = None
try:
username = str(service['username'])
except:
username = None
port = str(service['port'])
protocol = str(service['protocol'])
service_score = str(service['service_score'])
blues[this_team].add_service(hostname, port, protocol,
service_score, uri, content,
username, password)
flags = blueteam_json['flags']
for flagname in blueteam_json['flags'].keys():
flagvalue = str(flags[flagname])
blues[this_team].add_flag(str(flagname), flagvalue)
return blues
def get_injects(inject_file, blues):
in_inject = False
inject_name = None
for line in inject_file:
line.rstrip('\r\n')
if inject_header_re.match(line):
inject_header_re_obj = inject_header_re.match(line)
(inject_name, value, duration) = inject_header_re_obj.groups()
injects.add_inject(inject_name, value, duration)
in_inject = True
elif inject_footer_re.match(line):
inject_name = None
in_inject = False
elif inject_subject_re.match(line):
inject_subject_re_obj = inject_subject_re.match(line)
subject = inject_subject_re_obj.groups()[0]
injects.set_subject(inject_name, subject)
elif in_inject:
injects.add_line(inject_name, line)
else:
sys.stderr.write("Waring, bad config line:\n\t%s\n" % line)
for team in blues.keys():
injects.add_team(team)
def main():
resume = False
try:
opts, args = getopt.getopt(sys.argv[1:], shortargs, longargs)
except getopt.GetoptError, err:
sys.stderr.write("Had a problem: %s" % err)
traceback.print_exc(file=sys.stderr)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--config"):
config = a
elif o in ("-b", "--binjitsu"):
globalvars.binjitsu = True
elif o in ("-v", "--verbose"):
globalvars.verbose = True
elif o in ("-q", "--quick"):
globalvars.quick = True
elif o in ("-q", "--quick"):
globalvars.quick = True
elif o in ("-n", "--nomovie"):
globalvars.nomovie = True
elif o in ("-r", "--resume"):
resume = True
else:
assert False, "unhandled option"
if resume:
filename = "scorebotcfg.json"
config = open(filename)
blueteams_cfg, injects_cfg = config.read().split("\n\n\n")
flag_queue_obj = Queue.Queue()
message_queue_obj = Queue.Queue()
logger_obj = Logger("scorebot")
flag_store = FlagStore(logger_obj, flag_queue_obj, dbname)
message_store = MessageStore(logger_obj, message_queue_obj)
flag_server = FlagServer(logger_obj, flag_queue_obj, message_queue_obj)
t = threading.Thread(target=flag_server.serve_forever)
t.start()
blue_teams = resume_blueTeams(flag_store)
get_injects(config, blue_teams)
myscoreboard = Scoreboard(blue_teams, flag_store, message_store)
myscoreboard.start()
for team in blue_teams.keys():
blue_teams[team].add_queue(flag_queue_obj)
blue_teams[team].start()
flag_store.start()
message_store.start()
injects.start()
else:
filename = "scorebotcfg.json"
config = open(filename)
blueteams_cfg, injects_cfg = config.read().split("\n\n\n")
flag_queue_obj = Queue.Queue()
message_queue_obj = Queue.Queue()
logger_obj = Logger("scorebot")
flag_store = FlagStore(logger_obj, flag_queue_obj, dbname)
message_store = MessageStore(logger_obj, message_queue_obj)
flag_server = FlagServer(logger_obj, flag_queue_obj, message_queue_obj)
t = threading.Thread(target=flag_server.serve_forever)
t.start()
blue_teams = get_blueTeams(blueteams_cfg, flag_store)
get_injects(config, blue_teams)
myscoreboard = Scoreboard(blue_teams, flag_store, message_store)
myscoreboard.start()
for team in blue_teams.keys():
blue_teams[team].add_queue(flag_queue_obj)
blue_teams[team].start()
flag_store.start()
message_store.start()
injects.start()
if __name__ == '__main__':
main()