/
Program.cs
6483 lines (5941 loc) · 427 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using XORCISMModel;
using XOVALModel;
using XVULNERABILITYModel;
using XWINDOWSModel;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
//using ICSharpCode.SharpZipLib.Zip;
using System.IO.Compression;
using System.Threading.Tasks;
//using System.Collections.Concurrent;
namespace Import_oval
{
static class Program
{
/// <summary>
/// Copyright (C) 2014-2015 Jerome Athias
/// Parser for MITRE OVAL XML file and import into an XORCISM database
/// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/// </summary>
public static XORCISMEntities model = new XORCISMEntities();
public static XOVALEntities oval_model = new XOVALEntities();
public static XVULNERABILITYEntities vuln_model = new XVULNERABILITYEntities();
public static XWINDOWSEntities windows_model = new XWINDOWSEntities();
//[STAThread]
static void Main()
{
string sOVALVersion = "5.11.1"; //HARDCODED
//TODO: Manage the StatusName of OVALDEFINITION as integers. Example: ACCEPTED=1, DEPRECATED=2... to save space
//Parse an OVAL XML file and import the values into an XORCISM database
////https://oval.mitre.org/rep-data/5.10/org.mitre.oval/v/index.html
//https://oval.cisecurity.org/repository/download
//TODO
//http://usgcb.nist.gov/usgcb_content.html
//https://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework
model.Configuration.AutoDetectChangesEnabled = false;
model.Configuration.ValidateOnSaveEnabled = false;
oval_model.Configuration.AutoDetectChangesEnabled = false;
oval_model.Configuration.ValidateOnSaveEnabled = false;
vuln_model.Configuration.AutoDetectChangesEnabled = false;
vuln_model.Configuration.ValidateOnSaveEnabled = false;
int iVocabularyOVALID = 0;// 9; //OVAL 5.10.1
//Create a Vocabulary for OVAL
#region vocabularyoval
try
{
iVocabularyOVALID = model.VOCABULARY.Where(o => o.VocabularyName == "OVAL" && o.VocabularyVersion == sOVALVersion).Select(o => o.VocabularyID).FirstOrDefault();
}
catch (Exception ex)
{
}
if (iVocabularyOVALID <= 0)
{
XORCISMModel.VOCABULARY oVocabulary = new XORCISMModel.VOCABULARY();
oVocabulary.CreatedDate = DateTimeOffset.Now;
oVocabulary.VocabularyName = "OVAL";
oVocabulary.VocabularyVersion = sOVALVersion;
model.VOCABULARY.Add(oVocabulary);
model.SaveChanges();
iVocabularyOVALID = oVocabulary.VocabularyID;
Console.WriteLine("DEBUG iVocabularyOVALID=" + iVocabularyOVALID);
}
else
{
//Update VOCABULARY
}
#endregion vocabularyoval
//<oval:schema_version>5.10.1</oval:schema_version>
XmlDocument doc= new XmlDocument();
//Review http://vsecurity.com/download/papers/XMLDTDEntityAttacks.pdf
//TODO state_operator
//TODO
//https://oval.mitre.org/repository/data/LatestUpdates
#region downloadoval
//string sDownloadFileURL = "http://oval.mitre.org/rep-data/5.10/org.mitre.oval/oval.xml.zip"; //HARDCODED
string sDownloadFileURL = "https://oval.cisecurity.org/repository/download/5.11.1/all/oval.xml.zip"; //HARDCODED
string sDownloadFileName = "oval.xml.zip"; //TODO Hardcoded
string sDownloadLocalFile = "oval.xml";
string sDownloadLocalPath = "C:/nvdcve/"; //TODO: Hardcoded
string sDownloadLocalFolder = @"C:\nvdcve\";//TODO: Hardcoded
FileInfo fileInfo = new FileInfo(sDownloadLocalFolder + sDownloadFileName);
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Int64 fileSizeRemote = new Int64();
long fileSizeLocal = 0;
try
{
fileSizeLocal = fileInfo.Length;
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG " + sDownloadLocalFolder + sDownloadFileName + " FileSize:" + fileSizeLocal);
}
catch (Exception exfileSizeLocal)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("Exception: exfileSizeLocal " + exfileSizeLocal.Message + " " + exfileSizeLocal.InnerException);
}
try
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG Getting filesize");
webRequest = (HttpWebRequest)WebRequest.Create(new Uri(sDownloadFileURL));
webRequest.Method = "HEAD";
webRequest.Timeout = 2 * 60 * 1000; //2 minutes Hardcoded
//webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse)webRequest.GetResponse();
fileSizeRemote = webResponse.ContentLength;
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG " + sDownloadFileURL + " FileSize:" + fileSizeRemote);
Console.WriteLine("DEBUG Last modified: " + webResponse.LastModified);
}
catch (Exception exGetSizeDownload)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("Exception: exGetSizeDownload " + exGetSizeDownload.Message + " " + exGetSizeDownload.InnerException);
}
if(fileSizeRemote<=0)
{
Console.WriteLine("ERROR: fileSizeRemote is incorrect: "+ fileSizeRemote);
}
if (fileSizeRemote == fileSizeLocal)
{
}
else
{
//Download the file
try
{
//WebClient wc = new WebClient();
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG Downloading " + sDownloadFileName);
//wc.DownloadFile(sDownloadFileURL, sDownloadLocalPath + sDownloadFileName);
//wc.Dispose();
webRequest = (HttpWebRequest)WebRequest.Create(new Uri(sDownloadFileURL));
webRequest.Method = "GET";
//webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Timeout = 20 * 60 * 1000; //20 minutes Hardcoded
webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream remoteStream = webResponse.GetResponseStream();
Stream localStream = File.Create(sDownloadLocalPath + sDownloadFileName);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
int bytesProcessed = 0;
// Simple do/while loop to read from stream until no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
//Console.WriteLine("DEBUG Downloading: " + bytesProcessed + " bytes processed");
} while (bytesRead > 0);
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
localStream.Dispose();
Console.WriteLine("DEBUG Download completed");
}
catch (Exception ex)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("Exception: while downloading\n" + ex.Message + " " + ex.InnerException);
}
//Extract Zip File
try
{
//FastZip fz = new FastZip(); //Now use .NET
//fz.ExtractZip(@"C:\nvdcve\cwec_v2.5.xml.zip", @"C:\nvdcve\", "");
//fz.ExtractZip(sDownloadLocalFolder + sDownloadFileName, sDownloadLocalFolder, "");
//with .NET 4.5
ZipArchive archive = ZipFile.Open(sDownloadLocalFolder + sDownloadFileName, ZipArchiveMode.Read);
//File.Delete(sDownloadLocalFolder + "\\oval_repository\\" + sDownloadLocalFile); //delete oval.xml //HARDCODED
File.Delete(sDownloadLocalFolder + "\\" + sDownloadLocalFile); //delete oval.xml
archive.ExtractToDirectory(sDownloadLocalFolder);
Console.WriteLine("DEBUG Extraction Complete");
}
catch (Exception exUnzip)
{
Console.WriteLine("Exception: exUnzip: " + exUnzip.Message + " " + exUnzip.InnerException);
}
}
#endregion downloadoval
//doc.Load(@"C:\nvdcve\cisco.pix.xml");
//doc.Load(@"C:\nvdcve\microsoft.windows.server.2012.xml");
//doc.Load(@"C:\nvdcve\red.hat.enterprise.linux.4.xml");
//doc.Load(@"C:\nvdcve\ubuntu.6.06.xml");
//doc.Load(@"C:\nvdcve\Norton.xml");
//doc.Load(@"C:\nvdcve\2014-05-02-OVAL-def-1.xml");
//doc.Load(@"microsoft.windows.2000.xml");
//doc.Load(@"CVE-2012-0159.xml");
//Console.WriteLine("DEBUG LOADING FILE " + sDownloadLocalFolder + sDownloadLocalFile);
//TODO Create the folder
try
{
//doc.Load(sDownloadLocalFolder + "\\oval_repository\\" + sDownloadLocalFile); //Hardcoded
doc.Load(sDownloadLocalFolder + "\\" + sDownloadLocalFile);
}
catch (Exception exLoadDowanloadedFile)
{
Console.WriteLine("Exception exLoadDowanloadedFile: " + exLoadDowanloadedFile.Message + " " + exLoadDowanloadedFile.InnerException);
return;
}
//TODO Check that the XML is valid against XSD
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("oval", "http://oval.mitre.org/XMLSchema/oval-common-5"); //Hardcoded
mgr.AddNamespace("oval-def", "http://oval.mitre.org/XMLSchema/oval-definitions-5"); //Hardcoded
//For testing
bool bImportOVALDefinitions = true;
bool bImportOVALTests = true;
bool bImportOVALObjects = true;
bool bImportOVALStates = true;
bool bImportOVALVariables = true;
//---------------------------------
if (bImportOVALDefinitions)
{
int iCptNode = 0;
#region ovaldefinition
//Note: this is not the best/fastest way to parse XML, but is clear/easy enough
XmlNodeList nodesDefinitions = doc.SelectNodes("/oval-def:oval_definitions/oval-def:definitions/oval-def:definition", mgr); //Hardcoded
//Console.WriteLine(nodes1.Count);
foreach (XmlNode nodeDefinition in nodesDefinitions) //definition
{
iCptNode++;
//Free memory sometimes (OutOfMemoryException)
#region freememory
if (iCptNode > 30) //TODO Hardcoded Review
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG FREE MEMORY");
//FREE
try
{
model.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionGLOBALSAVE " + sb.ToString());
}
catch (Exception exGLOBALSAVE)
{
Console.WriteLine("Exception: exGLOBALSAVE " + exGLOBALSAVE.Message + " " + exGLOBALSAVE.InnerException);
}
model.Dispose();
model = new XORCISMEntities();
model.Configuration.AutoDetectChangesEnabled = false;
model.Configuration.ValidateOnSaveEnabled = false;
iCptNode = 1;
}
#endregion freememory
//<definition id="oval:org.mitre.oval:def:11958" version="3" class="inventory">
Console.WriteLine("DEBUG *********************************************");
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
string myDefID = nodeDefinition.Attributes["id"].InnerText; //oval:org.mitre.oval:def:11958
Console.WriteLine("DEBUG OVALDefinitionID=" + myDefID);
string sDefVersion = nodeDefinition.Attributes["version"].InnerText; //3
Console.WriteLine("DEBUG version=" + sDefVersion);
string sDefDeprecated = "";
try
{
sDefDeprecated = nodeDefinition.Attributes["deprecated"].InnerText; //true
}
catch (Exception ex)
{
//Console.WriteLine("Exception: sDefDeprecated " + ex.Message + " " + ex.InnerException);
}
List<int> ListPlatformID = new List<int>(); //List of PlatformID for relationships
List<int> ListProductID = new List<int>(); //List of ProductID for relationships
//XORCISMModel.OVALMETADATA myOVALMetadata=new OVALMETADATA();
XOVALModel.OVALCRITERIA myCriteria = new OVALCRITERIA(); //We attach one principal criteria to each definition
string sClassValue = nodeDefinition.Attributes["class"].InnerText; //inventory vulnerability patch compliance
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG sClassValue=" + sClassValue);
int iOVALClassEnumerationID = 0;
//TODO: could be hardcoded for performance
#region ovalclassenumeration
try
{
iOVALClassEnumerationID = oval_model.OVALCLASSENUMERATION.Where(o => o.ClassValue == sClassValue).Select(o => o.OVALClassEnumerationID).FirstOrDefault();
}
catch (Exception ex)
{
//e.g. table empty
}
//XORCISMModel.OVALCLASSENUMERATION myclass=null;
//myclass = oval_model.OVALCLASSENUMERATION.FirstOrDefault(o => o.ClassValue == sClassValue && o.VocabularyID == iVocabularyOVALID);
//if (myclass == null)
if (iOVALClassEnumerationID <= 0)
{
try
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine(string.Format("DEBUG Adding new OVALCLASSENUMERATION [{0}] in table OVALCLASSENUMERATION", sClassValue));
OVALCLASSENUMERATION myclass = new OVALCLASSENUMERATION();
myclass.ClassValue = sClassValue; //inventory vulnerability
myclass.VocabularyID = iVocabularyOVALID;
myclass.CreatedDate = DateTimeOffset.Now;
myclass.timestamp = DateTimeOffset.Now;
oval_model.OVALCLASSENUMERATION.Add(myclass);
oval_model.SaveChanges();
iOVALClassEnumerationID = myclass.OVALClassEnumerationID;
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionUPDATECAPEC " + sb.ToString());
}
catch (Exception exmyclass)
{
Console.WriteLine("Exception: exmyclass " + exmyclass.Message + " " + exmyclass.InnerException);
}
}
else
{
//Update OVALCLASSENUMERATION
}
#endregion ovalclassenumeration
OVALDEFINITION oOVALDefinition = new OVALDEFINITION();
Boolean bAddDefinition = false;
//Boolean bAddMetadata = false;
//int iDefVersion = Convert.ToInt32(sDefVersion); //3
//int iOVALDefinitionID=
oOVALDefinition = oval_model.OVALDEFINITION.FirstOrDefault(o => o.OVALDefinitionIDPattern == myDefID && o.OVALDefinitionVersion == sDefVersion); //VocabularyID
if (oOVALDefinition == null)
{
bAddDefinition = true;
//We need to Add the Metadata
//bAddMetadata = true;
Console.WriteLine("DEBUG Adding OVALDEFINITION");
try
{
oOVALDefinition = new OVALDEFINITION();
oOVALDefinition.CreatedDate = DateTimeOffset.Now;
oOVALDefinition.VocabularyID = iVocabularyOVALID;
oOVALDefinition.OVALDefinitionIDPattern = myDefID;
//NOTE: we still don't know the principal OVALCriteriaID (retieved/created later)
//TODO Review: create it here?
oOVALDefinition.OVALDefinitionVersion = sDefVersion;
oOVALDefinition.OVALClassEnumerationID = iOVALClassEnumerationID;
//oOVALDefinition.ClassValue = sClassValue; //Removed
if (sDefDeprecated == "true")
{
oOVALDefinition.deprecated = true;
}
oOVALDefinition.timestamp = DateTimeOffset.Now;
//model.OVALDEFINITION.Attach(oOVALDefinition);
//model.Entry(oOVALDefinition).State = EntityState.Modified;
oval_model.OVALDEFINITION.Add(oOVALDefinition);
oval_model.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionUPDATECAPEC " + sb.ToString());
}
catch (Exception exoOVALDefinition)
{
Console.WriteLine("Exception: exoOVALDefinition " + exoOVALDefinition.Message + " " + exoOVALDefinition.InnerException);
}
}
else
{
//Update OVALDEFINITION
//The definition exists
//We should have a metadata for it
/*
myOVALMetadata = oval_model.OVALMETADATA.FirstOrDefault(o => o.OVALMetadataID == myOVALDefinition.OVALMetadataID);
if (myOVALMetadata == null)
{
Console.WriteLine("ERROR: METADATA not found for OVALDEFINITION");
//return;
bAddMetadata = true;
}
*/
try
{
oOVALDefinition.VocabularyID = iVocabularyOVALID;
//oOVALDefinition.OVALDefinitionVersion = iDefVersion;
oOVALDefinition.OVALClassEnumerationID = iOVALClassEnumerationID;
//oOVALDefinition.ClassValue = sClassValue; //Removed
if (sDefDeprecated == "true")
{
oOVALDefinition.deprecated = true;
}
oOVALDefinition.timestamp = DateTimeOffset.Now;
oval_model.OVALDEFINITION.Attach(oOVALDefinition);
oval_model.Entry(oOVALDefinition).State = EntityState.Modified;
oval_model.SaveChanges();
Console.WriteLine("DEBUG Updated OVALDEFINITION " + oOVALDefinition.OVALDefinitionID);
}
catch (Exception exUpdateoOVALDefinition)
{
Console.WriteLine("Exception: exUpdateoOVALDefinition " + exUpdateoOVALDefinition.Message + " " + exUpdateoOVALDefinition.InnerException);
}
}
//Parallel.ForEach(XmlNode nodeDefinitionInfo in nodeDefinition.ChildNodes)
foreach(XmlNode nodeDefinitionInfo in nodeDefinition.ChildNodes)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG nodeDefinitionInfo.Name=" + nodeDefinitionInfo.Name);
if (nodeDefinitionInfo.Name == "metadata")
{
foreach (XmlNode nodeMetadata in nodeDefinitionInfo.ChildNodes)
{
if (nodeMetadata.Name == "title")
{
string sOVALDefinitionTitle = nodeMetadata.InnerText.Trim();
Console.WriteLine("DEBUG sOVALDefinitionTitle=" + sOVALDefinitionTitle);
//TODO check for typos
oOVALDefinition.OVALDefinitionTitle = sOVALDefinitionTitle;
//Norton Internet Security is installed
//TODO Regex ELSA-2013:1813
//http://linux.oracle.com/errata/ELSA-2014-0139.html
//Note sReferenceSourceID should be =ELSA-2014:0139-00
//TODO
//CVE-2013-3163 (MS13-055)
#region regexCVE
Regex myRegexCVE = new Regex(@"CVE-(19|20)\d\d-(0\d{3}|[1-9]\d{3,})");
//https://cve.mitre.org/cve/identifiers/tech-guidance.html
string strTemp = myRegexCVE.Match(sOVALDefinitionTitle).ToString(); //TODO: can we have multiple CVEs here?
if (strTemp != "")
{
string sCVEID = strTemp; // "CVE-" + strTemp.Replace("'", "");
//Console.WriteLine(fichiers[i]);
Console.WriteLine("DEBUG CVE:" + sCVEID);
//VULNERABILITY myCVE = new VULNERABILITY();
//myCVE = model.VULNERABILITY.FirstOrDefault(o => o.VULReferentialID == sCVEID);
////int vulnID = myCVE.VulnerabilityID;
int iVulnerabilityID = 0;
try
{
iVulnerabilityID = vuln_model.VULNERABILITY.Where(o => o.VULReferentialID == sCVEID).Select(o => o.VulnerabilityID).FirstOrDefault();
}
catch (Exception ex)
{
}
//Console.WriteLine("DEBUGTEST");
//if (myCVE == null)
if (iVulnerabilityID <= 0)
{
Console.WriteLine("ERROR: Problem: this CVE is unknown: " + sCVEID);
//TODO Add VULNERABILITY
}
else
{
Console.WriteLine("DEBUG VulnerabilityID:" + iVulnerabilityID);
}
}
#endregion regexCVE
//APPLE-SA-2012-03-07-1 and APPLE-SA-2012-03-07-2
//USN-1879-1
//http://www.ubuntu.com/usn/usn-1335-1/
//DSA-1549-1
//KB2608646
////TODO check if we have a related CVE- here WARNING!!! "not related, different bug"...
////OVALDefinitionVulnerabilityRelationship = "Related";
}
if (nodeMetadata.Name == "description")
{
oOVALDefinition.OVALDefinitionDescription = nodeMetadata.InnerText;
//Norton Internet Security is installed
//TODO check for typos
//TODO RegexCVE
//oval:org.mitre.oval:def:7878
/*
Insufficient checking for out-of-memory conditions results in null pointer dereferences (CVE-2008-3912). Incorrect error handling logic leads to memory leaks (CVE-2008-3913) and file descriptor leaks (CVE-2008-3914).
*/
}
}
}
if (nodeDefinitionInfo.Name == "criteria")
{
#region criteria
Boolean bAddCriteria = false;
int iOVALCriteriaID = 0;
if (bAddDefinition)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine(string.Format("DEBUG Adding new OVALCRITERIA [{0}] in table OVALCRITERIA", myDefID));
myCriteria = new OVALCRITERIA();
//myCriteria.VocabularyID = iVocabularyOVALID;
myCriteria.comment = "";
//myCriteria.OperatorValue = ""; //Removed
bAddCriteria = true;
}
else
{
//Update OVALCRITERIA
//The definition exists, we should have a criteria for it
myCriteria = oval_model.OVALCRITERIA.FirstOrDefault(o => o.OVALCriteriaID == oOVALDefinition.OVALCriteriaID);
//myCriteria = myOVALDefinition.OVALCRITERIA;
if (myCriteria == null)
{
Console.WriteLine("ERROR: no criteria for the OVALDefinition");
myCriteria = new OVALCRITERIA();
//myCriteria.CreatedDate = DateTimeOffset.Now;
//myCriteria.VocabularyID = iVocabularyOVALID;
myCriteria.comment = "";
//myCriteria.OperatorValue = ""; //Removed
//myCriteria.CreatedDate = DateTimeOffset.Now;
//model.OVALCRITERIA.Add(myCriteria);
//model.SaveChanges();
//iOVALCriteriaID = myCriteria.OVALCriteriaID;
bAddCriteria = true;
}
else
{
//Update OVALCRITERIA
//iOVALCriteriaID = myCriteria.OVALCriteriaID;
}
//Console.WriteLine("DEBUG iOVALCriteriaID=" + iOVALCriteriaID);
}
try
{
string sOperatorValue = "";
try
{
sOperatorValue = nodeDefinitionInfo.Attributes["operator"].InnerText; //AND
}
catch (Exception exNOsOperatorValue)
{
//TODO REVIEW
Console.WriteLine("DEBUG ERROR? no operator, replacing by AND");
sOperatorValue = "AND";
}
//myCriteria.OperatorValue = sOperatorValue; // REMOVED
//TODO Hardcode that?
#region OPERATORENUMERATION
int iOperatorEnumerationID = 0;
try
{
iOperatorEnumerationID = oval_model.OPERATORENUMERATION.Where(o => o.OperatorValue == sOperatorValue).Select(o => o.OperatorEnumerationID).FirstOrDefault();
}
catch (Exception exiOperatorEnumerationID)
{
Console.WriteLine("Exception: exiOperatorEnumerationID " + exiOperatorEnumerationID.Message + " " + exiOperatorEnumerationID.InnerException);
}
if (iOperatorEnumerationID <= 0)
{
try
{
XOVALModel.OPERATORENUMERATION oOperatorEnumeration = new XOVALModel.OPERATORENUMERATION();
oOperatorEnumeration.CreatedDate = DateTimeOffset.Now;
oOperatorEnumeration.OperatorValue = sOperatorValue;
oOperatorEnumeration.VocabularyID = iVocabularyOVALID;
oOperatorEnumeration.timestamp = DateTimeOffset.Now;
oval_model.OPERATORENUMERATION.Add(oOperatorEnumeration);
oval_model.SaveChanges();
iOperatorEnumerationID = oOperatorEnumeration.OperatorEnumerationID;
}
catch (Exception exOPERATORENUMERATION)
{
Console.WriteLine("Exception: exOPERATORENUMERATION " + exOPERATORENUMERATION.Message + " " + exOPERATORENUMERATION.InnerException);
}
}
else
{
//Update OPERATORENUMERATION
}
#endregion OPERATORENUMERATION
//Update OVALCRITERIA
myCriteria.OperatorEnumerationID = iOperatorEnumerationID;
Console.WriteLine("DEBUG iOperatorEnumerationID=" + iOperatorEnumerationID);
}
catch (Exception exmyCriteriaOperatorValue)
{
Console.WriteLine("Exception: exmyCriteriaOperatorValue myCriteria.OperatorValue " + exmyCriteriaOperatorValue.Message + " " + exmyCriteriaOperatorValue.InnerException);
}
try
{
//Update OVALCRITERIA
myCriteria.comment = nodeDefinitionInfo.Attributes["comment"].InnerText;
}
catch (Exception ex)
{
Console.WriteLine("NOTE: No comment for CRITERIA"); //DEBUG ERROR?
myCriteria.comment = "";
}
try
{
if (bAddCriteria)
{
myCriteria.CreatedDate = DateTimeOffset.Now;
oval_model.OVALCRITERIA.Add(myCriteria);
oval_model.SaveChanges();
//iOVALCriteriaID = myCriteria.OVALCriteriaID;
}
//Update OVALCRITERIA (or create it)
myCriteria.VocabularyID = iVocabularyOVALID;
myCriteria.timestamp = DateTimeOffset.Now;
oval_model.OVALDEFINITION.Attach(oOVALDefinition);
oval_model.Entry(oOVALDefinition).State = EntityState.Modified;
oval_model.SaveChanges();
iOVALCriteriaID = myCriteria.OVALCriteriaID;
//Console.WriteLine("DEBUG Update OVALDEFINITION with OVALCriteriaID=" + iOVALCriteriaID);
//Update OVALDEFINITION
oOVALDefinition.OVALCriteriaID = iOVALCriteriaID; // myCriteria.OVALCriteriaID;
oOVALDefinition.timestamp = DateTimeOffset.Now;
//model.OVALDEFINITION.Attach(oOVALDefinition);
oval_model.Entry(oOVALDefinition).State = EntityState.Modified;
oval_model.SaveChanges(); //keep this
//Console.WriteLine("DEBUG Updated OVALDEFINITION with OVALCriteriaID=" + oOVALDefinition.OVALCriteriaID);
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionUPDATECAPEC " + sb.ToString());
}
catch (Exception exAddToOVALCRITERIA)
{
Console.WriteLine("Exception: exAddToOVALCRITERIA " + exAddToOVALCRITERIA.Message + " " + exAddToOVALCRITERIA.InnerException);
}
//********************************************************************************************************************************************************
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG calling OVALParseCriteria() iOVALCriteriaID=" + iOVALCriteriaID);
//OVALParseCriteria(model, node2, myCriteria, iVocabularyOVALID);
//OVALParseCriteria(node2, myCriteria, iVocabularyOVALID);
OVALParseCriteria(nodeDefinitionInfo, iOVALCriteriaID, iVocabularyOVALID);
#endregion criteria
}
}
/*
try
{
if (bAddMetadata)
{
Console.WriteLine(string.Format("DEBUG Adding new OVALMETADATA [{0}] in table OVALMETADATA", myDefID));
myOVALMetadata.CreatedDate = DateTimeOffset.Now;
myOVALMetadata.VocabularyID = iVocabularyOVALID;
model.OVALMETADATA.Add(myOVALMetadata);
}
else
{
//Update OVALMETADATA
}
myOVALMetadata.timestamp = DateTimeOffset.Now;
model.SaveChanges();
}
catch (Exception exAddToOVALMETADATA)
{
Console.WriteLine("Exception: exAddToOVALMETADATA " + exAddToOVALMETADATA.Message + " " + exAddToOVALMETADATA.InnerException);
}
*/
//TODO: not optimized
//Parse it again to add links
foreach (XmlNode nodeDefinitionInfo in nodeDefinition.ChildNodes)
{
Console.WriteLine("DEBUG2 " + DateTimeOffset.Now);
Console.WriteLine("DEBUG2 nodeDefinitionInfo.Name=" + nodeDefinitionInfo.Name);
if (nodeDefinitionInfo.Name == "metadata")
{
foreach (XmlNode nodeMetadata in nodeDefinitionInfo.ChildNodes)
{
switch (nodeMetadata.Name)
{
case "affected":
//TODO: could be hardcoded for performance
#region ovalosfamily
string sOSFamily = nodeMetadata.Attributes["family"].InnerText; //pixos windows
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine("DEBUG sOSFamily=" + sOSFamily);
int iOSFamilyID = 0;
try
{
iOSFamilyID = model.OSFAMILY.Where(o => o.FamilyName == sOSFamily).Select(o => o.OSFamilyID).FirstOrDefault();
}
catch (Exception ex)
{
}
//XORCISMModel.OSFAMILY myOSFamily;
//myOSFamily = model.OSFAMILY.FirstOrDefault(o => o.FamilyName == sOSFamily && o.VocabularyID == iVocabularyOVALID);
//if (myOSFamily == null)
if (iOSFamilyID <= 0)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine(string.Format("DEBUG Adding new OSFAMILY [{0}] in table OSFAMILY", sOSFamily));
try
{
OSFAMILY myOSFamily = new OSFAMILY();
myOSFamily.FamilyName = sOSFamily; //windows
myOSFamily.VocabularyID = iVocabularyOVALID;
myOSFamily.CreatedDate = DateTimeOffset.Now;
myOSFamily.timestamp = DateTimeOffset.Now;
model.OSFAMILY.Add(myOSFamily);
model.SaveChanges();
iOSFamilyID = myOSFamily.OSFamilyID;
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionOSFAMILY " + sb.ToString());
}
catch (Exception exAddToOSFAMILY)
{
Console.WriteLine("Exception: exAddToOSFAMILY " + exAddToOSFAMILY.Message + " " + exAddToOSFAMILY.InnerException);
}
}
else
{
//Update OSFAMILY
}
#region OVALDEFINITIONFAMILY
int iOVALDefinitionFamilyID = 0;
try
{
iOVALDefinitionFamilyID = oval_model.OVALDEFINITIONFAMILY.Where(o => o.OVALDefinitionID == oOVALDefinition.OVALDefinitionID && o.OSFamilyID == iOSFamilyID).Select(o => o.OVALDefinitionFamilyID).FirstOrDefault();
}
catch (Exception ex)
{
}
//XORCISMModel.OVALDEFINITIONFAMILY oOVALDefinitionOSFamily;
//oOVALDefinitionOSFamily = oval_model.OVALDEFINITIONFAMILY.FirstOrDefault(o => o.OVALDefinitionID==oOVALDefinition.OVALDefinitionID && o.OSFamilyID == iOSFamilyID);
//if (oOVALDefinitionOSFamily == null)
if (iOVALDefinitionFamilyID <= 0)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine(string.Format("DEBUG Adding new OVALDEFINITIONFAMILY [{0}] in table OVALDEFINITIONFAMILY", sOSFamily));
try
{
OVALDEFINITIONFAMILY oOVALDefinitionOSFamily = new OVALDEFINITIONFAMILY();
oOVALDefinitionOSFamily.OVALDefinitionID = oOVALDefinition.OVALDefinitionID;
oOVALDefinitionOSFamily.OSFamilyID = iOSFamilyID; // myOSFamily.OSFamilyID;
oOVALDefinitionOSFamily.VocabularyID = iVocabularyOVALID;
oOVALDefinitionOSFamily.CreatedDate = DateTimeOffset.Now;
oOVALDefinitionOSFamily.timestamp = DateTimeOffset.Now;
oval_model.OVALDEFINITIONFAMILY.Add(oOVALDefinitionOSFamily);
oval_model.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionOVALDEFINITIONFAMILY " + sb.ToString());
}
catch (Exception exAddToOVALDEFINITIONFAMILY)
{
Console.WriteLine("Exception: exAddToOVALDEFINITIONFAMILY " + exAddToOVALDEFINITIONFAMILY.Message + " " + exAddToOVALDEFINITIONFAMILY.InnerException);
}
}
else
{
//Update OVALDEFINITIONFAMILY
}
#endregion OVALDEFINITIONFAMILY
#endregion ovalosfamily
//OVALAFFECTEDFOROVALMETADATA
//TODO: PRODUCTPLATFORM (done later in Vulnerability)
foreach (XmlNode nodePlatform in nodeMetadata.ChildNodes)
{
if (nodePlatform.Name == "platform")
{
#region ovalplatform
//OVALDEFINITIONPLATFORM
string sPlatform = nodePlatform.InnerText.Trim(); //Cisco PIX Microsoft Windows 8
//Console.WriteLine("DEBUG " + DateTimeOffset.Now);
//Console.WriteLine("DEBUG sPlatform=" + sPlatform);
//TODO: check for "typos"
int iPlatformID = 0;
try
{
iPlatformID = model.PLATFORM.Where(o => o.PlatformName == sPlatform).Select(o => o.PlatformID).FirstOrDefault(); //Note: case insensitive
}
catch (Exception ex)
{
}
//XORCISMModel.PLATFORM myPlatform;
//myPlatform = model.PLATFORM.FirstOrDefault(o => o.PlatformName == sPlatform);// && o.VocabularyID == iVocabularyOVALID);
//if (myPlatform == null)
if (iPlatformID <= 0)
{
Console.WriteLine("DEBUG " + DateTimeOffset.Now);
Console.WriteLine(string.Format("DEBUG Adding new PLATFORM [{0}] in table PLATFORM", sPlatform));
try
{
PLATFORM myPlatform = new PLATFORM();
myPlatform.PlatformName = sPlatform;
//TODO algo for PLATFORMMAPPING (OVAL/CCE/Metasploit)
myPlatform.VocabularyID = iVocabularyOVALID;
myPlatform.CreatedDate = DateTimeOffset.Now;
myPlatform.timestamp = DateTimeOffset.Now;
model.PLATFORM.Add(myPlatform);
model.SaveChanges();
iPlatformID = myPlatform.PlatformID;
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
//throw new DbEntityValidationException(sb.ToString(), e);
Console.WriteLine("Exception: DbEntityValidationExceptionPLATFORM " + sb.ToString());
}
catch (Exception exAddToPLATFORM)
{
Console.WriteLine("Exception: exAddToPLATFORM " + exAddToPLATFORM.Message + " " + exAddToPLATFORM.InnerException);
}
}
else
{
//Update PLATFORM
//TODO: the case could have changed
//sPlatform.Equals(o.PlatformName, StringComparison.Ordinal);