-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
327 lines (290 loc) · 8.25 KB
/
main.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"golang.org/x/net/idna"
)
const SECTION_SMTP = "smtp"
const SECTION_HTTP = "http"
const SECTION_NONE = "none"
type HostStat struct {
Name string `json:"name"`
DNSpoints int `json:"dns"`
WEBpoints int `json:"web"`
EMAILpoints int `json:"email"`
TOTALpoints int `json:"total"`
RANK int `json:"rank"`
}
type GroupStat struct {
Name string `json:"name"`
Id string `json:"id"`
DNSpoints int `json:"dns"`
WEBpoints int `json:"web"`
EMAILpoints int `json:"email"`
TOTALpoints int `json:"total"`
HostStats []HostStat `json:"hosts"`
RANK int `json:"rank"`
}
type entry struct {
name string
points float32
}
func main() {
var url string
// Command line parameters
config := getConfig()
// login to web interface
if config.Verbose > 0 {
log.Println("Login to web interface")
}
hardclient := GetHardenizeClient(config.HardenizeUser, config.HardenizePasswd, config.HardenizeWebUser, config.HardenizeWebPasswd)
// Get group list from hardenize
if config.Verbose > 0 {
log.Println("Get Groups")
}
url = fmt.Sprintf("%s/%s/%s", config.HardenizeRoot, config.Organization, "groups")
body := hardclient.GetAPIData(url)
var groups hgroups
err := json.Unmarshal(body, &groups)
if err != nil {
log.Fatal(err)
}
groupstats := make([]GroupStat, 0)
for _, group := range groups.Groups {
if string(group.Name[0]) == "#" {
if config.Verbose > 0 {
log.Printf("Skip group %s\n", group.Name)
}
continue
}
if config.Verbose > 0 {
log.Printf("Process group %s\n", group.Name)
}
// keep statistics
stat := GroupStat{Name: group.Name, Id: group.Id}
// get CSV file
// example: https: //www.hardenize.com/org/sweden-health-status/hosts/statligtgdabolag?format=csv
url = fmt.Sprintf("%s/org/%s/hosts/%s?format=csv", config.HardenizeWebRoot, config.Organization, group.Id)
csv := hardclient.GetCSV(url)
for _, record := range csv {
// jump over header
if record[0] == "hostname" {
if record[30] != "nameServers" {
log.Printf("record index 30 is %s", record[30])
log.Fatal("Index to CSV is broken")
}
continue
}
// compute host points
if record[0][0:4] == "xn--" {
newr, err := idna.ToUnicode(record[0])
if err != nil {
panic(err)
}
record[0] = newr
}
host := HostStat{Name: record[0]}
// we are just investigating one domain
if config.Domain != "" {
if config.Verbose > 1 {
log.Printf("Config.Domain: %s Record[0]: %s Host: %s\n", config.Domain, record[0], host.Name)
}
if config.Domain != record[0] && config.Domain != host.Name {
continue
}
// Domain found, print values
log.Println("Hälsoläget för ", config.Domain)
}
// nameServers
host.DNSpoints += str2points(record[30])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "DNS", str2points(record[30]))
}
// dnssec
host.DNSpoints += str2points(record[31])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "DNSSEC", str2points(record[31]))
}
// emailTls
host.EMAILpoints += str2points(record[32])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "EMAIL TLS", str2points(record[32]))
}
// emailDane
host.EMAILpoints += str2points(record[33])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "EMAIL DANE", str2points(record[33]))
}
// spf
host.EMAILpoints += str2points(record[34])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "EMAIL SPF", str2points(record[34]))
}
// dmarc
host.EMAILpoints += str2points(record[35])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "EMAIL DMARC", str2points(record[35]))
}
// wwwTls
host.WEBpoints += str2points(record[36])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB TLS", str2points(record[36]))
}
// wwwDane
// host.WEBpoints += str2points[record[37]]
// if config.Verbose==7 {
// log.Printf("%-20s%3d\n","WEB DANE", str2points[record[37]))
// }
// hsts
// host.WEBpoints += str2points[record[38]]
// if config.Verbose==7 {
// log.Printf("%-20s%3d\n","WEB HSTS", str2points[record[38]))
// }
// hpkp
// host.WEBpoints += str2points[record[39]]
// if config.Verbose==7 {
// log.Printf("%-20s%3d\n","WEB HPKP", str2points[record[39]))
// }
// csp
host.WEBpoints += str2points(record[40])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB CSP", str2points(record[40]))
}
// securityHeaders
host.WEBpoints += str2points(record[41])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB Security headers", str2points(record[41]))
}
// cookies
host.WEBpoints += str2points(record[42])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB Cookies", str2points(record[42]))
}
// mixedContent
host.WEBpoints += str2points(record[43])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB mixed content", str2points(record[43]))
}
// wwwXssProtection
host.WEBpoints += str2points(record[44])
if config.Domain != "" {
log.Printf("%-20s%3d\n", "WEB XSS", str2points(record[44]))
}
// host TOTAL
host.TOTALpoints = host.DNSpoints + host.EMAILpoints + host.WEBpoints
if config.Domain != "" {
log.Println("\n\n")
log.Printf("DNS : %3d\n", host.DNSpoints)
log.Printf("EMAIL: %3d\n", host.EMAILpoints)
log.Printf("WEB: %3d\n", host.WEBpoints)
log.Println("----------")
log.Printf("TOTAL: %3d\n", host.TOTALpoints)
// Done
os.Exit(0)
}
// now keep group stats
stat.DNSpoints += host.DNSpoints
stat.EMAILpoints += host.EMAILpoints
stat.WEBpoints += host.WEBpoints
stat.TOTALpoints += host.TOTALpoints
stat.HostStats = append(stat.HostStats, host)
}
// store group stats
groupstats = append(groupstats, stat)
}
// Nothing to do if we just investigate one domain
if config.Domain != "" {
log.Printf("Domain %s not found.\n", config.Domain)
os.Exit(0)
}
// compute rank groups
if config.Verbose > 0 {
log.Printf("Compute ranks for groups\n")
}
list := make([]entry, 0)
for _, group := range groupstats {
entry := entry{name: group.Name, points: float32(group.TOTALpoints) / float32(len(group.HostStats))}
list = append(list, entry)
}
for n := 0; n < len(list)-1; n++ {
for m := n + 1; m < len(list); m++ {
if list[n].points < list[m].points {
list[n], list[m] = list[m], list[n]
}
}
}
for n := range list {
for m := range groupstats {
if groupstats[m].Name == list[n].name {
groupstats[m].RANK = n + 1
}
}
}
// compute rank hosts
for g := range groupstats {
if config.Verbose > 0 {
log.Printf("Compute ranks for group %s\n", groupstats[g].Name)
}
hosts := make([]entry, 0)
for n := range groupstats[g].HostStats {
hosts = append(hosts, entry{name: groupstats[g].HostStats[n].Name, points: float32(groupstats[g].HostStats[n].TOTALpoints)})
}
for n := 0; n < len(hosts)-1; n++ {
for m := n + 1; m < len(hosts); m++ {
if hosts[n].points < hosts[m].points {
hosts[n], hosts[m] = hosts[m], hosts[n]
}
}
}
for n := range hosts {
for m := range groupstats[g].HostStats {
if groupstats[g].HostStats[m].Name == hosts[n].name {
groupstats[g].HostStats[m].RANK = n + 1
}
}
}
}
today := fmt.Sprintf("%4d-%02d-%02d", time.Now().Year(), time.Now().Month(), time.Now().Day())
if config.Verbose > 0 {
log.Printf("Marshal %s data to json\n", today)
}
data := map[string][]GroupStat{today: groupstats}
// compute data
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
jsonfilename := fmt.Sprintf("data-%d-%02d-%02d.json", time.Now().Year(), time.Now().Month(), time.Now().Day())
if config.Verbose > 0 {
log.Printf("Write json file %s\n", jsonfilename)
}
f, err := os.Create(jsonfilename)
if err != nil {
panic(err)
}
defer f.Close()
n, err := f.Write(b)
if err != nil {
panic(err)
}
if n != len(b) {
panic(fmt.Errorf("Marshaled data is %d bytes, written only %d bytes", len(b), n))
}
f.Sync()
// done
if config.Verbose > 0 {
log.Println("Done")
}
}
var data2points map[string]int = map[string]int{
"good": 1,
"neutral": 0,
"warning": -1,
"error": -2,
}
func str2points(value string) int {
return data2points[value]
}