Issues (121)

report/syslog.go (2 issues)

1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2018  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 report
19
20
import (
21
	"fmt"
22
	"strings"
23
24
	syslog "github.com/RackSec/srslog"
25
	"golang.org/x/xerrors"
26
27
	"github.com/future-architect/vuls/config"
28
	"github.com/future-architect/vuls/models"
29
)
30
31
// SyslogWriter send report to syslog
32
type SyslogWriter struct{}
33
34
func (w SyslogWriter) Write(rs ...models.ScanResult) (err error) {
35
	conf := config.Conf.Syslog
36
	facility, _ := conf.GetFacility()
37
	severity, _ := conf.GetSeverity()
38
	raddr := fmt.Sprintf("%s:%s", conf.Host, conf.Port)
39
40
	sysLog, err := syslog.Dial(conf.Protocol, raddr, severity|facility, conf.Tag)
41
	if err != nil {
42
		return xerrors.Errorf("Failed to initialize syslog client: %w", err)
0 ignored issues
show
unrecognized printf verb 'w'
Loading history...
43
	}
44
45
	for _, r := range rs {
46
		messages := w.encodeSyslog(r)
47
		for _, m := range messages {
48
			if _, err = fmt.Fprintf(sysLog, m); err != nil {
0 ignored issues
show
can't check non-constant format in call to Fprintf
Loading history...
49
				return err
50
			}
51
		}
52
	}
53
	return nil
54
}
55
56
func (w SyslogWriter) encodeSyslog(result models.ScanResult) (messages []string) {
57
	ipv4Addrs := strings.Join(result.IPv4Addrs, ",")
58
	ipv6Addrs := strings.Join(result.IPv6Addrs, ",")
59
60
	var commonKvPairs []string
61
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`scanned_at="%s"`, result.ScannedAt))
62
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`server_name="%s"`, result.ServerName))
63
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`os_family="%s"`, result.Family))
64
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`os_release="%s"`, result.Release))
65
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`ipv4_addr="%s"`, ipv4Addrs))
66
	commonKvPairs = append(commonKvPairs, fmt.Sprintf(`ipv6_addr="%s"`, ipv6Addrs))
67
68
	for cveID, vinfo := range result.ScannedCves {
69
		kvPairs := commonKvPairs
70
71
		var pkgNames []string
72
		for _, pkg := range vinfo.AffectedPackages {
73
			pkgNames = append(pkgNames, pkg.Name)
74
		}
75
		pkgs := strings.Join(pkgNames, ",")
76
		kvPairs = append(kvPairs, fmt.Sprintf(`packages="%s"`, pkgs))
77
78
		kvPairs = append(kvPairs, fmt.Sprintf(`cve_id="%s"`, cveID))
79
		for _, cvss := range vinfo.Cvss2Scores(result.Family) {
80
			kvPairs = append(kvPairs, fmt.Sprintf(`cvss_score_%s_v2="%.2f"`, cvss.Type, cvss.Value.Score))
81
			kvPairs = append(kvPairs, fmt.Sprintf(`cvss_vector_%s_v2="%s"`, cvss.Type, cvss.Value.Vector))
82
		}
83
84
		for _, cvss := range vinfo.Cvss3Scores() {
85
			kvPairs = append(kvPairs, fmt.Sprintf(`cvss_score_%s_v3="%.2f"`, cvss.Type, cvss.Value.Score))
86
			kvPairs = append(kvPairs, fmt.Sprintf(`cvss_vector_%s_v3="%s"`, cvss.Type, cvss.Value.Vector))
87
		}
88
89
		if content, ok := vinfo.CveContents[models.NvdXML]; ok {
90
			cwes := strings.Join(content.CweIDs, ",")
91
			kvPairs = append(kvPairs, fmt.Sprintf(`cwe_ids="%s"`, cwes))
92
			if config.Conf.Syslog.Verbose {
93
				kvPairs = append(kvPairs, fmt.Sprintf(`source_link="%s"`, content.SourceLink))
94
				kvPairs = append(kvPairs, fmt.Sprintf(`summary="%s"`, content.Summary))
95
			}
96
		}
97
		if content, ok := vinfo.CveContents[models.RedHat]; ok {
98
			kvPairs = append(kvPairs, fmt.Sprintf(`title="%s"`, content.Title))
99
		}
100
101
		// message: key1="value1" key2="value2"...
102
		messages = append(messages, strings.Join(kvPairs, " "))
103
	}
104
105
	if len(messages) == 0 {
106
		commonKvPairs = append(commonKvPairs, `message="No CVE-IDs are found"`)
107
		messages = append(messages, strings.Join(commonKvPairs, " "))
108
	}
109
	return messages
110
}
111