-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvecase.go
1742 lines (1681 loc) · 52 KB
/
cvecase.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package alertmanager
import (
"encoding/xml"
"github.com/huandu/go-sqlbuilder"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"regexp"
"strconv"
"strings"
)
const Null = "\xff"
// Model for a CVE Case
//
// swagger:model CveCase
type CveCase struct {
// The case ID of the case
//
// example: 1
CaseId string `db:"id"`
// The source of the case
//
// example: CERT
Source string `db:"source"`
// The RefNum of the case
//
// example: CB-K20/0056 Update 3
RefNum string `db:"refnum"`
// The original RefNum of the case
//
// example: CB-K20/0056
OriginalRefNum string `db:"orig_refnum"`
// The status of the case
//
// example: ASSIGNED
Status string `db:"status"`
// The creation date of the case
//
// example: 2020-01-29 13:04:15
DateCreated string `db:"date_created"`
// The date of receiving the case
//
// example: 2020-03-19 08:27:03
DateReceived string `db:"date_received"`
// The risk of the case
//
// example: 2
Risk string `db:"risk"`
// The subject of the case
//
// example: [CERT-Bund] CB-K20/0056 Update 3 - Google Chrome: Mehrere Schwachstellen
Subject string `db:"subject"`
// The verified status of the case
//
// example: 1
Verified string `db:"verified"`
// The consignor of the case
//
// example: [email protected]
MsgFrom string `db:"msg_from"`
// The consignee of the case
//
// example: [email protected]
MsgTo string `db:"msg_to"`
// The description of the case
//
// example: Ein entfernter, anonymer Angreifer kann mehrere Schwachstellen in Google Chrome ausnutzen, um einen nicht näher spezifizierten Angriff durchzuführen oder um Sicherheitsmechanismen zu umgehen.
Description string
RawData string `json:"-"`
// The CVE nums of the case
//
// example: ["CVE-2020-0601","CVE-2020-6378","CVE-2020-6379","CVE-2020-6380"]
CveNumList []string
// The categories of the case
//
// example: ["Anwendung/Clients/Browser/Chrome","Anwendung/Clients/Browser","Anwendung/Clients/Browser/Chrome","Anwendung/Clients/Browser/Chrome","Betriebssystem/Linux_Unix/RedHat"],"PlatformList":["Linux","MacOS X","UNIX","Windows"]
CategoryList []string
// The platforms of the case
//
// example: ["Linux","MacOS X","UNIX","Windows"]
PlatformList []string
// The softwares of the case
//
// example: ["Google Chrome 79.0.3945.130","Google Chrome for Linux 79.0.3945.130","Google Chrome for Mac 79.0.3945.130","Open Source Arch Linux","Red Hat Enterprise Linux","Microsoft Edge (Chromium-based)"]
SoftwareList []string
// The update list of the case
//
// example: "CB-K20/0063 Update 2","CB-K20/0063 Update 3","CB-K20/0063 Update 4","CB-K20/0063 Update 5"]
UpdateList []string
}
// CveAdvisory ...
//
// structure to parse the CERT-BUND XML data
type CveAdvisory struct {
XMLName xml.Name `xml:"Advisory"`
Risk string `xml:"Risk"`
CveList cveList `xml:"CVEList"`
Date string `xml:"Date"`
CategoryList []string `xml:"CategoryTree"`
RefNum string `xml:"Ref_Num"`
Platform string `xml:"Platform"`
Software string `xml:"Software"`
}
type cveList struct {
CveNum []string `xml:"CVE"`
}
// Model of a comment
//
// swagger:model CveCaseComment
type CveCaseComment struct {
// The ID of the comment
//
// example: 1
CommentId string `json:"id" db:"id"`
// The RefNum which the comment is specified to
//
// example: CB-K20/0056
RefNum string `json:"refnum" db:"refnum"`
// The Status of the CVE case
//
// example: ASSIGNED
NewStatus string `json:"new_status" db:"new_status"`
// The date on which the comment was created
//
// example: 2020-03-19 08:27:05
DateCreate string `json:"date_created" db:"date_created"`
// The date on which the comment was last updated
//
// example: 2020-03-19 08:27:05
DateUpdated string `json:"date_updated" db:"date_updated"`
// The Username of the comment creator
//
// example: Unkn0wn User
Username string `json:"username" db:"username"`
// The comment itself
//
// example: Working on issue
Comment string `json:"comment" db:"comment"`
// The filter id the comment is assigned to
//
// example: 3
FilterId string `json:"filter_id" db:"filter_id"`
}
// Model for searching CVE Cases
//
// swagger:model CveCaseSearchCriteria
type CveCaseSearchCriteria struct {
// The filter which the case is assigned to
//
// example: 1
FilterId string `json:"filterId"`
// The case ID of the case
//
// example: 1
CaseId string `json:"caseId"`
// The source of the case
//
// example: CERT
Source string `json:"source"`
// The RefNum of the case
//
// example: CB-K20/0056 Update 3
RefNum string `json:"refNum"`
// The status of the case
//
// example: ASSIGNED
Status string `json:"status"`
// The subject of the case
//
// example: [CERT-Bund] CB-K20/0056 Update 3 - Google Chrome: Mehrere Schwachstellen
Subject string `json:"subject"`
// The category of the case
//
// example: Anwendung/Clients/Browser/Chrome
Category string `json:"category"`
// The smallest risk of cases which should be shown
//
// example: 4
Risk string `json:"risk"`
// The platform of the case
//
// example: Linux
Platform string `json:"platform"`
// The software of the case
//
// example: Google Chrome 79.0.3945.130
Software string `json:"software"`
// The date of which the listing should start
//
// example: 2020-04-09
FirstDate string `json:"firstDate"`
// The date of which the listing should end
//
// example: 2020-04-14
LastDate string `json:"lastDate"`
// The offset to the first entry shown. Only works if amount is specified
//
// example: 134
Offset string `json:"offset"`
// The amount of entries shown
//
// example: 50
Amount string `json:"amount"`
}
// Model for a CVE Case Search Result
//
// swagger:model CveCaseSearchResult
type CveCaseSearchResult struct {
// The total amount of results
//
// example: 102
Total string `json:"total"`
// The amount of currently shown results
//
// example: 50
CurrentlyShown string `json:"currentlyShown"`
// The results as an array of CVE cases
Data []CveCase `json:"data"`
}
// Model for a filter
//
// swagger:model Filter
type Filter struct {
// The ID of the filter
//
// example: 1
Id string `db:"id" json:"id"`
// The filter name
//
// example: Linux
Filter string `db:"filter" json:"filter"`
// The default risk level filter of the filter
//
// example: 1
RiskLevel string `db:"risk_level" json:"risk_level"`
// The Keywords of the filter.
//
// example: Citrix; Cisco
Keywords string `db:"keywords" json:"keywords"`
}
// Model for a filter category
//
// swagger:model FilterCategory
type FilterCategory struct {
// The ID of the filter category
//
// example: 1
Id string `db:"id" json:"id"`
// The ID of the filter the filter category is assigned to
//
// example: 1
FilterId string `db:"filter_id" json:"filter_id"`
// The category of the Filter
//
// example: Anwendung
Category string `db:"category" json:"category"`
}
// Model for a category
//
// swagger:model Category
type Category struct {
// The category name
//
// example: Anwendung
Category string `db:"category" json:"category"`
}
type categoriesConfig struct {
Categories string `yaml:"categories"`
}
// Model for a platform
//
// swagger:model Platform
type Platform struct {
// The platform name
//
// example: Linux
Platform string `db:"platform" json:"platform"`
}
// Model for exporting filter_config.yaml
//
// swagger:model FilterConfig
type filterConfig struct {
// List of all filters
//
// example:filters: 1, Linux; 2, Windows; 3, Network; 4, Other
Filters string `yaml:"filters"`
// List of all filter categories
//
// example: filter_categories: |\n 1, Anwendung;\n 2, Anwendung;\n...
FilterCategories string `yaml:"filter_categories"`
// List of all excluded filter categories
//
// example: filter_categories: |\n 1, Anwendung;\n 2, Anwendung;\n...
ExcludedFilterCategories string `yaml:"excluded_filter_categories"`
}
var sqlSchemaArr = []string{
`DROP TABLE IF EXISTS case_comments;`,
`DROP TABLE IF EXISTS case_rawdatas;`,
`DROP TABLE IF EXISTS case_categories;`,
`DROP TABLE IF EXISTS case_cvenums;`,
`DROP TABLE IF EXISTS case_platforms;`,
`DROP TABLE IF EXISTS case_softwares;`,
`DROP TABLE IF EXISTS case_descriptions;`,
`DROP TABLE IF EXISTS case_status;`,
`DROP TABLE IF EXISTS cases;`,
`DROP TABLE IF EXISTS filter_categories;`,
`DROP TABLE IF EXISTS filter_excluded_categories;`,
`DROP TABLE IF EXISTS filters;`,
`DROP TABLE IF EXISTS categories;`,
`CREATE TABLE cases (
id int(11) NOT NULL,
source enum('CERT','NIST') NOT NULL DEFAULT 'CERT',
refnum varchar(255) NOT NULL,
orig_refnum varchar(255) NOT NULL,
date_created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_received datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
risk int(11) NOT NULL DEFAULT 3,
subject varchar(255) DEFAULT NULL,
verified tinyint(1) NOT NULL DEFAULT 0,
msg_from varchar(255) DEFAULT NULL,
msg_to varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_rawdatas (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
rawdata blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_categories (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
category varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_cvenums (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
cvenum varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_platforms (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
platform varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_softwares (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
software varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_comments (
id int(11) NOT NULL,
refnum varchar(255) NOT NULL,
filter_id int(11) NOT NULL,
date_created datetime NOT NULL DEFAULT current_timestamp(),
date_updated datetime DEFAULT current_timestamp(),
new_status enum('NEW','ASSIGNED','CLOSED') DEFAULT NULL,
username varchar(255) DEFAULT NULL,
comment text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_descriptions (
id int(11) NOT NULL,
case_id int(11) NOT NULL,
description text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE case_status (
id int(11) NOT NULL,
refnum varchar(255) NOT NULL,
filter_id int(11) NOT NULL,
status enum('NEW','ASSIGNED','CLOSED','') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE filters (
id int(11) NOT NULL,
filter varchar(255) NOT NULL,
risk_level int(11) DEFAULT 1,
keywords varchar(255) DEFAULT ""
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE filter_categories (
id int(11) NOT NULL,
filter_id int(11) NOT NULL,
category varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE filter_excluded_categories (
id int(11) NOT NULL,
filter_id int(11) NOT NULL,
category varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`CREATE TABLE categories (
id int(11) NOT NULL,
category varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`,
`ALTER TABLE cases ADD PRIMARY KEY (id), ADD UNIQUE KEY source_refnum (source,refnum), ADD KEY orig_refnum (orig_refnum);`,
`ALTER TABLE case_rawdatas ADD PRIMARY KEY (id), ADD KEY case_id (case_id);`,
`ALTER TABLE case_categories ADD PRIMARY KEY (id), ADD KEY case_id (case_id), ADD KEY category (category);`,
`ALTER TABLE case_cvenums ADD PRIMARY KEY (id), ADD KEY case_id (case_id), ADD KEY cvenum (cvenum);`,
`ALTER TABLE case_platforms ADD PRIMARY KEY (id), ADD KEY case_id (case_id), ADD KEY platform (platform);`,
`ALTER TABLE case_softwares ADD PRIMARY KEY (id), ADD KEY case_id (case_id), ADD KEY software (software);`,
`ALTER TABLE case_comments ADD PRIMARY KEY (id), ADD KEY refnum (refnum), ADD KEY filter_id (filter_id);`,
`ALTER TABLE case_descriptions ADD PRIMARY KEY (id), ADD KEY case_id (case_id);`,
`ALTER TABLE case_status ADD PRIMARY KEY (id), ADD KEY refnum (refnum), ADD KEY filter_id (filter_id);`,
`ALTER TABLE filters ADD PRIMARY KEY (id), ADD UNIQUE KEY filter (filter), ADD KEY risk_level (risk_level);`,
`ALTER TABLE filter_categories ADD PRIMARY KEY (id);`,
`ALTER TABLE filter_excluded_categories ADD PRIMARY KEY (id);`,
`ALTER TABLE categories ADD PRIMARY KEY (id), ADD KEY category (category);`,
`ALTER TABLE cases MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_rawdatas MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_categories MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_cvenums MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_platforms MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_softwares MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_comments MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_descriptions MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_status MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE filters MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE filter_categories MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE filter_excluded_categories MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE categories MODIFY id int(11) NOT NULL AUTO_INCREMENT;`,
`ALTER TABLE case_categories ADD CONSTRAINT case_categories_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE case_rawdatas ADD CONSTRAINT case_rawdatas_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE case_cvenums ADD CONSTRAINT case_cvenums_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE case_platforms ADD CONSTRAINT case_platforms_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE case_softwares ADD CONSTRAINT case_softwares_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE case_comments ADD CONSTRAINT case_comments_ibfk_1 FOREIGN KEY (refnum) REFERENCES cases (orig_refnum);`,
`ALTER TABLE case_comments ADD CONSTRAINT case_comments_ibfk_2 FOREIGN KEY (filter_id) REFERENCES filters (id);`,
`ALTER TABLE case_status ADD CONSTRAINT case_status_ibfk_1 FOREIGN KEY (refnum) REFERENCES cases (orig_refnum);`,
`ALTER TABLE case_status ADD CONSTRAINT case_status_ibfk_2 FOREIGN KEY (filter_id) REFERENCES filters (id);`,
`ALTER TABLE case_descriptions ADD CONSTRAINT case_descriptions_ibfk_1 FOREIGN KEY (case_id) REFERENCES cases (id);`,
`ALTER TABLE filter_categories ADD CONSTRAINT filter_categories_ibfk_1 FOREIGN KEY (filter_id) REFERENCES filters (id);`,
`ALTER TABLE filter_categories ADD CONSTRAINT filter_categories_ibfk_2 FOREIGN KEY (category) REFERENCES categories (category);`,
`ALTER TABLE filter_excluded_categories ADD CONSTRAINT filter_excluded_categories_ibfk_1 FOREIGN KEY (filter_id) REFERENCES filters (id);`,
}
// InitDB initializes the DB.
func InitDB(db *sqlx.DB) error {
for i := 0; i < len(sqlSchemaArr); i++ {
_, err := db.Exec(sqlSchemaArr[i])
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not set up database schema - query:" + sqlSchemaArr[i])
return err
}
}
err := ImportCategories(db)
if err != nil {
log.Error().
AnErr("Error", err)
return err
}
err = ImportFilterFromConfig(db)
if err != nil {
log.Error().
AnErr("Error", err)
return err
}
return nil
}
// WriteToDB writes a case to the DB.
func (c *CveCase) WriteToDB(db *sqlx.DB) error {
tx := db.MustBegin()
dbresult, err := tx.Exec(tx.Rebind("INSERT INTO cases (source, refnum, orig_refnum, date_created, risk, subject, verified, msg_from, msg_to) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"), c.Source, c.RefNum, c.OriginalRefNum, c.DateCreated, c.Risk, c.Subject, c.Verified, c.MsgFrom, c.MsgTo)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
dblastid, err := dbresult.LastInsertId()
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not get last insert id")
return err
}
log.Debug().
Msg("case inserted into database - ID: " + string(dblastid))
_, err = tx.Exec(tx.Rebind("INSERT INTO case_rawdatas (case_id, rawdata) VALUES (?, ?)"), dblastid, c.RawData)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
_, err = tx.Exec(tx.Rebind("INSERT INTO case_descriptions (case_id, description) VALUES (?, ?)"), dblastid, c.Description)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
for i := 0; i < len(c.CategoryList); i++ {
_, err := tx.Exec(tx.Rebind("INSERT INTO case_categories (case_id, category) VALUES (?, ?)"), dblastid, c.CategoryList[i])
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
for i := 0; i < len(c.CveNumList); i++ {
_, err := tx.Exec(tx.Rebind("INSERT INTO case_cvenums (case_id, cvenum) VALUES (?, ?)"), dblastid, c.CveNumList[i])
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
for i := 0; i < len(c.PlatformList); i++ {
_, err := tx.Exec(tx.Rebind("INSERT INTO case_platforms (case_id, platform) VALUES (?, ?)"), dblastid, c.PlatformList[i])
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
for i := 0; i < len(c.SoftwareList); i++ {
_, err := tx.Exec(tx.Rebind("INSERT INTO case_softwares (case_id, software) VALUES (?, ?)"), dblastid, c.SoftwareList[i])
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
var filters []Filter
err = db.Select(&filters, "SELECT * FROM filters")
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
var status []string
for _, filter := range filters {
err = db.Select(&status, "SELECT status FROM case_status WHERE refnum = '"+c.OriginalRefNum+"' AND filter_id = '"+filter.Id+"'")
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
if status == nil {
_, err := tx.Exec(tx.Rebind("INSERT INTO case_status (refnum, filter_id, status) VALUES (?, ?, ?)"), c.OriginalRefNum, filter.Id, "NEW")
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
}
var categories []Category
for _, category := range c.CategoryList {
err = db.Select(&categories, "SELECT category FROM categories WHERE category = '"+category+"'")
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
if categories == nil {
_, err := tx.Exec(tx.Rebind("INSERT INTO categories (category) VALUES (?)"), category)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
}
}
err = tx.Commit()
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not commit transaction")
return err
}
c.CaseId = strconv.Itoa(int(dblastid))
return nil
}
// ReadFromDB reads in a case from the DB.
func (c *CveCase) ReadFromDB(db *sqlx.DB, filterId string) error {
caseId, err := strconv.Atoi(c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not parse case id")
return err
}
if caseId < 1 {
log.Error().
Msg("CaseId is out of range")
return errors.New("CaseId out of range")
}
err = db.Get(c, db.Rebind("SELECT * FROM cases WHERE id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Get(c, db.Rebind("SELECT rawdata FROM case_rawdatas WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Get(c, db.Rebind("SELECT description FROM case_descriptions WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Select(&c.CategoryList, db.Rebind("SELECT category FROM case_categories WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Select(&c.CveNumList, db.Rebind("SELECT cvenum FROM case_cvenums WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Select(&c.PlatformList, db.Rebind("SELECT platform FROM case_platforms WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Select(&c.SoftwareList, db.Rebind("SELECT software FROM case_softwares WHERE case_id=?"), c.CaseId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
err = db.Select(&c.UpdateList, db.Rebind("SELECT refnum FROM cases WHERE orig_refnum = '"+c.OriginalRefNum+"' ORDER BY orig_refnum DESC"))
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
if filterId != "" {
err = db.Get(c, db.Rebind("SELECT status FROM case_status WHERE refnum=? AND filter_id=?"), c.OriginalRefNum, filterId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute SELECT query")
err := errors.Wrap(err, "Could not execute query")
return err
}
}
return nil
}
// SearchCases searches all cve cases.
func SearchCases(db *sqlx.DB, searchCriteria CveCaseSearchCriteria) ([]CveCase, error) {
var cveCaselist []CveCase
var selectFields = "cases.id, cases.source, cases.refnum, cases.date_created, cases.date_received, cases.risk, cases.subject, cases.verified, cases.msg_from, cases.msg_to"
sb := sqlbuilder.MySQL.NewSelectBuilder()
sb.Select(selectFields)
sb.From("cases")
var keywords []string
var filters []Filter
if searchCriteria.FilterId != "" {
sb2 := sqlbuilder.MySQL.NewSelectBuilder()
sb2.Select("*").From("filters").Where(sb2.Equal("id", searchCriteria.FilterId))
sql, args := sb2.Build()
query, err := sqlbuilder.MySQL.Interpolate(sql, args)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not build sql query")
return nil, err
}
err = db.Select(&filters, query)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not select filter from DB")
return nil, err
}
if filters == nil {
err := errors.New("filter not in DB")
return nil, err
}
if filters[0].Filter == "Other" {
sb.Where("cases.id IN (SELECT case_categories.case_id FROM case_categories WHERE case_categories.category NOT IN (SELECT category FROM filter_categories))")
} else {
sb.Where(sb.Equal("cases.id IN (SELECT case_categories.case_id FROM case_categories WHERE case_categories.category IN (SELECT category FROM filter_categories WHERE filter_categories.filter_id",
searchCriteria.FilterId) + "))")
sb.Where(sb.Equal("cases.id NOT IN (SELECT case_categories.case_id FROM case_categories WHERE case_categories.category IN (SELECT category FROM filter_excluded_categories WHERE filter_excluded_categories.filter_id",
searchCriteria.FilterId) + "))")
}
riskLevel, err := strconv.Atoi(filters[0].RiskLevel)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not parse risk level")
return nil, err
}
if riskLevel > 5 || riskLevel < 0 {
log.Error().
AnErr("Error", err).
Msg("Invalid risk level")
return nil, err
}
if filters[0].RiskLevel != "1" {
sb.Where(sb.GreaterEqualThan("risk", filters[0].RiskLevel))
}
if filters[0].Keywords != "" {
regex := regexp.MustCompile(";")
for _, keyword := range regex.Split(filters[0].Keywords, -1) {
keyword = strings.TrimSpace(keyword)
if keyword != "" {
keywords = append(keywords, keyword)
}
}
}
}
if searchCriteria.Category != "" {
sb.JoinWithOption(sqlbuilder.LeftJoin, "case_categories", "case_categories.case_id = cases.id")
sb.Where(sb.Equal("case_categories.category", searchCriteria.Category))
}
if searchCriteria.CaseId != "" {
sb.Where(sb.Equal("cases.id", searchCriteria.CaseId))
}
if searchCriteria.Subject != "" {
sb.Where(sb.Equal("cases.subject", searchCriteria.Subject))
}
if searchCriteria.RefNum != "" {
sb.Where(sb.Like("cases.refnum", "%"+searchCriteria.RefNum+"%"))
}
if searchCriteria.Source != "" {
if searchCriteria.Source != "CERT" {
return nil, errors.New("invalid source (only CERT valid)")
}
sb.Where(sb.Equal("cases.source", searchCriteria.Source))
}
if searchCriteria.Status != "" {
if searchCriteria.FilterId == "" {
err := errors.New("filter needed when searching after status")
return nil, err
}
if !(searchCriteria.Status == "ASSIGNED" || searchCriteria.Status == "NEW" || searchCriteria.Status == "CLOSED") {
err := errors.New("invalid status (only NEW, ASSIGNED and CLOSED valid)")
return nil, err
}
sb.Where(sb.Equal("cases.orig_refnum IN (SELECT case_status.refnum FROM case_status WHERE case_status.status = '"+searchCriteria.Status+"' AND case_status.filter_id", searchCriteria.FilterId) + ")")
} else if searchCriteria.FilterId != "" {
sb.Where(sb.Equal("cases.orig_refnum IN (SELECT case_status.refnum FROM case_status WHERE case_status.filter_id", searchCriteria.FilterId) + ")")
}
if searchCriteria.Risk != "" {
sb.Where(sb.GreaterEqualThan("cases.risk", searchCriteria.Risk))
}
if searchCriteria.Platform != "" {
sb.Where(sb.Equal("cases.id IN (SELECT case_platforms.case_id FROM case_platforms WHERE case_platforms.platform", searchCriteria.Platform) + ")")
}
if searchCriteria.Software != "" {
sb.Where(sb.Like("cases.id IN (SELECT case_softwares.case_id FROM case_softwares WHERE case_softwares.software", "%"+searchCriteria.Software+"%") + ")")
}
if searchCriteria.FirstDate != "" {
regex := regexp.MustCompile("^....-..-..\\z")
if !regex.MatchString(searchCriteria.FirstDate) {
err := errors.New("invalid format of firstDate")
return nil, err
}
sb.Where(sb.GreaterEqualThan(`cases.date_received`, searchCriteria.FirstDate))
}
if searchCriteria.LastDate != "" {
regex := regexp.MustCompile("^....-..-..\\z")
if !regex.MatchString(searchCriteria.LastDate) {
err := errors.New("invalid format of lastDate")
return nil, err
}
sb.Where(sb.LessEqualThan(`cases.date_received`, searchCriteria.LastDate))
}
if keywords == nil {
sb.OrderBy("id").Desc()
if searchCriteria.Amount != "" {
limit, err := strconv.Atoi(searchCriteria.Amount)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Couldn't parse amount")
return nil, err
}
sb.Limit(limit)
}
if searchCriteria.Offset != "" {
offset, err := strconv.Atoi(searchCriteria.Offset)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Couldn't parse amount")
return nil, err
}
sb.Offset(offset)
}
}
var sql string
var args []interface{}
if keywords != nil {
sbKeywords := sqlbuilder.MySQL.NewSelectBuilder()
sbKeywordsWhere1 := sqlbuilder.MySQL.NewSelectBuilder()
sbKeywordsWhere2 := sqlbuilder.MySQL.NewSelectBuilder()
sbKeywords.Select(selectFields).From("cases")
sbKeywords.Where(sbKeywords.In("cases.id", sbKeywordsWhere1))
sbKeywords.Where(sbKeywords.In("cases.orig_refnum", sbKeywordsWhere2))
var subQuery string
for i, keyword := range keywords {
if i != 0 {
subQuery += " OR "
}
subQuery += "cases.subject LIKE '%" + keyword + "%'"
}
sbKeywordsWhere1.Select("cases.id").From("cases").Where(subQuery)
sbKeywordsWhere2.Select("case_status.refnum").From("case_status").Where("case_status.filter_id = " + filters[0].Id)
sbKeywords.OrderBy("id").Desc()
if searchCriteria.Amount != "" {
limit, err := strconv.Atoi(searchCriteria.Amount)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Couldn't parse amount")
return nil, err
}
sbKeywords.Limit(limit)
}
if searchCriteria.Offset != "" {
offset, err := strconv.Atoi(searchCriteria.Offset)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Couldn't parse amount")
return nil, err
}
sbKeywords.Offset(offset)
}
union := sqlbuilder.Buildf("%v UNION %v", sb, sbKeywords)
sql, args = union.Build()
} else {
sql, args = sb.Build()
}
query, err := sqlbuilder.MySQL.Interpolate(sql, args)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not build sql query")
return nil, err
}
err = db.Select(&cveCaselist, query)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not select id from DB")
return nil, err
}
for i, cve := range cveCaselist {
err := cve.ReadFromDB(db, searchCriteria.FilterId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not get details of cve case")
return nil, err
}
cveCaselist[i] = cve
}
return cveCaselist, nil
}
// AddComment adds a comment
func AddComment(db *sqlx.DB, cveComment CveCaseComment) error {
if (cveComment.RefNum == "") || (cveComment.Comment == "") || (cveComment.NewStatus == "") || (cveComment.Username == "") || (cveComment.FilterId == "") {
err := errors.New("comment data is defective")
return err
}
cvecase, err := SearchCases(db, CveCaseSearchCriteria{RefNum: cveComment.RefNum})
if (err != nil) || (cvecase == nil) {
return errors.New("CaseID is not in DB")
}
if !(cveComment.NewStatus == "ASSIGNED" || cveComment.NewStatus == "NEW" || cveComment.NewStatus == "CLOSED") {
return errors.New("invalid status (only NEW, ASSIGNED and CLOSED valid)")
}
_, err = db.Exec(db.Rebind("INSERT INTO case_comments (refnum, new_status, username, comment, filter_id) VALUES (?, ?, ?, ?, ?)"), cveComment.RefNum, cveComment.NewStatus, cveComment.Username, cveComment.Comment, cveComment.FilterId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
_, err = db.Exec(db.Rebind("UPDATE case_status SET status = ? WHERE refnum = ? AND filter_id = ?"), cveComment.NewStatus, cveComment.RefNum, cveComment.FilterId)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not execute query")
return err
}
return nil
}
// SearchComment searches after a comment.
func SearchComment(db *sqlx.DB, comment CveCaseComment) ([]CveCaseComment, error) {
var cveComment []CveCaseComment
sb := sqlbuilder.MySQL.NewSelectBuilder()
sb.Select("*")
sb.From("case_comments")
sb.Where(sb.Equal("case_comments.id", comment.CommentId))
sql, args := sb.Build()
query, err := sqlbuilder.MySQL.Interpolate(sql, args)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not build sql query")
return nil, err
}
err = db.Select(&cveComment, query)
if err != nil {
log.Error().
Msg("Could not select comment")
return nil, err
}
return cveComment, nil
}
// SearchCommentsOfCveCase searches after all comments of a cve case.
func SearchCommentsOfCveCase(db *sqlx.DB, cvecase CveCase, filterId string) ([]CveCaseComment, error) {
var result []CveCaseComment
sb := sqlbuilder.MySQL.NewSelectBuilder()
sb2 := sqlbuilder.MySQL.NewSelectBuilder()
sb2.Select("orig_refnum").From("cases").Where(sb2.Equal("id", cvecase.CaseId))
sb.Select("*")
sb.From("case_comments")
sb.Where(sb.In("case_comments.refnum", sb2))
sb.Where(sb.Equal("case_comments.filter_id", filterId))
sql, args := sb.Build()
query, err := sqlbuilder.MySQL.Interpolate(sql, args)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not build sql query")
return nil, err
}
err = db.Select(&result, query)
if err != nil {
log.Error().
Msg("Could not select comment")
return nil, err
}
return result, nil
}
// ChangeComment changes a comment.
func ChangeComment(db *sqlx.DB, comment CveCaseComment) error {
checkComment, err := SearchComment(db, comment)
if len(checkComment) == 0 {
log.Error().