-
Notifications
You must be signed in to change notification settings - Fork 21
/
exploitdb.go
101 lines (87 loc) · 2.8 KB
/
exploitdb.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
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exploitdb
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/openclarity/vmclarity/scanner/families/exploits/common"
"github.com/openclarity/vmclarity/scanner/families/exploits/exploitdb/config"
"github.com/openclarity/vmclarity/scanner/job_manager"
"github.com/openclarity/vmclarity/scanner/utils"
)
const ScannerName = "exploitdb"
type Scanner struct {
name string
logger *log.Entry
config config.Config
resultChan chan job_manager.Result
}
func New(_ string, c job_manager.IsConfig, logger *log.Entry, resultChan chan job_manager.Result) job_manager.Job {
conf := c.(*common.ScannersConfig) // nolint:forcetypeassert
return &Scanner{
name: ScannerName,
logger: logger.Dup().WithField("scanner", ScannerName),
config: config.Config{BaseURL: conf.ExploitDB.BaseURL},
resultChan: resultChan,
}
}
func (a *Scanner) Run(sourceType utils.SourceType, userInput string) error {
go func() {
retResults := common.Results{
ScannedInput: userInput,
ScannerName: ScannerName,
}
if !a.isValidInputType(sourceType) {
a.sendResults(retResults, nil)
return
}
cveIDs := stringToArray(userInput)
if cveIDs == nil {
a.sendResults(retResults, nil)
return
}
// get exploits (get request to exploit db)
exploits, err := a.getExploitsFromCVEIDs(cveIDs)
if err != nil {
a.sendResults(retResults, fmt.Errorf("failed to get exploits from cve ids: %w", err))
return
}
retResults.Exploits = exploits
a.sendResults(retResults, nil)
}()
return nil
}
func (a *Scanner) isValidInputType(sourceType utils.SourceType) bool {
switch sourceType {
case "csv":
return true
case utils.DIR, utils.ROOTFS, utils.FILE, utils.IMAGE, utils.SBOM, utils.DOCKERARCHIVE, utils.OCIARCHIVE, utils.OCIDIR:
a.logger.Infof("source type %v is not supported for exploitsdb, skipping.", sourceType)
default:
a.logger.Infof("unknown source type %v, skipping.", sourceType)
}
return false
}
func (a *Scanner) sendResults(results common.Results, err error) {
if err != nil {
a.logger.Error(err)
results.Error = err
}
select {
case a.resultChan <- &results:
default:
a.logger.Error("Failed to send results on channel")
}
}