-
Notifications
You must be signed in to change notification settings - Fork 446
/
analyzer.go
215 lines (183 loc) · 5.38 KB
/
analyzer.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
package msrc
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/fleetdm/fleet/v4/server/vulnerabilities/io"
msrc "github.com/fleetdm/fleet/v4/server/vulnerabilities/msrc/parsed"
utils "github.com/fleetdm/fleet/v4/server/vulnerabilities/utils"
kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
)
const (
vulnBatchSize = 500
)
func Analyze(
ctx context.Context,
ds fleet.Datastore,
os fleet.OperatingSystem,
vulnPath string,
collectVulns bool,
logger kitlog.Logger,
) ([]fleet.OSVulnerability, error) {
bulletin, err := loadBulletin(os, vulnPath)
if err != nil {
return nil, err
}
// Find matching products inside the bulletin
matchingPIDs := make(map[string]bool)
pID, err := bulletin.Products.GetMatchForOS(ctx, os)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "Analyzing MSRC vulnerabilities")
}
if pID != "" {
matchingPIDs[pID] = true
}
if len(matchingPIDs) == 0 {
return nil, nil
}
toInsert := make(map[string]fleet.OSVulnerability)
toDelete := make(map[string]fleet.OSVulnerability)
// Run vulnerability detection for all hosts in this batch (hIDs)
// and store the results in 'found'.
var found []fleet.OSVulnerability
for cve, v := range bulletin.Vulnerabities {
// Check if this vulnerability targets the OS
if !utils.ProductIDsIntersect(v.ProductIDs, matchingPIDs) {
continue
}
// Check if the OS is vulnerable to the vulnerability by referencing the OS kernel version
isVuln, riv := isOSVulnerable(os.KernelVersion, bulletin, v, matchingPIDs, cve, logger)
if isVuln {
found = append(found, fleet.OSVulnerability{
OSID: os.ID,
CVE: cve,
Source: fleet.MSRCSource,
ResolvedInVersion: ptr.String(riv),
})
}
}
// Fetch all stored vulnerabilities for the current batch
osVulns, err := ds.ListOSVulnerabilitiesByOS(ctx, os.ID)
if err != nil {
return nil, err
}
var existing []fleet.OSVulnerability
existing = append(existing, osVulns...)
// Compute what needs to be inserted/deleted for this batch
insrt, del := utils.VulnsDelta(found, existing)
for _, i := range insrt {
toInsert[i.Key()] = i
}
for _, d := range del {
toDelete[d.Key()] = d
}
err = utils.BatchProcess(toDelete, func(v []fleet.OSVulnerability) error {
return ds.DeleteOSVulnerabilities(ctx, v)
}, vulnBatchSize)
if err != nil {
return nil, err
}
var inserted []fleet.OSVulnerability
if collectVulns {
inserted = make([]fleet.OSVulnerability, 0, len(toInsert))
}
err = utils.BatchProcess(toInsert, func(v []fleet.OSVulnerability) error {
n, err := ds.InsertOSVulnerabilities(ctx, v, fleet.MSRCSource)
if err != nil {
return err
}
if collectVulns && n > 0 {
inserted = append(inserted, v...)
}
return nil
}, vulnBatchSize)
if err != nil {
return nil, err
}
return inserted, nil
}
// isOSVulnerable returns true if the OS is vulnerable to the given vulnerability.
// If the OS is vulnerable, the function returns the version in which the vulnerability
// was resolved.
func isOSVulnerable(
osKernel string,
b *msrc.SecurityBulletin,
v msrc.Vulnerability,
matchingPIDs map[string]bool,
cve string,
logger kitlog.Logger,
) (isVulnerable bool, resolvedInVersion string) {
for KBID := range v.RemediatedBy {
fix := b.VendorFixes[KBID]
// Check if the MSRC FixedBuild version targets the OS version
if !utils.ProductIDsIntersect(fix.ProductIDs, matchingPIDs) {
continue
}
for _, build := range fix.FixedBuilds {
// An empty FixBuild is a bug in the MSRC feed, last
// seen around apr-2021. Ignoring it to avoid false
// positive vulnerabilities.
if build == "" {
continue
}
fixedBuild, feedParts, err := getBuildNumber(build)
if err != nil {
level.Debug(logger).Log("msg", "invalid msrc feed version", "cve", cve, "err", err)
continue
}
osBuild, osParts, err := getBuildNumber(osKernel)
if err != nil {
continue
}
// skip if the product version number does not match
// ie. 10.0.22000.X vs 10.0.22631.X
if isProductVersionMismatch(feedParts, osParts) {
continue
}
if osBuild < fixedBuild {
return true, build
}
}
}
return
}
func isProductVersionMismatch(feedVersion, osVersion []string) bool {
for i := 0; i < 3; i++ {
if feedVersion[i] != osVersion[i] {
return true
}
}
return false
}
// loadBulletin loads the most recent bulletin for the given os
func loadBulletin(os fleet.OperatingSystem, dir string) (*msrc.SecurityBulletin, error) {
product := msrc.NewProductFromOS(os)
fileName := io.MSRCFileName(product.Name(), time.Now())
latest, err := utils.LatestFile(fileName, dir)
if err != nil {
return nil, err
}
return msrc.UnmarshalBulletin(latest)
}
// getBuildNumber expects a version string in the format "10.0.22000.194" and
// returns the final part (build number) as an integer and the parts as a slice of strings.
func getBuildNumber(version string) (int, []string, error) {
if version == "" {
return 0, nil, fmt.Errorf("empty version string %s", version)
}
parts := strings.Split(version, ".")
if len(parts) != 4 {
return 0, nil, fmt.Errorf("parts count mismatch %s", version)
}
build, err := strconv.Atoi(parts[3])
if err != nil {
return 0, nil, fmt.Errorf("unable to parse build number %s", version)
}
return build, parts, nil
}