-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.go
286 lines (242 loc) · 5.76 KB
/
db.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
package main
import (
"strings"
"sync"
"time"
//"log"
"github.com/DSU-DefSec/DWAYNE-INATOR-5000/checks"
)
const (
EMPTY = iota
SUBMITTED
GRADED
)
var (
recordsStaging = []TeamRecord{}
)
type ResultEntry struct {
ID uint
Time time.Time
TeamID uint
TeamRecordID uint
Round int
RoundCount int
// Total points check has earned
Points int
// Uptime is only used in the uptime view
Uptime int `gorm:"-"`
checks.Result
}
type TeamRecord struct {
ID uint
Time time.Time
TeamID uint
Team TeamData
Round int
Results []ResultEntry
ResultsMap map[string]ResultEntry `gorm:"-"`
RedTeamPoints int
ServicePoints int
InjectPoints int
SlaViolations int
ManualAdjustment int
// Field must be calculated before displaying.
// We don't want to hardcode weights.
Total int
// Others persisting on us.
Persists []Persist
PointsLost int
PointsStolen int
PersistPoints int
}
type Persist struct {
ID uint
Round int
Box string
TeamID uint
Team TeamData
TeamRecordID uint
OffenderID uint
Offender TeamData
}
type SLA struct {
Time time.Time
TeamID uint `gorm:"primaryKey"`
Reason string `gorm:"primaryKey"`
Counter int
Violations int
}
type TeamData struct {
ID uint
Name, IP, Pw string
Token string
}
type InjectSubmission struct {
ID uint
Time time.Time `json:"time"`
Updated time.Time `json:"updated"`
TeamID uint
Team TeamData
InjectID uint `json:"inject"`
FileName string `json:"filename"`
DiskFile string `json:"diskfile"`
Invalid bool `json:"invalid"`
Graded bool
Score int `json:"score"`
Content string
Feedback string `json:"feedback"`
}
type Inject struct {
ID uint
Time time.Time `json:"time"`
Due time.Time `json:"due"`
Closes time.Time `json:"closes"`
Submissions []InjectSubmission
Title string `json:"title"`
Body string `json:"body"`
File string `json:"file"`
Points int `json:"points"`
Status int `json:"status"`
}
// add start time
func (i *Inject) OpenTime() time.Time {
if i.Time.IsZero() {
return startTime
}
return startTime.Add(i.Time.Sub(ZeroTime)).In(loc)
}
func (i *Inject) DueTime() time.Time {
return startTime.Add(i.Due.Sub(ZeroTime)).In(loc)
}
func (i *Inject) CloseTime() time.Time {
return startTime.Add(i.Closes.Sub(ZeroTime)).In(loc)
}
type CredentialTable struct {
Creds map[uint]map[string]map[string]string
Mutex *sync.Mutex
}
// PCRs are stored in memory as a map, constructed from a series
// of PCRs that contain changes. Kind of like a git repo.
//
// The structure goes like:
//
// creds[team][check][username]
//
// Thus, it serves to create a lookup table state for each team.
func constructPCRState() {
ct.Mutex.Lock()
// nuke current state
ct.Creds = make(map[uint]map[string]map[string]string)
// get all pcr entries
var submissions []InjectSubmission
// password change inject is always inject 1
res := db.Where("inject_id = 1").Find(&submissions)
if res.Error != nil {
errorPrint(res.Error)
return
}
// for each pcr entry
for _, submission := range submissions {
if submission.Feedback != "" {
continue
}
if submission.Invalid && !submission.Graded {
continue
}
if !submission.Invalid {
submission.Invalid = true
}
fileName := strings.Split(submission.FileName, "_pcr_")
if len(fileName) != 4 {
submission.Feedback = "file name is improperly formatted"
db.Save(&submission)
continue
}
check, err := dwConf.getCheck(fileName[1])
if err != nil {
submission.Feedback = err.Error()
db.Save(&submission)
continue
}
content := readInject(submission)
// parse and add to table
usernames := []string{}
passwords := []string{}
splitPcr := strings.Split(content, "\n")
if len(splitPcr) == 0 || splitPcr[0] == "" {
submission.Feedback = "input empty"
db.Save(&submission)
continue
}
if len(splitPcr) > 10000 {
submission.Feedback = "input too large (over 10000 lines)"
db.Save(&submission)
continue
}
allUsernames := []string{}
for _, cred := range dwConf.Creds {
allUsernames = append(allUsernames, cred.Usernames...)
}
empty := true
for _, p := range splitPcr {
p = strings.TrimSpace(p)
if p == "" {
continue
}
splitItem := strings.Split(p, ",")
if len(splitItem) != 2 {
submission.Feedback = "username was an invalid format: " + p
db.Save(&submission)
break
}
if splitItem[1] == "" {
continue
}
empty = false
if splitItem[0] == "all" {
for _, user := range allUsernames {
usernames = append(usernames, user)
passwords = append(passwords, splitItem[1])
}
} else {
validUser := false
for _, user := range allUsernames {
if user == splitItem[0] {
validUser = true
break
}
}
if !validUser {
continue
}
usernames = append(usernames, splitItem[0])
passwords = append(passwords, splitItem[1])
}
}
if submission.Feedback != "" {
continue
}
if empty {
submission.Feedback = "input empty"
db.Save(&submission)
continue
}
if !submission.Graded {
submission.Updated = time.Now()
submission.Graded = true
db.Save(&submission)
}
// add creds to pcrItem
for i, u := range usernames {
if _, ok := ct.Creds[submission.TeamID]; !ok {
ct.Creds[submission.TeamID] = make(map[string]map[string]string)
}
if _, ok := ct.Creds[submission.TeamID][check.FetchName()]; !ok {
ct.Creds[submission.TeamID][check.FetchName()] = make(map[string]string)
}
ct.Creds[submission.TeamID][check.FetchName()][u] = passwords[i]
}
}
checks.Creds = ct.Creds
ct.Mutex.Unlock()
}