Completed
Push — master ( 76037c...7585f9 )
by kota
09:11 queued 01:58
created

commands/report.go   C

Size/Duplication

Total Lines 401
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 55
eloc 238
dl 0
loc 401
rs 6
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B commands.*ReportCmd.SetFlags 0 90 1
A commands.*ReportCmd.Synopsis 0 1 1
A commands.*ReportCmd.Name 0 1 1
A commands.*ReportCmd.Usage 0 2 1
F commands.*ReportCmd.Execute 0 217 51
1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Corporation , Japan.
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
package commands
19
20
import (
21
	"context"
22
	"flag"
23
	"os"
24
	"path/filepath"
25
26
	c "github.com/future-architect/vuls/config"
27
	"github.com/future-architect/vuls/exploit"
28
	"github.com/future-architect/vuls/gost"
29
	"github.com/future-architect/vuls/models"
30
	"github.com/future-architect/vuls/oval"
31
	"github.com/future-architect/vuls/report"
32
	"github.com/future-architect/vuls/util"
33
	"github.com/google/subcommands"
34
	"github.com/k0kubun/pp"
35
	cvelog "github.com/kotakanbe/go-cve-dictionary/log"
36
)
37
38
// ReportCmd is subcommand for reporting
39
type ReportCmd struct {
40
	configPath  string
41
	cveDict     c.GoCveDictConf
42
	ovalDict    c.GovalDictConf
43
	gostConf    c.GostConf
44
	exploitConf c.ExploitConf
45
	httpConf    c.HTTPConf
46
}
47
48
// Name return subcommand name
49
func (*ReportCmd) Name() string { return "report" }
50
51
// Synopsis return synopsis
52
func (*ReportCmd) Synopsis() string { return "Reporting" }
53
54
// Usage return usage
55
func (*ReportCmd) Usage() string {
56
	return `report:
57
	report
58
		[-lang=en|ja]
59
		[-config=/path/to/config.toml]
60
		[-results-dir=/path/to/results]
61
		[-log-dir=/path/to/log]
62
		[-refresh-cve]
63
		[-cvss-over=7]
64
		[-diff]
65
		[-ignore-unscored-cves]
66
		[-ignore-unfixed]
67
		[-to-email]
68
		[-to-http]
69
		[-to-slack]
70
		[-to-stride]
71
		[-to-hipchat]
72
		[-to-chatwork]
73
		[-to-localfile]
74
		[-to-s3]
75
		[-to-azure-blob]
76
		[-to-saas]
77
		[-format-json]
78
		[-format-xml]
79
		[-format-one-email]
80
		[-format-one-line-text]
81
		[-format-list]
82
		[-format-full-text]
83
		[-gzip]
84
		[-uuid]
85
		[-http-proxy=http://192.168.0.1:8080]
86
		[-debug]
87
		[-debug-sql]
88
		[-pipe]
89
		[-cvedb-type=sqlite3|mysql|postgres|redis|http]
90
		[-cvedb-sqlite3-path=/path/to/cve.sqlite3]
91
		[-cvedb-url=http://127.0.0.1:1323 or DB connection string]
92
		[-ovaldb-type=sqlite3|mysql|redis|http]
93
		[-ovaldb-sqlite3-path=/path/to/oval.sqlite3]
94
		[-ovaldb-url=http://127.0.0.1:1324 or DB connection string]
95
		[-gostdb-type=sqlite3|mysql|redis|http]
96
		[-gostdb-sqlite3-path=/path/to/gost.sqlite3]
97
		[-gostdb-url=http://127.0.0.1:1325 or DB connection string]
98
		[-exploitdb-type=sqlite3|mysql|redis|http]
99
		[-exploitdb-sqlite3-path=/path/to/exploitdb.sqlite3]
100
		[-exploitdb-url=http://127.0.0.1:1326 or DB connection string]
101
		[-http="http://vuls-report-server"]
102
103
		[RFC3339 datetime format under results dir]
104
`
105
}
106
107
// SetFlags set flag
108
func (p *ReportCmd) SetFlags(f *flag.FlagSet) {
109
	f.StringVar(&c.Conf.Lang, "lang", "en", "[en|ja]")
110
	f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode")
111
	f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode")
112
113
	wd, _ := os.Getwd()
114
	defaultConfPath := filepath.Join(wd, "config.toml")
115
	f.StringVar(&p.configPath, "config", defaultConfPath, "/path/to/toml")
116
117
	defaultResultsDir := filepath.Join(wd, "results")
118
	f.StringVar(&c.Conf.ResultsDir, "results-dir", defaultResultsDir, "/path/to/results")
119
120
	defaultLogDir := util.GetDefaultLogDir()
121
	f.StringVar(&c.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log")
122
123
	f.BoolVar(&c.Conf.RefreshCve, "refresh-cve", false,
124
		"Refresh CVE information in JSON file under results dir")
125
126
	f.Float64Var(&c.Conf.CvssScoreOver, "cvss-over", 0,
127
		"-cvss-over=6.5 means reporting CVSS Score 6.5 and over (default: 0 (means report all))")
128
129
	f.BoolVar(&c.Conf.Diff, "diff", false,
130
		"Difference between previous result and current result ")
131
132
	f.BoolVar(&c.Conf.IgnoreUnscoredCves, "ignore-unscored-cves", false,
133
		"Don't report the unscored CVEs")
134
135
	f.BoolVar(
136
		&c.Conf.IgnoreUnfixed, "ignore-unfixed", false,
137
		"Don't report the unfixed CVEs")
138
139
	f.StringVar(
140
		&c.Conf.HTTPProxy, "http-proxy", "",
141
		"http://proxy-url:port (default: empty)")
142
143
	f.BoolVar(&c.Conf.FormatJSON, "format-json", false, "JSON format")
144
	f.BoolVar(&c.Conf.FormatXML, "format-xml", false, "XML format")
145
	f.BoolVar(&c.Conf.FormatOneEMail, "format-one-email", false,
146
		"Send all the host report via only one EMail (Specify with -to-email)")
147
	f.BoolVar(&c.Conf.FormatOneLineText, "format-one-line-text", false,
148
		"One line summary in plain text")
149
	f.BoolVar(&c.Conf.FormatList, "format-list", false, "Display as list format")
150
	f.BoolVar(&c.Conf.FormatFullText, "format-full-text", false,
151
		"Detail report in plain text")
152
153
	f.BoolVar(&c.Conf.ToSlack, "to-slack", false, "Send report via Slack")
154
	f.BoolVar(&c.Conf.ToStride, "to-stride", false, "Send report via Stride")
155
	f.BoolVar(&c.Conf.ToHipChat, "to-hipchat", false, "Send report via hipchat")
156
	f.BoolVar(&c.Conf.ToChatWork, "to-chatwork", false, "Send report via chatwork")
157
	f.BoolVar(&c.Conf.ToEmail, "to-email", false, "Send report via Email")
158
	f.BoolVar(&c.Conf.ToSyslog, "to-syslog", false, "Send report via Syslog")
159
	f.BoolVar(&c.Conf.ToLocalFile, "to-localfile", false, "Write report to localfile")
160
	f.BoolVar(&c.Conf.ToS3, "to-s3", false,
161
		"Write report to S3 (bucket/yyyyMMdd_HHmm/servername.json/xml/txt)")
162
	f.BoolVar(&c.Conf.ToHTTP, "to-http", false, "Send report via HTTP POST")
163
	f.BoolVar(&c.Conf.ToAzureBlob, "to-azure-blob", false,
164
		"Write report to Azure Storage blob (container/yyyyMMdd_HHmm/servername.json/xml/txt)")
165
	f.BoolVar(&c.Conf.ToSaas, "to-saas", false,
166
		"Upload report to Future Vuls(https://vuls.biz/) before report")
167
168
	f.BoolVar(&c.Conf.GZIP, "gzip", false, "gzip compression")
169
	f.BoolVar(&c.Conf.UUID, "uuid", false,
170
		"Auto generate of scan target servers and then write to config.toml and scan result")
171
	f.BoolVar(&c.Conf.Pipe, "pipe", false, "Use args passed via PIPE")
172
173
	f.StringVar(&p.cveDict.Type, "cvedb-type", "",
174
		"DB type of go-cve-dictionary (sqlite3, mysql, postgres, redis or http)")
175
	f.StringVar(&p.cveDict.SQLite3Path, "cvedb-sqlite3-path", "", "/path/to/sqlite3")
176
	f.StringVar(&p.cveDict.URL, "cvedb-url", "",
177
		"http://go-cve-dictionary.com:1323 or DB connection string")
178
179
	f.StringVar(&p.ovalDict.Type, "ovaldb-type", "",
180
		"DB type of goval-dictionary (sqlite3, mysql, postgres, redis or http)")
181
	f.StringVar(&p.ovalDict.SQLite3Path, "ovaldb-sqlite3-path", "", "/path/to/sqlite3")
182
	f.StringVar(&p.ovalDict.URL, "ovaldb-url", "",
183
		"http://goval-dictionary.com:1324 or DB connection string")
184
185
	f.StringVar(&p.gostConf.Type, "gostdb-type", "",
186
		"DB type of gost (sqlite3, mysql, postgres, redis or http)")
187
	f.StringVar(&p.gostConf.SQLite3Path, "gostdb-sqlite3-path", "", "/path/to/sqlite3")
188
	f.StringVar(&p.gostConf.URL, "gostdb-url", "",
189
		"http://gost.com:1325 or DB connection string")
190
191
	f.StringVar(&p.exploitConf.Type, "exploitdb-type", "",
192
		"DB type of exploit (sqlite3, mysql, postgres, redis or http)")
193
	f.StringVar(&p.exploitConf.SQLite3Path, "exploitdb-sqlite3-path", "", "/path/to/sqlite3")
194
	f.StringVar(&p.exploitConf.URL, "exploitdb-url", "",
195
		"http://exploit.com:1326 or DB connection string")
196
197
	f.StringVar(&p.httpConf.URL, "http", "", "-to-http http://vuls-report")
198
199
}
200
201
// Execute execute
202
func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
203
	util.Log = util.NewCustomLogger(c.ServerInfo{})
204
	cvelog.SetLogger(c.Conf.LogDir, false, c.Conf.Debug, false)
205
206
	if err := c.Load(p.configPath, ""); err != nil {
207
		util.Log.Errorf("Error loading %s, %s", p.configPath, err)
208
		return subcommands.ExitUsageError
209
	}
210
211
	c.Conf.CveDict.Overwrite(p.cveDict)
212
	c.Conf.OvalDict.Overwrite(p.ovalDict)
213
	c.Conf.Gost.Overwrite(p.gostConf)
214
	c.Conf.Exploit.Overwrite(p.exploitConf)
215
	c.Conf.HTTP.Overwrite(p.httpConf)
216
217
	var dir string
218
	var err error
219
	if c.Conf.Diff {
220
		dir, err = report.JSONDir([]string{})
221
	} else {
222
		dir, err = report.JSONDir(f.Args())
223
	}
224
	if err != nil {
225
		util.Log.Errorf("Failed to read from JSON: %s", err)
226
		return subcommands.ExitFailure
227
	}
228
229
	// report
230
	reports := []report.ResultWriter{
231
		report.StdoutWriter{},
232
	}
233
234
	if c.Conf.ToSlack {
235
		reports = append(reports, report.SlackWriter{})
236
	}
237
238
	if c.Conf.ToStride {
239
		reports = append(reports, report.StrideWriter{})
240
	}
241
242
	if c.Conf.ToHipChat {
243
		reports = append(reports, report.HipChatWriter{})
244
	}
245
246
	if c.Conf.ToChatWork {
247
		reports = append(reports, report.ChatWorkWriter{})
248
	}
249
250
	if c.Conf.ToEmail {
251
		reports = append(reports, report.EMailWriter{})
252
	}
253
254
	if c.Conf.ToSyslog {
255
		reports = append(reports, report.SyslogWriter{})
256
	}
257
258
	if c.Conf.ToHTTP {
259
		reports = append(reports, report.HTTPRequestWriter{})
260
	}
261
262
	if c.Conf.ToLocalFile {
263
		reports = append(reports, report.LocalFileWriter{
264
			CurrentDir: dir,
265
		})
266
	}
267
268
	if c.Conf.ToS3 {
269
		if err := report.CheckIfBucketExists(); err != nil {
270
			util.Log.Errorf("Check if there is a bucket beforehand: %s, err: %s",
271
				c.Conf.AWS.S3Bucket, err)
272
			return subcommands.ExitUsageError
273
		}
274
		reports = append(reports, report.S3Writer{})
275
	}
276
277
	if c.Conf.ToAzureBlob {
278
		if len(c.Conf.Azure.AccountName) == 0 {
279
			c.Conf.Azure.AccountName = os.Getenv("AZURE_STORAGE_ACCOUNT")
280
		}
281
282
		if len(c.Conf.Azure.AccountKey) == 0 {
283
			c.Conf.Azure.AccountKey = os.Getenv("AZURE_STORAGE_ACCESS_KEY")
284
		}
285
286
		if len(c.Conf.Azure.ContainerName) == 0 {
287
			util.Log.Error("Azure storage container name is required with -azure-container option")
288
			return subcommands.ExitUsageError
289
		}
290
		if err := report.CheckIfAzureContainerExists(); err != nil {
291
			util.Log.Errorf("Check if there is a container beforehand: %s, err: %s",
292
				c.Conf.Azure.ContainerName, err)
293
			return subcommands.ExitUsageError
294
		}
295
		reports = append(reports, report.AzureBlobWriter{})
296
	}
297
298
	if c.Conf.ToSaas {
299
		if !c.Conf.UUID {
300
			util.Log.Errorf("If you use the -to-saas option, you need to enable the uuid option")
301
			return subcommands.ExitUsageError
302
		}
303
		reports = append(reports, report.SaasWriter{})
304
	}
305
306
	if !(c.Conf.FormatJSON || c.Conf.FormatOneLineText ||
307
		c.Conf.FormatList || c.Conf.FormatFullText || c.Conf.FormatXML) {
308
		c.Conf.FormatList = true
309
	}
310
311
	util.Log.Info("Validating config...")
312
	if !c.Conf.ValidateOnReport() {
313
		return subcommands.ExitUsageError
314
	}
315
316
	var loaded models.ScanResults
317
	if loaded, err = report.LoadScanResults(dir); err != nil {
318
		util.Log.Error(err)
319
		return subcommands.ExitFailure
320
	}
321
	util.Log.Infof("Loaded: %s", dir)
322
323
	var res models.ScanResults
324
	for _, r := range loaded {
325
		if len(r.Errors) == 0 {
326
			res = append(res, r)
327
		} else {
328
			util.Log.Warnf("Ignored since errors occurred during scanning: %s",
329
				r.ServerName)
330
		}
331
	}
332
333
	for _, r := range res {
334
		util.Log.Debugf("%s: %s",
335
			r.ServerInfo(),
336
			pp.Sprintf("%s", c.Conf.Servers[r.ServerName]))
337
	}
338
339
	if c.Conf.UUID {
340
		// Ensure UUIDs of scan target servers in config.toml
341
		if err := report.EnsureUUIDs(p.configPath, res); err != nil {
342
			util.Log.Errorf("Failed to ensure UUIDs: %s", err)
343
			return subcommands.ExitFailure
344
		}
345
	}
346
347
	if !c.Conf.ToSaas {
348
		util.Log.Info("Validating db config...")
349
		if !c.Conf.ValidateOnReportDB() {
350
			return subcommands.ExitUsageError
351
		}
352
353
		if c.Conf.CveDict.URL != "" {
354
			if err := report.CveClient.CheckHealth(); err != nil {
355
				util.Log.Errorf("CVE HTTP server is not running. err: %s", err)
356
				util.Log.Errorf("Run go-cve-dictionary as server mode before reporting or run with `-cvedb-type=sqlite3 -cvedb-sqlite3-path` option instead of -cvedb-url")
357
				return subcommands.ExitFailure
358
			}
359
		}
360
361
		if c.Conf.OvalDict.URL != "" {
362
			err := oval.Base{}.CheckHTTPHealth()
363
			if err != nil {
364
				util.Log.Errorf("OVAL HTTP server is not running. err: %s", err)
365
				util.Log.Errorf("Run goval-dictionary as server mode before reporting or run with `-ovaldb-type=sqlite3 -ovaldb-sqlite3-path` option instead of -ovaldb-url")
366
				return subcommands.ExitFailure
367
			}
368
		}
369
370
		if c.Conf.Gost.URL != "" {
371
			util.Log.Infof("gost: %s", c.Conf.Gost.URL)
372
			err := gost.Base{}.CheckHTTPHealth()
373
			if err != nil {
374
				util.Log.Errorf("gost HTTP server is not running. err: %s", err)
375
				util.Log.Errorf("Run gost as server mode before reporting or run with `-gostdb-type=sqlite3 -gostdb-sqlite3-path` option instead of -gostdb-url")
376
				return subcommands.ExitFailure
377
			}
378
		}
379
380
		if c.Conf.Exploit.URL != "" {
381
			err := exploit.CheckHTTPHealth()
382
			if err != nil {
383
				util.Log.Errorf("exploit HTTP server is not running. err: %s", err)
384
				util.Log.Errorf("Run go-exploitdb as server mode before reporting")
385
				return subcommands.ExitFailure
386
			}
387
		}
388
		dbclient, locked, err := report.NewDBClient(report.DBClientConf{
389
			CveDictCnf:  c.Conf.CveDict,
390
			OvalDictCnf: c.Conf.OvalDict,
391
			GostCnf:     c.Conf.Gost,
392
			ExploitCnf:  c.Conf.Exploit,
393
			DebugSQL:    c.Conf.DebugSQL,
394
		})
395
		if locked {
396
			util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %s", err)
397
			return subcommands.ExitFailure
398
		}
399
		if err != nil {
400
			util.Log.Errorf("Failed to init DB Clients: %s", err)
401
			return subcommands.ExitFailure
402
		}
403
		defer dbclient.CloseDB()
404
405
		if res, err = report.FillCveInfos(*dbclient, res, dir); err != nil {
406
			util.Log.Error(err)
407
			return subcommands.ExitFailure
408
		}
409
	}
410
411
	for _, w := range reports {
412
		if err := w.Write(res...); err != nil {
413
			util.Log.Errorf("Failed to report: %s", err)
414
			return subcommands.ExitFailure
415
		}
416
	}
417
418
	return subcommands.ExitSuccess
419
}
420