#!/usr/bin/perl
require 'config/paths.pl';
$flag = 0;
$cert = "";
$arg = $ARGV[0];
if (! "$arg") {
  print "usage get_cert.pl <VUxxxxx>, <CA-xxxx-xx>, <IN-xxxx-xx> or <TAxx-xxx>\n\n";
  exit;
}
if ($arg =~ /VU/) {
  $_arg = $arg;
  $_arg =~ s/VU//g;
  $_arg =~ s/\n//g;
  $testurl = "/vuls/id/$_arg";
  $args = "-B 40000 -bs \"GET $testurl HTTP/1.0\n\n\" ";
  open(TCP_SCAN, "$TCP_SCAN $args www.kb.cert.org 80|");
  while (<TCP_SCAN>) {
    $_ =~ s/\\n/\n/g;
    $cert .= $_;
  } 
  open (TMP,"> cert_tmp.$$");
  open (CERT, "> $arg.html");
  print  TMP "$cert";
  close (TMP);

  open (TMP, "cert_tmp.$$");
  while (<TMP>) {
    $flag = 1 if (/\<H1\>Vulnerability Note VU\#/);
    $flag = 0 if (/If you have feedback, comments, or additional information /);
    if (/HREF=\/[A-Z][A-Z][A-Z][A-Z]-/) {
      $_ =~ s/A HREF="/A HREF="http:\/\/www.kb.cert.org\/vuls\/id\//g;
    }
    print CERT if $flag == 1;
    $printing = 1 if $flag == 1;
  }
    if ($printing == 1) {
      print CERT "<CENTER><A HREF=\"http://www.cert.org/legal_stuff/legal_stuff.html\">CERT Copyright Material</A></CENTER>\n";
      close (CERT);
    } else {
      print "CERT Vulnerability not found\n\n";
      close (CERT);
      unlink ("$arg.html");
    }
  unlink("cert_tmp.$$");
} elsif ($arg =~ /CA-/ || $arg =~ /IN-/) {

  $testurl = "/advisories/$arg.html";
  $testurl = "/incident_notes/$arg.html";
  $args = "-B 40000 -bs \"GET $testurl HTTP/1.0\n\n\" ";
  open(TCP_SCAN, "$TCP_SCAN $args www.cert.org 80|");
  while (<TCP_SCAN>) {
    $_ =~ s/\\n/\n/g;
    $cert .= $_;
  }
  open (TMP,"> cert_tmp.$$");
  open (CERT, "> $arg.html");
  print  TMP "$cert";
  close (TMP);

  open (TMP, "cert_tmp.$$");
  while (<TMP>) {
    $flag = 1 if (/\<H1\>CERT\<SUP\>\&reg\<\/SUP\> Advisory/i);
    $flag = 1 if (/\<H1\>CERT\<SUP\>\&reg\;\<\/SUP\> Advisory/i);
    $flag = 1 if (/\<h1\>CERT\<SUP\>\&reg\<\/SUP\> Incident/i);
    $flag = 1 if (/\<h1\>CERT\<SUP\>\&reg\;\<\/SUP\> Incident/i);
    $flag = 0 if (/\<\/BODY\>/);
    print CERT if $flag == 1;
    $printing = 1 if $flag == 1;
  }
    if ($printing == 1) {
      print CERT "<CENTER><A HREF=\"http://www.cert.org/legal_stuff/legal_stuff.html\">CERT Copyright Material</A></CENTER>\n";
      close (CERT);
    } else {
      print "CERT Vulnerability not found\n\n";
      close (CERT);
      unlink ("CV$arg");
    }
  unlink("cert_tmp.$$");
} elsif ($arg =~ /TA[0-9][0-9]-/) {

  $testurl = "/cas/techalerts/$arg.html";
  $args = "-B 40000 -bs \"GET $testurl HTTP\1.0\n\n\" ";
  open(TCP_SCAN, "$TCP_SCAN $args www.us-cert.gov 80|");
  while (<TCP_SCAN>) {
      $_ =~ s/\\n/\n/g;
      $cert .= $_;
  }
  open (TMP,"> cert_tmp.$$");
  open (CERT, "> $arg.html");
  print  TMP "$cert";
  close (TMP);

  open (TMP, "cert_tmp.$$");
  while (<TMP>) {
    if (/\<small\>\<h2\>/i) {
      $flag = 1;
      ($dum, $line) = split (/\<small\>\<h2\>/);
      $_ = "\<small\>\<h3\>$arg - $line\<\/h3\>";
    }  
    $flag = 0 if (/\<\/small\>\<\/p\>/);
    print CERT if $flag == 1;
    $printing = 1 if $flag == 1;
  }
  if ($printing == 1) {
    close (CERT);
  } else {
     print "CERT Vulnerability not found\n\n";
     close (CERT);
     unlink ("CV$arg");
  }
  unlink("cert_tmp.$$");

} else {
        print "CERT Vulnerability prefix not found\n\n";
}
