-
Notifications
You must be signed in to change notification settings - Fork 446
/
parser.go
170 lines (147 loc) · 4.75 KB
/
parser.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
package msrc
import (
"encoding/xml"
"fmt"
"io"
"os"
"strconv"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/fleetdm/fleet/v4/server/vulnerabilities/msrc/parsed"
msrcxml "github.com/fleetdm/fleet/v4/server/vulnerabilities/msrc/xml"
)
func ParseFeed(fPath string) (map[string]*parsed.SecurityBulletin, error) {
r, err := os.Open(fPath)
if err != nil {
return nil, fmt.Errorf("msrc parser: %w", err)
}
defer r.Close()
feedResultXML, err := parseXML(r)
if err != nil {
return nil, fmt.Errorf("msrc parser: %w", err)
}
bulletins, err := mapToSecurityBulletins(feedResultXML)
if err != nil {
return nil, fmt.Errorf("msrc parser: %w", err)
}
return bulletins, nil
}
func mapToSecurityBulletins(rXML *msrcxml.FeedResult) (map[string]*parsed.SecurityBulletin, error) {
// We will have one bulletin for each product name.
bulletins := make(map[string]*parsed.SecurityBulletin)
pIDToPName := make(map[string]string, len(rXML.WinProducts))
for pID, p := range rXML.WinProducts {
name := parsed.NewProductFromFullName(p.FullName).Name()
// If the name could not be determined means that we have an un-supported Windows product
if name == "" {
continue
}
if bulletins[name] == nil {
bulletins[name] = parsed.NewSecurityBulletin(name)
}
bulletins[name].Products[pID] = parsed.NewProductFromFullName(p.FullName)
pIDToPName[pID] = name
}
for _, v := range rXML.WinVulnerabities {
for _, rem := range v.Remediations {
// We will only be able to detect vulns for which they are vendor fixes.
if !rem.IsVendorFix() {
continue
}
// We assume that rem.Description will contain the ID portion of a KBID, which should
// be always a numeric value.
remediatedKBIDRaw, err := strconv.Atoi(rem.Description)
if err != nil {
return nil, fmt.Errorf("invalid remediation KBID %q for %s", rem.Description, v.CVE)
}
if remediatedKBIDRaw < 0 {
return nil, fmt.Errorf("invalid remediation KBID %q for %s", rem.Description, v.CVE)
}
remediatedKBID := uint(remediatedKBIDRaw)
// rem.Supercedence should have the ID portion of a KBID which the current vendor fix replaces.
var supersedes *uint
if rem.Supercedence != "" {
r, err := strconv.Atoi(rem.Supercedence)
if err != nil {
return nil, fmt.Errorf("invalid supercedence KBID %q for %s", rem.Supercedence, v.CVE)
}
if r < 0 {
return nil, fmt.Errorf("invalid supercedence KBID %q for %s", rem.Supercedence, v.CVE)
}
supersedes = ptr.Uint(uint(r))
}
for _, pID := range rem.ProductIDs {
// Get the bulletin for the current product ID, skip further processing if is a
// non-windows product.
b, ok := bulletins[pIDToPName[pID]]
if !ok {
continue
}
// Check if the vulnerability referenced by this remediation exists, if not
// initialize it.
var vuln parsed.Vulnerability
if vuln, ok = b.Vulnerabities[v.CVE]; !ok {
vuln = parsed.NewVulnerability(v.PublishedDateEpoch())
}
// At this point we know that the remediation is a vendor fix that targets a windows
// product, so we add the remediation's product ID to the vulnerability's targeted products.
vuln.ProductIDs[pID] = true
vuln.RemediatedBy[remediatedKBID] = true
// Check if the vendor fix referenced by this remediation exists, if not
// initialize it.
var vFix parsed.VendorFix
if vFix, ok = b.VendorFixes[remediatedKBID]; !ok {
vFix = parsed.NewVendorFix(rem.FixedBuild)
} else {
vFix.AddFixedBuild(rem.FixedBuild)
}
vFix.Supersedes = supersedes
vFix.ProductIDs[pID] = true
// Update the bulletin
b.Vulnerabities[v.CVE] = vuln
b.VendorFixes[remediatedKBID] = vFix
}
}
}
return bulletins, nil
}
func parseXML(reader io.Reader) (*msrcxml.FeedResult, error) {
r := &msrcxml.FeedResult{
WinProducts: map[string]msrcxml.Product{},
}
d := xml.NewDecoder(reader)
for {
t, err := d.Token()
if err != nil {
if err == io.EOF {
return r, nil
}
return nil, fmt.Errorf("decoding token: %v", err)
}
switch t := t.(type) { //nolint:gocritic // ignore singleCaseSwitch
case xml.StartElement:
if t.Name.Local == "Branch" {
branch := msrcxml.ProductBranch{}
if err = d.DecodeElement(&branch, &t); err != nil {
return nil, err
}
for _, p := range branch.WinProducts() {
r.WinProducts[p.ProductID] = p
}
}
if t.Name.Local == "Vulnerability" {
vuln := msrcxml.Vulnerability{}
if err = d.DecodeElement(&vuln, &t); err != nil {
return nil, err
}
for pID := range r.WinProducts {
// We only care about vulnerabilities that have a vendor fix targeting a Windows
// product.
if vuln.IncludesVendorFix(pID) {
r.WinVulnerabities = append(r.WinVulnerabities, vuln)
break
}
}
}
}
}
}