forked from mozilla/tls-observatory
/
main.go
308 lines (288 loc) · 8.28 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"time"
"github.com/Sirupsen/logrus"
"github.com/mozilla/tls-observatory/config"
"github.com/mozilla/tls-observatory/connection"
pg "github.com/mozilla/tls-observatory/database"
"github.com/mozilla/tls-observatory/logger"
"github.com/mozilla/tls-observatory/worker"
)
var db *pg.DB
var log = logger.GetLogger()
func main() {
var (
cfgFile, cipherscan string
debug bool
)
flag.StringVar(&cfgFile, "c", "/etc/tls-observatory/scanner.cfg", "Configuration file")
flag.StringVar(&cipherscan, "b", "/opt/cipherscan/cipherscan", "Cipherscan binary location")
flag.BoolVar(&debug, "debug", false, "Set debug logging")
flag.Parse()
if debug {
logger.SetLevelToDebug()
}
conf, err := config.Load(cfgFile)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to load configuration: %v", err))
}
if !conf.General.Enable && os.Getenv("TLSOBS_SCANNER_ENABLE") != "on" {
log.Fatal("Scanner is disabled in configuration")
}
_, err = os.Stat(cipherscan)
if err != nil {
log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Could not locate cipherscan executable. TLS connection capabilities will not be available.")
}
// increase the n
runtime.GOMAXPROCS(conf.General.MaxProc)
dbtls := "disable"
if conf.General.PostgresUseTLS {
dbtls = "verify-full"
}
db, err = pg.RegisterConnection(
conf.General.PostgresDB,
conf.General.PostgresUser,
conf.General.PostgresPass,
conf.General.Postgres,
dbtls)
defer db.Close()
if err != nil {
log.WithFields(logrus.Fields{
"error": err.Error(),
}).Fatal("Failed to connect to database")
}
db.SetMaxOpenConns(conf.General.MaxProc)
db.SetMaxIdleConns(10)
// simple DB watchdog, crashes the process if connection dies
go func() {
for {
var one uint
err = db.QueryRow("SELECT 1").Scan(&one)
if err != nil {
log.Fatal("Database connection failed:", err)
}
if one != 1 {
log.Fatal("Apparently the database doesn't know the meaning of one anymore. Crashing.")
}
time.Sleep(10 * time.Second)
}
}()
incomingScans := db.RegisterScanListener(
conf.General.PostgresDB,
conf.General.PostgresUser,
conf.General.PostgresPass,
conf.General.Postgres,
dbtls)
Setup(conf)
activeScanners := 0
for {
select {
case scanID := <-incomingScans:
// new scan, send it to the first available scanner
for {
if activeScanners >= conf.General.MaxProc {
time.Sleep(time.Second)
} else {
break
}
}
go func() {
activeScanners++
scan(scanID, cipherscan)
activeScanners--
}()
case <-time.After(conf.General.Timeout * time.Minute):
log.Fatalf("No new scan received in %d minutes, shutting down", conf.General.Timeout)
}
}
}
func scan(scanID int64, cipherscan string) {
log.WithFields(logrus.Fields{
"scan_id": scanID,
}).Info("Received new scan")
db.Exec("UPDATE scans SET attempts = attempts + 1 WHERE id=$1", scanID)
scan, err := db.GetScanByID(scanID)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not find/decode scan")
return
}
var completion int
// Retrieve the certificate from the target
certID, trustID, chain, err := handleCert(scan.Target)
if err != nil {
db.Exec("UPDATE scans SET has_tls=FALSE, completion_perc=100 WHERE id=$1", scanID)
log.WithFields(logrus.Fields{
"scan_id": scanID,
"scan_Target": scan.Target,
"error": err.Error(),
}).Error("Could not get certificate info")
return
}
log.WithFields(logrus.Fields{
"scan_id": scanID,
"cert_id": certID,
"trust_id": trustID,
}).Debug("Certificate retrieved from target")
completion += 20
_, err = db.Exec(`UPDATE scans SET cert_id=$1, has_tls=TRUE, completion_perc=$2
WHERE id=$3`, certID, completion, scanID)
if err != nil {
db.Exec("UPDATE scans SET has_tls=FALSE, completion_perc=100 WHERE id=$1", scanID)
log.WithFields(logrus.Fields{
"scan_id": scanID,
"cert_id": certID,
"error": err.Error(),
}).Error("Could not update scans for cert")
return
}
completion += 30
if trustID > 0 {
isTrustValid, err := db.IsTrustValid(trustID)
if err != nil {
db.Exec("UPDATE scans SET has_tls=FALSE, completion_perc=100 WHERE id=$1", scanID)
log.WithFields(logrus.Fields{
"scan_id": scanID,
"cert_id": certID,
"trust_id": trustID,
"error": err.Error(),
}).Error("Failed to determine certificate trust")
return
}
_, err = db.Exec(`UPDATE scans SET trust_id=$1, is_valid=$2, completion_perc=$3
WHERE id=$4`, trustID, isTrustValid, completion, scanID)
if err != nil {
db.Exec("UPDATE scans SET has_tls=FALSE, completion_perc=100 WHERE id=$1", scanID)
log.WithFields(logrus.Fields{
"scan_id": scanID,
"cert_id": certID,
"error": err.Error(),
}).Error("Could not update scans for cert")
return
}
}
// Cipherscan the target
js, err := connection.Connect(scan.Target, cipherscan)
if err != nil {
err, ok := err.(connection.NoTLSConnErr)
if ok {
//does not implement TLS
db.Exec("UPDATE scans SET has_tls=FALSE, completion_perc=100 WHERE id=$1", scanID)
} else {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not get TLS connection info")
}
return
}
completion += 20
_, err = db.Exec("UPDATE scans SET conn_info=$1, completion_perc=$2 WHERE id=$3",
js, completion, scanID)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not update connection information for scan")
}
// Prepare worker input
cert, err := db.GetCertByID(certID)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"cert_id": certID,
"err": err,
}).Error("Could not get certificate from db to pass to workers")
return
}
var conn_info connection.Stored
err = json.Unmarshal(js, &conn_info)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
}).Error("Could not parse connection info to pass to workers")
return
}
workerInput := worker.Input{
DBHandle: db,
Scanid: scanID,
Certificate: *cert,
CertificateChain: chain,
Connection: conn_info,
}
// launch workers that evaluate the results
resChan := make(chan worker.Result)
totalWorkers := 0
for k, wrkInfo := range worker.AvailableWorkers {
workerInput.Params, _ = scan.AnalysisParams[k]
go wrkInfo.Runner.(worker.Worker).Run(workerInput, resChan)
totalWorkers++
}
log.WithFields(logrus.Fields{
"scan_id": scanID,
"count": totalWorkers,
}).Info("Running workers")
// read the results from the results chan in a loop until all workers have ran or expired
for endedWorkers := 0; endedWorkers < totalWorkers; endedWorkers++ {
select {
case <-time.After(30 * time.Second):
log.WithFields(logrus.Fields{
"scan_id": scanID,
}).Error("Analysis workers timed out after 30 seconds")
goto updatecompletion
case res := <-resChan:
completion = ((endedWorkers/totalWorkers)*60 + completion)
log.WithFields(logrus.Fields{
"scan_id": scanID,
"worker_name": res.WorkerName,
"success": res.Success,
"result": string(res.Result),
}).Debug("Received results from worker")
err = db.UpdateScanCompletionPercentage(scanID, completion)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not update completion percentage")
continue
}
if !res.Success {
log.WithFields(logrus.Fields{
"worker_name": res.WorkerName,
"errors": res.Errors,
}).Error("Worker returned with errors")
}
_, err = db.Exec("INSERT INTO analysis(scan_id,worker_name,output,success) VALUES($1,$2,$3,$4)",
scanID, res.WorkerName, res.Result, res.Success)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not insert worker results in database")
continue
}
log.WithFields(logrus.Fields{
"scan_id": scanID,
"worker_name": res.WorkerName,
}).Info("Results from worker stored in database")
}
}
updatecompletion:
err = db.UpdateScanCompletionPercentage(scanID, 100)
if err != nil {
log.WithFields(logrus.Fields{
"scan_id": scanID,
"error": err.Error(),
}).Error("Could not update completion percentage")
}
return
}