forked from miketanderson/nvd-xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvd_xml_parser.php
executable file
·405 lines (384 loc) · 11.9 KB
/
nvd_xml_parser.php
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
<?php
/* XML parser for the NIST's National Vulnerability Database (NVD)
* Open Source license, NCSA/U. of Illinois
* Version 1.01, September 2, 2005
* Pascal Meunier
* Purdue University CERIAS
* Contact: pmeunier
*
* Version 1.02, January 2014
* Mike Anderson
*
* Uses the basic XML parser included with PHP
* that doesn't perform schema validation
* see http://us2.php.net/xml
*
* The NVD's XML schema doesn't have conflicting name tags
* at various nesting levels, so its parsing is relatively
* simple. Therefore, a flat namespace (independent of nesting level)
* works fine. This simple approach doesn't work, for example, with Microsoft's
* XML export of vulnerabilities.
* Note that this parser does not have cases for all tags in the NVD schema.
* Simply add "case" statements for the additional tags that matter to you.
*
* The data from parsing needs further validation or escaping if it is
* to be inserted into another database.
* -Replace the first line with the path to your installation of PHP
* for command line execution. Delete the path to PHP on the first line for
* execution with an Apache web server.
* -Choose a source for the XML file (see below)
*ChangeLog:
* 1.0 first version August 29, 2005
* 1.01 September 2, 2005
-Changed the name nvdcve-1999-2002.xml to nvdcve-2002.xml to match nvd changes
-Removed comment "from Apache" regarding https locations, as they work from the command line too, depending on how PHP was installed
-Now uses command line argument if present as XML source
-Now captures the content of <REF> tags
*/
error_reporting (E_ALL);
$debug = false; // will print messages about every field found if true
$output = true; //set to true to output to a csv format
$html_output = true; //set to true to enable html links & features
$count_entries = 0; // count number of new CVE entries found today
//print the csv header
if ($output) {
print_header();
}
// Choose a source for parsing
// Check if a source was passed as argument
if (isset($argv[1])) {
parse_this($argv[1]);
} else {
//parse_this("nvdcve-2004.xml");
// other possibilities:
//parse_this("http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml");
//parse_this("http://nvd.nist.gov/download/nvdcve-2014.xml");
parse_this("http://nvd.nist.gov/download/nvdcve-recent.xml");
//
// The following do not work with vanilla PHP installs
// without the wrapper "https" (e.g., MacOS X default install)
//parse_this("https://nvd.nist.gov/download/nvdcve-2002.xml");
//parse_this("https://nvd.nist.gov/download/nvdcve-2003.xml");
//parse_this("https://nvd.nist.gov/download/nvdcve-2004.xml");
//parse_this("https://nvd.nist.gov/download/nvdcve-2005.xml");
//parse_this("https://nvd.nist.gov/download/nvdcve-2006.xml");
}
echo "Found $count_entries entries in this NVD location\n";
die; // not necessary, added for clarity
function startElement($parser, $name, $attrs) {
global $all_data, $CVE, $debug, $output, $insert, $vendor, $vendors, $product, $products, $vers, $count_entries, $desc_type, $html_output, $vendor_array;
if ($debug) {
echo "processing tag named '$name'\n";
}
//
// the switch statement should enumerate all of the possible name tags.
// State is kept using global variables, so that nested tags can
// access their context.
//
switch ($name) {
case 'ENTRY':
$count_entries++;
// get name and publication date
$CVE = validate_CVE($attrs['NAME']);
$published = validate_date($attrs['PUBLISHED']);
$CVSS_score = '';
$severity = '';
$CVSS_base = '';
$custom_severity = '';
if (isset($attrs['CVSS_SCORE'])) {
$CVSS_score = $attrs['CVSS_SCORE'];
}
if (isset($attrs['SEVERITY'])) {
$severity = $attrs['SEVERITY'];
}
if (isset($attrs['CVSS_BASE_SCORE'])) {
$CVSS_base = $attrs['CVSS_BASE_SCORE'];
}
//assign custom severity here
//example:
if (floatval($CVSS_base) > 4) {
$custom_severity = 'HIGH';
} else if (floatval($CVSS_base) > 3) {
$custom_severity = 'MEDIUM';
} else {
$custom_severity = 'LOW';
}
$vendors = '';
$products = '';
$versions = '';
// other attributes, such as severity, are available
// see http://nvd.nist.gov/download/nvdcve-xmldoc.cfm
if ($debug) {
echo "found CVE entry $CVE with publication date ". $attrs['PUBLISHED']."\n";
}
if ($output) {
if ($html_output) {
echo "\"<a href = \"http://web.nvd.nist.gov/view/vuln/detail?vulnId=$CVE\">$CVE</a>\",";
}
else {
echo "\"$CVE\",";
}
echo "\"$published\",\"$CVSS_score\",\"$severity\",\"$custom_severity\",";
}
break;
case 'DESCRIPT':
// if there ever was more than one type of description
// remember which source with a global
// NVD or CVE
$desc_type = $attrs['SOURCE'];
// reset data accumulator
$all_data = '';
// need to wait for end tag to get contents
//if ($output) {
// echo "\"$desc_type\",";
//}
break;
case 'PROD':
$vendor = $attrs['VENDOR'];
$product = $attrs['NAME'];
$vendor_array = '';
$vers = '';
if ($CVE == '') {
echo "error, no CVE number";
die;
}
if ($debug) {
echo "found vendor $vendor";
echo " found product $product\n";
}
if ($vendor == "") {
// this happens
echo "NVD integrity alert: no vendor for product $product, CVE is $CVE\n";
}
if ($product == "") {
echo "NVD integrity alert: no product, CVE is $CVE\n";
}
if ($output) {
$vendor_array .= "$vendor - $product;";
//$products .= $product;
echo "\"$vendor\",\"$product\",";
}
break;
case 'LOSS_TYPES':
break;
case 'VULNS_TYPES':
break;
case 'REF':
// source, url, etc...
if ($debug) {
echo " reference from ". $attrs['SOURCE'];
echo " is available at ". $attrs['URL'];
echo "\n";
}
$all_data = '';
// need to wait for end tag to get contents
#if ($output) {
# echo "\"" . $attrs['SOURCE'] . "\",\"" . $attrs['URL'] . "\",";
#}
break;
case 'VERS':
if (isset($attrs['NUM'])) {
$vers .= $attrs['NUM'];
}
if (isset($attrs['EDITION'])) {
$vers .= ' ' . $attrs['EDITION'];
}
if ($debug) {
echo " version ". $attrs['NUM'];
echo " of product $product is vulnerable\n";
}
if ($output) {
echo "\"$vers\",";
}
break;
case '':
break;
}
}
function endElement($parser, $name) {
global $all_data, $CVE, $debug, $output, $desc_type, $vendor_array;
switch ($name) {
case 'ENTRY':
$CVE = '';
if ($output) {
echo "<br>";
}
break;
case 'DESCRIPT':
if ($CVE == '') {
echo "error, no CVE number";
die;
}
if ($desc_type != 'cve') {
if ($debug) {
echo "Found non CVE description, skipping\n";
}
} else {
if ($output) {
echo "\"". validate_output($all_data) ."\",";
}
}
if ($debug) {
echo "found description $all_data\n for CVE entry $CVE\n";
}
// reset data accumulator
$all_data = '';
break;
case 'REF':
if ($debug) {
echo "found reference content '$all_data'\n for CVE entry $CVE\n";
}
// reset data accumulator
$all_data = '';
break;
case 'AVAIL':
if ($debug) {
echo "loss type is availability\n";
}
break;
case 'CONF':
if ($debug) {
echo "loss type is confidentiality\n";
}
break;
case 'INT':
if ($debug) {
echo "loss type is integrity\n";
}
break;
case 'SEC_PROT':
if ($debug) {
echo "loss type is security protection\n";
}
break;
case 'ACCESS':
if ($debug) {
echo "vulnerability type is access\n";
}
break;
case 'INPUT':
if ($debug) {
echo "vulnerability type is input\n";
}
break;
case 'DESIGN':
if ($debug) {
echo "vulnerability type is design\n";
}
break;
case 'EXCEPTION':
if ($debug) {
echo "vulnerability type is exception\n";
}
break;
case 'ENV':
if ($debug) {
echo "vulnerability type is environment\n";
}
break;
case 'CONFIG':
if ($debug) {
echo "vulnerability type is configuration\n";
}
break;
case 'RACE':
if ($debug) {
echo "vulnerability type is race\n";
}
break;
case 'OTHER':
if ($debug) {
echo "vulnerability type is other\n";
}
break;
case 'REMOTE':
if ($debug) {
echo "vulnerability range is remote\n";
}
break;
case 'LOCAL':
if ($debug) {
echo "vulnerability range is local\n";
}
break;
case 'USER_INIT':
if ($debug) {
echo "vulnerability range is through a user\n";
}
break;
case 'VULN_SOFT':
if ($debug) {
echo "vulnerable software\n";
}
if ($output) {
//not ready for go-live, turns out it is hard to show dynamically formed data in csv format
//echo "\"$vendor_array\",";
}
$vendor_array = '';
break;
case '':
break;
}
}
function characterData($parser, $data) {
global $all_data;
// concatenate due to bug with & according to a user entry in PHP docs
// entry by sam at cwa dot co dot nz, Sept 2000
// This issue might have been fixed later. The fix can't hurt though.
$all_data .= $data;
//echo $all_data;
}
function parse_this($url) {
// code for this function is from http://us2.php.net/xml
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($url, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
function validate_CVE($input) {
// expecting something like CAN-2004-0002
// NVD documentation suggests this reg exp,
// (http://nvd.nist.gov/download/nvdcve-xmldoc.cfm)
// (CAN|CVE)-/d/d/d/d-/d/d/d/d
// which doesn't work. See below
if (preg_match('/(CAN|CVE)-\d\d\d\d-\d\d\d\d/', $input, $matches)) {
return $matches[0];
} else {
// if there was an error (false) or if 0 matches
die ("Invalid CVE number encountered: $input");
}
}
function validate_output($input) {
// escapes anything with double quotes
// to conform to csv standard
// by converting double quotes to single
if (preg_match('/"/', $input)) {
return preg_replace('/\"/', '""', $input);
}
return $input;
}
function validate_Date($input) {
// expecting something like 2004-03-03
if (preg_match('/\d\d\d\d-\d\d-\d\d/', $input, $matches)) {
return $matches[0];
} else {
// if there was an error (false) or if 0 matches
die ("Invalid date encountered: $input");
}
}
function print_header() {
echo "\"CVE\",\"Published Date\",\"CVSS Score\",\"CVE Severity\",\"Custom Severity\",\"Description\",\"Vendor - Product - Version\"<br>";
}
?>