-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpysel.py
More file actions
164 lines (132 loc) · 7.17 KB
/
pysel.py
File metadata and controls
164 lines (132 loc) · 7.17 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
import configparser
import subprocess
import time
import Event_checks
import hashlib
import requests
from collections import OrderedDict
import os
import io
DEBUG = False
scoreReportLocation = ''
teamIdLocation = '/usr/local/bin/pysel/TEAM'
## Dump your config here in order to test without installing
#s_config = """ """
class Pysel:
def __init__(self, s_file, team_conf):
buf = io.StringIO(s_file)
config_parser = configparser.ConfigParser()
config_parser.read_file(buf)
team_config = configparser.ConfigParser()
team_config.read(team_conf)
self.events = {}
self.general = {}
self.team_config = {}
self.currentScore = 0
self.possibleScore = 0
## Parse team config
for section in dict(team_config._sections):
if section == "Team":
self.team_config[section] = dict(team_config._sections[section])
## Parse the config
for section in dict(config_parser._sections):
if section == 'General:Options': ## Look at the general options
self.general[section] = dict(config_parser._sections['General:Options'])
else:
self.events[section] = dict(config_parser._sections[section])
if self.events[section]['enabled'] == 'yes':
if int(self.events[section]['pointvalue']) > 0:
self.possibleScore += (int(self.events[section]['pointvalue']) * len(self.events[section]['parameters'].split()))
self.sortedEvents = OrderedDict(sorted(self.events.items()))
if self.general['General:Options']['debug'] == 'yes':
global DEBUG
DEBUG = True
def __hash_score__(self, score):
hashstring = str(score) + 'qwerty'
hashedval = hashlib.md5(hashstring.encode('utf-8').rstrip())
return hashedval
def play_noise(self, file):
pass
#subprocess.call(["/usr/bin/aplay", file])
def draw_html_head(self, team, round):
f = open(self.general['General:Options']['scorereportlocation'], 'w')
f.write('<!DOCTYPE html><html lang="en">\n<head><title>PySEL Score Report</title><meta http-equiv="refresh" content="40"></head>\n<body><table align="center"><tr><td><img src="/pysel-static/cplogo.png"></td><td><div align="center"><H1>Oklahoma</H1><H5>Cybersecurity Competition</H5></div></td><td><img src="/pysel-static/eoclogo.png"</td></tr></table><br><hr><br><table border="1"; align="center"><tr><td colspan=3><div align="center"><b>Team: ' + team + ' Round: ' + round + '</b></div></td></tr><tr><td>Pts</td><td>Event</td><td>Tag</td></tr>\n')
f.close()
def update_html_body(self, score, event, parameter, tag):
if '%PARAMETER%' not in event:
reportedEvent = event
else:
reportedEvent = str(event).replace('%PARAMETER%', parameter)
if score == 'MISS':
payload = '<tr bgcolor="lightgray"><td>' + str(score) + '</td><td>' + reportedEvent + '</td><td>' + tag + '</td></tr>'
else:
if int(score) < 0:
payload = '<tr bgcolor="crimson"><td>' + str(score) + '</td><td>' + reportedEvent + '</td><td>' + tag + '</td></tr>'
else:
payload = '<tr bgcolor="lightgreen"><td>' + str(score) + '</td><td>' + reportedEvent + '</td><td>' + tag + '</td></tr>'
f = open(self.general['General:Options']['scorereportlocation'], 'a')
f.write(payload)
f.write('\n')
f.close()
def get_team_id(self, teamIdLocation):
if os.path.exists(teamIdLocation):
f = open(teamIdLocation, 'r')
team = f.readline()
else:
team = '<font color="red">NO TEAM!</font>'
return (team)
def draw_html_tail(self, currentScore, totalScore):
f = open(self.general['General:Options']['scorereportlocation'], 'a')
payload = '</table><div align="center"><br><H3>Total Score: ' + currentScore + ' out of ' + totalScore + '</H3></div><hr><br>\n<div align="center">Last updated: ' + str(time.ctime()) + '</div></body></html>'
f.write(payload)
f.close()
def send_notification(self):
pass
def start_engine(self):
timeLeft = int(self.general['General:Options']['timelimit'])
initialScore = 0
while True:
self.draw_html_head(self.get_team_id(teamIdLocation), self.general['General:Options']['remotereportinground'])
# print(' +------------------------------+')
# print(' | PySEL Score Report |')
# print(' | ' + self.general['General:Options']['remotereportinground'] + " |")
# print(' +------------------------------+')
self.currentScore = 0
for name, event in self.sortedEvents.items():
## parse the parameters list
if event['enabled'] != "yes":
continue
else:
params = event['parameters'].split(' ')
for parameter in params:
## Eval the event to call the correct Event_checks function
if eval("Event_checks."+name.split(":")[1]+"(parameter)"):
print('[X] ',event['pointvalue'], 'pts for',event['tag'], parameter)
self.currentScore += int(event['pointvalue'])
self.update_html_body(event['pointvalue'], event['msg'], parameter, event['tag'])
else:
if DEBUG == True and int(event['pointvalue']) > 0:
self.update_html_body('MISS', event['msg'], parameter, event['tag'])
print("[ ] 0 pts for",event['msg'], parameter)
## Did we gain or lose points?
if initialScore < self.currentScore:
print("_____I LIKE YOUR STYLE!____")
self.play_noise('/pysel-static/gain.wav')
elif initialScore > self.currentScore:
print("_____YOU DISGUST ME!____")
self.play_noise('/pysel-static/lose.wav')
initialScore = self.currentScore
print('Current score: {} out of {}'.format(self.currentScore, self.possibleScore))
self.draw_html_tail(str(self.currentScore), str(self.possibleScore))
if self.general['General:Options']['remotereportingenabled'] == "yes":
url = self.general['General:Options']['remotereportingserver']
url += "/ss/ss.php?mode=send&game=okcyberrun_r1&team={self.team['team_id']}&player={self.general['General:Options']['remotereportinground']}&score={str(self.currentScore)}&md5={str(self.__hash_score__(self.currentScore))}"
print('sending', url)
r = requests.get(url)
print(r.status_code, r.text)
print('You have', timeLeft, 'minutes remaining.\n\n')
timeLeft -= 1
time.sleep(60)
if __name__ == "__main__":
Engine = Pysel(s_config, "team.conf")
Engine.start_engine()