-
Notifications
You must be signed in to change notification settings - Fork 29
/
remote.go
240 lines (216 loc) Β· 6.33 KB
/
remote.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// Use non-ASCII bytes as a delimiter.
var delimiter = string(byte(255)) + string(byte(222))
const (
FAIL = "FAIL"
GREEN = "green"
RED = "red"
)
func readTeamID() {
fileContent, err := readFile(dirPath + "TeamID.txt")
fileContent = strings.TrimSpace(fileContent)
if err != nil {
teamID = ""
if conf.Remote != "" {
fail("TeamID.txt does not exist!")
conn.OverallColor = RED
conn.OverallStatus = "Your TeamID files does not exist! Failed to score image."
conn.Status = false
} else {
warn("TeamID.txt does not exist! This image is local only, so we will continue.")
}
sendNotification("TeamID.txt does not exist!")
} else if fileContent == "" {
teamID = ""
fail("TeamID.txt is empty!")
sendNotification("TeamID.txt is empty!")
if conf.Remote != "" {
conn.OverallStatus = RED
conn.OverallStatus = "Your TeamID is empty! Failed to score image."
conn.Status = false
}
} else {
teamID = fileContent
}
}
func writeString(stringToWrite *strings.Builder, key, value string) {
stringToWrite.WriteString(key)
stringToWrite.WriteString(delimiter)
stringToWrite.WriteString(value)
stringToWrite.WriteString(delimiter)
}
func genUpdate() (string, error) {
var update strings.Builder
// Write values for score update
writeString(&update, "team", teamID)
writeString(&update, "image", conf.Name)
writeString(&update, "score", strconv.Itoa(image.Score))
writeString(&update, "vulns", genVulns())
writeString(&update, "time", strconv.Itoa(int(time.Now().Unix())))
info("Encrypting score update...")
if err := deobfuscateData(&conf.Password); err != nil {
fail(err)
return "", err
}
finishedUpdate := ""
// If DisableRemoteEncryption has been set to true in the configuration, don't encrypt the update.
if conf.DisableRemoteEncryption {
finishedUpdate = hexEncode(update.String())
} else {
finishedUpdate = hexEncode(encryptString(conf.Password, update.String()))
}
if err := obfuscateData(&conf.Password); err != nil {
fail(err)
return "", err
}
return finishedUpdate, nil
}
func genVulns() string {
var vulnString strings.Builder
// Vulns achieved
vulnString.WriteString(fmt.Sprintf("%d%s", len(image.Points), delimiter))
// Total vulns
vulnString.WriteString(fmt.Sprintf("%d%s", image.ScoredVulns, delimiter))
// Build vuln string
for _, penalty := range image.Penalties {
if err := deobfuscateData(&penalty.Message); err != nil {
fail(err)
}
vulnString.WriteString(fmt.Sprintf("%s - N%.0f pts", penalty.Message, math.Abs(float64(penalty.Points))))
if err := obfuscateData(&penalty.Message); err != nil {
fail(err)
}
vulnString.WriteString(delimiter)
}
for _, point := range image.Points {
if err := deobfuscateData(&point.Message); err != nil {
fail(err)
}
vulnString.WriteString(fmt.Sprintf("%s - %d pts", point.Message, point.Points))
if err := obfuscateData(&point.Message); err != nil {
fail(err)
}
vulnString.WriteString(delimiter)
}
info("Encrypting vulnerabilities...")
deobfuscateData(&conf.Password)
finishedVulns := hexEncode(encryptString(conf.Password, vulnString.String()))
obfuscateData(&conf.Password)
return finishedVulns
}
func reportScore() error {
update, err := genUpdate()
if err != nil {
fail(err.Error())
return err
}
resp, err := http.PostForm(conf.Remote+"/update",
url.Values{"update": {update}})
if err != nil {
fail(err.Error())
return err
}
if resp.StatusCode != 200 {
conn.OverallColor = RED
conn.OverallStatus = "Failed to upload score! Please ensure that your Team ID is correct."
conn.Status = false
fail("Failed to upload score!")
sendNotification("Failed to upload score!")
return errors.New("Non-200 response from remote scoring endpoint")
}
return nil
}
func checkServer() {
// Internet check (requisite)
info("Checking for internet connection...")
// Poor example.org :(
client := http.Client{
Timeout: 10 * time.Second,
}
_, err := client.Get("http://example.org")
if err != nil {
conn.NetColor = RED
conn.NetStatus = FAIL
} else {
conn.NetColor = GREEN
conn.NetStatus = "OK"
}
// Scoring engine check
info("Checking for scoring engine connection...")
resp, err := client.Get(conf.Remote + "/status/" + teamID + "/" + conf.Name)
if err != nil {
conn.ServerColor = RED
conn.ServerStatus = FAIL
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fail("Error reading Status body.")
conn.ServerColor = RED
conn.ServerStatus = FAIL
} else {
if resp.StatusCode == 200 {
conn.ServerColor = GREEN
conn.ServerStatus = "OK"
} else {
conn.ServerColor = RED
conn.ServerStatus = "ERROR"
}
handleStatus(string(body))
}
}
// Overall
if conn.NetStatus == FAIL && conn.ServerStatus == "OK" {
timeStart = time.Now()
conn.OverallColor = "goldenrod"
conn.OverallStatus = "Server connection good but no Internet. Assuming you're on an isolated LAN."
conn.Status = true
} else if conn.ServerStatus == FAIL {
timeStart = time.Now()
conn.OverallColor = RED
conn.OverallStatus = "Failure! Can't access remote scoring server."
fail("Can't access remote scoring server!")
sendNotification("Score upload failure! Unable to access remote server.")
conn.Status = false
} else if conn.ServerStatus == "ERROR" {
timeWithoutID = time.Since(timeStart)
conn.OverallColor = RED
conn.OverallStatus = "Scoring engine rejected your TeamID!"
fail("Remote server returned an error for its status! Your ID is probably wrong.")
sendNotification("Status check failed, TeamID incorrect!")
conn.Status = false
} else if conn.ServerStatus == "DISABLED" {
conn.OverallColor = RED
conn.OverallStatus = "Remote scoring server is no longer accepting scores."
fail("Remote scoring server is no longer accepting scores.")
sendNotification("Remote scoring server is no longer accepting scores.")
conn.Status = false
} else {
timeStart = time.Now()
conn.OverallColor = GREEN
conn.OverallStatus = "OK"
conn.Status = true
}
}
func handleStatus(status string) {
var statusStruct statusRes
if err := json.Unmarshal([]byte(status), &statusStruct); err != nil {
fail("Failed to parse JSON response (status): " + err.Error())
}
switch statusStruct.Status {
case "DISABLED":
conn.ServerStatus = "DISABLED"
}
}