-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsscan.go
557 lines (519 loc) · 16.4 KB
/
tlsscan.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
package tlsaudit
import (
"crypto/tls"
"fmt"
"net"
"os"
"sort"
"strings"
"sync"
"time"
portscan "github.com/adedayo/tcpscan"
gotls "github.com/adedayo/tlsaudit/pkg/golang"
tlsmodel "github.com/adedayo/tlsaudit/pkg/model"
)
var (
//timeout for network read deadlines
patientTimeout = 10 * time.Second
defaultTimeout = 5 * time.Second
)
type orderedCipherStruct struct {
Protocol uint16
Preference bool
Ciphers []uint16
}
//ScanCIDRTLS combines a port scan with TLS scan for a CIDR range to return the open ports, and the TLS setting for each port over the result channel
func ScanCIDRTLS(cidr string, config tlsmodel.ScanConfig) <-chan tlsmodel.ScanResult {
scanResults := make(chan tlsmodel.ScanResult)
defer func() {
if r := recover(); r != nil {
fmt.Printf("Error: %+v\n", r)
os.Exit(1)
}
}()
go func() {
defer close(scanResults)
scan := make(map[string]portscan.PortACK)
resultChannels := []<-chan tlsmodel.ScanResult{}
result := portscan.ScanCIDR(portscan.ScanConfig{
Timeout: config.Timeout,
PacketsPerSecond: config.PacketsPerSecond,
Quiet: true,
}, cidr)
select {
case <-time.After(12 * time.Duration(config.Timeout) * time.Second):
//Timeout (in a minute by default) if port scan doesn't terminate in time
return
case <-func() chan bool {
out := make(chan bool)
go func() {
defer close(out)
hostnames := make(map[string]string)
for ack := range result {
// fmt.Printf("Got %s ACK %#v\n", ack.Status(), ack)
if ack.IsOpen() {
port := strings.Split(ack.Port, "(")[0]
key := ack.Host + ack.Port
domain := ""
if _, present := hostnames[ack.Host]; !present {
cname, err := net.LookupCNAME(ack.Host)
if err == nil {
domain = cname
}
hostnames[ack.Host] = domain
} else {
domain = hostnames[ack.Host]
}
if _, present := scan[key]; !present {
scan[key] = ack
channel := scanHost(tlsmodel.HostAndPort{
Hostname: ack.Host,
Port: port,
}, config, domain)
resultChannels = append(resultChannels, channel)
}
}
}
for res := range MergeResultChannels(resultChannels...) {
scanResults <- res
}
}()
return out
}():
//NOOP
}
}()
return scanResults
}
func mergeACKChannels(ackChannels ...<-chan portscan.PortACK) <-chan portscan.PortACK {
var wg sync.WaitGroup
out := make(chan portscan.PortACK)
output := func(c <-chan portscan.PortACK) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(ackChannels))
for _, c := range ackChannels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
//MergeResultChannels as suggested
func MergeResultChannels(channels ...<-chan tlsmodel.ScanResult) <-chan tlsmodel.ScanResult {
var wg sync.WaitGroup
out := make(chan tlsmodel.ScanResult)
output := func(c <-chan tlsmodel.ScanResult) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(channels))
for _, c := range channels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
//ServerHelloAndCert struct holds server hello message and certificate (indicating whether it was STARTTLS), otherwise the error
type ServerHelloAndCert struct {
ServerHello tlsmodel.ServerHelloMessage
Cert tlsmodel.CertificateMessage
StartTLS bool
Err error
}
func mergeHandShakeChannels(channels ...<-chan ServerHelloAndCert) <-chan ServerHelloAndCert {
var wg sync.WaitGroup
out := make(chan ServerHelloAndCert)
output := func(c <-chan ServerHelloAndCert) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(channels))
for _, c := range channels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func mergeHelloKeyChannels(channels ...<-chan tlsmodel.HelloAndKey) <-chan tlsmodel.HelloAndKey {
var wg sync.WaitGroup
out := make(chan tlsmodel.HelloAndKey)
output := func(c <-chan tlsmodel.HelloAndKey) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(channels))
for _, c := range channels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
//scanHost finds whether a port on a host supports TLS and if so what protocols and ciphers are supported
func scanHost(hostPort tlsmodel.HostAndPort, config tlsmodel.ScanConfig, serverName string) chan tlsmodel.ScanResult {
resultChannel := make(chan tlsmodel.ScanResult)
patientTimeout = time.Duration(config.Timeout) * time.Second
go func() {
defer close(resultChannel)
host := hostPort.Hostname
port := hostPort.Port
hostnameWithPort := fmt.Sprintf("%s:%s", host, port)
result := tlsmodel.ScanResult{}
result.Server = host
result.Port = port
result.CipherSuiteByProtocol = make(map[uint16][]uint16)
result.OcspStaplingByProtocol = make(map[uint16]bool)
result.SelectedCipherByProtocol = make(map[uint16]uint16)
result.ALPNByProtocol = make(map[uint16]string)
result.SecureRenegotiationSupportedByProtocol = make(map[uint16]bool)
result.HasCipherPreferenceOrderByProtocol = make(map[uint16]bool)
result.CipherPreferenceOrderByProtocol = make(map[uint16][]uint16)
result.ServerHelloMessageByProtocolByCipher = make(map[uint16]map[uint16]tlsmodel.ServerHelloMessage)
result.CertificatesPerProtocol = make(map[uint16]tlsmodel.CertificateMessage)
result.KeyExchangeByProtocolByCipher = make(map[uint16]map[uint16]tlsmodel.ServerKeyExchangeMsg)
handshakeChannels := []<-chan ServerHelloAndCert{}
//check for protocol support with all ciphersuites present
for _, tlsVersion := range tlsmodel.TLSVersions {
hsc := func(versionOfTLS uint16) <-chan ServerHelloAndCert {
config := &gotls.Config{
InsecureSkipVerify: true,
MinVersion: versionOfTLS,
MaxVersion: versionOfTLS,
CipherSuites: tlsmodel.AllCipherSuites,
NextProtos: tlsmodel.AllALPNProtos,
}
if serverName != "" {
config.ServerName = serverName
}
handshakeChan := HandShakeClientHelloGetServerCert(hostnameWithPort, config, patientTimeout)
return handshakeChan
}(tlsVersion)
handshakeChannels = append(handshakeChannels, hsc)
}
for res := range mergeHandShakeChannels(handshakeChannels...) {
process(res, &result)
}
sort.Sort(uint16Sorter(result.SupportedProtocols))
//chech support for TLS_FALLBACK_SCSV
checkFallbackSCSVSupport(&result, hostnameWithPort, serverName, patientTimeout)
// fmt.Printf("Got result %s, %s, %#v \n", result.Server, result.Port, result.SupportedProtocols)
if !config.ProtocolsOnly {
//now test each cipher for only the supported protocols
outChannels := []<-chan tlsmodel.HelloAndKey{}
for _, tlsVersion := range result.SupportedProtocols {
for _, cipher := range tlsmodel.AllCipherSuites {
out := testConnection(hostnameWithPort, tlsVersion, cipher, result.IsSTARTLS, serverName)
outChannels = append(outChannels, out)
}
}
processConnectionTest(outChannels, &result)
//check cipher ordering per protocol
orderedCipherChannels := make([]<-chan orderedCipherStruct, len(result.SupportedProtocols))
for ind, tlsVersion := range result.SupportedProtocols {
result.CipherSuiteByProtocol[tlsVersion] = makeUnique(result.CipherSuiteByProtocol[tlsVersion])
oChan := func() chan orderedCipherStruct {
outOrdered := make(chan orderedCipherStruct)
go func(tlsVer uint16) {
defer close(outOrdered)
sort.Sort(uint16Sorter(result.CipherSuiteByProtocol[tlsVer]))
ciphers := result.CipherSuiteByProtocol[tlsVer]
preference := false
cipherCount := len(ciphers)
nextCipher := 0
orderedCiphers := make([]uint16, cipherCount)
CheckPoint:
cipherCount = len(ciphers)
if cipherCount == 1 {
//single cipher is assumed to support ordering ;-)
preference = true
} else {
config := &gotls.Config{
InsecureSkipVerify: true,
MinVersion: tlsVer,
MaxVersion: tlsVer,
CipherSuites: ciphers,
}
if serverName != "" {
config.ServerName = serverName
}
msg, err := HandShakeClientHello(hostnameWithPort, config, result.IsSTARTLS, defaultTimeout)
if err == nil {
//reverse the ciphers
reverseCiphers := make([]uint16, cipherCount)
for i := cipherCount - 1; i >= 0; i-- {
reverseCiphers[cipherCount-i-1] = ciphers[i]
}
config.CipherSuites = reverseCiphers
msg2, err := HandShakeClientHello(hostnameWithPort, config, result.IsSTARTLS, defaultTimeout)
if err == nil && msg.CipherSuite == msg2.CipherSuite {
//if on reverse we get the same cipher, then there is a server preference
preference = true
orderedCiphers[nextCipher] = msg.CipherSuite
nextCipher++
cipherCount = len(reverseCiphers) - 1
retryCount := 0
maxRetries := 2 * cipherCount
for cipherCount >= 2 {
ciphers = make([]uint16, cipherCount)
next := 0
for _, c := range reverseCiphers {
if c != msg.CipherSuite && len(ciphers) > next {
ciphers[next] = c
next++
}
}
config.CipherSuites = ciphers
reverseCiphers = ciphers
cipherCount = len(reverseCiphers) - 1
msg2, err = HandShakeClientHello(hostnameWithPort, config, result.IsSTARTLS, defaultTimeout)
if err == nil {
msg = msg2
orderedCiphers[nextCipher] = msg.CipherSuite
nextCipher++
} else {
retryCount++
if retryCount > maxRetries {
//assume server does not support order
preference = false
break //stop trying
}
}
}
//Last two ciphers - add the cipher that was not preferred
if preference {
for _, c := range reverseCiphers {
if c != msg.CipherSuite {
orderedCiphers[nextCipher] = c
nextCipher++
}
}
}
} else {
//0xCCA8: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" causes problems for the cipher ordering check. Exclude and redo if present
if msg.CipherSuite == 0xcca8 || msg2.CipherSuite == 0xcca8 {
ciphers2 := []uint16{}
for _, c := range ciphers {
if c != 0xcca8 {
ciphers2 = append(ciphers2, c)
}
}
ciphers = ciphers2
orderedCiphers[nextCipher] = 0xcca8
nextCipher++
goto CheckPoint
}
}
}
}
outOrdered <- orderedCipherStruct{
Protocol: tlsVer,
Preference: preference,
Ciphers: orderedCiphers,
}
}(tlsVersion)
return outOrdered
}()
orderedCipherChannels[ind] = oChan
}
for out := range mergeOrderedCiperChannnels(orderedCipherChannels...) {
vers := out.Protocol
result.HasCipherPreferenceOrderByProtocol[vers] = out.Preference
if out.Preference {
result.CipherPreferenceOrderByProtocol[vers] = out.Ciphers
}
}
}
resultChannel <- result
}()
return resultChannel
}
func checkFallbackSCSVSupport(result *tlsmodel.ScanResult, hostnameWithPort, serverName string, patientTimeout time.Duration) {
maxProtocol := tlsmodel.VersionSSL20
if len(result.SupportedProtocols) > 0 {
switch result.SupportedProtocols[0] {
case tls.VersionTLS12:
maxProtocol = tls.VersionTLS11
case tls.VersionTLS11:
maxProtocol = tls.VersionTLS10
case tls.VersionTLS10:
maxProtocol = tls.VersionSSL30
default:
maxProtocol = tlsmodel.VersionSSL20
}
ciphers := []uint16{}
for _, p := range result.SupportedProtocols {
ciphers = append(ciphers, result.SelectedCipherByProtocol[p])
}
ciphers = append(ciphers, 0x5600) //add TLS_FALLBACK_SCSV last in line with section 4 of https://datatracker.ietf.org/doc/rfc7507/
config := &gotls.Config{
InsecureSkipVerify: true,
MinVersion: maxProtocol,
MaxVersion: maxProtocol,
CipherSuites: ciphers,
NextProtos: tlsmodel.AllALPNProtos,
}
if serverName != "" {
config.ServerName = serverName
}
rawConn, err := net.DialTimeout("tcp", hostnameWithPort, patientTimeout)
if err != nil {
return
}
defer rawConn.Close()
rawConn.SetDeadline(time.Now().Add(patientTimeout))
c := gotls.MakeClientConnection(rawConn, config)
hello, err := gotls.MakeClientHello(config)
if err != nil {
return
}
if _, err := c.WriteRecord(gotls.RecordTypeHandshake, hello.Marshal()); err != nil {
return
}
if _, err := c.ReadServerHello(); err != nil {
if strings.Contains(err.Error(), "inappropriate fallback") {
result.SupportsTLSFallbackSCSV = true
}
}
}
}
//make sure the slice only contains unique values
func makeUnique(data []uint16) (result []uint16) {
m := make(map[uint16]bool)
for _, d := range data {
m[d] = true
}
for d := range m {
result = append(result, d)
}
return
}
type uint16Sorter []uint16
func (k uint16Sorter) Len() int {
return len(k)
}
func (k uint16Sorter) Swap(i, j int) {
k[i], k[j] = k[j], k[i]
}
func (k uint16Sorter) Less(i, j int) bool {
return k[i] > k[j]
}
func mergeOrderedCiperChannnels(channels ...<-chan orderedCipherStruct) <-chan orderedCipherStruct {
var wg sync.WaitGroup
out := make(chan orderedCipherStruct)
output := func(c <-chan orderedCipherStruct) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(channels))
for _, c := range channels {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func process(res ServerHelloAndCert, result *tlsmodel.ScanResult) {
if res.Err == nil { //protocol supported
msg := res.ServerHello
c := msg.CipherSuite
result.SupportedProtocols = append(result.SupportedProtocols, msg.Vers)
result.OcspStaplingByProtocol[msg.Vers] = msg.OcspStapling
result.SecureRenegotiationSupportedByProtocol[msg.Vers] = msg.SecureRenegotiationSupported
result.SelectedCipherByProtocol[msg.Vers] = c
result.CipherSuiteByProtocol[msg.Vers] = append(result.CipherSuiteByProtocol[msg.Vers], c)
result.IsSTARTLS = res.StartTLS
result.ALPNByProtocol[msg.Vers] = msg.AlpnProtocol
result.CertificatesPerProtocol[msg.Vers] = res.Cert
}
}
func processConnectionTest(outChannels []<-chan tlsmodel.HelloAndKey, scan *tlsmodel.ScanResult) {
count := 0
for hk := range mergeHelloKeyChannels(outChannels...) {
count++
hello := hk.Hello
tlsVersion := hello.Vers
scan.CipherSuiteByProtocol[tlsVersion] = append(scan.CipherSuiteByProtocol[tlsVersion], hello.CipherSuite)
if val, ok := scan.ServerHelloMessageByProtocolByCipher[tlsVersion]; ok {
val[hello.CipherSuite] = hello
scan.ServerHelloMessageByProtocolByCipher[tlsVersion] = val
} else {
scan.ServerHelloMessageByProtocolByCipher[tlsVersion] = make(map[uint16]tlsmodel.ServerHelloMessage)
scan.ServerHelloMessageByProtocolByCipher[tlsVersion][hello.CipherSuite] = hello
}
processECDHCipher(hk, scan)
}
}
func processECDHCipher(hk tlsmodel.HelloAndKey, result *tlsmodel.ScanResult) {
if hk.HasKey {
hello := hk.Hello
tlsVersion := hello.Vers
serverKey := hk.Key
if k, ok := result.KeyExchangeByProtocolByCipher[tlsVersion]; ok {
k[hello.CipherSuite] = serverKey
result.KeyExchangeByProtocolByCipher[tlsVersion] = k
} else {
result.KeyExchangeByProtocolByCipher[tlsVersion] = make(map[uint16]tlsmodel.ServerKeyExchangeMsg)
result.KeyExchangeByProtocolByCipher[tlsVersion][hello.CipherSuite] = serverKey
}
}
}
func fixedLength(data string, length int, delim string) string {
raws := []string{}
runes := []rune{}
for i, r := range []rune(data) {
runes = append(runes, r)
if (i+1)%length == 0 {
raws = append(raws, string(runes))
runes = []rune{}
}
}
raws = append(raws, string(runes))
return strings.Join(raws, delim)
}
func testConnection(hostnameWithPort string, tlsVersion, cipher uint16, startTLS bool, serverName string) <-chan tlsmodel.HelloAndKey {
out := make(chan tlsmodel.HelloAndKey)
go func() {
defer close(out)
config := &gotls.Config{
InsecureSkipVerify: true,
MinVersion: tlsVersion,
MaxVersion: tlsVersion,
CipherSuites: []uint16{cipher},
}
if serverName != "" {
config.ServerName = serverName
}
hk, err := HandShakeUpToKeyExchange(hostnameWithPort, config, startTLS, patientTimeout)
if err != nil && hk.Hello.Vers != 0 && !hk.HasKey { // the scenario for ciphers that pass server hello stage but don't use key exchange
out <- hk
}
if err == nil || err.Error() == tlsmodel.NkxErrorMessage {
out <- hk
}
}()
return out
}