forked from ssllabs/ssllabs-scan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssllabs-scan.go
870 lines (729 loc) · 20.3 KB
/
ssllabs-scan.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// +build go1.3
/*
* Licensed to Qualys, Inc. (QUALYS) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* QUALYS licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import "crypto/tls"
import "encoding/json"
import "flag"
import "fmt"
import "io/ioutil"
import "bufio"
import "os"
import "log"
import "math/rand"
import "net"
import "net/http"
import "net/url"
import "strconv"
import "strings"
import "sync/atomic"
import "time"
import "sort"
const (
LOG_NONE = -1
LOG_EMERG = 0
LOG_ALERT = 1
LOG_CRITICAL = 2
LOG_ERROR = 3
LOG_WARNING = 4
LOG_NOTICE = 5
LOG_INFO = 6
LOG_DEBUG = 7
LOG_TRACE = 8
)
var USER_AGENT = "ssllabs-scan v0.1"
var logLevel = LOG_INFO
var activeAssessments = 0
var maxAssessments = 1
var requestCounter uint64 = 0
var apiLocation = "https://api.dev.ssllabs.com/api/fa78d5a4/"
var globalClearCache = true
var globalFromCache = false
var httpClient *http.Client
type LabsError struct {
Field string
Message string
}
type LabsErrorResponse struct {
ResponseErrors []LabsError `json:"errors"`
}
func (e LabsErrorResponse) Error() string {
msg, err := json.Marshal(e)
if err != nil {
return err.Error()
} else {
return string(msg)
}
}
type LabsKey struct {
Size int
Strength int
Alg string
DebianFlaw bool
Q int
}
type LabsCert struct {
Subject string
CommonNames []string
AltNames []string
NotBefore int64
NotAfter int64
IssuerSubject string
SigAlg string
IssuerLabel string
RevocationInfo int
CrlURIs []string
OcspURIs []string
RevocationStatus int
Sgc int
ValidationType string
Issues int
}
type LabsChainCert struct {
Subject string
Label string
IssuerSubject string
IssuerLabel string
Issues int
Raw string
}
type LabsChain struct {
Certs []LabsChainCert
Issues int
}
type LabsProtocol struct {
Id int
Name string
Version string
V2SuitesDisabled bool
ErrorMessage bool
Q int
}
type LabsSimClient struct {
Id int
Name string
Platform string
Version string
IsReference bool
}
type LabsSimulation struct {
Client LabsSimClient
ErrorCode int
Attempts int
ProtocolId int
SuiteId int
}
type LabsSimDetails struct {
Results []LabsSimulation
}
type LabsSuite struct {
Id int
Name string
CipherStrength int
DhStrength int
DhP int
DhG int
DhYs int
EcdhBits int
EcdhStrength int
Q int
}
type LabsSuites struct {
List []LabsSuite
Preference bool
}
type LabsEndpointDetails struct {
HostStartTime int64
Key LabsKey
Cert LabsCert
Chain LabsChain
Protocols []LabsProtocol
Suites LabsSuites
ServerSignature string
PrefixDelegation bool
NonPrefixDelegation bool
VulnBeast bool
RenegSupport int
StsResponseHeader string
StsMaxAge int64
StsSubdomains bool
PkpResponseHeader string
SessionResumption int
CompressionMethods int
SupportsNpn bool
NpnProtocols string
SessionTickets int
OcspStapling bool
SniRequired bool
HttpStatusCode int
HttpForwarding string
SupportsRc4 bool
ForwardSecrecy int
Rc4WithModern bool
Sims LabsSimDetails
Heartbleed bool
Heartbeat bool
OpenSslCcs int
PoodleTls int
}
type LabsEndpoint struct {
IpAddress string
ServerName string
StatusMessage string
StatusDetailsMessage string
Grade string
HasWarnings bool
IsExceptional bool
Progress int
Duration int
Eta int
Delegation int
Details LabsEndpointDetails
}
type LabsReport struct {
Host string
Port int
Protocol string
IsPublic bool
Status string
StatusMessage string
StartTime int64
TestTime int64
EngineVersion string
CriteriaVersion string
CacheExpiryTime int64
Endpoints []LabsEndpoint
CertHostnames []string
rawJSON string
}
type LabsResults struct {
reports []LabsReport
responses []string
}
type LabsInfo struct {
EngineVersion string
CriteriaVersion string
ClientMaxAssessments int
}
func invokeGetRepeatedly(url string) (*http.Response, []byte, error) {
retryCount := 0
for {
var reqId = atomic.AddUint64(&requestCounter, 1)
if logLevel >= LOG_DEBUG {
log.Printf("[DEBUG] Request (%v): %v", reqId, url)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
req.Header.Add("User-Agent", USER_AGENT)
resp, err := httpClient.Do(req)
if err == nil {
if logLevel >= LOG_DEBUG {
log.Printf("[DEBUG] Response status code (%v): %v", reqId, resp.StatusCode)
}
// Adjust maximum concurrent requests.
headerValue := resp.Header.Get("X-ClientMaxAssessments")
if headerValue != "" {
i, err := strconv.Atoi(headerValue)
if err == nil {
if maxAssessments != i {
maxAssessments = i
if logLevel >= LOG_INFO {
log.Printf("[INFO] Server set max concurrent assessments to %v", headerValue)
}
}
} else {
if logLevel >= LOG_WARNING {
log.Printf("[WARNING] Ignoring invalid X-ClientMaxAssessments value (%v): %v", headerValue, err)
}
}
}
// Retrieve the response body.
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
if logLevel >= LOG_TRACE {
log.Printf("[TRACE] Response (%v):\n%v", reqId, string(body))
}
return resp, body, nil
} else {
if err.Error() == "EOF" {
// Server closed a persistent connection on us, which
// Go doesn't seem to be handling well. So we'll try one
// more time.
if retryCount > 1 {
log.Fatalf("[ERROR] Too many HTTP requests failed with EOF")
}
} else {
if retryCount > 5 {
log.Fatalf("[ERROR] Too many failed HTTP requests")
}
time.Sleep(30 * time.Second)
}
retryCount++
}
}
}
func invokeApi(command string) (*http.Response, []byte, error) {
var url = apiLocation + "/" + command
for {
resp, body, err := invokeGetRepeatedly(url)
if err != nil {
return nil, nil, err
}
// Status codes 429, 503, and 529 essentially mean try later. Thus,
// if we encounter them, we sleep for a while and try again.
if (resp.StatusCode == 429) || (resp.StatusCode == 503) {
if logLevel >= LOG_NOTICE {
log.Printf("[NOTICE] Sleeping for 5 minutes after a %v response", resp.StatusCode)
}
time.Sleep(5 * time.Minute)
} else if resp.StatusCode == 529 {
// In case of the overloaded server, randomize the sleep time so
// that some clients reconnect earlier and some later.
sleepTime := 15 + rand.Int31n(15)
if logLevel >= LOG_NOTICE {
log.Printf("[NOTICE] Sleeping for %v minutes after a 529 response", sleepTime)
}
time.Sleep(time.Duration(sleepTime) * time.Minute)
} else if (resp.StatusCode != 200) && (resp.StatusCode != 400) {
log.Fatalf("[ERROR] Unexpected response status code %v", resp.StatusCode)
} else {
return resp, body, nil
}
}
}
func invokeInfo() (*LabsInfo, error) {
var command = "info"
_, body, err := invokeApi(command)
if err != nil {
return nil, err
}
var labsInfo LabsInfo
err = json.Unmarshal(body, &labsInfo)
if err != nil {
return nil, err
}
return &labsInfo, nil
}
func invokeAnalyze(host string, clearCache bool, fromCache bool) (*LabsReport, error) {
var command = "analyze?host=" + host + "&all=done"
if fromCache {
command = command + "&fromCache=on"
} else if clearCache {
command = command + "&clearCache=on"
}
resp, body, err := invokeApi(command)
if err != nil {
return nil, err
}
// Use the status code to determine if the response is an error.
if resp.StatusCode == 400 {
// Parameter validation error.
var apiError LabsErrorResponse
err = json.Unmarshal(body, &apiError)
if err != nil {
return nil, err
}
return nil, apiError
} else {
// We should have a proper response.
var analyzeResponse LabsReport
err = json.Unmarshal(body, &analyzeResponse)
if err != nil {
return nil, err
}
// Add the JSON body to the response
analyzeResponse.rawJSON = string(body)
return &analyzeResponse, nil
}
}
type Event struct {
host string
eventType int
report *LabsReport
}
const (
ASSESSMENT_STARTING = 0
ASSESSMENT_COMPLETE = 1
)
func NewAssessment(host string, eventChannel chan Event) {
eventChannel <- Event{host, ASSESSMENT_STARTING, nil}
var report *LabsReport
var startTime int64 = -1
var clearCache = globalClearCache
for {
myResponse, err := invokeAnalyze(host, clearCache, globalFromCache)
if err != nil {
log.Fatalf("[ERROR] API invocation failed: %v", err)
}
if startTime == -1 {
startTime = myResponse.StartTime
clearCache = false
} else {
if myResponse.StartTime != startTime {
log.Fatalf("[ERROR] Inconsistent startTime. Expected %v, got %v.", startTime, myResponse.StartTime)
}
}
if (myResponse.Status == "READY") || (myResponse.Status == "ERROR") {
report = myResponse
break
}
time.Sleep(5 * time.Second)
}
eventChannel <- Event{host, ASSESSMENT_COMPLETE, report}
}
type HostProvider struct {
hostnames []string
i int
}
func NewHostProvider(hs []string) *HostProvider {
hostProvider := HostProvider{hs, 0}
return &hostProvider
}
func (hp *HostProvider) next() (string, bool) {
if hp.i < len(hp.hostnames) {
host := hp.hostnames[hp.i]
hp.i = hp.i + 1
return host, true
} else {
return "", false
}
}
type Manager struct {
hostProvider *HostProvider
FrontendEventChannel chan Event
BackendEventChannel chan Event
results *LabsResults
}
func NewManager(hostProvider *HostProvider) *Manager {
manager := Manager{
hostProvider: hostProvider,
FrontendEventChannel: make(chan Event),
BackendEventChannel: make(chan Event),
results: &LabsResults{reports: make([]LabsReport, 0)},
}
go manager.run()
return &manager
}
func (manager *Manager) startAssessment(h string) {
go NewAssessment(h, manager.BackendEventChannel)
activeAssessments++
}
func (manager *Manager) run() {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
httpClient = &http.Client{Transport: transport}
// Ping SSL Labs to determine how many concurrent
// assessments we're allowed to use. Print the API version
// information and the limits.
labsInfo, err := invokeInfo()
if err != nil {
// TODO Signal error so that we return the correct exit code
close(manager.FrontendEventChannel)
}
if logLevel >= LOG_INFO {
log.Printf("[INFO] SSL Labs v%v (criteria version %v)", labsInfo.EngineVersion, labsInfo.CriteriaVersion)
}
moreAssessments := true
for {
select {
// Handle assessment events (e.g., starting and finishing).
case e := <-manager.BackendEventChannel:
if e.eventType == ASSESSMENT_STARTING {
if logLevel >= LOG_INFO {
log.Printf("[INFO] Assessment starting: %v", e.host)
}
}
if e.eventType == ASSESSMENT_COMPLETE {
if logLevel >= LOG_INFO {
msg := ""
// Missing C's ternary operator here.
if len(e.report.Endpoints) == 0 {
msg = fmt.Sprintf("[WARN] Assessment failed: %v (%v)", e.host, e.report.StatusMessage)
} else if len(e.report.Endpoints) > 1 {
msg = fmt.Sprintf("[INFO] Assessment complete: %v (%v hosts in %v seconds)",
e.host, len(e.report.Endpoints), (e.report.TestTime-e.report.StartTime)/1000)
} else {
msg = fmt.Sprintf("[INFO] Assessment complete: %v (%v host in %v seconds)",
e.host, len(e.report.Endpoints), (e.report.TestTime-e.report.StartTime)/1000)
}
for _, endpoint := range e.report.Endpoints {
if endpoint.Grade != "" {
msg = msg + "\n " + endpoint.IpAddress + ": " + endpoint.Grade
} else {
msg = msg + "\n " + endpoint.IpAddress + ": Err: " + endpoint.StatusMessage
}
}
log.Println(msg)
}
activeAssessments--
manager.results.reports = append(manager.results.reports, *e.report)
manager.results.responses = append(manager.results.responses, e.report.rawJSON)
// Are we done?
if (activeAssessments == 0) && (moreAssessments == false) {
close(manager.FrontendEventChannel)
return
}
}
break
// Once a second, start a new assessment, provided there are
// hostnames left and we're not over the concurrent assessment limit.
default:
<-time.NewTimer(time.Second).C
if moreAssessments {
if activeAssessments < maxAssessments {
host, hasNext := manager.hostProvider.next()
if hasNext {
manager.startAssessment(host)
} else {
// We've run out of hostnames and now just need
// to wait for all the assessments to complete.
moreAssessments = false
if activeAssessments == 0 {
close(manager.FrontendEventChannel)
return
}
}
}
}
break
}
}
}
func parseLogLevel(level string) int {
switch {
case level == "error":
return LOG_ERROR
case level == "info":
return LOG_INFO
case level == "debug":
return LOG_DEBUG
case level == "trace":
return LOG_TRACE
}
log.Fatalf("[ERROR] Unrecognized log level: %v", level)
return -1
}
func flattenJSON(inputJSON map[string]interface{}, rootKey string, flattened *map[string]interface{}) {
var keysep = "." // Char to separate keys
var Q = "\"" // Char to envelope strings
for rkey, value := range inputJSON {
key := rootKey + rkey
if _, ok := value.(string); ok {
(*flattened)[key] = Q + value.(string) + Q
} else if _, ok := value.(float64); ok {
(*flattened)[key] = value.(float64)
} else if _, ok := value.(bool); ok {
(*flattened)[key] = value.(bool)
} else if _, ok := value.([]interface{}); ok {
for i := 0; i < len(value.([]interface{})); i++ {
aKey := key + keysep + strconv.Itoa(i)
if _, ok := value.([]interface{})[i].(string); ok {
(*flattened)[aKey] = Q + value.([]interface{})[i].(string) + Q
} else if _, ok := value.([]interface{})[i].(float64); ok {
(*flattened)[aKey] = value.([]interface{})[i].(float64)
} else if _, ok := value.([]interface{})[i].(bool); ok {
(*flattened)[aKey] = value.([]interface{})[i].(bool)
} else {
flattenJSON(value.([]interface{})[i].(map[string]interface{}), key+keysep+strconv.Itoa(i)+keysep, flattened)
}
}
} else if value == nil {
(*flattened)[key] = nil
} else {
flattenJSON(value.(map[string]interface{}), key+keysep, flattened)
}
}
}
func flattenAndFormatJSON(inputJSON []byte) *[]string {
var flattened = make(map[string]interface{})
mappedJSON := map[string]interface{}{}
err := json.Unmarshal(inputJSON, &mappedJSON)
if err != nil {
log.Fatalf("[ERROR] Reconsitution of JSON failed: %v", err)
}
// Flatten the JSON structure, recursively
flattenJSON(mappedJSON, "", &flattened)
// Make a sorted index, so we can print keys in order
kIndex := make([]string, len(flattened))
ki := 0
for key, _ := range flattened {
kIndex[ki] = key
ki++
}
sort.Strings(kIndex)
// Ordered flattened data
var flatStrings []string
for _, value := range kIndex {
flatStrings = append(flatStrings, fmt.Sprintf("\"%v\": %v\n", value, flattened[value]))
}
return &flatStrings
}
func readLines(path *string) ([]string, error) {
file, err := os.Open(*path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func validateURL(URL string) bool {
_, err := url.Parse(URL)
if err != nil {
return false
} else {
return true
}
}
func validateHostname(hostname string) bool {
addrs, err := net.LookupHost(hostname)
// In some cases there is no error
// but there are also no addresses
if err != nil || len(addrs) < 1 {
return false
} else {
return true
}
}
func main() {
var conf_api = flag.String("api", "BUILTIN", "API entry point, for example https://www.example.com/api/")
var conf_verbosity = flag.String("verbosity", "info", "Configure log verbosity: error, info, debug, or trace.")
var conf_quiet = flag.Bool("quiet", false, "Disable status messages (logging)")
var conf_json_flat = flag.Bool("json-flat", false, "Output results in flattened JSON format")
var conf_hostfile = flag.String("hostfile", "", "File containing hosts to scan (one per line)")
var conf_usecache = flag.Bool("usecache", false, "If true, accept cached results (if available), else force live scan.")
var conf_grade = flag.Bool("grade", false, "Output only the hostname: grade")
var conf_hostcheck = flag.Bool("hostcheck", false, "If true, host resolution failure will result in a fatal error.")
flag.Parse()
logLevel = parseLogLevel(strings.ToLower(*conf_verbosity))
if *conf_quiet {
logLevel = LOG_NONE
}
// We prefer cached results
if *conf_usecache {
globalFromCache = true
globalClearCache = false
}
// Verify that the API entry point is a URL.
if *conf_api != "BUILTIN" {
apiLocation = *conf_api
}
if validateURL(apiLocation) == false {
log.Fatalf("[ERROR] Invalid API URL: %v", apiLocation)
}
var hostnames []string
if *conf_hostfile != "" {
// Open file, and read it
var err error
hostnames, err = readLines(conf_hostfile)
if err != nil {
log.Fatalf("[ERROR] Reading from specified hostfile failed: %v", err)
}
} else {
// Read hostnames from the rest of the args
hostnames = flag.Args()
}
if *conf_hostcheck {
// Validate all hostnames before we attempt to test them. At least
// one hostname is required.
for _, host := range hostnames {
if validateHostname(host) == false {
log.Fatalf("[ERROR] Invalid hostname: %v", host)
}
}
}
hp := NewHostProvider(hostnames)
manager := NewManager(hp)
// Respond to events until all the work is done.
for {
_, running := <-manager.FrontendEventChannel
if running == false {
var results []byte
var err error
if *conf_grade {
// Just the grade(s). We use flatten and RAW
/*
"endpoints.0.grade": "A"
"host": "testing.spatialkey.com"
*/
for i := range manager.results.responses {
results := []byte(manager.results.responses[i])
name := ""
grade := ""
flattened := flattenAndFormatJSON(results)
for _, fval := range *flattened {
if strings.HasPrefix(fval, "\"host\"") {
// hostname
parts := strings.Split(fval, ": ")
name = strings.TrimSuffix(parts[1], "\n")
if grade != "" {
break
}
} else if strings.HasPrefix(fval, "\"endpoints.0.grade\"") {
// grade
parts := strings.Split(fval, ": ")
grade = strings.TrimSuffix(parts[1], "\n")
if name != "" {
break
}
}
}
if grade != "" && name != "" {
fmt.Println(name + ": " + grade)
}
}
} else if *conf_json_flat {
// Flat JSON and RAW
for i := range manager.results.responses {
results := []byte(manager.results.responses[i])
flattened := flattenAndFormatJSON(results)
// Print the flattened data
fmt.Println(*flattened)
}
} else {
// Raw (non-Go-mangled) JSON output
fmt.Println("[")
for i := range manager.results.responses {
results := manager.results.responses[i]
if i > 0 {
fmt.Println(",")
}
fmt.Println(results)
}
fmt.Println("]")
}
if err != nil {
log.Fatalf("[ERROR] Output to JSON failed: %v", err)
}
fmt.Println(string(results))
if logLevel >= LOG_INFO {
log.Println("[INFO] All assessments complete; shutting down")
}
return
}
}
}