-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.go
203 lines (179 loc) · 5.22 KB
/
parse.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
package main
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
func parseUpdate(cryptUpdate string) (scoreEntry, error) {
if cryptUpdate == "" || !validateString(cryptUpdate) {
return scoreEntry{}, errors.New("Empty or invalid characters in cryptUpdate.")
}
cryptUpdate, err := hexDecode(cryptUpdate)
if err != nil {
return scoreEntry{}, errors.New("Error decoding hex input.")
}
plainUpdate, err := decryptString(sarpConfig.Password, cryptUpdate)
if err != nil {
return scoreEntry{}, err
}
if err := validateUpdate(plainUpdate); err != nil {
return scoreEntry{}, errors.Wrap(err, "Update validation failed")
}
mapUpdate := make(map[string]string)
splitUpdate := strings.Split(plainUpdate, delimiter)[:updateLen]
for i := 0; i < len(splitUpdate)-2; i += 2 {
mapUpdate[splitUpdate[i]] = splitUpdate[i+1]
}
pointValue, err := strconv.Atoi(mapUpdate["score"])
if err != nil {
return scoreEntry{}, err
}
vulns, err := parseVulns(mapUpdate["vulns"], pointValue)
if err != nil {
return scoreEntry{}, err
}
newEntry := scoreEntry{
Time: time.Now().UTC(),
Team: getTeam(mapUpdate["team"]),
Image: getImage(mapUpdate["image"]),
Vulns: vulns,
Points: pointValue,
}
lastRecord, err := getLastScore(&newEntry)
if err != nil {
lastRecord = scoreEntry{}
}
calcPlayTime(&newEntry, &lastRecord)
calcElapsedTime(&newEntry, &lastRecord)
calcCompletionTime(&newEntry, &lastRecord)
newEntry.PlayTimeStr = formatTime(newEntry.PlayTime)
newEntry.ElapsedTimeStr = formatTime(newEntry.ElapsedTime)
replaceScore(&newEntry)
return newEntry, nil
}
func parseVulns(vulnText string, imagePoints int) (vulnWrapper, error) {
wrapper := vulnWrapper{}
vulnText, err := hexDecode(vulnText)
if err != nil {
return wrapper, errors.New("Error decoding hex input.")
}
plainVulns, err := decryptString(sarpConfig.Password, vulnText)
if err != nil {
return wrapper, err
}
splitVulns := strings.Split(plainVulns, delimiter)
scored, err := strconv.Atoi(splitVulns[0])
if err != nil {
return wrapper, err
}
wrapper.VulnsScored = scored
total, err := strconv.Atoi(splitVulns[1])
if err != nil {
return wrapper, err
}
wrapper.VulnsTotal = total
pointTotal := 0
splitVulns = splitVulns[2 : len(splitVulns)-1]
for _, vuln := range splitVulns {
splitVuln := strings.Split(vuln, "-")
if debugEnabled {
fmt.Println("splitvulns", splitVuln, "len", len(splitVuln))
}
if len(splitVuln) < 2 {
return wrapper, errors.New(fmt.Sprintln("Error splitting vuln on delimiter:", splitVuln, "length of", len(splitVuln)))
}
splitText := splitVuln[:len(splitVuln)-1]
vulnText := ""
for index, subString := range splitText {
if index != 0 {
vulnText += "-"
}
vulnText += subString
}
splitVuln = strings.Split(strings.TrimSpace(splitVuln[len(splitVuln)-1]), " ")
if len(splitVuln) != 2 {
return wrapper, errors.New("Error splitting vuln on space")
}
vulnPointsText := strings.TrimSpace(splitVuln[0])
if string(vulnPointsText[0]) == "N" {
vulnPointsText = "-" + string(vulnPointsText[1:])
}
vulnPoints, err := strconv.Atoi(vulnPointsText)
if err != nil {
return wrapper, errors.New("Error parsing vuln point value")
}
pointTotal += vulnPoints
if debugEnabled {
fmt.Println("appending vuln text:", vulnText, "vuln points:", vulnPoints)
}
wrapper.VulnItems = append(wrapper.VulnItems, vulnItem{VulnText: vulnText, VulnPoints: vulnPoints})
}
if pointTotal != imagePoints {
fmt.Println("!!! SOMEONE REVERSED THE CRYTPO !!!")
fmt.Println("!!! POINTS FOR UPDATE DON'T ADD UP !!")
fmt.Println("!!! Image points is", imagePoints, "vuln point total is", pointTotal)
fmt.Println("!!! Wrapper:", wrapper)
return wrapper, errors.New("Vuln points don't add up")
}
return wrapper, nil
}
func parseScoresIntoTeam(scores []scoreEntry) (teamData, error) {
data, err := parseScoresIntoTeams(scores)
if err != nil || len(data) <= 0 {
return teamData{}, err
}
return data[0], nil
}
func parseScoresIntoTeams(scores []scoreEntry) ([]teamData, error) {
td := []teamData{}
if len(scores) <= 0 {
return td, nil
}
sort.SliceStable(scores, func(i, j int) bool {
return scores[i].Team.ID < scores[j].Team.ID
})
imageCount := 0
totalScore := 0
playTime, _ := time.ParseDuration("0s")
currentTeam := scores[0].Team
for _, score := range scores {
if currentTeam.ID != score.Team.ID {
td = append(td, teamData{
ID: currentTeam.ID,
Alias: currentTeam.Alias,
Email: currentTeam.Email,
ImageCount: imageCount,
Score: totalScore,
Time: formatTime(playTime),
})
imageCount = 0
totalScore = 0
playTime, _ = time.ParseDuration("0s")
currentTeam = score.Team
}
imageCount++
totalScore += score.Points
playTime += score.PlayTime
}
td = append(td, teamData{
ID: scores[len(scores)-1].Team.ID,
Alias: scores[len(scores)-1].Team.Alias,
Email: scores[len(scores)-1].Team.Email,
ImageCount: imageCount,
Score: totalScore,
Time: formatTime(playTime),
})
sort.SliceStable(td, func(i, j int) bool {
var result bool
if td[i].Score == td[j].Score {
result = td[i].Time < td[j].Time
} else {
result = td[i].Score > td[j].Score
}
return result
})
return td, nil
}