-
Notifications
You must be signed in to change notification settings - Fork 14
/
PluginVulnerability.go
152 lines (143 loc) · 4.17 KB
/
PluginVulnerability.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package wpfinger
import (
"encoding/json"
"errors"
"github.com/hashicorp/go-version"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"gorm.io/gorm"
"net/http"
"strings"
)
type WordfenceResponse map[string]WordfenceVulnerability
type WordfenceVulnerability struct {
Id string `json:"id"`
Software []struct {
Type string `json:"type"`
Name string `json:"name"`
Slug string `json:"slug"`
AffectedVersions map[string]struct {
FromVersion string `json:"from_version"`
FromInclusive bool `json:"from_inclusive"`
ToVersion string `json:"to_version"`
ToInclusive bool `json:"to_inclusive"`
} `json:"affected_versions"`
} `json:"software"`
CVSS struct {
Vector string `json:"vector"`
Score float64 `json:"score"`
Rating string `json:"rating"`
}
CWE struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
CVE string
}
func UpdateWordfenceDB(db *gorm.DB) {
setupPogressBar()
req, err := http.NewRequest(http.MethodGet, "https://www.wordfence.com/api/intelligence/v2/vulnerabilities/production", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
panic(errors.New("not found"))
}
var wfResponse WordfenceResponse
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&wfResponse)
if err != nil {
panic(err)
}
for _, vulnerability := range wfResponse {
vulnBar.ChangeMax(len(wfResponse))
vulnBar.Describe("[cyan][2/2][reset] Saving vulnerabilities in database")
for _, software := range vulnerability.Software {
for affectedVersionLabel, affectedVersion := range software.AffectedVersions {
affectedVersion.FromVersion = strings.ReplaceAll(affectedVersion.FromVersion, "*", "0.0.0")
affectedVersion.FromVersion = strings.ReplaceAll(affectedVersion.FromVersion, "*", "999999.0.0")
vulnId := vulnerability.CVE
if len(vulnId) < 1 {
continue
}
vuln := Vulnerability{
Slug: software.Slug,
AffectedVersion: affectedVersionLabel,
Type: software.Type,
Id: vulnerability.CVE,
FromVersion: affectedVersion.FromVersion,
FromInclusive: affectedVersion.FromInclusive,
ToVersion: affectedVersion.ToVersion,
ToInclusive: affectedVersion.ToInclusive,
Severity: strings.ToLower(vulnerability.CVSS.Rating),
}
tx := db.Save(&vuln)
if tx.Error != nil {
panic(err)
}
}
}
vulnBar.Add(1)
}
}
var vulnBar *progressbar.ProgressBar
func setupPogressBar() {
vulnBar = progressbar.NewOptions(1,
progressbar.OptionClearOnFinish(),
progressbar.OptionSetWriter(ansi.NewAnsiStderr()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowIts(),
progressbar.OptionSetDescription("[cyan][1/2][reset] Downloading database from Wordfence..."),
progressbar.OptionShowDescriptionAtLineEnd(),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
vulnBar.RenderBlank()
}
type Vulnerability struct {
Slug string `gorm:"primaryKey,index"`
AffectedVersion string `gorm:"primaryKey"`
Type string `gorm:"primaryKey,index"`
Id string
FromVersion string
FromInclusive bool
ToVersion string
ToInclusive bool
Severity string
}
func (v Vulnerability) Check(checkVersion string) (bool, error) {
pVersion, err := version.NewVersion(checkVersion)
if err != nil {
return false, err
}
fromVersion, err := version.NewVersion(v.FromVersion)
if err != nil {
return false, err
}
toVersion, err := version.NewVersion(v.ToVersion)
if err != nil {
return false, err
}
if !v.FromInclusive && pVersion.LessThan(fromVersion) {
return false, nil
}
if v.FromInclusive && pVersion.LessThanOrEqual(fromVersion) {
return false, nil
}
if v.ToInclusive && pVersion.GreaterThan(toVersion) {
return false, nil
}
if !v.ToInclusive && pVersion.GreaterThanOrEqual(toVersion) {
return false, nil
}
return true, nil
}