-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvesearh.php
executable file
·259 lines (252 loc) · 9.34 KB
/
cvesearh.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
<?php
/**
* FileName: cvesearh.php
*
* PHP version 5
*
* @category Plot
* @package None
* @author aka <[email protected]>
* @license http://localhost/lic.html custom
* @link None
* Description: Search the CVE Database, php script.
* The script downloads the required files, if not present.
* You can search the CVE files based on ID or keyword
* Version: 1.0
* Created: Fri Sep 21, 2012 04:14PM
* Revision: none
* Author: Amit Agarwal (aka)
* Company: http://blog.amit-agarwal.co.in
* Last modified: Tue Dec 14, 2021 01:09PM
*/
global $debug;
$debug = 0;
global $mycve;
if ($debug) print "<pre>";
$headhtmlorig = <<<EOF
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>PHP CVE Search - Amit Agarwal </title>
<!-- table style -->
<style>
table, th, td { border: 1px solid black; }
table { border-collapse: collapse; }
table, th, td { border: 1px solid black; }
table, td, th { border: 1px solid green; overflow: hidden; word-wrap: break-word; }
th { background-color: green; color: white; }
#cvetable { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; width: 100%; border-collapse: collapse; white-space: nowrap; }
#cvetable td, #cvetable th { font-size: 1em; border: 1px solid #98bf21; padding: 3px 7px 2px 7px; text-overflow: ellipsis; }
#cvetable th { font-size: 1.1em; text-align: left; padding-top: 5px; padding-bottom: 4px; background-color: #A7C942; color: #ffffff; }
#cvetable tr.alt td { color: #000000; background-color: #EAF2D3; }
caption { caption-side: top; }
table { border-collapse: collapse; width: 100%; }
table, td, th { border: 1px solid black; }
.collapse{ font-size: 16px; display:block; }
.collapse + input{ display:none; }
.collapse + input + *{ display:none; }
.collapse+ input:checked + *{ display:block; }
// body{ font-family: 'Open Sans', sans-serif; }
/ h1{ font-family: 'Tangerine', serif; font-size: 48px; text-align: center; }
// a:not([class]) { font-family: 'Yanone Kaffeesatz', sans-serif; font-size: 14px; }
</style>
<!-- Google Fonts
<link
href='http://fonts.googleapis.com/css?family=Open+Sans'
rel='stylesheet'
type='text/css'>
<link
rel="stylesheet"
type="text/css"
href="http://fonts.googleapis.com/css?family=Tangerine">
<link
href='http://fonts.googleapis.com/css?family=Inconsolata'
rel='stylesheet'
type='text/css'>
<link
href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz'
rel='stylesheet'
type='text/css'>
-->
</head>
<body>
<h1>CVE Search - Amit Agarwal</h1>
<h1><a class="none" href="http://blog.amit-agarwal.co.in">My Blog</a></h1>
<form action="?">
ID: <input type="text" name="id"> OR
Keyword: <input type="text" name="kwd">
<input type="radio" name="exact" value="true">Exact Match
<input type="submit" value="Submit">
</form>
EOF;
$headhtml = preg_replace('/[\n\r\s]+/', " ", trim($headhtmlorig));
if (!array_key_exists('id', $_REQUEST) || !array_key_exists('kwd', $_REQUEST)) {
debug("No input ... just print the form");
print $headhtml;
} else {
if (array_key_exists('id', $_REQUEST)) $searchid = $_REQUEST['id'];
if (array_key_exists('kwd', $_REQUEST)) $searchkwd = $_REQUEST['kwd'];
if (array_key_exists('exact', $_REQUEST)) $exactmatch = $_REQUEST['exact'];
else $exactmatch = "";
global $mycve;
$headerf = "%20s\t%20s\t%40s\n";
$header = array("CVE ID", "Title", "Notes");
$path = ".";
function DownlodFile($year = 15) {
debug("Getting the file for $year");
file_put_contents("allitems-cvrf-year-$year.xml", file_get_contents("http://cve.mitre.org/data/downloads/allitems-cvrf-year-$year.xml"));
return;
}
function printcve($mcount = 0) {
global $mycve;
//print_r($mycve);
$cve = $mycve->Vulnerability[$mcount]->CVE;
print "<td>" . $mycve->Vulnerability[$mcount]->CVE . "</td>";
print "<td>" . $mycve->Vulnerability[$mcount]->Title . "</td>";
print "<td>";
/*foreach ($mycve->Vulnerability[$mcount] as $key => $value ) {
print "Key: $key, Value=> value\n";
} */
debug($mycve->Vulnerability[$mcount]->CVE);
debug($mycve->Vulnerability[$mcount]->Title);
debug("Notes: \n");
debug($mycve->Vulnerability[$mcount]->Notes);
foreach ($mycve->Vulnerability[$mcount]->Notes->Note as $key => $value) {
debug("Key: $key");
debug($value);
// print "== Note $key: ". $value;
print str_replace(array("\n", "\r"), ' ', $value);
}
echo <<<EOF
<label class="collapse" for="_$mcount">
References $cve (Click to expand) </label>
<input id="_$mcount" type="checkbox">
<div>
EOF;
print "References:<br>";
foreach ($mycve->Vulnerability[$mcount]->References->Reference as $key => $value) {
debug("Key: $key");
debug($value);
//print "== Note $key: ". $value;
print "<a href='" . $value->URL . "'>" . $value->Description . "</a><br>";
}
print "</div></td>\n";
}
$count = 0;
$mcount = 0;
$y = date("y");
for ($i = 8;$i <= $y;$i++) {
$a = $path . sprintf("/%s-20%02d.xml", "allitems-cvrf-year", $i);
if (file_exists($a)) {
debug("File $a exists");
//Do Nothing
} else {
//echo "download file for $i\n";
DownlodFile(sprintf("20%02d", $i));
}
$year = date("Y");
if (time() - filemtime("allitems-cvrf-year-".$year.".xml") > 6*3600) DownlodFile($year);
//print $a."\n";;
$mycve1 = simplexml_load_file($a);
// Un-comment below to see the complete data ...
//foreach ( explode ('\n', print_r($mycve)) as $val ) debug ($val);
//
foreach ($mycve1->Vulnerability as $key => $value) {
if (!empty($searchid)) {
//debug( "Searching with ID: $searchid");
if ($exactmatch == "true") $pat = '/^CVE-\d{4}-' . sprintf("%04d", $searchid) . '/';
else $pat = "/$searchid/";
//debug( "matching pattern : $pati against ".$value->CVE);
if (preg_match($pat, $value->CVE)) {
$mycve->Vulnerability[$mcount++] = $value;
debug("Setting mcount to " . $mcount . "for count $count ");
}
} else if (!empty($searchkwd)) {
$found = 0;
if (preg_match('/\s+/', $searchkwd)) $pat = preg_replace('/\s+/', '|', $searchkwd);
else $pat = $searchkwd;
if ($exactmatch == "true") $pat = '/' . $pat . '/';
else $pat = '/' . $pat . '/i';
//debug( "Searching for $pat");
foreach ($value->Notes->Note as $key => $val) {
if (preg_match($pat, $val)) $found = 1;
}
if (is_array($value->References->Reference)) foreach ($value->References->Reference as $key => $val1) {
debug("Searching in " . $val1->Description);
if (preg_match($pat, $val1->Description)) {
$found = 1;
debug("Found $pat in " . $value->CVE);
}
if (preg_match($pat, $val1->URL)) {
$found = 1;
debug("Found $pat in " . $value->CVE);
}
}
if ($found) {
$mycve->Vulnerability[$mcount++] = $value;
debug("Adding $mcount to CVE for $count");
//print_r($value);
}
}
$count++;
}
}
// print_r ($mycve->Vulnerability);
echo <<<EOF
$headhtml
<table>
<thead>
<tr>
<td>$header[0]</td>
<td>$header[1]</td>
<td>$header[2]</td>
</tr>
</thead>
<tbody>
EOF;
echo "<b>Found $mcount matches</b>";
for ($i = 0;$i < $mcount;$i++) {
//foreach ($mcount as $value) {
if ( $alt ==0 ) { print "<tr class=alt>";$alt=1;}
else { print "<tr>";$alt=0;}
printcve($i);
print "</tr>";
}
echo <<<EOF
</tbody>
</table>
</body>
</html>
EOF;
}
function debug($line = NULL) {
global $debug;
if ($debug == 0) return;
//$a = get_caller();
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$a[3] = end(explode('/', $stack[0]['file']));
$a[2] = $stack[0]['line'];
$a[0] = date('m/d/Y h:i:s a', time());
if (is_array($line)) {
$a[1] = "Printing Array";
printf("DEBUG>> %s|%-100s|%s|%s\n", $a[0], $a[1], $a[2], $a[3]);
foreach ($line as $key => $val) {
if (is_array($val)) {
$a[1] = "Printing array ==> $key";
printf("DEBUG>> %s|%-100s|%s|%s\n", $a[0], $a[1], $a[2], $a[3]);
$iii = explode("\n", print_r($val, true));
foreach ($iii as $jjj) {
$a[1] = "$jjj";
printf("DEBUG>> %s|%-100s|%s|%s\n", $a[0], $a[1], $a[2], $a[3]);
}
} else {
$a[1] = "$key => $val";
printf("DEBUG>> %s|%-100s|%s|%s\n", $a[0], $a[1], $a[2], $a[3]);
}
}
} else {
$l = preg_replace('/[\r\n|\r|\n|\n\r]+/', '', $line);
printf("DEBUG>> %s|%-100s|%s|%s\n", $a[0], $l, $a[2], $a[3]);
}
}
?>