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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
// mirror files from upstream NVD source
package nvdmirror
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"github.com/pablotron/cvez/atomictemp"
"github.com/pablotron/cvez/feed"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
// Sync() configuration.
type SyncConfig struct {
// CVE 1.1 Base URL. The full meta and JSON URLs are constructed by
// appending the file name to this base.
Cve11BaseUrl string
// CPE Match 1.0 base URL. The full meta and JSON URLs are
// constructed by appending the file name to this base.
CpeMatch10BaseUrl string
// CPE 2.3 dictionary URL.
Cpe23DictUrl string
// User agent string. Set to "" for default user agent string.
UserAgent string
// Maximum number of idle connections.
MaxIdleConns int
// Idle connection timeout.
IdleConnTimeout time.Duration
}
// NVD URLs
var DefaultConfig = SyncConfig {
Cve11BaseUrl: "https://nvd.nist.gov/feeds/json/cve/1.1",
CpeMatch10BaseUrl: "https://nvd.nist.gov/feeds/json/cpematch/1.0",
Cpe23DictUrl: "https://nvd.nist.gov/feeds/xml/cpe/dictionary/official-cpe-dictionary_v2.3.xml.gz",
UserAgent: "cvez/0.1.0",
}
// Initial (first) CVE year.
const baseYear = 2002
// Additional non-year CVE feeds.
var cveExtraFiles = []string {
"modified",
"recent",
}
// Get user agent string.
func (me SyncConfig) GetUserAgent() string {
if len(me.UserAgent) > 0 {
return me.UserAgent
} else {
return DefaultConfig.UserAgent
}
}
// Get URL for CVE feed file.
func (me SyncConfig) GetCveUrl(id, ext string) string {
return fmt.Sprintf("%s/nvdcve-1.1-%s.%s", me.Cve11BaseUrl, id, ext)
}
// Get URL for CVE feed file for given year.
func (me SyncConfig) GetCveYearUrl(year int, ext string) string {
return me.GetCveUrl(fmt.Sprintf("%04d", year), ext)
}
// Get URL for CPE match file.
func (me SyncConfig) GetCpeMatchUrl(ext string) string {
return fmt.Sprintf("%s/nvdcpematch-1.0.%s", me.CpeMatch10BaseUrl, ext)
}
// Get CPE dictionary URL.
func (me SyncConfig) GetCpeDictUrl() string {
if len(me.Cpe23DictUrl) > 0 {
return me.Cpe23DictUrl
} else {
return DefaultConfig.Cpe23DictUrl
}
}
// get meta URL map.
func (me SyncConfig) getMetaUrls() map[string]string {
// calculate total number of years
numYears := time.Now().Year() - baseYear + 1
r := make(map[string]string)
// fetch cve feed metas
for i := 0; i < numYears; i++ {
metaUrl := me.GetCveYearUrl(baseYear + i, "meta")
feedUrl := me.GetCveYearUrl(baseYear + i, "json.gz")
r[metaUrl] = feedUrl
}
// fetch cve extra file metas
for _, s := range(cveExtraFiles) {
metaUrl := me.GetCveUrl(s, "meta")
feedUrl := me.GetCveUrl(s, "json.gz")
r[metaUrl] = feedUrl
}
{
// add cpe match
metaUrl := me.GetCpeMatchUrl("meta")
feedUrl := me.GetCpeMatchUrl("json.gz")
r[metaUrl] = feedUrl
}
// return map
return r
}
// Fetch result.
type fetchResult struct {
src string // source URL
err error // fetch result
modified bool // Was the result modified?
path string // Destination file.
headers http.Header // response headers
}
// Check result.
type checkResult struct {
metaUrl string // meta full url
metaPath string // meta file path
fullPath string // full file path
err error // error
match bool // true if size and hash match
}
type syncMessage struct {
fetch fetchResult // fetch result
check checkResult // check result
}
// sync context
type syncContext struct {
config SyncConfig // sync config
client *http.Client // shared HTTP client
cache Cache // cache
dstDir string // destination directory
ch chan syncMessage // sync message channel
}
// Create sync context.
func newSyncContext(config SyncConfig, cache Cache, dstDir string) syncContext {
// create shared transport and client
tr := &http.Transport {
MaxIdleConns: config.MaxIdleConns,
IdleConnTimeout: config.IdleConnTimeout,
}
return syncContext {
config: config,
client: &http.Client{Transport: tr},
cache: cache,
dstDir: dstDir,
ch: make(chan syncMessage),
}
}
// Build request
func (me syncContext) getRequest(srcUrl string) (*http.Request, error) {
// create HTTP request
req, err := http.NewRequest("GET", srcUrl, nil)
if err != nil {
return nil, err
}
// Add user-agent, if-none-match, and if-modified-since headers.
req.Header.Add("user-agent", me.config.GetUserAgent())
if headers, ok := me.cache.Get(srcUrl); ok {
for k, v := range(headers) {
req.Header.Add(k, v)
}
}
// return success
return req, nil
}
// Fetch URL and write result to destination directory.
//
// Note: This method is called from a goroutine and writes the results
// back via the member channel.
func (me syncContext) fetch(srcUrl string) {
// parse source url
src, err := url.Parse(srcUrl)
if err != nil {
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl, err: err },
}
return
}
// build destination path
path := filepath.Join(me.dstDir, filepath.Base(src.Path))
log.Debug().Str("url", srcUrl).Str("path", path).Send()
// create request
req, err := me.getRequest(srcUrl)
if err != nil {
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl, err: err },
}
return
}
// send request
resp, err := me.client.Do(req)
if err != nil {
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl, err: err },
}
return
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200: // success
// write to output file
err := atomictemp.Create(path, func(f io.Writer) error {
_, err := io.Copy(f, resp.Body)
return err
})
if err != nil {
// write failed
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl, err: err },
}
} else {
me.ch <- syncMessage {
fetch: fetchResult {
src: srcUrl,
modified: true,
path: path,
headers: resp.Header,
},
}
}
case 304: // not modified
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl },
}
default: // error
code := resp.StatusCode
err := fmt.Errorf("%d: %s", code, http.StatusText(code))
me.ch <- syncMessage {
fetch: fetchResult { src: srcUrl, err: err },
}
}
}
// read hash from given meta file.
func (me syncContext) getMeta(path string) (*feed.Meta, error) {
// open meta file
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
// parse meta
return feed.NewMeta(f)
}
// get hash of file in destination directory.
func (me syncContext) getFileHash(path string) ([32]byte, error) {
var r [32]byte
// open file
f, err := os.Open(path)
if err != nil {
return r, err
}
defer f.Close()
// hash file
hash := sha256.New()
if _, err := io.Copy(hash, f); err != nil {
return r, err
}
// copy sum to result, return success
hash.Sum(r[:])
return r, nil
}
// Get file size, in bytes.
func getFileSize(path string) (uint64, error) {
// verify that full path exists
if st, err := os.Stat(path); err != nil {
return 0, err
} else {
return uint64(st.Size()), err
}
}
// Check the size and hash in the metadata file against the full file.
//
// Note: This method is called from a goroutine and returns it's value
// via the internal channel.
func (me syncContext) check(metaUrl, fullUrl string) {
// build result
r := syncMessage {
check: checkResult {
metaUrl: metaUrl,
// build paths
metaPath: filepath.Join(me.dstDir, filepath.Base(metaUrl)),
fullPath: filepath.Join(me.dstDir, filepath.Base(fullUrl)),
},
}
// get size of full file
size, err := getFileSize(r.check.fullPath)
if errors.Is(err, fs.ErrNotExist) {
r.check.match = false
me.ch <- r
return
} else if err != nil {
r.check.err = err
me.ch <- r
return
}
// get meta hash
m, err := me.getMeta(r.check.metaPath)
if err != nil {
r.check.err = err
me.ch <- r
return
}
// check for file size match
if size != m.GzSize {
r.check.match = false
me.ch <- r
return
}
// get full hash
fh, err := me.getFileHash(r.check.fullPath)
if err != nil {
r.check.err = err
me.ch <- r
return
}
// return result
r.check.match = (bytes.Compare(m.Sha256[:], fh[:]) == 0)
me.ch <- r
}
// log array of strings
func logArray(key string, strs []string) {
// populate array
a := zerolog.Arr()
for _, v := range(strs) {
a.Str(v)
}
// log array
log.Info().Array(key, a).Send()
}
// Sync to destination directory and return an array of updated files.
func Sync(config SyncConfig, cache Cache, dstDir string) []string {
// log.Debug().Str("dstDir", dstDir).Msg("Sync")
// build sync context
ctx := newSyncContext(config, cache, dstDir)
// get meta URL to full URL map
metaUrls := config.getMetaUrls()
// fetch meta URLs
for metaUrl, _ := range(metaUrls) {
log.Debug().Str("url", metaUrl).Msg("init")
go ctx.fetch(metaUrl)
}
// build list of metas to check
checkUrls := make([]string, 0, len(metaUrls))
// read meta results
for range(metaUrls) {
r := <-ctx.ch
sl := log.With().Str("url", r.fetch.src).Logger()
if r.fetch.err != nil {
// URL error
sl.Error().Err(r.fetch.err).Send()
} else if !r.fetch.modified {
// URL not modified
sl.Debug().Msg("not modified")
} else {
// URL updated
sl.Debug().Msg("update")
// build request headers
headers := map[string]string {
"if-none-match": r.fetch.headers.Get("etag"),
"if-modified-since": r.fetch.headers.Get("last-modified"),
}
// save headers to cache
if err := cache.Set(r.fetch.src, headers); err != nil {
sl.Error().Err(r.fetch.err).Msg("cache.Set")
} else {
// append to list of check URLs
checkUrls = append(checkUrls, r.fetch.src)
}
}
}
// check size and hash in updated metas
logArray("checkUrls", checkUrls)
for _, metaUrl := range(checkUrls) {
go ctx.check(metaUrl, metaUrls[metaUrl])
}
// build list of non-meta URLs to sync.
syncUrls := make([]string, 0, len(metaUrls))
syncUrls = append(syncUrls, config.GetCpeDictUrl())
for range(checkUrls) {
r := <-ctx.ch
// create sublogger
sl := log.With().
Str("metaUrl", r.check.metaUrl).
Str("metaPath", r.check.metaPath).
Str("fullPath", r.check.fullPath).
Logger()
if r.check.err != nil {
sl.Error().Err(r.check.err).Send()
} else if r.check.match {
sl.Debug().Msg("match")
} else {
syncUrls = append(syncUrls, metaUrls[r.check.metaUrl])
}
}
logArray("syncUrls", syncUrls)
for _, fullUrl := range(syncUrls) {
go ctx.fetch(fullUrl)
}
// build list of changed files
changed := make([]string, 0, len(syncUrls))
// read sync results
for range(syncUrls) {
r := <-ctx.ch
// build sublogger
sl := log.With().Str("url", r.fetch.src).Logger()
if r.fetch.err != nil {
sl.Error().Err(r.fetch.err).Send()
} else if !r.fetch.modified {
sl.Debug().Msg("not modified")
} else {
sl.Debug().Msg("update")
// build request headers
headers := map[string]string {
"if-none-match": r.fetch.headers.Get("etag"),
"if-modified-since": r.fetch.headers.Get("last-modified"),
}
// save headers to cache
if err := cache.Set(r.fetch.src, headers); err != nil {
sl.Error().Err(r.fetch.err).Msg("cache.Set")
} else {
// append to list of changed files
changed = append(changed, filepath.Base(r.fetch.src))
}
}
}
// log changed files
logArray("changed", changed)
// return success
return changed
}
|