/
scoring.go
235 lines (213 loc) Β· 6.17 KB
/
scoring.go
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
package cmd
import (
"fmt"
"strconv"
)
func scoreImage() {
// Ensure checks aren't blank, and grab TeamID.
checkConfigData()
// If local is enabled, we want to:
// 1. Score checks
// 2. Check if server is up (if remote)
// 3. If connection, report score
// 4. Generate report
if mc.Config.Local {
scoreChecks()
if mc.Config.Remote != "" {
checkServer()
if mc.Connection {
err := reportScore()
if err != nil {
errorPrint(err)
}
}
}
genReport(mc.Image)
// If local is disabled, we want to:
// 1. Check if server is up
// 2. If no connection, generate report with err text
// 3. If connection, score checks
// 4. Report the score
// 5. If reporting failed, show error, wipe scoring data
// 6. Generate report
} else {
checkServer()
if !mc.Connection {
warnPrint("Connection failed-- generating blank report.")
genReport(mc.Image)
return
}
scoreChecks()
err := reportScore()
if err != nil {
mc.Image = imageData{}
warnPrint("Local is disabled, scoring data removed.")
}
genReport(mc.Image)
}
// Check if points increased/decreased
prevPoints, err := readFile(mc.DirPath + "previous.txt")
// Write previous.txt before playing sound, in case execution is
// interrupted while playing it.
writeFile(mc.DirPath+"previous.txt", strconv.Itoa(mc.Image.Score))
if err == nil {
prevScore, err := strconv.Atoi(prevPoints)
if err != nil {
failPrint("Don't mess with previous.txt!")
} else {
if prevScore < mc.Image.Score {
sendNotification("You gained points!")
playAudio(mc.DirPath + "assets/wav/gain.wav")
} else if prevScore > mc.Image.Score {
sendNotification("You lost points!")
playAudio(mc.DirPath + "assets/wav/alarm.wav")
}
}
} else {
warnPrint("Reading from previous.txt failed. This is probably fine.")
}
}
// checkConfigData performs preliminary checks on the configuration data,
// and reads the TeamID file.
func checkConfigData() {
if len(mc.Config.Check) == 0 {
mc.Conn.OverallColor = "red"
mc.Conn.OverallStatus = "There were no checks found in the configuration."
} else {
// For none-remote local connections
mc.Conn.OverallColor = "green"
mc.Conn.OverallStatus = "OK"
}
readTeamID()
}
// scoreChecks runs through every check configured.
func scoreChecks() {
mc.Image = imageData{}
assignPoints()
for _, check := range mc.Config.Check {
scoreCheck(check)
}
infoPrint("Finished running all checks.")
infoPrint(fmt.Sprintf("Score: %d", mc.Image.Score))
}
// scoreCheck will go through each condition inside a check, and determine
// whether or not the check passes. It does this concurrently.
func scoreCheck(check check) {
status := true
emptyMessage := true
if check.Message == "" {
emptyMessage = false
}
// If a fail condition passes, the check fails, no other checks required.
if len(check.Fail) > 0 {
status = checkFails(&check)
if status {
return
}
}
// If a PassOverride succeeds, that overrides the Pass checks
passOverrideStatus := false
if len(check.PassOverride) > 0 {
passOverrideStatus = checkPassOverrides(&check)
status = passOverrideStatus
}
if !passOverrideStatus && len(check.Pass) > 0 {
status = checkPass(&check)
}
if !emptyMessage {
obfuscateData(&check.Message)
}
if status {
if check.Points >= 0 {
if verboseEnabled {
deobfuscateData(&check.Message)
passPrint(fmt.Sprintf("Check passed: %s - %d pts", check.Message, check.Points))
obfuscateData(&check.Message)
}
mc.Image.Points = append(mc.Image.Points, scoreItem{check.Message, check.Points})
mc.Image.Contribs += check.Points
} else {
if verboseEnabled {
deobfuscateData(&check.Message)
failPrint(fmt.Sprintf("Penalty triggered: %s - %d pts", check.Message, check.Points))
obfuscateData(&check.Message)
}
mc.Image.Penalties = append(mc.Image.Penalties, scoreItem{check.Message, check.Points})
mc.Image.Detracts += check.Points
}
mc.Image.Score += check.Points
}
}
func checkFails(check *check) bool {
for _, condition := range check.Fail {
failStatus := processCheckWrapper(check, condition.Type, condition.Arg1, condition.Arg2, condition.Arg3)
if failStatus {
return true
}
}
return false
}
func checkPassOverrides(check *check) bool {
for _, condition := range check.PassOverride {
status := processCheckWrapper(check, condition.Type, condition.Arg1, condition.Arg2, condition.Arg3)
if status {
return true
}
}
return false
}
func checkPass(check *check) bool {
status := true
passStatus := []bool{}
for _, condition := range check.Pass {
passItemStatus := processCheckWrapper(check, condition.Type, condition.Arg1, condition.Arg2, condition.Arg3)
passStatus = append(passStatus, passItemStatus)
}
// For multiple pass conditions, will only be true if ALL of them are
for _, result := range passStatus {
status = status && result
if !status {
break
}
}
return status
}
// assignPoints is used to automatically assign points to checks that don't
// have a hardcoded points value.
func assignPoints() {
pointlessChecks := []int{}
for i, check := range mc.Config.Check {
if check.Points == 0 {
pointlessChecks = append(pointlessChecks, i)
mc.Image.ScoredVulns++
} else if check.Points > 0 {
mc.Image.TotalPoints += check.Points
mc.Image.ScoredVulns++
}
}
pointsLeft := 100 - mc.Image.TotalPoints
if pointsLeft <= 0 && len(pointlessChecks) > 0 || len(pointlessChecks) > 100 {
// If the specified points already value over 100, yet there are
// checks without points assigned, we assign the default point value
// of 3 (arbitrarily chosen).
for _, check := range pointlessChecks {
mc.Config.Check[check].Points = 3
}
} else if pointsLeft > 0 && len(pointlessChecks) > 0 {
pointsEach := pointsLeft / len(pointlessChecks)
for _, check := range pointlessChecks {
mc.Config.Check[check].Points = pointsEach
}
mc.Image.TotalPoints += (pointsEach * len(pointlessChecks))
if mc.Image.TotalPoints < 100 {
for i := 0; mc.Image.TotalPoints < 100; mc.Image.TotalPoints++ {
mc.Config.Check[pointlessChecks[i]].Points++
i++
if i > len(pointlessChecks)-1 {
i = 0
}
}
mc.Image.TotalPoints += (100 - mc.Image.TotalPoints)
}
}
}