candyapplecorn

nist vulnerability extractor 2

Jan 1st, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.21 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use WWW::Curl::Simple;
  3. use strict;
  4. use warnings;
  5.  
  6. # The path to this script's stored files
  7. my $bpath = "/home/user/bin/vulns/";
  8. # Store last time accessed in var lasta
  9. my $lafile = $bpath . 'lastaccessed.txt',
  10. my $outfile = 'payload.txt',
  11. my $dif,
  12. my $week = 7 * 24 * 60 * 60;
  13.  
  14. sub getXML {
  15.     # If it's been less than one week,
  16.     # just get the modified file
  17.     if ($dif < $week) {
  18.         getFile("modified.xml.gz",
  19.             'https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-Modified.xml.gz');
  20.         return;
  21.     }
  22.     # However if it's been more than a week,
  23.     # get all the files except for modified + recent
  24.     # https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-2002.xml.gz
  25.     for (my $year = 2002, my $result = 0; $result == 0 && $year < 2030;$year++) {
  26.         my $url = "https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-".$year.".xml.gz";
  27.         my $filename = $url;
  28.         $filename =~ s/.*(nvdcve.*)/$1/;
  29.         print "Downloaded ".$filename . "\n";
  30.         # get the file. If there's an error, $result will == 1
  31.         my $result = getFile($filename, $url);
  32.         print $result . " " . $year . "\n";
  33.     }
  34. }
  35.  
  36. # Sub getFile - Curls a compressed gz file from a url,
  37. # deletes the older uncompressed version, and then
  38. # uncompresses the new archive file.
  39. # @params:
  40. #   filename: the name to save the archive file as
  41. #   url: the location of the archive download
  42. # @returns:
  43. #   0 on success, 1 on failure
  44. sub getFile {
  45.     my ($filename, $url) = @_;
  46.     my $unzippedName = $filename;
  47.     $unzippedName =~ s/\.gz//;
  48.  
  49.     my $curl = WWW::Curl::Simple->new();
  50.     my $res  = $curl->get($url);
  51.     # If there was an error with the request,
  52.     # as in, we requested a file that doesn't
  53.     # exist (like a file for year 2100), stop.
  54.     return 1 if $res->is_error;
  55.  
  56.     open(my $fh, '>', $bpath . $filename) or die "Could not open file $filename $!";
  57.     print $fh $res->decoded_content;
  58.     close $fh;
  59.     system ("find $bpath -name $unzippedName -exec rm {} \\;");
  60.     system ("gunzip " . $bpath . $filename);
  61.     return 0;
  62. }
  63.  
  64. # sub calcdif
  65. # Read in the contents of the lastAccessedFile
  66. # Use that date to calculate the elapsed time
  67. # since this script ran
  68. # @POSTCONDITION:
  69. #   $lasta will contain the last time this script ran
  70. #   $dif will contain the elapsed time since script ran
  71. sub calcdif {
  72.     my $lasta = '';
  73.     if (open(my $fh, '<:encoding(UTF-8)', $lafile)) {
  74.         while (my $row = <$fh>) {
  75.             chomp $row;
  76.             $lasta = $row;
  77.         }
  78.     } else {
  79.         warn "Could not open file '$lafile' $!";
  80.     }
  81.     # if lasta is null, put some old date in
  82.     $lasta = int (time() / 2) if ($lasta eq "");
  83.     $dif = time() - $lasta;
  84. }
  85.  
  86. # updateLA updates the last accessed time for this program.
  87. # That time is stored as seconds since 1970, in a file.
  88. sub updateLA {
  89.     open(my $fh, '>', $lafile) or die "Could not open file '$lafile' $!";
  90.     print $fh time();
  91.     close $fh;
  92. }
  93.  
  94. sub xmlToCols {
  95.     opendir DIR, $bpath or die "cannot open dir $bpath: $!";
  96.     my @files = readdir DIR;
  97.     closedir DIR;
  98.  
  99.     if ($dif < $week) {
  100.         @files = ("modified.xml");
  101.     }
  102.     else {
  103.         @files = grep { /nvdcve.*xml/ } @files;
  104.     }
  105.     foreach my $xml (@files) {
  106.         my $txt = $xml;
  107.         $txt =~ s/xml/txt/;
  108.         print "Creating \"" . $txt . "\" from \"" . $xml . "\"\n";
  109.  
  110.         if (open(my $fh, '<:encoding(UTF-8)', $bpath . $xml)) {
  111.             open(my $out, '>:encoding(UTF-8)', $bpath . $txt) or die "Could not open file '$txt' $!";
  112.             while (my $row = <$fh>) {
  113.                 chomp $row;
  114.                 if ($row =~ /<entry id="CVE.+">/){
  115.                     $row =~ s/.*(CVE.*)">/$1/;
  116.                     print $out $row; # row is eq CVE ..
  117.                 }
  118.                 if ($row =~ /<cvss:score>\d+\.\d*</) {
  119.                     $row =~ s/.*>(\d+\.\d*)<.*/$1/;
  120.                     print $out "\t" . $row; # lineline  is a score
  121.                 }
  122.                 if ($row =~ /<\/entry/) {
  123.                     print $out "\n"; # end of an entry
  124.                 }  
  125.             }
  126.             close $fh;
  127.         } else {
  128.             warn "Could not open file '$xml' $!";
  129.         }
  130.         close $txt;
  131.     }
  132. }
  133. sub mergePayload {
  134.     opendir DIR, $bpath or die "cannot open dir $bpath: $!";
  135.     my @files = readdir DIR;
  136.     closedir DIR;
  137.  
  138.     if ($dif < $week) {
  139.         @files = ("modified.txt");
  140.     }
  141.     else {
  142.         @files = grep { /nvdcve.*txt/ } @files;
  143.     }
  144.     # Overwrite old payload file
  145.     open(my $out, '>:encoding(UTF-8)', $bpath . $outfile) or die "Could not open file '$outfile' $!";
  146.     print $out "";
  147.     close $out;
  148.  
  149.     # For each CVE, add it to payload file
  150.     open($out, '>>:encoding(UTF-8)', $bpath . $outfile) or die "Could not open file '$outfile' $!";
  151.     foreach my $txt (@files) {
  152.         open(my $in, '<:encoding(UTF-8)', $bpath . $txt) or die "Could not open file '$txt' $!";
  153.         print "file: " .$txt . "\n";
  154.         while (my $row = <$in>) {
  155.             print $out $row;
  156.         }
  157.     }
  158.     close $out;
  159. }
  160. calcdif();
  161. updateLA();
  162. # just hard code it since want everything every time
  163. $dif = $week * 2;
  164. getXML();
  165. xmlToCols();
  166. mergePayload();
Advertisement
Add Comment
Please, Sign In to add comment