-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.go
1187 lines (1015 loc) · 30.4 KB
/
datasource.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cpe
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/scagogogo/cve"
)
// DataSourceType 表示数据源类型
type DataSourceType string
const (
// DataSourceNVD NVD数据源
DataSourceNVD DataSourceType = "NVD"
// DataSourceMITRE MITRE数据源
DataSourceMITRE DataSourceType = "MITRE"
// DataSourceGitHub GitHub安全公告
DataSourceGitHub DataSourceType = "GitHub"
// DataSourceRedHatCVE RedHat CVE数据库
DataSourceRedHatCVE DataSourceType = "RedHat"
// DataSourceOWASP OWASP数据源
DataSourceOWASP DataSourceType = "OWASP"
// DataSourceCustom 自定义数据源
DataSourceCustom DataSourceType = "Custom"
)
// VulnDataSource 表示一个漏洞数据源
type VulnDataSource struct {
// 数据源类型
Type DataSourceType
// 数据源名称
Name string
// 数据源描述
Description string
// 数据源URL
URL string
// 认证信息
Authentication *DataSourceAuth
// HTTP客户端
Client *http.Client
// 上次更新时间
LastUpdated time.Time
// 缓存设置
CacheSettings *CacheSettings
// 自定义选项
Options map[string]interface{}
}
// DataSourceAuth 数据源认证信息
type DataSourceAuth struct {
// API密钥
APIKey string
// 用户名
Username string
// 密码
Password string
// 认证令牌
Token string
// 自定义头信息
Headers map[string]string
}
// CacheSettings 缓存设置
type CacheSettings struct {
// 是否启用缓存
Enabled bool
// 缓存目录
Directory string
// 缓存过期时间(小时)
ExpiryHours int
// 缓存文件名模板
FileNameTemplate string
}
// NewDataSource 创建新的数据源
func NewVulnDataSource(sourceType DataSourceType, name, description, url string) *VulnDataSource {
return &VulnDataSource{
Type: sourceType,
Name: name,
Description: description,
URL: url,
Client: &http.Client{Timeout: 60 * time.Second},
CacheSettings: &CacheSettings{
Enabled: true,
Directory: "./cache",
ExpiryHours: 24,
},
Options: make(map[string]interface{}),
}
}
// SetAuthentication 设置数据源认证信息
func (ds *VulnDataSource) SetAuthentication(auth *DataSourceAuth) {
ds.Authentication = auth
}
// SetCacheSettings 设置缓存设置
func (ds *VulnDataSource) SetCacheSettings(cache *CacheSettings) {
ds.CacheSettings = cache
}
// FetchData 从数据源获取数据
func (ds *VulnDataSource) FetchData(endpoint string) ([]byte, error) {
url := ds.URL
if endpoint != "" {
if !strings.HasSuffix(url, "/") && !strings.HasPrefix(endpoint, "/") {
url += "/"
}
url += endpoint
}
// 创建请求
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %w", err)
}
// 添加认证信息
if ds.Authentication != nil {
// API密钥认证
if ds.Authentication.APIKey != "" {
// 根据不同数据源类型处理API密钥
switch ds.Type {
case DataSourceNVD:
req.Header.Add("apiKey", ds.Authentication.APIKey)
case DataSourceGitHub:
req.Header.Add("Authorization", "token "+ds.Authentication.APIKey)
default:
req.Header.Add("X-API-Key", ds.Authentication.APIKey)
}
}
// 基本认证
if ds.Authentication.Username != "" && ds.Authentication.Password != "" {
req.SetBasicAuth(ds.Authentication.Username, ds.Authentication.Password)
}
// 令牌认证
if ds.Authentication.Token != "" {
req.Header.Add("Authorization", "Bearer "+ds.Authentication.Token)
}
// 自定义头信息
for key, value := range ds.Authentication.Headers {
req.Header.Add(key, value)
}
}
// 设置通用头信息
req.Header.Add("User-Agent", "CPE-Library/1.0")
req.Header.Add("Accept", "application/json")
// 发送请求
resp, err := ds.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %w", err)
}
defer resp.Body.Close()
// 检查响应状态
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("请求返回非成功状态码: %d", resp.StatusCode)
}
// 读取响应内容
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %w", err)
}
// 更新最后更新时间
ds.LastUpdated = time.Now()
return body, nil
}
// GetVulnerabilities 获取漏洞信息
func (ds *VulnDataSource) GetVulnerabilities(params map[string]string) ([]*CVEReference, error) {
// 构建查询参数
endpoint := ""
queryParams := []string{}
for key, value := range params {
queryParams = append(queryParams, fmt.Sprintf("%s=%s", key, value))
}
if len(queryParams) > 0 {
endpoint = "?" + strings.Join(queryParams, "&")
}
// 根据数据源类型设置特定的端点
switch ds.Type {
case DataSourceNVD:
endpoint = "vuln/search" + endpoint
case DataSourceGitHub:
endpoint = "advisories" + endpoint
case DataSourceRedHatCVE:
endpoint = "rest/cve" + endpoint
default:
// 使用默认端点或自定义端点
if ep, ok := ds.Options["endpoint"].(string); ok && ep != "" {
endpoint = ep + endpoint
}
}
// 获取数据
data, err := ds.FetchData(endpoint)
if err != nil {
return nil, err
}
// 解析为CVE引用对象
var vulnerabilities []*CVEReference
switch ds.Type {
case DataSourceNVD:
// 解析NVD格式
vulnerabilities, err = ds.parseNVDVulnerabilities(data)
case DataSourceGitHub:
// 解析GitHub格式
vulnerabilities, err = ds.parseGitHubVulnerabilities(data)
case DataSourceRedHatCVE:
// 解析RedHat格式
vulnerabilities, err = ds.parseRedHatVulnerabilities(data)
default:
// 尝试通用解析
err = json.Unmarshal(data, &vulnerabilities)
}
if err != nil {
return nil, fmt.Errorf("解析漏洞数据失败: %w", err)
}
return vulnerabilities, nil
}
// GetVulnerabilityById 根据CVE ID获取漏洞信息
func (ds *VulnDataSource) GetVulnerabilityById(cveID string) (*CVEReference, error) {
// 标准化CVE ID
cveID = cve.Format(cveID)
var endpoint string
// 根据数据源类型构建请求
switch ds.Type {
case DataSourceNVD:
endpoint = fmt.Sprintf("vuln/%s", cveID)
case DataSourceRedHatCVE:
endpoint = fmt.Sprintf("rest/cve/%s", cveID)
default:
// 使用查询参数
endpoint = fmt.Sprintf("?cve=%s", cveID)
// 使用自定义端点如果有提供
if ep, ok := ds.Options["endpoint"].(string); ok && ep != "" {
endpoint = ep + endpoint
}
}
// 获取数据
data, err := ds.FetchData(endpoint)
if err != nil {
return nil, err
}
// 解析CVE信息
var cveRef *CVEReference
switch ds.Type {
case DataSourceNVD:
// 解析NVD单个CVE格式
cveRefs, err := ds.parseNVDVulnerabilities(data)
if err != nil {
return nil, err
}
if len(cveRefs) > 0 {
cveRef = cveRefs[0]
}
case DataSourceRedHatCVE:
// 解析RedHat单个CVE格式
cveRefs, err := ds.parseRedHatVulnerabilities(data)
if err != nil {
return nil, err
}
if len(cveRefs) > 0 {
cveRef = cveRefs[0]
}
default:
// 尝试通用解析
err = json.Unmarshal(data, &cveRef)
}
if err != nil {
return nil, fmt.Errorf("解析CVE数据失败: %w", err)
}
if cveRef == nil {
return nil, fmt.Errorf("未找到CVE: %s", cveID)
}
return cveRef, nil
}
// SearchVulnerabilitiesByCPE 根据CPE查找相关漏洞
func (ds *VulnDataSource) SearchVulnerabilitiesByCPE(cpe *CPE) ([]*CVEReference, error) {
params := map[string]string{
"cpe": cpe.Cpe23,
}
return ds.GetVulnerabilities(params)
}
// 解析NVD格式的漏洞数据
func (ds *VulnDataSource) parseNVDVulnerabilities(data []byte) ([]*CVEReference, error) {
// NVD API响应结构
type NVDVuln struct {
CVE struct {
ID string `json:"id"`
Description struct {
DescriptionData []struct {
Value string `json:"value"`
} `json:"description_data"`
} `json:"description"`
References struct {
ReferenceData []struct {
URL string `json:"url"`
} `json:"reference_data"`
} `json:"references"`
} `json:"cve"`
Impact struct {
BaseMetricV3 struct {
CVSSV3 struct {
BaseScore float64 `json:"baseScore"`
} `json:"cvssV3"`
} `json:"baseMetricV3"`
} `json:"impact"`
PublishedDate string `json:"publishedDate"`
LastModifiedDate string `json:"lastModifiedDate"`
Configurations struct {
Nodes []struct {
CPEMatch []struct {
CPE23URI string `json:"cpe23Uri"`
} `json:"cpe_match"`
} `json:"nodes"`
} `json:"configurations"`
}
type NVDResponse struct {
ResultCount int `json:"resultsPerPage"`
Results []NVDVuln `json:"result"`
}
var nvdResp NVDResponse
err := json.Unmarshal(data, &nvdResp)
if err != nil {
// 尝试解析单个漏洞响应
var singleVuln NVDVuln
err = json.Unmarshal(data, &singleVuln)
if err != nil {
return nil, err
}
nvdResp.Results = []NVDVuln{singleVuln}
}
var cveRefs []*CVEReference
for _, vuln := range nvdResp.Results {
cveRef := &CVEReference{
CVEID: vuln.CVE.ID,
}
// 提取描述
if len(vuln.CVE.Description.DescriptionData) > 0 {
cveRef.Description = vuln.CVE.Description.DescriptionData[0].Value
}
// 提取CVSS评分
cveRef.CVSSScore = vuln.Impact.BaseMetricV3.CVSSV3.BaseScore
// 提取日期
if vuln.PublishedDate != "" {
pubDate, err := time.Parse(time.RFC3339, vuln.PublishedDate)
if err == nil {
cveRef.PublishedDate = pubDate
}
}
if vuln.LastModifiedDate != "" {
modDate, err := time.Parse(time.RFC3339, vuln.LastModifiedDate)
if err == nil {
cveRef.LastModifiedDate = modDate
}
}
// 提取参考链接
var refs []string
for _, ref := range vuln.CVE.References.ReferenceData {
refs = append(refs, ref.URL)
}
cveRef.References = refs
// 提取受影响的CPE
var affectedProducts []string
for _, node := range vuln.Configurations.Nodes {
for _, match := range node.CPEMatch {
affectedProducts = append(affectedProducts, match.CPE23URI)
}
}
cveRef.AffectedCPEs = affectedProducts
cveRefs = append(cveRefs, cveRef)
}
return cveRefs, nil
}
// 解析GitHub格式的漏洞数据
func (ds *VulnDataSource) parseGitHubVulnerabilities(data []byte) ([]*CVEReference, error) {
// GitHub Security Advisory响应结构
type GitHubAdvisory struct {
ID string `json:"ghsa_id"`
CVEID string `json:"cve_id"`
Summary string `json:"summary"`
Description string `json:"description"`
Severity string `json:"severity"`
PublishedAt string `json:"published_at"`
UpdatedAt string `json:"updated_at"`
References []struct {
URL string `json:"url"`
} `json:"references"`
Vulnerabilities []struct {
Package struct {
Ecosystem string `json:"ecosystem"`
Name string `json:"name"`
} `json:"package"`
Ranges []struct {
Introduced string `json:"introduced"`
Fixed string `json:"fixed"`
} `json:"ranges"`
} `json:"vulnerabilities"`
}
var advisories []GitHubAdvisory
err := json.Unmarshal(data, &advisories)
if err != nil {
// 尝试解析单个公告
var singleAdvisory GitHubAdvisory
err = json.Unmarshal(data, &singleAdvisory)
if err != nil {
return nil, err
}
advisories = []GitHubAdvisory{singleAdvisory}
}
var cveRefs []*CVEReference
for _, advisory := range advisories {
// 跳过没有CVE ID的公告
if advisory.CVEID == "" {
continue
}
cveRef := &CVEReference{
CVEID: advisory.CVEID,
Description: advisory.Description,
}
// 设置CVSS评分 (GitHub使用严重性描述而不是具体分数)
switch advisory.Severity {
case "critical":
cveRef.CVSSScore = 9.0
case "high":
cveRef.CVSSScore = 7.0
case "medium":
cveRef.CVSSScore = 5.0
case "low":
cveRef.CVSSScore = 3.0
default:
cveRef.CVSSScore = 0.0
}
// 解析日期
if advisory.PublishedAt != "" {
pubDate, err := time.Parse(time.RFC3339, advisory.PublishedAt)
if err == nil {
cveRef.PublishedDate = pubDate
}
}
if advisory.UpdatedAt != "" {
modDate, err := time.Parse(time.RFC3339, advisory.UpdatedAt)
if err == nil {
cveRef.LastModifiedDate = modDate
}
}
// 提取参考链接
var refs []string
for _, ref := range advisory.References {
refs = append(refs, ref.URL)
}
cveRef.References = refs
// 为受影响的包创建CPE
var affectedProducts []string
for _, vuln := range advisory.Vulnerabilities {
// 创建CPE 2.3格式字符串
// 格式: cpe:2.3:a:[ecosystem]:[package_name]:[version_range]:*:*:*:*:*:*:*
for _, r := range vuln.Ranges {
var version string
if r.Introduced != "" && r.Fixed != "" {
version = fmt.Sprintf("%s-%s", r.Introduced, r.Fixed)
} else if r.Introduced != "" {
version = fmt.Sprintf("%s-*", r.Introduced)
} else if r.Fixed != "" {
version = fmt.Sprintf("*-%s", r.Fixed)
} else {
version = "*"
}
cpe := fmt.Sprintf("cpe:2.3:a:%s:%s:%s:*:*:*:*:*:*:*",
strings.ToLower(vuln.Package.Ecosystem),
strings.ToLower(vuln.Package.Name),
version)
affectedProducts = append(affectedProducts, cpe)
}
}
cveRef.AffectedCPEs = affectedProducts
cveRefs = append(cveRefs, cveRef)
}
return cveRefs, nil
}
// 解析RedHat格式的漏洞数据
func (ds *VulnDataSource) parseRedHatVulnerabilities(data []byte) ([]*CVEReference, error) {
// RedHat CVE响应结构
type RedHatCVE struct {
CVE string `json:"CVE"`
BugzillaID string `json:"bugzilla"`
CVSSScore float64 `json:"cvss_score"`
CVSSVersion string `json:"cvss_version"`
Description string `json:"description"`
PublicDate string `json:"public_date"`
Modified string `json:"modified_date"`
Details []string `json:"details"`
AffectedPackages []struct {
Name string `json:"name"`
Version string `json:"version"`
} `json:"affected_packages"`
References []struct {
URL string `json:"url"`
} `json:"references"`
}
var redhatCVEs []RedHatCVE
err := json.Unmarshal(data, &redhatCVEs)
if err != nil {
// 尝试解析单个CVE
var singleCVE RedHatCVE
err = json.Unmarshal(data, &singleCVE)
if err != nil {
return nil, err
}
redhatCVEs = []RedHatCVE{singleCVE}
}
var cveRefs []*CVEReference
for _, rh := range redhatCVEs {
cveRef := &CVEReference{
CVEID: rh.CVE,
Description: rh.Description,
CVSSScore: rh.CVSSScore,
}
// 解析日期
if rh.PublicDate != "" {
pubDate, err := time.Parse("2006-01-02", rh.PublicDate)
if err == nil {
cveRef.PublishedDate = pubDate
}
}
if rh.Modified != "" {
modDate, err := time.Parse("2006-01-02", rh.Modified)
if err == nil {
cveRef.LastModifiedDate = modDate
}
}
// 提取参考链接
var refs []string
for _, ref := range rh.References {
refs = append(refs, ref.URL)
}
cveRef.References = refs
// 为受影响的包创建CPE
var affectedProducts []string
for _, pkg := range rh.AffectedPackages {
// 创建CPE 2.3格式字符串
// 格式: cpe:2.3:a:redhat:[package_name]:[version]:*:*:*:*:*:*:*
cpe := fmt.Sprintf("cpe:2.3:a:redhat:%s:%s:*:*:*:*:*:*:*",
strings.ToLower(pkg.Name),
pkg.Version)
affectedProducts = append(affectedProducts, cpe)
}
cveRef.AffectedCPEs = affectedProducts
cveRefs = append(cveRefs, cveRef)
}
return cveRefs, nil
}
// MultiSourceVulnerabilitySearch 多数据源漏洞搜索
type MultiSourceVulnerabilitySearch struct {
// 数据源列表
Sources []*VulnDataSource
// 并发级别
ConcurrencyLevel int
// 超时时间(秒)
TimeoutSeconds int
// 是否合并结果
MergeResults bool
}
// NewMultiSourceSearch 创建新的多数据源搜索
func NewMultiSourceSearch(sources []*VulnDataSource) *MultiSourceVulnerabilitySearch {
return &MultiSourceVulnerabilitySearch{
Sources: sources,
ConcurrencyLevel: 3,
TimeoutSeconds: 30,
MergeResults: true,
}
}
// SearchByCVE 根据CVE ID在多个数据源中搜索
func (ms *MultiSourceVulnerabilitySearch) SearchByCVE(cveID string) ([]*CVEReference, error) {
// 标准化CVE ID
cveID = cve.Format(cveID)
// 用于保存搜索结果
type searchResult struct {
source string
cveRefs []*CVEReference
err error
}
// 创建通道和等待组
resultChan := make(chan searchResult, len(ms.Sources))
// 限制并发
sem := make(chan struct{}, ms.ConcurrencyLevel)
// 为每个数据源创建goroutine
for _, source := range ms.Sources {
sem <- struct{}{} // 获取信号量
go func(s *VulnDataSource) {
defer func() { <-sem }() // 释放信号量
// 设置超时
ctx := s.Client.Timeout
s.Client.Timeout = time.Duration(ms.TimeoutSeconds) * time.Second
defer func() { s.Client.Timeout = ctx }()
// 搜索漏洞
params := map[string]string{
"cve": cveID,
}
cveRefs, err := s.GetVulnerabilities(params)
// 发送结果到通道
resultChan <- searchResult{
source: s.Name,
cveRefs: cveRefs,
err: err,
}
}(source)
}
// 收集所有数据源的结果
var allResults []*CVEReference
var errors []string
// 存储CVE引用的map,用于去重
cveRefMap := make(map[string]*CVEReference)
// 等待所有搜索完成
for i := 0; i < len(ms.Sources); i++ {
result := <-resultChan
if result.err != nil {
errors = append(errors, fmt.Sprintf("%s: %v", result.source, result.err))
continue
}
if ms.MergeResults {
// 合并结果
for _, cveRef := range result.cveRefs {
// 如果这个CVE已经在map中
if existing, ok := cveRefMap[cveRef.CVEID]; ok {
// 合并受影响产品
existing.AffectedCPEs = mergeStringSlices(existing.AffectedCPEs, cveRef.AffectedCPEs)
// 合并参考链接
existing.References = mergeStringSlices(existing.References, cveRef.References)
// 使用最高的CVSS评分
if cveRef.CVSSScore > existing.CVSSScore {
existing.CVSSScore = cveRef.CVSSScore
}
// 使用最早的发布日期
if !cveRef.PublishedDate.IsZero() && (existing.PublishedDate.IsZero() || cveRef.PublishedDate.Before(existing.PublishedDate)) {
existing.PublishedDate = cveRef.PublishedDate
}
// 使用最新的修改日期
if !cveRef.LastModifiedDate.IsZero() && (existing.LastModifiedDate.IsZero() || cveRef.LastModifiedDate.After(existing.LastModifiedDate)) {
existing.LastModifiedDate = cveRef.LastModifiedDate
}
// 如果当前描述更长,使用当前描述
if len(cveRef.Description) > len(existing.Description) {
existing.Description = cveRef.Description
}
} else {
// 添加新的CVE引用
cveRefMap[cveRef.CVEID] = cveRef
}
}
} else {
// 不合并,直接添加所有结果
allResults = append(allResults, result.cveRefs...)
}
}
// 如果合并结果,将map转换为切片
if ms.MergeResults {
for _, cveRef := range cveRefMap {
allResults = append(allResults, cveRef)
}
}
// 如果有错误但我们仍然获得了一些结果,只是返回结果
if len(errors) > 0 && len(allResults) > 0 {
return allResults, nil
}
// 如果只有错误,返回第一个错误
if len(errors) > 0 {
return nil, fmt.Errorf("多源搜索错误: %s", errors[0])
}
return allResults, nil
}
// SearchByCPE 根据CPE在多个数据源中搜索
func (ms *MultiSourceVulnerabilitySearch) SearchByCPE(cpe *CPE) ([]*CVEReference, error) {
// 用于保存搜索结果
type searchResult struct {
source string
cveRefs []*CVEReference
err error
}
// 创建通道和等待组
resultChan := make(chan searchResult, len(ms.Sources))
// 限制并发
sem := make(chan struct{}, ms.ConcurrencyLevel)
// 为每个数据源创建goroutine
for _, source := range ms.Sources {
sem <- struct{}{} // 获取信号量
go func(s *VulnDataSource) {
defer func() { <-sem }() // 释放信号量
// 设置超时
ctx := s.Client.Timeout
s.Client.Timeout = time.Duration(ms.TimeoutSeconds) * time.Second
defer func() { s.Client.Timeout = ctx }()
// 搜索漏洞
cveRefs, err := s.SearchVulnerabilitiesByCPE(cpe)
// 发送结果到通道
resultChan <- searchResult{
source: s.Name,
cveRefs: cveRefs,
err: err,
}
}(source)
}
// 收集所有数据源的结果
var allResults []*CVEReference
var errors []string
// 存储CVE引用的map,用于去重
cveRefMap := make(map[string]*CVEReference)
// 等待所有搜索完成
for i := 0; i < len(ms.Sources); i++ {
result := <-resultChan
if result.err != nil {
errors = append(errors, fmt.Sprintf("%s: %v", result.source, result.err))
continue
}
if ms.MergeResults {
// 合并结果
for _, cveRef := range result.cveRefs {
// 如果这个CVE已经在map中
if existing, ok := cveRefMap[cveRef.CVEID]; ok {
// 合并受影响产品
existing.AffectedCPEs = mergeStringSlices(existing.AffectedCPEs, cveRef.AffectedCPEs)
// 合并参考链接
existing.References = mergeStringSlices(existing.References, cveRef.References)
// 使用最高的CVSS评分
if cveRef.CVSSScore > existing.CVSSScore {
existing.CVSSScore = cveRef.CVSSScore
}
// 使用最早的发布日期
if !cveRef.PublishedDate.IsZero() && (existing.PublishedDate.IsZero() || cveRef.PublishedDate.Before(existing.PublishedDate)) {
existing.PublishedDate = cveRef.PublishedDate
}
// 使用最新的修改日期
if !cveRef.LastModifiedDate.IsZero() && (existing.LastModifiedDate.IsZero() || cveRef.LastModifiedDate.After(existing.LastModifiedDate)) {
existing.LastModifiedDate = cveRef.LastModifiedDate
}
// 如果当前描述更长,使用当前描述
if len(cveRef.Description) > len(existing.Description) {
existing.Description = cveRef.Description
}
} else {
// 添加新的CVE引用
cveRefMap[cveRef.CVEID] = cveRef
}
}
} else {
// 不合并,直接添加所有结果
allResults = append(allResults, result.cveRefs...)
}
}
// 如果合并结果,将map转换为切片
if ms.MergeResults {
for _, cveRef := range cveRefMap {
allResults = append(allResults, cveRef)
}
}
// 如果有错误但我们仍然获得了一些结果,只是返回结果
if len(errors) > 0 && len(allResults) > 0 {
return allResults, nil
}
// 如果只有错误,返回第一个错误
if len(errors) > 0 {
return nil, fmt.Errorf("多源搜索错误: %s", errors[0])
}
return allResults, nil
}
// 合并两个字符串切片,去除重复项
func mergeStringSlices(slice1, slice2 []string) []string {
// 使用map去重
uniqueMap := make(map[string]bool)
for _, item := range slice1 {
uniqueMap[item] = true
}
for _, item := range slice2 {
uniqueMap[item] = true
}
// 转换回切片
result := make([]string, 0, len(uniqueMap))
for item := range uniqueMap {
result = append(result, item)
}
return result
}
// CreateNVDDataSource 创建NVD数据源
func CreateNVDDataSource(apiKey string) *VulnDataSource {
ds := NewVulnDataSource(
DataSourceNVD,
"National Vulnerability Database",
"美国国家漏洞数据库",
"https://services.nvd.nist.gov/rest/json/",
)
if apiKey != "" {
ds.SetAuthentication(&DataSourceAuth{
APIKey: apiKey,
})
}
return ds
}
// CreateGitHubDataSource 创建GitHub数据源
func CreateGitHubDataSource(token string) *VulnDataSource {
ds := NewVulnDataSource(
DataSourceGitHub,
"GitHub Security Advisories",
"GitHub安全公告",
"https://api.github.com/security-advisories/",
)
if token != "" {
ds.SetAuthentication(&DataSourceAuth{
APIKey: token,
})
}
return ds
}
// CreateRedHatDataSource 创建RedHat数据源
func CreateRedHatDataSource() *VulnDataSource {
return NewVulnDataSource(
DataSourceRedHatCVE,
"Red Hat Security Data API",
"Red Hat安全数据API",
"https://access.redhat.com/labs/securitydataapi/",
)
}
// CreateDefaultMultiSourceSearch 创建默认的多源搜索
func CreateDefaultMultiSourceSearch() *MultiSourceVulnerabilitySearch {
// 创建默认数据源
nvd := CreateNVDDataSource("")
redhat := CreateRedHatDataSource()
sources := []*VulnDataSource{nvd, redhat}
return NewMultiSourceSearch(sources)
}
/**
* CPEDataSource接口 定义了获取和查询CPE数据的标准方法集
*
* 此接口允许从不同来源(如NVD、本地文件、内存等)获取CPE数据,
* 并提供统一的查询接口。实现此接口的类型可以作为CPE数据提供者,
* 用于漏洞扫描、资产管理等安全应用场景。
*/
type CPEDataSource interface {
QueryByCPE(cpe string) ([]string, error)
GetCVEInfo(cveID string) (*CVEReference, error)
}
/**