-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
301 lines (272 loc) · 10.2 KB
/
models.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
package tlsmodel
import (
"crypto/rsa"
"crypto/x509"
"fmt"
"net"
"sort"
"strings"
)
//ScanConfig describes details of how the TLS scan should be carried out
type ScanConfig struct {
ProtocolsOnly bool
Timeout int
//Number of Packets per Second to send out during underlying port scan
PacketsPerSecond int
//Suppress certificate output
HideCerts bool
//control whether to produce a running commentary of scan progress or stay quiet till the end
Quiet bool
}
// KeyExchangeAlgorithm says what it is
type KeyExchangeAlgorithm int
// //See https://www.gnu.org/software/gnutls/reference/gnutls-gnutls.html#gnutls-kx-algorithm-t
// //https://github.com/jatovm/classpath/blob/master/gnu/javax/net/ssl/provider/KeyExchangeAlgorithm.java
// const (
// UNKNOWN KeyExchangeAlgorithm = iota
// RSA
// DHE_DSS
// DHE_RSA
// ANON_DH
// SRP
// RSA_EXPORT
// SRP_RSA
// SRP_DSS
// PSK
// DHE_PSK
// ANON_ECDH
// ECDHE_RSA
// ECDHE_ECDSA
// ECDHE_PSK
// )
//HostAndPort is a model representing a hostname and a given port
type HostAndPort struct {
Hostname string
Port string
}
// ServerHelloMessage is the TLS server hello message
type ServerHelloMessage struct {
Raw []byte
Vers uint16
Random []byte
SessionId []byte
CipherSuite uint16
CompressionMethod uint8
NextProtoNeg bool
NextProtos []string
OcspStapling bool
Scts [][]byte
TicketSupported bool
SecureRenegotiation []byte
SecureRenegotiationSupported bool
AlpnProtocol string
}
// ServerKeyExchangeMsg is the key exchange message
type ServerKeyExchangeMsg struct {
Raw []byte
Key []byte
}
// HelloAndKey bundles server hello and ServerKeyExchange messages
type HelloAndKey struct {
Hello ServerHelloMessage
Key ServerKeyExchangeMsg
HasKey bool
}
// CertificateMessage simply exporting the internal certificateMsg
type CertificateMessage struct {
Raw []byte
Certificates [][]byte
}
//GetCertificates returns the list of certificates in a TLS certificate message
func (cert CertificateMessage) GetCertificates() (certs []*x509.Certificate, e error) {
for _, c := range cert.Certificates {
cc, err := x509.ParseCertificate(c)
if err != nil {
return certs, err
}
certs = append(certs, cc)
}
return
}
// ScanResult is the scan result of a server TLS settings
type ScanResult struct {
Server string
Port string
SupportedProtocols []uint16
HasCipherPreferenceOrderByProtocol map[uint16]bool
CipherPreferenceOrderByProtocol map[uint16][]uint16
OcspStaplingByProtocol map[uint16]bool
SelectedCipherByProtocol map[uint16]uint16
ALPNByProtocol map[uint16]string
SecureRenegotiationSupportedByProtocol map[uint16]bool
CipherSuiteByProtocol map[uint16][]uint16
ServerHelloMessageByProtocolByCipher map[uint16]map[uint16]ServerHelloMessage
CertificatesPerProtocol map[uint16]CertificateMessage
KeyExchangeByProtocolByCipher map[uint16]map[uint16]ServerKeyExchangeMsg
IsSTARTLS bool
IsSSH bool
}
//HumanScanResult is a Stringified version of ScanResult
type HumanScanResult struct {
Server string
HostName string
Port string
SupportsTLS bool
SupportedProtocols []string
HasCipherPreferenceOrderByProtocol map[string]bool
CipherPreferenceOrderByProtocol map[string][]string
OcspStaplingByProtocol map[string]bool
SelectedCipherByProtocol map[string]string
ALPNByProtocol map[string]string
SecureRenegotiationSupportedByProtocol map[string]bool
CipherSuiteByProtocol map[string][]string
// ServerHelloMessageByProtocolByCipher map[string]map[string]ServerHelloMessage
// CertificatesPerProtocol map[string]CertificateMessage
// KeyExchangeByProtocolByCipher map[string]map[string]ServerKeyExchangeMsg
IsSTARTLS bool
IsSSH bool
}
func addCurve(protocol, cipher uint16, scan ScanResult) string {
curveID := ""
if c, ok := scan.KeyExchangeByProtocolByCipher[protocol]; ok {
if ex, ok := c[cipher]; ok {
key := ex.Key
if len(key) > 4 && key[0] == 3 {
//named curve
cid := uint16(key[1])<<8 | uint16(key[2])
curveID = fmt.Sprintf(" (Named Curve: %s)", NamedCurves[cid])
}
}
}
return CipherSuiteMap[cipher] + curveID
}
//ToStringStruct returns a string-decoded form of ScanResult
func (s ScanResult) ToStringStruct() (out HumanScanResult) {
out.Server = s.Server
ip, err := net.LookupAddr(s.Server)
if err == nil {
out.HostName = strings.Join(ip, ", ")
}
out.Port = s.Port
out.SupportsTLS = s.SupportsTLS()
for _, p := range s.SupportedProtocols {
out.SupportedProtocols = append(out.SupportedProtocols, TLSVersionMap[p])
}
out.HasCipherPreferenceOrderByProtocol = make(map[string]bool)
for p := range s.HasCipherPreferenceOrderByProtocol {
out.HasCipherPreferenceOrderByProtocol[TLSVersionMap[p]] = s.HasCipherPreferenceOrderByProtocol[p]
}
out.CipherPreferenceOrderByProtocol = make(map[string][]string)
for k, v := range s.CipherPreferenceOrderByProtocol {
ciphers := []string{}
for _, c := range v {
ciphers = append(ciphers, addCurve(k, c, s))
}
out.CipherPreferenceOrderByProtocol[TLSVersionMap[k]] = ciphers
}
out.OcspStaplingByProtocol = make(map[string]bool)
for k, v := range s.OcspStaplingByProtocol {
out.OcspStaplingByProtocol[TLSVersionMap[k]] = v
}
out.SelectedCipherByProtocol = make(map[string]string)
for k, v := range s.SelectedCipherByProtocol {
out.SelectedCipherByProtocol[TLSVersionMap[k]] = addCurve(k, v, s)
}
out.ALPNByProtocol = make(map[string]string)
for k, v := range s.ALPNByProtocol {
out.ALPNByProtocol[TLSVersionMap[k]] = v
}
out.SecureRenegotiationSupportedByProtocol = make(map[string]bool)
for k, v := range s.SecureRenegotiationSupportedByProtocol {
out.SecureRenegotiationSupportedByProtocol[TLSVersionMap[k]] = v
}
out.CipherSuiteByProtocol = make(map[string][]string)
for k, v := range s.CipherSuiteByProtocol {
ciphers := []string{}
for _, c := range v {
ciphers = append(ciphers, addCurve(k, c, s))
}
out.CipherSuiteByProtocol[TLSVersionMap[k]] = ciphers
}
out.IsSTARTLS = s.IsSTARTLS
out.IsSSH = s.IsSSH
return
}
//ToJSON returns a JSON-formatted string representation of the ScanResult
func (s ScanResult) ToJSON() (js string) {
return
}
// SupportsTLS determines whether the port on the specified server supports TLS at all
func (s ScanResult) SupportsTLS() bool {
if len(s.SupportedProtocols) == 0 {
return false
}
return true
}
//ToString generates a string output
func (s ScanResult) ToString(config ScanConfig) (result string) {
hn, err := net.LookupAddr(s.Server)
hostname := s.Server
if err == nil {
hostname = strings.Join(hn, ",")
}
result += fmt.Sprintf("%s (%s)\n\tPort: %s\n", s.Server, hostname, s.Port)
if len(s.SupportedProtocols) == 0 {
result += "\tNo supported SSL/TLS protocol found\n"
} else {
sortedSupportedProtocols := s.SupportedProtocols
sort.Slice(sortedSupportedProtocols, func(i, j int) bool { return sortedSupportedProtocols[i] > sortedSupportedProtocols[j] })
for _, tls := range sortedSupportedProtocols {
result += fmt.Sprintf("\t%s:\n", TLSVersionMap[tls])
result += fmt.Sprintf("\t\tSupports secure renegotiation: %t\n", s.SecureRenegotiationSupportedByProtocol[tls])
result += fmt.Sprintf("\t\tApplication Layer Protocol Negotiation: %s\n", s.ALPNByProtocol[tls])
if !config.ProtocolsOnly {
result += fmt.Sprintf("\t\tHas a cipher preference order: %t\n", s.HasCipherPreferenceOrderByProtocol[tls])
result += fmt.Sprintf("\t\tSelected cipher when all client ciphers (in numerical order) are presented: %s\n", CipherSuiteMap[s.SelectedCipherByProtocol[tls]])
if s.HasCipherPreferenceOrderByProtocol[tls] {
result += "\t\tSupported Ciphersuites (in order of preference):\n"
for _, cipher := range s.CipherPreferenceOrderByProtocol[tls] {
result += fmt.Sprintf("\t\t\t%s\n", addCurve(tls, cipher, s))
}
} else {
result += "\n\t\tSupported Ciphersuites (server has no order preference):\n"
for _, cipher := range s.CipherSuiteByProtocol[tls] {
result += fmt.Sprintf("\t\t\t%s\n", addCurve(tls, cipher, s))
}
}
}
if certM, ok := s.CertificatesPerProtocol[tls]; !config.HideCerts && ok {
certs, err := certM.GetCertificates()
if err == nil && len(certs) > 0 {
result += "\tCertificate Information:\n"
cert := certs[0]
result += "\t\tSubject: " + cert.Subject.String() + "\n\t\tSubject Serial Number: " + cert.Subject.SerialNumber + "\n"
result += "\t\tSubject Common names: " + cert.Subject.CommonName + "\n"
result += "\t\tAlternative names: " + strings.Join(cert.DNSNames, ", ") + "\n"
result += fmt.Sprintf("\t\tSerial Number: %x\n", cert.SerialNumber)
result += "\t\tValid from: " + cert.NotBefore.String() + "\n"
result += "\t\tValid until: " + cert.NotAfter.String() + "\n"
result += "\t\tIssuer: " + cert.Issuer.String() + "\n"
result += "\t\tSignature algorithm: " + cert.SignatureAlgorithm.String() + "\n"
sigLen := len(cert.Signature) - 1
result += fmt.Sprintf("\t\tSignature: %x...%x\n", cert.Signature[:8], cert.Signature[sigLen-8:sigLen])
if key, ok := cert.PublicKey.(*rsa.PublicKey); ok {
result += fmt.Sprintf("\t\tKey: %d bits (e %d)\n", key.N.BitLen(), key.E)
}
result += fmt.Sprintf("\t\tSupports OCSP stapling: %t\n", s.OcspStaplingByProtocol[tls])
result += fmt.Sprintf("\t\tChain length: %d\n", len(certs))
if len(certs) > 1 {
for i := 0; i < len(certs); i++ {
c := certs[i]
result += fmt.Sprintf("\t\t\tChain %d (CA: %t): %s. (Expires: %s)\n", len(certs)-i-1, c.IsCA, c.Subject.String(), c.NotAfter.String())
}
}
}
}
}
}
return
}
func (s ScanResult) String() string {
return s.ToString(ScanConfig{})
}