-
Notifications
You must be signed in to change notification settings - Fork 30
/
score.go
311 lines (279 loc) Β· 7.97 KB
/
score.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"errors"
"fmt"
"log"
"os"
"strconv"
)
var (
teamID string
conf = &config{}
image = &imageData{}
conn = &connData{}
// checkCount keeps track of the current check being scored, and is used
// for identifying which check caused a given error or warning.
checkCount int
)
// imageData is the current scoring data for the image. It is able to be
// wiped, removed, etc, on each run without affecting anything else.
type imageData struct {
Contribs int
Detracts int
Score int
ScoredVulns int
TotalPoints int
Penalties []scoreItem
Points []scoreItem
Hints []hintItem
}
// connData represents the current connectivity state of the image to the
// internet and the scoring server.
type connData struct {
Status bool
OverallColor string
OverallStatus string
NetColor string
NetStatus string
ServerColor string
ServerStatus string
}
// scoreItem is the scoring report representation of a check, containing only
// the message and points associated with it.
type scoreItem struct {
Index int
Message string
Points int
}
// hintItem is the scoring report representation of a hint, which can contain
// multiple messages.
type hintItem struct {
Index int
Messages []string
Points int
}
// config is a representation of the TOML configuration typically
// specific in scoring.conf.
type config struct {
DisableRemoteEncryption bool
Local bool
Shell bool
EndDate string
Name string
OS string
Password string
Remote string
Title string
User string
Version string
Check []check
}
// statusRes is to parse a JSON response from the remote server.
type statusRes struct {
Status string `json:"status"`
}
// readScoringData is a convenience function around readData and decodeString,
// which parses the encrypted scoring configuration file.
func readScoringData() error {
info("Decrypting data from " + dirPath + scoringData + "...")
// Read in the encrypted configuration file
dataFile, err := readFile(dirPath + scoringData)
if err != nil {
return err
} else if dataFile == "" {
return errors.New("Scoring data is empty!")
}
decryptedData, err := decryptConfig(dataFile)
if err != nil {
fail("Error reading in scoring data: " + err.Error())
return err
} else if decryptedData == "" {
fail("Scoring data is empty! Is the file corrupted?")
return errors.New("Scoring data is empty!")
} else {
info("Data decryption successful!")
}
parseConfig(decryptedData)
return nil
}
// ScoreImage is the main function for scoring the image.
func scoreImage() {
checkTrace()
if timeCheck() {
log.Fatal("Image is running outside of the specified end date.")
}
info("Scoring image...")
// 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 conf.Local {
scoreChecks()
if conf.Remote != "" {
checkServer()
if conn.Status {
err := reportScore()
if err != nil {
fail(err)
}
}
}
genReport(image)
} else {
// 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
checkServer()
if !conn.Status {
warn("Connection failed-- generating blank report.")
genReport(image)
return
}
scoreChecks()
err := reportScore()
if err != nil {
image = &imageData{}
warn("Reporting image score failed, and local is disabled. Score data removed.")
}
genReport(image)
}
// Check if points increased/decreased.
prevPoints, err := readFile(dirPath + "assets/previous.txt")
if err == nil {
prevScore, err := strconv.Atoi(prevPoints)
if err != nil {
fail("Don't mess with previous.txt! It only helps us know when to play sound and send notifications.")
} else {
if prevScore < image.Score {
sendNotification("You gained points!")
playAudio(dirPath + "assets/wav/gain.wav")
} else if prevScore > image.Score {
sendNotification("You lost points!")
playAudio(dirPath + "assets/wav/alarm.wav")
}
}
} else if os.IsExist(err) {
fail("Reading from previous.txt failed!")
}
// Write previous.txt from current round.
writeFile(dirPath+"assets/previous.txt", strconv.Itoa(image.Score))
// Remove imageData for next scoring round.
image = &imageData{}
}
// checkConfigData performs preliminary checks on the configuration data, reads
// in the TeamID, and autogenerates missing values.
func checkConfigData() {
if len(conf.Check) == 0 {
conn.OverallColor = "red"
conn.OverallStatus = "There were no checks found in the configuration."
} else {
// For none-remote local connections
conn.OverallColor = "green"
conn.OverallStatus = "OK"
conn.Status = true
}
readTeamID()
}
// scoreChecks runs through every check configured.
func scoreChecks() {
for i, check := range conf.Check {
// checkCount is the same as in printConfig, 1-based count
checkCount = i + 1
scoreCheck(check)
}
checkCount = 0
info(fmt.Sprintf("Score: %d", image.Score))
}
// scoreCheck will go through each condition inside a check, and determine
// whether or not the check passes.
func scoreCheck(check check) {
status := false
failed := false
// Create hint var in case any checks have hints
hint := hintItem{
Index: checkCount,
Points: check.Points,
}
// If a Fail condition passes, the check fails, no other checks required.
if len(check.Fail) > 0 {
failed = checkOr(check.Fail, &hint)
}
// If a PassOverride succeeds, that overrides the Pass checks
if !failed && len(check.PassOverride) > 0 {
status = checkOr(check.PassOverride, &hint)
}
// Finally, we check the normal Pass checks
if !failed && !status && len(check.Pass) > 0 {
status = checkAnd(check.Pass, &hint)
}
if status {
if check.Points >= 0 {
if verboseEnabled {
deobfuscateData(&check.Message)
pass(fmt.Sprintf("Check passed: %s - %d pts", check.Message, check.Points))
obfuscateData(&check.Message)
}
image.Points = append(image.Points, scoreItem{checkCount, check.Message, check.Points})
image.Contribs += check.Points
} else {
if verboseEnabled {
deobfuscateData(&check.Message)
fail(fmt.Sprintf("Penalty triggered: %s - %d pts", check.Message, check.Points))
obfuscateData(&check.Message)
}
image.Penalties = append(image.Penalties, scoreItem{checkCount, check.Message, check.Points})
image.Detracts += check.Points
}
image.Score += check.Points
} else {
// If there is a check-wide hint, add to start of hint messages.
if check.Hint != "" {
hints := []string{check.Hint}
hints = append(hints, hint.Messages...)
hint.Messages = hints
}
// If the check failed, and there are hints, see if we should display them.
// All hints triggered (based on which conditions ran) are displayed in sequential order.
if len(hint.Messages) > 0 {
image.Hints = append(image.Hints, hint)
}
}
// If check is not a penalty, add to total
if check.Points >= 0 {
image.ScoredVulns++
image.TotalPoints += check.Points
}
}
// checkOr runs a set of conditions and returns true if any of them pass.
// It is a logical "OR".
func checkOr(conds []cond, hint *hintItem) bool {
for _, cond := range conds {
if runCheck(cond) {
return true
}
if cond.Hint != "" {
hint.Messages = append(hint.Messages, cond.Hint)
}
}
return false
}
// checkAnd runs a set of conditions and returns true iff ALL of them pass.
// It is a logical "AND".
func checkAnd(conds []cond, hint *hintItem) bool {
for _, cond := range conds {
if !runCheck(cond) {
if cond.Hint != "" {
hint.Messages = append(hint.Messages, cond.Hint)
}
return false
}
}
return true
}