Skip to content

Commit

Permalink
ext: Parse CVSSv3 data from JSON NVD feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Katee committed Oct 16, 2018
1 parent 14277a8 commit b81e445
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ext/vulnmdsrc/nvd/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type nvdCVEMetadata struct {

type nvdImpact struct {
BaseMetricV2 nvdBaseMetricV2 `json:"baseMetricV2"`
BaseMetricV3 nvdBaseMetricV3 `json:"baseMetricV3"`
}

type nvdBaseMetricV2 struct {
Expand All @@ -57,6 +58,22 @@ type nvdCVSSv2 struct {
AvailImpact string `json:"availabilityImpact"`
}

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"`
}

var vectorValuesToLetters map[string]string

func init() {
Expand All @@ -72,6 +89,12 @@ func init() {
vectorValuesToLetters["MULTIPLE"] = "M"
vectorValuesToLetters["PARTIAL"] = "P"
vectorValuesToLetters["COMPLETE"] = "C"

// CVSSv3 only
vectorValuesToLetters["PHYSICAL"] = "P"
vectorValuesToLetters["REQUIRED"] = "R"
vectorValuesToLetters["CHANGED"] = "C"
vectorValuesToLetters["UNCHANGED"] = "U"
}

func (n nvdEntry) Metadata() *NVDMetadata {
Expand All @@ -81,6 +104,10 @@ func (n nvdEntry) Metadata() *NVDMetadata {
Vectors: n.Impact.BaseMetricV2.CVSSv2.String(),
Score: n.Impact.BaseMetricV2.CVSSv2.Score,
},
CVSSv3: NVDmetadataCVSSv3{
Vectors: n.Impact.BaseMetricV3.CVSSv3.String(),
Score: n.Impact.BaseMetricV3.CVSSv3.Score,
},
}

if metadata.CVSSv2.Vectors == "" {
Expand All @@ -106,6 +133,24 @@ func (n nvdCVSSv2) String() string {
return str
}

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
}

func addVec(str *string, vec, val string) {
if val != "" {
if let, ok := vectorValuesToLetters[val]; ok {
Expand Down
6 changes: 6 additions & 0 deletions ext/vulnmdsrc/nvd/nvd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type appender struct {

type NVDMetadata struct {
CVSSv2 NVDmetadataCVSSv2
CVSSv3 NVDmetadataCVSSv3
}

type NVDmetadataCVSSv2 struct {
Expand All @@ -63,6 +64,11 @@ type NVDmetadataCVSSv2 struct {
Score float64
}

type NVDmetadataCVSSv3 struct {
Vectors string
Score float64
}

func init() {
vulnmdsrc.RegisterAppender(appenderName, &appender{})
}
Expand Down
4 changes: 4 additions & 0 deletions ext/vulnmdsrc/nvd/nvd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func TestNVDParser(t *testing.T) {
Vectors: "AV:N/AC:L/Au:N/C:P/I:P/A:P",
Score: 7.5,
},
CVSSv3: NVDmetadataCVSSv3{
Vectors: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
Score: 9.8,
},
}
assert.Equal(t, wantMetadata, gotMetadata)
}
Expand Down

0 comments on commit b81e445

Please sign in to comment.