forked from DSU-DefSec/DWAYNE-INATOR-5000
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscore.go
271 lines (235 loc) · 6.22 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
package main
import (
"log"
"math/rand"
"sort"
"sync"
"time"
"github.com/DSU-DefSec/DWAYNE-INATOR-5000/checks"
"github.com/pkg/errors"
)
func Score(m *config) {
err := checkConfig(dwConf)
if err != nil {
log.Fatalln(errors.Wrap(err, "illegal config"))
}
var record TeamRecord
res := db.Limit(1).Order("time desc").Find(&record)
if res.Error == nil {
roundNumber = record.Round + 1
} else {
roundNumber = 1
}
rand.Seed(time.Now().UnixNano())
// checkList = append(checkList, m.Web...)
//mux := &sync.Mutex{}
// Build initial PCR table
constructPCRState()
for {
debugPrint("===================================")
debugPrint("[SCORE] round", roundNumber)
allTeamsWg := &sync.WaitGroup{}
for _, t := range m.Team {
allTeamsWg.Add(1)
go func(team TeamData) {
wg := &sync.WaitGroup{}
resChan := make(chan checks.Result)
debugPrint("team going into teamrecord is", team)
newRecord := TeamRecord{
Time: time.Now().In(loc),
TeamID: team.ID,
Team: team,
Round: roundNumber,
}
for _, b := range m.Box {
for _, check := range b.CheckList {
wg.Add(1)
log.Println("running check for", team.Name, check)
go checks.RunCheck(team.ID, team.IP, b.IP, b.Name, check, wg, resChan)
}
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
// team record
doneSwitch := false
for {
select {
case res := <-resChan:
resEntry := ResultEntry{
Time: time.Now(),
TeamID: team.ID,
Round: roundNumber,
Result: checks.Result{
Name: res.Name,
Status: res.Status,
Error: res.Error,
Debug: res.Debug,
IP: res.IP,
Box: res.Box,
},
}
newRecord.Results = append(newRecord.Results, resEntry)
case <-done:
debugPrint("[SCORE] checks for team", team.Name, "are done")
doneSwitch = true
}
if doneSwitch {
break
}
}
teamMutex.Lock()
recordsStaging = append(recordsStaging, newRecord)
teamMutex.Unlock()
allTeamsWg.Done()
}(t)
}
allTeamsWg.Wait()
// Process all team records
teamMutex.Lock()
for _, rec := range recordsStaging {
processNewRecord(&rec)
}
recordsStaging = []TeamRecord{}
// Calculate persist points
if dwConf.Persists {
calculatePersists()
// Reset persists
persistHits = make(map[uint]map[string][]uint)
}
teamMutex.Unlock()
// Next round!
roundNumber++
// Build PCR state before sleep.
// We want submitted PCRs to miss at least one check round.
debugPrint("[PCR] constructing PCR state")
constructPCRState()
jitter := time.Duration(0)
if dwConf.Jitter != 0 {
jitter = time.Duration(time.Duration(rand.Intn(dwConf.Jitter+1)) * time.Second)
}
debugPrint("[SCORE] sleeping for", dwConf.Delay, "with jitter", jitter)
time.Sleep((time.Duration(dwConf.Delay) * time.Second) + jitter)
}
}
func processNewRecord(rec *TeamRecord) {
var currentRec TeamRecord
result := db.Limit(1).Preload("Results").Order("time desc").Find(¤tRec, "team_id = ?", rec.Team.ID)
if result.Error != nil {
errorPrint(result.Error)
return
}
// Calculate service and SLA values
for _, res := range rec.Results {
var slaRecord SLA
result = db.First(&slaRecord, "team_id = ? and check_name = ?", rec.TeamID, res.Name)
if result.Error != nil {
slaRecord = SLA{
TeamID: rec.TeamID,
CheckName: res.Name,
}
}
if !res.Status {
slaRecord.Counter++
if slaRecord.Counter >= dwConf.SlaThreshold {
rec.SlaViolations++
slaRecord.Violations++
slaRecord.Counter = 0
}
} else {
slaRecord.Counter = 0
rec.ServicePoints += dwConf.ServicePoints
}
if result = db.Save(&slaRecord); result.Error != nil {
errorPrint(result.Error)
return
}
}
// Add carry-over points
rec.RedTeamPoints = currentRec.RedTeamPoints
rec.SlaViolations += currentRec.SlaViolations
rec.ServicePoints += currentRec.ServicePoints
rec.ManualAdjustment += currentRec.ManualAdjustment
// Calculate inject points
rec.InjectPoints = calculateInjects(currentRec)
if dwConf.Persists {
rec.PointsLost += currentRec.PointsLost
rec.PointsStolen += currentRec.PointsStolen
rec.PersistPoints += currentRec.PersistPoints
}
db.Create(&rec)
}
func calculateInjects(rec TeamRecord) int {
var injects []Inject
result := db.Find(&injects)
if result.Error != nil {
errorPrint(result.Error)
return 0
}
totalInjectPoints := 0
// For each inject, get best submission, multiply by points, and add them up
var bestSubmission InjectSubmission
for _, inj := range injects {
res := db.Limit(1).Where("team_id = ?", rec.TeamID).Order("score desc").Find(&bestSubmission)
if res.Error != nil {
errorPrint(res.Error)
return 0
}
totalInjectPoints += int((bestSubmission.Score * inj.Points) / 100)
}
return totalInjectPoints
}
func calculatePersists() {
var records []TeamRecord
res := db.Limit(len(dwConf.Team)).Preload("Team").Preload("Results").Order("time desc").Find(&records)
if res.Error != nil {
errorPrint(res.Error)
return
}
// Sort by team ID.
sort.SliceStable(records, func(i, j int) bool {
return records[i].TeamID < records[j].TeamID
})
// I giveth points, I taketh points
for team, boxes := range persistHits {
for box, persists := range boxes {
if len(persists) > 0 {
// Get box points to split up.
victim := records[team-1]
totalPoints := 0
for _, res := range victim.Results {
if box == res.Box {
if res.Status {
totalPoints += dwConf.ServicePoints
}
}
}
distributedPoints := oneOfN(totalPoints, len(persists)+1)
for _, p := range persists {
victim.Persists = append(victim.Persists, Persist{
Round: roundNumber,
Box: box,
TeamID: team,
OffenderID: p,
})
records[p-1].PersistPoints += distributedPoints / 2
records[p-1].PointsStolen += distributedPoints
}
victim.PointsLost += distributedPoints * len(persists)
records[team-1] = victim
}
}
}
for _, rec := range records {
rec.Results = []ResultEntry{}
db.Save(&rec)
}
}
/*
func percentChangedCreds() map[string]float {
// get all usernames
// for each team, see which % of creds exist in pcritems
}
*/