-
Notifications
You must be signed in to change notification settings - Fork 42
/
Queryer.go
465 lines (412 loc) · 11.5 KB
/
Queryer.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package cvequeryops
import (
"context"
"encoding/json"
"fmt"
"github.com/jinzhu/gorm"
"github.com/yaklang/yaklang/common/cve/cveresources"
"github.com/yaklang/yaklang/common/log"
"regexp"
"strconv"
"strings"
"time"
)
type CVEOption func(info *CVEQueryInfo)
type CVEQueryInfo struct {
CVE string
CPE []cveresources.CPE
CWE []string
Vendors []string
Products []string
Severity []string
ExploitScore float64
After time.Time
Before time.Time
Start int
Quantity int
OrderBy string
Desc bool
Strict bool
}
func QueryCVEYields(db *gorm.DB, opts ...CVEOption) chan *cveresources.CVE {
db, queryConfig := Filter(db, opts...)
if queryConfig != nil && len(queryConfig.CPE) > 0 {
ch := make(chan *cveresources.CVE)
go func() {
defer close(ch)
for c := range cveresources.YieldCVEs(db, context.Background()) {
var level = 0.0
var config cveresources.Configurations
err := json.Unmarshal(c.CPEConfigurations, &config)
if err != nil {
continue
}
for _, node := range config.Nodes {
if level < node.Result(queryConfig.CPE) {
level = node.Result(queryConfig.CPE)
}
}
if level > 0 {
ch <- c
}
}
}()
return ch
}
return cveresources.YieldCVEs(db, context.Background())
}
func Filter(db *gorm.DB, opts ...CVEOption) (*gorm.DB, *CVEQueryInfo) {
if len(opts) <= 0 {
return db, &CVEQueryInfo{}
}
cveQuery := &CVEQueryInfo{}
cveQuery.Strict = false
for _, opt := range opts {
opt(cveQuery)
}
if (len(cveQuery.Products) > 0 || len(cveQuery.CPE) > 0) && !cveQuery.Strict {
cveQuery = FixCVEProduct(cveQuery, db)
}
var sqlSentence string
var param []interface{}
sqlSentence, param = MakeSqlSentence(cveQuery)
db = db.Where(sqlSentence, param...)
if cveQuery.Quantity != 0 {
if cveQuery.OrderBy != "" {
if cveQuery.Desc {
db = db.Offset(cveQuery.Start).Order(cveQuery.OrderBy + "desc").Limit(cveQuery.Quantity)
} else {
db = db.Offset(cveQuery.Start).Order(cveQuery.OrderBy).Limit(cveQuery.Quantity)
}
} else {
db = db.Offset(cveQuery.Start).Limit(cveQuery.Quantity)
}
}
return db, cveQuery
}
func FixCVEProduct(cveQuery *CVEQueryInfo, db *gorm.DB) *CVEQueryInfo {
var fixName []string
var fixCPE []cveresources.CPE
for i := 0; i < len(cveQuery.Products); i++ {
if info, ok := cveresources.CommonFix[cveQuery.Products[i]]; ok { //查询基础修复里有没有对应的畸形名
if info.Vendor != "" {
fixCPE = append(fixCPE, cveresources.CPE{
Part: "*",
Vendor: info.Vendor,
Product: info.ProductName,
Version: "*",
Edition: "*",
})
}
fixName = append(fixName, cveresources.CommonFix[cveQuery.Products[i]].ProductName)
continue
}
fixRes, err := cveresources.FixProductName(cveQuery.Products[i], db) //尝试通用方法修复
if err != nil {
log.Warningf("find product name failed: %s[%s]", err, cveQuery.Products[i])
} else { //修复好的所有产品名放入查询条件里
fixName = append(fixName, fixRes...)
}
}
if fixName != nil {
cveQuery.Products = fixName
}
for i := 0; i < len(cveQuery.CPE); i++ {
if info, ok := cveresources.CommonFix[cveQuery.CPE[i].Product]; ok { //查询基础修复里有没有对应的畸形名
vendorStr := cveQuery.CPE[i].Vendor
if info.Vendor != "" {
vendorStr = info.Vendor
}
fixCPE = append(fixCPE, cveresources.CPE{
Part: cveQuery.CPE[i].Part,
Vendor: vendorStr,
Product: info.ProductName,
Version: cveQuery.CPE[i].Version,
Edition: cveQuery.CPE[i].Edition,
})
continue
}
fixRes, err := cveresources.FixProductName(cveQuery.CPE[i].Product, db)
if err != nil {
log.Warningf("find product name failed: %s[%s]", err, cveQuery.CPE[i].Product)
} else {
//修复后所有可能的产品名放入CPE中
for _, name := range fixRes {
cpeItem := cveresources.CPE{
Part: cveQuery.CPE[i].Part,
Vendor: cveQuery.CPE[i].Vendor,
Product: name,
Version: cveQuery.CPE[i].Version,
Edition: cveQuery.CPE[i].Edition,
}
fixCPE = append(fixCPE, cpeItem)
}
}
}
if fixCPE != nil {
cveQuery.CPE = fixCPE
}
return cveQuery
}
func CVE(id string) CVEOption {
return func(info *CVEQueryInfo) {
info.CVE = id
}
}
func CWE(cwe string) CVEOption {
return func(info *CVEQueryInfo) {
info.CWE = append(info.CWE, cwe)
}
}
func After(year int, data ...int) CVEOption {
dataStringFormat := "%d-%02d-%02d"
dataString := ""
if len(data) == 2 && data[0] > 0 && data[0] <= 12 && data[1] > 0 && data[1] <= 31 {
dataString = fmt.Sprintf(dataStringFormat, year, data[0], data[1])
} else if len(data) == 1 && data[0] > 0 && data[0] <= 12 {
dataString = fmt.Sprintf(dataStringFormat, year, data[0], 1)
} else if len(data) == 0 {
dataString = fmt.Sprintf(dataStringFormat, year, 1, 1)
} else {
log.Error("time args error:", data)
panic("time args error")
}
afterTime, err := time.Parse("2006-01-02", dataString)
if err != nil {
log.Error(err)
panic(err)
}
return func(info *CVEQueryInfo) {
info.After = afterTime
}
}
func AfterByTimeStamp(timeStamp int64) CVEOption {
AfterTime := time.Unix(timeStamp, 0)
return func(info *CVEQueryInfo) {
info.After = AfterTime
}
}
func Before(year int, data ...int) CVEOption {
dataStringFormat := "%d-%02d-%02d"
dataString := ""
if len(data) == 2 && data[0] > 0 && data[0] <= 12 && data[1] > 0 && data[1] <= 31 {
dataString = fmt.Sprintf(dataStringFormat, year, data[0], data[1])
} else if len(data) == 1 && data[0] > 0 && data[0] <= 12 {
dataString = fmt.Sprintf(dataStringFormat, year, data[0], 1)
} else if len(data) == 0 {
dataString = fmt.Sprintf(dataStringFormat, year, 1, 1)
} else {
log.Error("time args error:", data)
panic("time args error")
}
beforeTime, err := time.Parse("2006-01-02", dataString)
if err != nil {
log.Error(err)
panic(err)
}
return func(info *CVEQueryInfo) {
info.Before = beforeTime
}
}
func BeforeByTimeStamp(timeStamp int64) CVEOption {
beforeTime := time.Unix(timeStamp, 0)
return func(info *CVEQueryInfo) {
info.Before = beforeTime
}
}
func Score(score float64) CVEOption {
return func(info *CVEQueryInfo) {
info.ExploitScore = score
}
}
func Severity(level string) CVEOption {
formatLevel := strings.ToUpper(level)
if strings.Contains(formatLevel, "HIGH") || strings.Contains(formatLevel, "HIG") {
level = "HIGH"
} else if strings.Contains(formatLevel, "MEDIUM") || strings.Contains(formatLevel, "MID") {
level = "MEDIUM"
} else if strings.Contains(formatLevel, "LOW") {
level = "LOW"
} else {
log.Error("Unknown Severity level")
panic("Unknown Severity level")
}
return func(info *CVEQueryInfo) {
info.Severity = append(info.Severity, level)
}
}
func Vendor(v string) CVEOption {
return func(info *CVEQueryInfo) {
info.Vendors = append(info.Vendors, v)
}
}
func Product(p string) CVEOption {
return func(info *CVEQueryInfo) {
info.Products = append(info.Products, p)
}
}
func ProductWithVersion(p string, v ...string) CVEOption {
if len(v) == 0 {
return Product(p)
} else if len(v) == 1 {
return func(info *CVEQueryInfo) {
info.Products = append(info.Products, p)
info.CPE = append(info.CPE, cveresources.CPE{
Part: "*",
Vendor: "*",
Product: p,
Version: v[0],
Edition: "",
})
}
} else {
log.Error("The number of parameters does not match")
panic("The number of parameters does not match")
}
}
func CPE(c string) CVEOption {
return func(info *CVEQueryInfo) {
rule, err := regexp.Compile("\\[(\\d+)-(\\d+)]")
if err != nil {
log.Error(err)
panic(err)
}
cpeStruct, err := cveresources.ParseToCPE(c)
if err != nil {
log.Error(err)
panic(err)
}
//var cpeInfo cmd.CPE
if rule.MatchString(cpeStruct.Version) {
scope := rule.FindSubmatch([]byte(cpeStruct.Version))
start, err := strconv.Atoi(string(scope[len(scope)-2]))
if err != nil {
log.Error(err)
panic(err)
}
end, err := strconv.Atoi(string(scope[len(scope)-1]))
if err != nil {
log.Error(err)
panic(err)
}
for i := start; i <= end; i++ {
info.CPE = append(info.CPE, cveresources.CPE{
Part: cpeStruct.Part,
Vendor: cpeStruct.Vendor,
Product: cpeStruct.Product,
Version: strings.Replace(cpeStruct.Version, string(scope[0]), strconv.Itoa(i), 1),
Edition: cpeStruct.Edition,
})
}
//cpeInfo = *cpeStruct
}
}
}
func Limit(quantity int) CVEOption {
return func(info *CVEQueryInfo) {
info.Quantity = quantity
}
}
func Offset(start int) CVEOption {
return func(info *CVEQueryInfo) {
info.Start = start
}
}
func OrderBy(name string) CVEOption {
return func(info *CVEQueryInfo) {
info.OrderBy = name
}
}
func Desc(flag bool) CVEOption {
return func(info *CVEQueryInfo) {
info.Desc = flag
}
}
func Strict(flag bool) CVEOption {
return func(info *CVEQueryInfo) {
info.Strict = flag
}
}
func MakeSqlSentence(info *CVEQueryInfo) (string, []interface{}) {
zeroTime := time.Time{}
var SqlSentences []string
var param []interface{}
if info.CVE != "" {
SqlSentences = append(SqlSentences, " cve = ? ")
param = append(param, info.CVE)
}
if len(info.CPE) > 0 {
for _, cpe := range info.CPE {
if cpe.Vendor != "*" {
info.Vendors = append(info.Vendors, cpe.Vendor)
}
if cpe.Product != "*" {
info.Products = append(info.Products, cpe.Product)
}
}
}
if len(info.Vendors) > 0 || len(info.Products) > 0 {
info.Vendors = cveresources.Set(info.Vendors)
info.Products = cveresources.Set(info.Products)
clause := " ("
//构造Vendor和Product查询子句
var inSideSql []string
for _, vendor := range info.Vendors {
inSideSql = append(inSideSql, " vendor LIKE ? ")
param = append(param, "%,"+vendor)
inSideSql = append(inSideSql, " vendor LIKE ? ")
param = append(param, "%,"+vendor+",%")
inSideSql = append(inSideSql, " vendor LIKE ? ")
param = append(param, vendor+",%")
inSideSql = append(inSideSql, " vendor = ? ")
param = append(param, vendor)
}
for _, product := range info.Products {
inSideSql = append(inSideSql, " product LIKE ? ")
param = append(param, "%,"+product)
inSideSql = append(inSideSql, " product LIKE ? ")
param = append(param, "%,"+product+",%")
inSideSql = append(inSideSql, " product LIKE ? ")
param = append(param, product+",%")
inSideSql = append(inSideSql, " product = ? ")
param = append(param, product)
}
clause += strings.Join(inSideSql, "OR")
clause += ") "
SqlSentences = append(SqlSentences, clause)
}
if len(info.CWE) > 0 {
for i := 0; i < len(info.CWE); i++ {
SqlSentences = append(SqlSentences, " cwe LIKE ? ")
param = append(param, "%"+info.CWE[i]+"%")
}
}
if len(info.Severity) > 0 {
clause := " ("
//构造Vendor和Product查询子句
var inSideSql []string
for _, level := range info.Severity {
inSideSql = append(inSideSql, " severity == ? ")
param = append(param, level)
}
clause += strings.Join(inSideSql, "OR")
clause += ") "
SqlSentences = append(SqlSentences, clause)
}
if info.ExploitScore > 0 {
//ScoreStr := fmt.Sprintf("%2.1f", info.ExploitScore)
//SqlSentences = append(SqlSentences, " base_cvs_sv2_score >= "+ScoreStr+" ")
SqlSentences = append(SqlSentences, " base_cvs_sv2_score >= ? ")
param = append(param, info.ExploitScore)
}
if info.Before != zeroTime {
SqlSentences = append(SqlSentences, " published_date < ? ")
param = append(param, info.Before)
}
if info.After != zeroTime {
SqlSentences = append(SqlSentences, " published_date > ? ")
param = append(param, info.After)
}
SqlSentence := strings.Join(SqlSentences, "AND")
return SqlSentence, param
}