-
Notifications
You must be signed in to change notification settings - Fork 0
/
govulners.go
254 lines (213 loc) · 4.85 KB
/
govulners.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package govulners
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
const (
apiURL = "https://vulners.com"
defaultLimit = 20
maxLimit = 10000
resultError = "error"
)
var (
defaultFields = []string{
"id",
"title",
"description",
"type",
"bulletinFamily",
"cvss",
"published",
"modified",
"lastseen",
"href",
"sourceHref",
"sourceData",
"cvelist",
}
)
type Package struct {
Software string
Version string
}
type Vulners struct {
apikey string
debug bool
logger Logger
http HTTPClient
}
func New(apikey string, opts ...Option) *Vulners {
options := Options{
logger: log.Default(),
httpClient: http.DefaultClient,
}
for _, opt := range opts {
opt(&options)
}
return &Vulners{
apikey: apikey,
logger: options.logger,
http: options.httpClient,
}
}
// SetDebug set debug flag
func (v *Vulners) SetDebug(debug bool) {
v.debug = debug
}
func (v *Vulners) do(request *http.Request, dst interface{}) error {
response, err := v.http.Do(request)
if err != nil {
return err
}
if response.Body != nil {
defer response.Body.Close()
}
if response.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("HTTP status: %d", response.StatusCode)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if v.debug {
v.logger.Println(string(body))
}
r := Response{}
err = json.Unmarshal(body, &r)
if err != nil {
return err
}
if r.Result == resultError {
return v.handleError(r)
}
if dst == nil {
return nil
}
return json.Unmarshal(r.Data, dst)
}
func (v *Vulners) handleError(r Response) error {
e := Error{}
err := json.Unmarshal(r.Data, &e)
if err != nil {
return err
}
return e
}
func (v *Vulners) get(path string, values url.Values, dst interface{}) error {
values.Add("apiKey", v.apikey)
request, err := http.NewRequest(http.MethodGet, apiURL+path+"?"+values.Encode(), nil)
if err != nil {
return err
}
if v.debug {
v.logger.Println(request.Method, request.URL.String())
}
return v.do(request, dst)
}
func (v *Vulners) post(path string, data map[string]interface{}, dst interface{}) error {
data["apiKey"] = v.apikey
body, err := json.Marshal(data)
if err != nil {
return err
}
request, err := http.NewRequest(http.MethodPost, apiURL+path, bytes.NewBuffer(body))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/json")
if v.debug {
v.logger.Println(request.Method, request.URL.String())
v.logger.Println(string(body))
}
return v.do(request, dst)
}
func (v *Vulners) search(query string, skip, size int, fields []string, dst interface{}) error {
return v.post("/api/v3/search/lucene/", map[string]interface{}{
"query": query,
"skip": skip,
"size": size,
"fields": fields,
}, dst)
}
// GetSoftwareVulnerabilities find software vulnerabilities using name and version.
func (v *Vulners) GetSoftwareVulnerabilities(name, version string) ([]CVE, error) {
response := SearchResponse{}
err := v.post("/api/v3/burp/software/", map[string]interface{}{
"type": "software",
"software": name,
"version": version,
}, &response)
if err != nil {
return nil, err
}
cves := make([]CVE, len(response.Search))
for i := range response.Search {
cves[i] = response.Search[i].Source
}
return cves, nil
}
// GetCPEVulnerabilities find software vulnerabilities using CPE string.
// See CPE references at https://cpe.mitre.org/specification/
func (v *Vulners) GetCPEVulnerabilities(cpe string) ([]CVE, error) {
version, err := getCPEversion(cpe)
if err != nil {
return nil, err
}
response := SearchResponse{}
err = v.post("/api/v3/burp/software/", map[string]interface{}{
"type": "cpe",
"software": cpe,
"version": version,
}, &response)
if err != nil {
return nil, err
}
cves := make([]CVE, len(response.Search))
for i := range response.Search {
cves[i] = response.Search[i].Source
}
return cves, nil
}
// GetMultipleBulletins fetch multiple bulletins by ids.
func (v *Vulners) GetMultipleBulletins(ids []string, fields []string) (map[string]CVE, error) {
if len(fields) == 0 {
fields = defaultFields
}
response := CVEsResponse{}
err := v.post("/api/v3/search/id/", map[string]interface{}{
"id": ids,
"fields": fields,
}, &response)
if err != nil {
return nil, err
}
return response.Documents, nil
}
// GetBulletin fetch bulletin by id.
func (v *Vulners) GetBulletin(id string, fields []string) (*CVE, error) {
cves, err := v.GetMultipleBulletins([]string{id}, fields)
if err != nil {
return nil, err
}
if cve, ok := cves[id]; ok {
return &cve, nil
}
return nil, nil
}
func getCPEversion(cpe string) (string, error) {
index := 4
if strings.HasPrefix(cpe, "cpe:2.3") {
index = 5
}
splits := strings.Split(cpe, ":")
if len(splits) <= index {
return "", fmt.Errorf("Malformed cpe: %s", cpe)
}
return splits[index], nil
}