2018-10-18 22:47:37 +00:00
|
|
|
// Copyright 2018 clair authors
|
2017-01-04 02:44:32 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-02-01 23:41:40 +00:00
|
|
|
package nvd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-05-04 17:21:25 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2016-02-01 23:41:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nvd struct {
|
2018-10-16 22:42:15 +00:00
|
|
|
Entries []nvdEntry `json:"CVE_Items"`
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nvdEntry struct {
|
2018-10-16 22:42:15 +00:00
|
|
|
CVE nvdCVE `json:"cve"`
|
|
|
|
Impact nvdImpact `json:"impact"`
|
|
|
|
PublishedDateTime string `json:"publishedDate"`
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:42:15 +00:00
|
|
|
type nvdCVE struct {
|
|
|
|
Metadata nvdCVEMetadata `json:"CVE_data_meta"`
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:42:15 +00:00
|
|
|
type nvdCVEMetadata struct {
|
|
|
|
CVEID string `json:"ID"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type nvdImpact struct {
|
|
|
|
BaseMetricV2 nvdBaseMetricV2 `json:"baseMetricV2"`
|
2018-10-16 23:08:17 +00:00
|
|
|
BaseMetricV3 nvdBaseMetricV3 `json:"baseMetricV3"`
|
2018-10-16 22:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nvdBaseMetricV2 struct {
|
|
|
|
CVSSv2 nvdCVSSv2 `json:"cvssV2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type nvdCVSSv2 struct {
|
|
|
|
Score float64 `json:"baseScore"`
|
|
|
|
AccessVector string `json:"accessVector"`
|
|
|
|
AccessComplexity string `json:"accessComplexity"`
|
|
|
|
Authentication string `json:"authentication"`
|
|
|
|
ConfImpact string `json:"confidentialityImpact"`
|
|
|
|
IntegImpact string `json:"integrityImpact"`
|
|
|
|
AvailImpact string `json:"availabilityImpact"`
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 23:08:17 +00:00
|
|
|
type nvdBaseMetricV3 struct {
|
|
|
|
CVSSv3 nvdCVSSv3 `json:"cvssV3"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type nvdCVSSv3 struct {
|
|
|
|
Score float64 `json:"baseScore"`
|
|
|
|
AttackVector string `json:"attackVector"`
|
|
|
|
AttackComplexity string `json:"attackComplexity"`
|
|
|
|
PrivilegesRequired string `json:"privilegesRequired"`
|
|
|
|
UserInteraction string `json:"userInteraction"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
ConfImpact string `json:"confidentialityImpact"`
|
|
|
|
IntegImpact string `json:"integrityImpact"`
|
|
|
|
AvailImpact string `json:"availabilityImpact"`
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:48:07 +00:00
|
|
|
var vectorValuesToLetters = map[string]string{
|
|
|
|
"NETWORK": "N",
|
|
|
|
"ADJACENT_NETWORK": "A",
|
|
|
|
"LOCAL": "L",
|
|
|
|
"HIGH": "H",
|
|
|
|
"MEDIUM": "M",
|
|
|
|
"LOW": "L",
|
|
|
|
"NONE": "N",
|
|
|
|
"SINGLE": "S",
|
|
|
|
"MULTIPLE": "M",
|
|
|
|
"PARTIAL": "P",
|
|
|
|
"COMPLETE": "C",
|
|
|
|
|
|
|
|
// CVSSv3 only
|
|
|
|
"PHYSICAL": "P",
|
|
|
|
"REQUIRED": "R",
|
|
|
|
"CHANGED": "C",
|
|
|
|
"UNCHANGED": "U",
|
|
|
|
}
|
2016-02-01 23:41:40 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
vectorValuesToLetters = make(map[string]string)
|
|
|
|
vectorValuesToLetters["NETWORK"] = "N"
|
|
|
|
vectorValuesToLetters["ADJACENT_NETWORK"] = "A"
|
|
|
|
vectorValuesToLetters["LOCAL"] = "L"
|
|
|
|
vectorValuesToLetters["HIGH"] = "H"
|
|
|
|
vectorValuesToLetters["MEDIUM"] = "M"
|
|
|
|
vectorValuesToLetters["LOW"] = "L"
|
|
|
|
vectorValuesToLetters["NONE"] = "N"
|
2018-10-16 22:42:15 +00:00
|
|
|
vectorValuesToLetters["SINGLE"] = "S"
|
|
|
|
vectorValuesToLetters["MULTIPLE"] = "M"
|
2016-02-01 23:41:40 +00:00
|
|
|
vectorValuesToLetters["PARTIAL"] = "P"
|
|
|
|
vectorValuesToLetters["COMPLETE"] = "C"
|
2018-10-16 23:08:17 +00:00
|
|
|
|
|
|
|
// CVSSv3 only
|
|
|
|
vectorValuesToLetters["PHYSICAL"] = "P"
|
|
|
|
vectorValuesToLetters["REQUIRED"] = "R"
|
|
|
|
vectorValuesToLetters["CHANGED"] = "C"
|
|
|
|
vectorValuesToLetters["UNCHANGED"] = "U"
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n nvdEntry) Metadata() *NVDMetadata {
|
|
|
|
metadata := &NVDMetadata{
|
|
|
|
CVSSv2: NVDmetadataCVSSv2{
|
2018-07-12 21:40:05 +00:00
|
|
|
PublishedDateTime: n.PublishedDateTime,
|
2018-10-16 22:42:15 +00:00
|
|
|
Vectors: n.Impact.BaseMetricV2.CVSSv2.String(),
|
|
|
|
Score: n.Impact.BaseMetricV2.CVSSv2.Score,
|
2016-02-01 23:41:40 +00:00
|
|
|
},
|
2018-10-16 23:08:17 +00:00
|
|
|
CVSSv3: NVDmetadataCVSSv3{
|
|
|
|
Vectors: n.Impact.BaseMetricV3.CVSSv3.String(),
|
|
|
|
Score: n.Impact.BaseMetricV3.CVSSv3.Score,
|
|
|
|
},
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if metadata.CVSSv2.Vectors == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-16 22:42:15 +00:00
|
|
|
|
2016-02-01 23:41:40 +00:00
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:42:15 +00:00
|
|
|
func (n nvdEntry) Name() string {
|
|
|
|
return n.CVE.Metadata.CVEID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n nvdCVSSv2) String() string {
|
2016-02-01 23:41:40 +00:00
|
|
|
var str string
|
|
|
|
addVec(&str, "AV", n.AccessVector)
|
|
|
|
addVec(&str, "AC", n.AccessComplexity)
|
|
|
|
addVec(&str, "Au", n.Authentication)
|
|
|
|
addVec(&str, "C", n.ConfImpact)
|
|
|
|
addVec(&str, "I", n.IntegImpact)
|
|
|
|
addVec(&str, "A", n.AvailImpact)
|
|
|
|
str = strings.TrimSuffix(str, "/")
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-10-16 23:08:17 +00:00
|
|
|
func (n nvdCVSSv3) String() string {
|
|
|
|
var str string
|
|
|
|
addVec(&str, "AV", n.AttackVector)
|
|
|
|
addVec(&str, "AC", n.AttackComplexity)
|
|
|
|
addVec(&str, "PR", n.PrivilegesRequired)
|
|
|
|
addVec(&str, "UI", n.UserInteraction)
|
|
|
|
addVec(&str, "S", n.Scope)
|
|
|
|
addVec(&str, "C", n.ConfImpact)
|
|
|
|
addVec(&str, "I", n.IntegImpact)
|
|
|
|
addVec(&str, "A", n.AvailImpact)
|
|
|
|
str = strings.TrimSuffix(str, "/")
|
|
|
|
|
|
|
|
if len(str) > 0 {
|
|
|
|
return fmt.Sprintf("CVSS:3.0/%s", str)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:41:40 +00:00
|
|
|
func addVec(str *string, vec, val string) {
|
|
|
|
if val != "" {
|
|
|
|
if let, ok := vectorValuesToLetters[val]; ok {
|
|
|
|
*str = fmt.Sprintf("%s%s:%s/", *str, vec, let)
|
|
|
|
} else {
|
2018-10-16 22:42:15 +00:00
|
|
|
log.WithFields(log.Fields{"value": val, "vector": vec}).Warning("unknown value for CVSS vector")
|
2016-02-01 23:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|