aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-21 22:09:26 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-21 22:09:26 -0500
commit5b78051b3a023e78634e8985807a7beddaa0aaa9 (patch)
tree6c69f330964e609713cc25d5b4afb03f557b9487
parentc163cfd9c727200b0b426060904a18fe9baf4151 (diff)
downloadcvez-5b78051b3a023e78634e8985807a7beddaa0aaa9.tar.bz2
cvez-5b78051b3a023e78634e8985807a7beddaa0aaa9.zip
add cvss/calc.go and cmd/cvss.go (cvss calculator)
-rw-r--r--cmd/cvss.go33
-rw-r--r--cvss/calc.go80
2 files changed, 113 insertions, 0 deletions
diff --git a/cmd/cvss.go b/cmd/cvss.go
new file mode 100644
index 0000000..d704811
--- /dev/null
+++ b/cmd/cvss.go
@@ -0,0 +1,33 @@
+package cmd
+
+import (
+ "encoding/json"
+ "github.com/pablotron/cvez/cvss"
+ "github.com/spf13/cobra"
+ "github.com/rs/zerolog"
+ "github.com/rs/zerolog/log"
+ "os"
+ "time"
+)
+
+var cvssCmd = &cobra.Command{
+ Use: "cvss",
+ Short: "CVSS vector calculator.",
+ Long: `Common Vulnerability Scoring System (CVSS) vector calculator.`,
+
+ Run: func(cmd *cobra.Command, args []string) {
+ // set global logging options
+ zerolog.TimeFieldFormat = time.RFC3339
+ zerolog.SetGlobalLevel(zerolog.InfoLevel)
+
+ // parse args, write result
+ e := json.NewEncoder(os.Stdout)
+ if err := e.Encode(cvss.Calc(args)); err != nil {
+ log.Error().Err(err).Msg("Encode")
+ }
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(cvssCmd)
+}
diff --git a/cvss/calc.go b/cvss/calc.go
new file mode 100644
index 0000000..7f22249
--- /dev/null
+++ b/cvss/calc.go
@@ -0,0 +1,80 @@
+package cvss
+
+// Calculated scores
+type CalcScores struct {
+ Base float32 `json:"base,omitempty"`
+ Temporal float32 `json:"temp,omitempty"`
+ Env float32 `json:"env,omitempty"`
+}
+
+// Calculated severities
+type CalcSeverities struct {
+ Base string `json:"base,omitempty"`
+ Temporal string `json:"temp,omitempty"`
+ Env string `json:"env,omitempty"`
+}
+
+// Row returned by Calc().
+type CalcRow struct {
+ // Input string
+ String string `json:"string"`
+
+ // vector error
+ Error string `json:"error,omitempty"`
+
+ // CVSS version
+ Version string `json:"version,omitempty"`
+
+ // CVSS scores
+ Scores CalcScores `json:"scores,omitempty"`
+
+ // severities
+ Severities CalcSeverities `json:"severities,omitempty"`
+}
+
+// Parse CVSS vector strings and extract the relevant attributes.
+func Calc(args []string) []CalcRow {
+ rows := make([]CalcRow, len(args))
+
+ for i, s := range(args) {
+ // parse vector
+ if vec, err := NewVector(s); err != nil {
+ // parse failed
+ rows[i] = CalcRow {
+ String: s,
+ Error: err.Error(),
+ }
+ } else {
+ // parse scores
+ if scores, err := vec.Scores(); err != nil {
+ // parse scores failed
+ rows[i] = CalcRow {
+ String: s,
+ Error: err.Error(),
+ Version: vec.Version().String(),
+ }
+ } else {
+ // parse scores succeeded, add to results
+ rows[i] = CalcRow {
+ String: s,
+ Version: vec.Version().String(),
+
+ Scores: CalcScores {
+ Base: scores.Base.Float(),
+ Temporal: scores.Temporal.Float(),
+ Env: scores.Env.Float(),
+ },
+
+ Severities: CalcSeverities {
+ Base: scores.Base.Severity().String(),
+ Temporal: scores.Temporal.Severity().String(),
+ Env: scores.Env.Severity().String(),
+ },
+ }
+ }
+ }
+ }
+
+ // return results
+ return rows
+}