-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
133 lines (124 loc) · 2.98 KB
/
controller.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
"strings"
"text/tabwriter"
"github.com/alecthomas/template"
"github.com/jweir/csv"
"github.com/kopolindo/cve-scraper/db"
)
func Printbanner() {
tmpl := template.New("banner")
template.Must(tmpl.Parse(banner))
_ = tmpl.Execute(os.Stderr, ActualVersion)
}
func Usage() {
Printbanner()
fmt.Println("\nUsage flags:")
flag.PrintDefaults()
return
}
func Init() {
flag.Parse()
if flag.NFlag() == 0 {
Usage()
os.Exit(0)
}
if *version {
Printbanner()
}
if *fields != "" {
if *fields != "help" {
fieldsToPrint = ValidateFields()
}
if *fields == "help" {
fmt.Println("Cve\t\tCVE Identification (CVE-YYYY-ID)")
fmt.Println("CveDesc\t\tVulnerability Description")
fmt.Println("Cwe\t\tCommon Weakness Enumeration ID")
fmt.Println("CweDesc\t\tCWE Description (type of vulnerability)")
fmt.Println("Exploit\t\tExistence of known exploits")
fmt.Println("Refs\t\tReferences")
fmt.Println("Cvssv2\t\tCommon Vulnerability Scoring System Vector (v2)")
fmt.Println("Cvssv3\t\tCommon Vulnerability Scoring System Vector (v3)")
fmt.Println("Software\tSoftware name")
fmt.Println("Version\t\tSoftware version")
os.Exit(0)
}
}
if *fields == "" {
fieldsToPrint = strings.Split("Cve,CveDesc,Cwe,CweDesc,Exploit,Refs,Cvssv2,Cvssv3,Software,Version", ",")
}
if *write != "" {
if _, err := os.Stat(*write); err == nil {
fmt.Println("This file exists")
os.Exit(1)
}
}
}
func ValidateFields() []string {
var out []string
checkArray := strings.Split(*fields, ",")
for _, toCheck := range checkArray {
for _, allowed := range AllowedFields {
if allowed == toCheck {
out = append(out, toCheck)
}
}
}
return out
}
func ValidateCve() []string {
var out []string
for _, cves := range strings.Split(*cve, ",") {
cveRegexp := regexp.MustCompile("[0-9]+")
regexpArray := cveRegexp.FindAllString(cves, -1)
cves = strings.Join(regexpArray, "")
if len(cves) < 5 {
fmt.Println("Please, insert at least year (YYYY) and ID number")
Usage()
os.Exit(71)
}
id := strings.TrimLeft(cves[4:], "0")
cves := strings.Join([]string{"CVE", cves[0:4], id}, "-")
out = append(out, cves)
}
return out
}
func PrintResults(resArray []db.Results, fields []string) {
const padding = 2
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.Debug)
if *header {
for _, f := range fields {
fmt.Fprint(w, f, "\t")
}
}
w.Flush()
for i, res := range resArray {
r := reflect.ValueOf(res)
for _, f := range fields {
fmt.Fprint(w, reflect.Indirect(r).FieldByName(f), "\t")
}
if i != 0 {
fmt.Println()
}
w.Flush()
}
}
func WriteCsv(resArray []db.Results) {
if *write != "" {
csvout, err := csv.Marshal(resArray)
if err != nil {
fmt.Println("Error durin marshalling [", err, "]")
}
//fmt.Println(string(*write))
errWrite := ioutil.WriteFile(*write, csvout, 0644)
if errWrite != nil {
fmt.Println("Error during writing to file [", err, "]")
}
}
}