| 1 |
#!/usr/bin/perl
|
| 2 |
|
| 3 |
use strict;
|
| 4 |
use File::Temp;
|
| 5 |
use Getopt::Std;
|
| 6 |
#use Smart::Comments;
|
| 7 |
use Term::ReadLine;
|
| 8 |
|
| 9 |
my %opts;
|
| 10 |
getopts('ln:fhi:t:Tca:', \%opts);
|
| 11 |
|
| 12 |
if ($opts{h}) {
|
| 13 |
print <<'EOF';
|
| 14 |
downloads allitems.txt from cve.mitre.org and shows full decription for each
|
| 15 |
"TODO: check" item (2003 and newer). Then
|
| 16 |
|
| 17 |
- tries to guess product name and php filename and does
|
| 18 |
apt-cache and apt-file search
|
| 19 |
- waits for input:
|
| 20 |
* blank line to skip to next issue
|
| 21 |
* .fname to do "apt-file search name"
|
| 22 |
* .cname to do "apt-cache search name"
|
| 23 |
* .mpackage to search data/embedded-code-copies for "package"
|
| 24 |
* v or e to launch an editor with the current item
|
| 25 |
* q to save and quit
|
| 26 |
* CTRL-C to quit without saving
|
| 27 |
* everything else is inserted as product name for a NOT-FOR-US
|
| 28 |
|
| 29 |
Use "svn diff" and "svn revert" as needed ;-)
|
| 30 |
|
| 31 |
OPTIONS: [ -l [-n <n>] [-f] ]
|
| 32 |
-l : just list issues
|
| 33 |
-n <n> : show max n lines of each description (default 2)
|
| 34 |
-f : show full CVE/list entry as well
|
| 35 |
-i regexp : use regexp to select issues (default: 'CVE-200[3-9]' )
|
| 36 |
-t regexp : use regexp to select todos (default: '^\s+TODO: check$' )
|
| 37 |
-T : same as -t '^\s+TODO: check' (note the missing $)
|
| 38 |
-c : only do syntax check of embedded-code-copies
|
| 39 |
-a <n> : If automatic apt-cache/apt-file search gives more than n results,
|
| 40 |
display only the count (default 10)
|
| 41 |
|
| 42 |
EOF
|
| 43 |
|
| 44 |
exit(0);
|
| 45 |
}
|
| 46 |
|
| 47 |
# TODO/BUGS:
|
| 48 |
# - go back to previous issue / undo
|
| 49 |
# - handle entries with several TODO lines
|
| 50 |
# - handle claimed-by
|
| 51 |
# - look for ITPs?
|
| 52 |
|
| 53 |
|
| 54 |
my $basedir;
|
| 55 |
if (-e "secure-testing/data/CVE/list") {
|
| 56 |
$basedir="secure-testing";
|
| 57 |
} elsif (-e "data/CVE/list") {
|
| 58 |
$basedir=".";
|
| 59 |
} elsif (-e "../data/CVE/list") {
|
| 60 |
$basedir="..";
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
my $embed_code = {};
|
| 65 |
my $embed_pkg = {};
|
| 66 |
my $embed_errors;
|
| 67 |
|
| 68 |
read_embedded_copies();
|
| 69 |
|
| 70 |
if ($opts{c}) {
|
| 71 |
exit($embed_errors);
|
| 72 |
}
|
| 73 |
|
| 74 |
|
| 75 |
my $datafile="$basedir/data/CVE/list";
|
| 76 |
my $allitemsfile="gunzip -c $basedir/../allitems.txt.gz|";
|
| 77 |
my $allitemsurl="http://cve.mitre.org/data/downloads/allitems.txt.gz";
|
| 78 |
|
| 79 |
my $issue_regexp= $opts{i} || 'CVE-200[3-9]';
|
| 80 |
my $todo_regexp= $opts{t} || ( $opts{T} ? '^\s+TODO: check' : '^\s+TODO: check$' );
|
| 81 |
my $auto_display_limit = 10;
|
| 82 |
$auto_display_limit = $opts{a} if defined $opts{a};
|
| 83 |
|
| 84 |
my $editor=$ENV{EDITOR} || $ENV{VISUAL} || "vi";
|
| 85 |
|
| 86 |
system "cd $basedir/.. ; wget -N $allitemsurl";
|
| 87 |
|
| 88 |
|
| 89 |
print "Reading data...\n";
|
| 90 |
|
| 91 |
my $entries=read_file($datafile, qr/^CVE/ );
|
| 92 |
my $CVEs=read_file($allitemsfile, qr/^=+$/ );
|
| 93 |
my $data;
|
| 94 |
my @todos;
|
| 95 |
my %afcache;
|
| 96 |
|
| 97 |
foreach my $cve (@{$CVEs}) {
|
| 98 |
$cve =~ /^Name:\s*(CVE\S+)/m or next;
|
| 99 |
$data->{$1}->{CVE}=\$cve;
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
foreach my $entry (@{$entries}) {
|
| 104 |
my $name;
|
| 105 |
if ( $entry =~ /^(CVE-....-\d\d\d\d)/ ) {
|
| 106 |
$name=$1;
|
| 107 |
}
|
| 108 |
elsif ( $entry =~ /^(CVE-....-XXXX.*)\n/ ){
|
| 109 |
$name=$1;
|
| 110 |
}
|
| 111 |
else {
|
| 112 |
die "invlid entry:\n$entry";
|
| 113 |
}
|
| 114 |
$data->{$name}->{entry}=\$entry;
|
| 115 |
if ( $entry =~ /$todo_regexp/m
|
| 116 |
and $name =~ /$issue_regexp/ ) {
|
| 117 |
push @todos, $name;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
print scalar(@{$CVEs}), " CVEs, ",
|
| 122 |
scalar(@{$entries}) - scalar(@{$CVEs}), " temp issues, ",
|
| 123 |
scalar(@todos), " todos matching /$todo_regexp/\n";
|
| 124 |
|
| 125 |
if ($opts{l}) {
|
| 126 |
#list only
|
| 127 |
foreach my $todo (reverse sort @todos) {
|
| 128 |
my $desc=description($todo);
|
| 129 |
if ($desc) {
|
| 130 |
my $lines=$opts{n} || 2;
|
| 131 |
if ($desc =~ /((?:.*\n){1,$lines})/) {
|
| 132 |
$desc = $1;
|
| 133 |
$desc =~ s/^/ /mg;
|
| 134 |
if ($opts{f}) {
|
| 135 |
print ${$data->{$todo}->{entry}}, $desc;
|
| 136 |
}
|
| 137 |
else {
|
| 138 |
print "$todo:\n$desc";
|
| 139 |
}
|
| 140 |
}
|
| 141 |
}
|
| 142 |
else {
|
| 143 |
print "${$data->{$todo}->{entry}}";
|
| 144 |
}
|
| 145 |
}
|
| 146 |
exit 0;
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
my $term = new Term::ReadLine 'check-new-issues';
|
| 151 |
if ($term->ReadLine() eq 'Term::ReadLine::Stub') {
|
| 152 |
print "Install libterm-readline-gnu-perl to get readline support!\n";
|
| 153 |
}
|
| 154 |
|
| 155 |
TODO: foreach my $todo (reverse sort @todos) {
|
| 156 |
print ${$data->{$todo}->{CVE}} if $data->{$todo}->{CVE};
|
| 157 |
print ${$data->{$todo}->{entry}};
|
| 158 |
|
| 159 |
auto_search($todo);
|
| 160 |
|
| 161 |
READ: while (my $r=$term->readline(">") ) {
|
| 162 |
chomp $r;
|
| 163 |
if ($r =~ /^\s*$/) {
|
| 164 |
next TODO;
|
| 165 |
}
|
| 166 |
elsif ($r=~ /^\.c(.*)$/ ) {
|
| 167 |
my $s = $1;
|
| 168 |
$s =~ tr{a-zA-Z0-9_@-}{ }cs;
|
| 169 |
print "=== apt-cache search $s :\n";
|
| 170 |
system("apt-cache search $s|less -FX");
|
| 171 |
print "===\n";
|
| 172 |
next READ;
|
| 173 |
}
|
| 174 |
elsif ($r=~ /^\.f(.*)$/ ) {
|
| 175 |
my $s = $1;
|
| 176 |
$s =~ s/^\s*(.*?)\s*$/$1/;
|
| 177 |
$s = quotemeta($s);
|
| 178 |
print "=== apt-file search $s:\n";
|
| 179 |
system("apt-file search $s|less -FX");
|
| 180 |
print "===\n";
|
| 181 |
next READ;
|
| 182 |
}
|
| 183 |
elsif ($r=~ /^\.m(.*)$/ ) {
|
| 184 |
my $s = $1;
|
| 185 |
$s =~ s/^\s+//;
|
| 186 |
$s =~ s/\s+$//;
|
| 187 |
print "references to $s in embedded-code-copies:\n";
|
| 188 |
search_embed($s) or print "none\n";
|
| 189 |
next READ;
|
| 190 |
}
|
| 191 |
elsif ($r=~ /^q$/i ) {
|
| 192 |
last TODO;
|
| 193 |
}
|
| 194 |
elsif ($r=~ /^[ve]$/i ) {
|
| 195 |
my $newentry=edit_entry(${$data->{$todo}->{entry}});
|
| 196 |
if ( $newentry eq ${$data->{$todo}->{entry}} ) {
|
| 197 |
print "Not changed.\n";
|
| 198 |
next READ;
|
| 199 |
}
|
| 200 |
else {
|
| 201 |
${$data->{$todo}->{entry}}=$newentry;
|
| 202 |
print "New entry set to:\n$newentry";
|
| 203 |
next TODO;
|
| 204 |
}
|
| 205 |
}
|
| 206 |
else {
|
| 207 |
${$data->{$todo}->{entry}} =~
|
| 208 |
s/^\s*TODO: check/\tNOT-FOR-US: $r/m ;
|
| 209 |
print "New entry set to:\n${$data->{$todo}->{entry}}";
|
| 210 |
next TODO;
|
| 211 |
}
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
open(my $fh, ">", $datafile);
|
| 216 |
print $fh @{$entries};
|
| 217 |
close($fh);
|
| 218 |
|
| 219 |
sub description {
|
| 220 |
my $name=shift;
|
| 221 |
|
| 222 |
defined $data->{$name}->{CVE} or return "";
|
| 223 |
|
| 224 |
${$data->{$name}->{CVE}} =~ /\n\n(.*)^Current Votes:/ms;
|
| 225 |
my $desc = $1;
|
| 226 |
$desc =~ s/\n\n+/\n/;
|
| 227 |
|
| 228 |
return $desc;
|
| 229 |
}
|
| 230 |
|
| 231 |
sub read_file
|
| 232 |
{
|
| 233 |
my $file=shift;
|
| 234 |
my $re=shift;
|
| 235 |
|
| 236 |
|
| 237 |
open(my $fh, $file) or die "could not open $file";
|
| 238 |
|
| 239 |
my @data;
|
| 240 |
my $cur="";
|
| 241 |
while (my $line=<$fh>) {
|
| 242 |
if ($line =~ $re and $cur) {
|
| 243 |
push @data, $cur;
|
| 244 |
$cur = "";
|
| 245 |
}
|
| 246 |
$cur.=$line;
|
| 247 |
}
|
| 248 |
push @data, $cur if $cur;
|
| 249 |
|
| 250 |
close($fh);
|
| 251 |
|
| 252 |
|
| 253 |
return \@data;
|
| 254 |
}
|
| 255 |
|
| 256 |
|
| 257 |
sub edit_entry {
|
| 258 |
my $entry=shift;
|
| 259 |
my $tmp=new File::Temp();
|
| 260 |
my $tmpname=$tmp->filename;
|
| 261 |
print $tmp $entry;
|
| 262 |
close $tmp;
|
| 263 |
system "$editor $tmpname";
|
| 264 |
|
| 265 |
local $/; #slurp
|
| 266 |
open($tmp, $tmpname);
|
| 267 |
return <$tmp>;
|
| 268 |
|
| 269 |
}
|
| 270 |
|
| 271 |
sub auto_search {
|
| 272 |
my $name=shift;
|
| 273 |
|
| 274 |
my $desc=description($name);
|
| 275 |
$desc =~ s/[\s\n]+/ /g;
|
| 276 |
|
| 277 |
my $file;
|
| 278 |
my $prog;
|
| 279 |
if ( $desc =~ /^(\S+(?: [A-Z]\w*)*) \d/ ) {
|
| 280 |
$prog = $1;
|
| 281 |
}
|
| 282 |
elsif ( $desc =~ / in (\S+\.\S+) in (?:the )?(\S+) / ) {
|
| 283 |
$file = $1;
|
| 284 |
$prog = $2;
|
| 285 |
}
|
| 286 |
elsif ( $desc =~ / in (?:the )?(\S+) / ) {
|
| 287 |
$prog = $1;
|
| 288 |
}
|
| 289 |
if ($prog) {
|
| 290 |
my $prog_esc = quotemeta($prog);
|
| 291 |
print "doing apt-cache search...";
|
| 292 |
my @ac=`apt-cache search $prog_esc`;
|
| 293 |
if (scalar @ac > $auto_display_limit || scalar @ac == 0) {
|
| 294 |
print "\r", scalar @ac, " results from apt-cache search $prog_esc\n";
|
| 295 |
}
|
| 296 |
else {
|
| 297 |
print "\r=== apt-cache search $prog_esc:\n", @ac, "===\n";
|
| 298 |
}
|
| 299 |
|
| 300 |
foreach my $p (split /\s+/, $prog) {
|
| 301 |
search_embed($p);
|
| 302 |
}
|
| 303 |
}
|
| 304 |
if ( $file =~ /^(?:index|default|login|search|admin)\.(?:php3?|asp|cgi|pl)$/i ) {
|
| 305 |
return;
|
| 306 |
}
|
| 307 |
if ( $file =~ /(php3?|asp|cgi|pl)$/ ) {
|
| 308 |
if (! exists $afcache{$file}) {
|
| 309 |
my $file_esc = quotemeta($file);
|
| 310 |
print "doing apt-file search...";
|
| 311 |
$afcache{$file}=[`apt-file -i search $file_esc`];
|
| 312 |
if (scalar @{$afcache{$file}} > $auto_display_limit) {
|
| 313 |
# replace with empty array to save mem
|
| 314 |
my $num = scalar @{$afcache{$file}};
|
| 315 |
$afcache{$file} = [];
|
| 316 |
$afcache{$file}->[$num-1] = undef;
|
| 317 |
}
|
| 318 |
}
|
| 319 |
if (scalar @{$afcache{$file}} > $auto_display_limit ||
|
| 320 |
scalar @{$afcache{$file}} == 0) {
|
| 321 |
print "\r", scalar @{$afcache{$file}},
|
| 322 |
" results from apt-file -i search $file\n";
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
print "\r=== apt-file -i search $file:\n", @{$afcache{$file}}, "===\n";
|
| 326 |
}
|
| 327 |
}
|
| 328 |
}
|
| 329 |
|
| 330 |
sub read_embedded_copies {
|
| 331 |
open(my $fh, "$basedir/data/embedded-code-copies");
|
| 332 |
|
| 333 |
# skip comments
|
| 334 |
while (<$fh>) {
|
| 335 |
last if /^---BEGIN/;
|
| 336 |
}
|
| 337 |
|
| 338 |
my ($code, $pkg);
|
| 339 |
while (my $line = <$fh>) {
|
| 340 |
if ($line =~ /^([-\w]+)/) {
|
| 341 |
$code = lc($1);
|
| 342 |
$pkg = undef;
|
| 343 |
if (exists $embed_code->{$code}) {
|
| 344 |
syntax_error("Duplicate embedded code $code")
|
| 345 |
}
|
| 346 |
}
|
| 347 |
elsif ($line =~ /^\s*$/) {
|
| 348 |
$code = undef;
|
| 349 |
$pkg = undef;
|
| 350 |
}
|
| 351 |
elsif ($line =~ /^\s+(?:\[\w+\]\s+)?-\s+(\w[\w.-]+)/) {
|
| 352 |
$pkg = $1;
|
| 353 |
$line =~ s/^\s+//;
|
| 354 |
if ($embed_code->{$code}->{$pkg}) {
|
| 355 |
$embed_code->{$code}->{$pkg} .= $line;
|
| 356 |
}
|
| 357 |
else {
|
| 358 |
$embed_code->{$code}->{$pkg} = $line;
|
| 359 |
push @{$embed_pkg->{$pkg}}, $code;
|
| 360 |
}
|
| 361 |
}
|
| 362 |
elsif ($line =~ /^\s+(?:NOTE|TODO)/) {
|
| 363 |
$line =~ s/^\s+//;
|
| 364 |
if ($pkg) {
|
| 365 |
$embed_code->{$code}->{$pkg} .= $line;
|
| 366 |
}
|
| 367 |
}
|
| 368 |
else {
|
| 369 |
syntax_error("Cannot parse $line");
|
| 370 |
}
|
| 371 |
}
|
| 372 |
}
|
| 373 |
|
| 374 |
sub syntax_error {
|
| 375 |
$embed_errors=1;
|
| 376 |
print STDERR "embedded-code-copies:$.: @_\n";
|
| 377 |
}
|
| 378 |
|
| 379 |
sub search_embed {
|
| 380 |
my $text = shift;
|
| 381 |
my $found = 0;
|
| 382 |
$text = lc($text);
|
| 383 |
if (exists $embed_code->{$text}) {
|
| 384 |
print "$text is embedded by: ",
|
| 385 |
join(" ", sort keys %{$embed_code->{$text}}),
|
| 386 |
"\n";
|
| 387 |
$found = 1;
|
| 388 |
}
|
| 389 |
if (exists $embed_pkg->{$text}) {
|
| 390 |
print "$text embeds: ",
|
| 391 |
join(" ", sort @{$embed_pkg->{$text}}),
|
| 392 |
"\n";
|
| 393 |
$found = 1;
|
| 394 |
}
|
| 395 |
return $found;
|
| 396 |
}
|