This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
tls_scan.go
183 lines (155 loc) · 5.02 KB
/
tls_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
package tlsscan
import (
"context"
"crypto/x509"
"fmt"
"net"
"sync"
"crypto/tls"
"strings"
"time"
"github.com/pivotal-cf/scantron"
"github.com/pivotal-cf/scantron/scanlog"
"golang.org/x/sync/semaphore"
)
const (
maxInFlight = 20
)
type result struct {
version string
suite string
}
func release(logger scanlog.Logger, sem *semaphore.Weighted, wg *sync.WaitGroup) {
logger.Debugf("Releasing locks")
sem.Release(1)
wg.Done()
}
type TlsScannerImpl struct{}
func (s *TlsScannerImpl) Scan(logger scanlog.Logger, host string, port string) (scantron.CipherInformation, error) {
results := scantron.CipherInformation{}
for _, version := range ProtocolVersions {
results[version.Name] = []string{}
}
supportedProtocols := getSupportedProtocols(logger, host, port)
if len(supportedProtocols) == 0 {
logger.Debugf("Skipping cipher scan for %s:%s (no supported protocols)", host, port)
return results, nil
}
logger.Debugf("Starting cipher scan for %s:%s", host, port)
sem := semaphore.NewWeighted(maxInFlight)
cipherSuites, err := BuildCipherSuites()
if err != nil {
return results, err
}
numCiphersuites := len(supportedProtocols) * len(cipherSuites)
resultChan := make(chan result, maxInFlight)
wg := &sync.WaitGroup{}
wg.Add(numCiphersuites)
go func(logger scanlog.Logger) {
for _, version := range supportedProtocols {
logger.Debugf("Starting TLS version %s", version.Name)
for _, cipherSuite := range cipherSuites {
logger.Debugf("Starting ciphersuite %s", cipherSuite.Name)
scanLogger := logger.With(
"host", host,
"port", port,
"version", version.Name,
"suite", cipherSuite.Name,
)
if err := sem.Acquire(context.Background(), 1); err != nil {
scanLogger.Errorf("Failed to acquire lock: %q", err)
}
scanLogger.Debugf("Acquired lock")
go testCipher(scanLogger, version, cipherSuite, sem, wg, host, port, resultChan)
}
}
}(logger)
go func(logger scanlog.Logger) {
wg.Wait()
logger.Debugf("Wait group done")
close(resultChan)
}(logger)
logger.Debugf("About to start reading from channel")
for res := range resultChan {
logger.Debugf("Read %s %s from channel", res.version, res.suite)
results[res.version] = append(results[res.version], res.suite)
}
logger.Debugf("Finished cipher scan for %s:%s", host, port)
return results, nil
}
func testCipher(
logger scanlog.Logger,
version ProtocolVersion,
cipherSuite CipherSuite,
sem *semaphore.Weighted,
wg *sync.WaitGroup,
host string,
port string,
resultChan chan result) {
defer release(logger, sem, wg)
found, err := tryHandshakeWithCipher(logger, host, port, version, cipherSuite)
if err != nil {
logger.Debugf("Remote server did not respond affirmatively to request: %s", err)
return
}
if found {
logger.Debugf("Sending result")
resultChan <- result{
version: version.Name,
suite: cipherSuite.Name,
}
logger.Debugf("Result sent")
}
logger.Debugf("Finished ciphersuite %s", cipherSuite.Name)
}
func getSupportedProtocols(logger scanlog.Logger, host string, port string) []ProtocolVersion {
supportedVersions := []ProtocolVersion{}
for _, version := range ProtocolVersions {
providesCert := false
wantsCert := false
config := tls.Config{
MinVersion: version.ID,
MaxVersion: version.ID,
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
providesCert = true
return nil
},
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
wantsCert = true
return nil, ErrExpectedAbort
},
}
err := AttemptHandshake(logger, &net.Dialer{Timeout: 1 * time.Second}, "tcp", fmt.Sprintf("%s:%s", host, port), &config)
if providesCert {
logger.Debugf("%s:%s accepts TLS (%s mutual=%t)", host, port, version.Name, wantsCert)
supportedVersions = append(supportedVersions, version)
} else {
logger.Debugf("%s:%s refuses TLS (%s %s)", host, port, version.Name, err)
}
}
return supportedVersions
}
func tryHandshakeWithCipher(logger scanlog.Logger, host string, port string, version ProtocolVersion, cipherSuite CipherSuite) (bool, error) {
config := tls.Config{
MinVersion: version.ID,
MaxVersion: version.ID,
CipherSuites: []uint16{cipherSuite.ID},
InsecureSkipVerify: true,
VerifyPeerCertificate: nil,
}
address := fmt.Sprintf("%s:%s", host, port)
logger.Debugf("Dialing %s %s %s", address, version.Name, cipherSuite.Name)
err := AttemptHandshake(logger, &net.Dialer{Timeout: 10 * time.Second}, "tcp", address, &config)
if err != nil {
if strings.Contains(err.Error(), "remote error") {
logger.Debugf("Dialed: no tls for %s %s %s (%s)", address, version.Name, cipherSuite.Name, err)
return false, nil
}
// TODO are these meant to be recorded in tls_scan_errors?
logger.Debugf("Dialed: error for %s %s %s: %s", address, version.Name, cipherSuite.Name, err)
return false, err
}
logger.Debugf("Dialed: tls available for %s %s %s", address, version.Name, cipherSuite.Name)
return true, nil
}