forked from cloudflare/cfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_common.go
281 lines (241 loc) · 6.93 KB
/
scan_common.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
package scan
import (
"crypto/x509"
"net"
"net/http"
"regexp"
"sync"
"time"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/scan/crypto/tls"
)
var (
// Network is the default network to use.
Network = "tcp"
// Dialer is the default dialer to use, with a 1s timeout.
Dialer = &net.Dialer{Timeout: time.Second}
// Client is the default HTTP Client.
Client = &http.Client{Transport: &http.Transport{Dial: Dialer.Dial}}
// RootCAs defines the default root certificate authorities to be used for scan.
RootCAs *x509.CertPool
)
// Grade gives a subjective rating of the host's success in a scan.
type Grade int
const (
// Bad describes a host with serious misconfiguration or vulnerability.
Bad Grade = iota
// Warning describes a host with non-ideal configuration that maintains support for Warning clients.
Warning
// Good describes host performing the expected state-of-the-art.
Good
// Skipped descibes the "grade" of a scan that has been skipped.
Skipped
)
// String gives the name of the Grade as a string.
func (g Grade) String() string {
switch g {
case Bad:
return "Bad"
case Warning:
return "Warning"
case Good:
return "Good"
case Skipped:
return "Skipped"
default:
return "Invalid"
}
}
// Output is the result of a scan, to be stored for potential use by later Scanners.
type Output interface{}
// multiscan scans all DNS addresses returned for the host, returning the lowest grade
// and the concatenation of all the output.
func multiscan(host string, scan func(string) (Grade, Output, error)) (grade Grade, output Output, err error) {
domain, port, _ := net.SplitHostPort(host)
var addrs []string
addrs, err = net.LookupHost(domain)
if err != nil {
return
}
grade = Good
out := make(map[string]Output)
for _, addr := range addrs {
var g Grade
var o Output
g, o, err = scan(net.JoinHostPort(addr, port))
if err != nil {
grade = Bad
return
}
if g < grade {
grade = g
}
out[addr] = o
}
output = out
return
}
// Scanner describes a type of scan to perform on a host.
type Scanner struct {
// Description describes the nature of the scan to be performed.
Description string `json:"description"`
// scan is the function that scans the given host and provides a Grade and Output.
scan func(string, string) (Grade, Output, error)
}
// Scan performs the scan to be performed on the given host and stores its result.
func (s *Scanner) Scan(addr, hostname string) (Grade, Output, error) {
grade, output, err := s.scan(addr, hostname)
if err != nil {
log.Debugf("scan: %v", err)
return grade, output, err
}
return grade, output, err
}
// Family defines a set of related scans meant to be run together in sequence.
type Family struct {
// Description gives a short description of the scans performed scan/scan_common.goon the host.
Description string `json:"description"`
// Scanners is a list of scanners that are to be run in sequence.
Scanners map[string]*Scanner `json:"scanners"`
}
// FamilySet contains a set of Families to run Scans from.
type FamilySet map[string]*Family
// Default contains each scan Family that is defined
var Default = FamilySet{
"Connectivity": Connectivity,
"TLSHandshake": TLSHandshake,
"TLSSession": TLSSession,
"PKI": PKI,
"Broad": Broad,
}
// ScannerResult contains the result for a single scan.
type ScannerResult struct {
Grade string `json:"grade"`
Output Output `json:"output,omitempty"`
Error string `json:"error,omitempty"`
}
// FamilyResult contains a scan response for a single Family
type FamilyResult map[string]ScannerResult
// A Result contains a ScannerResult along with it's scanner and family names.
type Result struct {
Family, Scanner string
ScannerResult
}
type context struct {
sync.WaitGroup
addr, hostname string
familyRegexp, scannerRegexp *regexp.Regexp
resultChan chan *Result
}
func newContext(addr, hostname string, familyRegexp, scannerRegexp *regexp.Regexp, numFamilies int) *context {
ctx := &context{
addr: addr,
hostname: hostname,
familyRegexp: familyRegexp,
scannerRegexp: scannerRegexp,
resultChan: make(chan *Result),
}
ctx.Add(numFamilies)
go func() {
ctx.Wait()
close(ctx.resultChan)
}()
return ctx
}
type familyContext struct {
sync.WaitGroup
ctx *context
}
func (ctx *context) newfamilyContext(numScanners int) *familyContext {
familyCtx := &familyContext{ctx: ctx}
familyCtx.Add(numScanners)
go func() {
familyCtx.Wait()
familyCtx.ctx.Done()
}()
return familyCtx
}
func (ctx *context) copyResults(timeout time.Duration) map[string]FamilyResult {
results := make(map[string]FamilyResult)
for {
var result *Result
select {
case <-time.After(timeout):
log.Warningf("Scan timed out after %v", timeout)
return results
case result = <-ctx.resultChan:
if result == nil {
return results
}
}
if results[result.Family] == nil {
results[result.Family] = make(FamilyResult)
}
results[result.Family][result.Scanner] = result.ScannerResult
}
}
func (familyCtx *familyContext) runScanner(familyName, scannerName string, scanner *Scanner) {
if familyCtx.ctx.familyRegexp.MatchString(familyName) && familyCtx.ctx.scannerRegexp.MatchString(scannerName) {
grade, output, err := scanner.Scan(familyCtx.ctx.addr, familyCtx.ctx.hostname)
result := &Result{
familyName,
scannerName,
ScannerResult{
Grade: grade.String(),
Output: output,
},
}
if err != nil {
result.Error = err.Error()
}
familyCtx.ctx.resultChan <- result
}
familyCtx.Done()
}
// RunScans iterates over AllScans, running each scan that matches the family
// and scanner regular expressions concurrently.
func (fs FamilySet) RunScans(host, ip, family, scanner string, timeout time.Duration) (map[string]FamilyResult, error) {
hostname, port, err := net.SplitHostPort(host)
if err != nil {
hostname = host
port = "443"
}
var addr string
if net.ParseIP(ip) != nil {
addr = net.JoinHostPort(ip, port)
} else {
addr = net.JoinHostPort(hostname, port)
}
familyRegexp, err := regexp.Compile(family)
if err != nil {
return nil, err
}
scannerRegexp, err := regexp.Compile(scanner)
if err != nil {
return nil, err
}
ctx := newContext(addr, hostname, familyRegexp, scannerRegexp, len(fs))
for familyName, family := range fs {
familyCtx := ctx.newfamilyContext(len(family.Scanners))
for scannerName, scanner := range family.Scanners {
go familyCtx.runScanner(familyName, scannerName, scanner)
}
}
return ctx.copyResults(timeout), nil
}
// LoadRootCAs loads the default root certificate authorities from file.
func LoadRootCAs(caBundleFile string) (err error) {
if caBundleFile != "" {
log.Debugf("Loading scan RootCAs: %s", caBundleFile)
RootCAs, err = helpers.LoadPEMCertPool(caBundleFile)
}
return
}
func defaultTLSConfig(hostname string) *tls.Config {
return &tls.Config{
ServerName: hostname,
RootCAs: RootCAs,
InsecureSkipVerify: true,
}
}