#   This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#  This code update is provided by the Advanced Research Corporation (r) to
#  further the utility of the SARA Report Writer.  We request that the
#  developers do not modify or reverse engineer this code for
#  their own gain.
#
#  Developed and distributed by the Advanced Research Corporation.
#
#
# Generate a report for a list of hosts
#
($_host) = split(/,/, $html_script_args);
$_ip  = $all_hosts{$_host}{IP};
$_mac = $all_hosts{$_host}{MAC};
($_type = $hosttype{$_host}) =~ tr / \//+!/;
($_hosttype = $hosttype{$_host});
$admin = &get_administrator() if $get_admins == 1;
# 
# Get Scan Info
#
$history_file = "results/$sara_data/history";
$history_data = `tail -1 $history_file`;
($d0,$history_date,$history_mode,$d1,$history_version)=split(/\|/,$history_data)
;
($history_date,$d0) = split(/\s/, $history_date);
$history_mode = $all_modes[$history_mode];
$mode_string = $history_mode;
$mode_string =~ s/$/_string/;
$history_mode = "$history_mode ($$mode_string)" if $$mode_string;
#
# Refresh the host type and service tables.
#
&make_hosttype_info();
&make_service_info();
&make_subnet_info();
&make_severity_info();
#
#  Could not figure out Dan's code, so here is a Bob kludge
$_services = "";
$vulnerable="FALSE";
$win_vulnerable="FALSE";
$xml_tmp  = "  <HOST>\r\n";
$xml_tmp .= "    <HOST_IP_ADDRESS>$_ip</HOST_IP_ADDRESS>\r\n";
$xml_tmp .= "    <HOST_DESCRIPTION>\r\n";
$xml_tmp .= "      <HOST_NAME>$_host</HOST_NAME>\r\n";
if ($_mac =~ /:/) {
  $xml_tmp .= "     <HOST_MACAddress>$_mac</HOST_MACAddress>\r\n";
}
$xml_tmp .= "      <HOST_NETBIOS_NAME>NBN Not Available</HOST_NETBIOS_NAME>\r\n";
$xml_tmp .= "      <HOST_TYPE>$_hosttype</HOST_TYPE>\r\n";
$xml_tmp .= "      <HOST_ADMIN>$admin</HOST_ADMIN>\r\n" if $get_admin == 1;
$xml_tmp .= "    </HOST_DESCRIPTION>\r\n";  
$xml_tmp .= "   <VULNERABILITIES>\r\n";  
for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_) if ($system eq "sara");
                        &saint_split($_) if ($system eq "saint");
                        &satan_split($_) if ($system eq "satan");

                  $_severity= $severity_brown_type_info{$service_output};
                  if($_severity) {
                    if ($pro_filter) {
                      $_pro_filter="FALSE";
                      for (split(/\,/, $pro_filter)){
                        $_tmp = $_;
                        $_tmp =~ s/\ //;
                        if ( $_severity =~ "$_tmp") {
                          $_pro_filter = "TRUE";
                        }
                    }
                    }
                    if ($_pro_filter eq "FALSE") {
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                   }
                  }
                  if($severity_yellow_type_info{$service_output}) {
	             $win_vulnerable="TRUE";
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                  }
                  if($severity_red_type_info{$service_output}) {
                     $win_vulnerable="TRUE";
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                  }
}
}

if ( $_type eq "not+scanned" ) {
return;
}

#Kludge so that it will import into MS Word
$hostname = $_host;
$hostname =~ s/\./\_/g;
$hostname =~ s/\-/\_/g;
$nbname= "";

# Filter if Report Writer is activated
    $todo = "print";
#  Check and see if filters are active
    if ($option eq "selected"){
#  Filter out hosts without vulnerabilities
    if ($novul eq "on" || $sans eq "on") {
     if ($vulnerable eq "FALSE") {
       $todo = "noprint";
     }
}
#  Filter out all Windows
    if ( $hosttype{$_host} =~ /Windows/ ) {
     if ($nowin eq "on" || $nowin1 eq "on"){
      $todo = "noprint";
     }
      if ($nowin1 eq "on"){
        if ($win_vulnerable eq "TRUE") {
          $todo="print";
        }
      }
     }
   }
    $csv_tmp = "";
    if (exists($server_info{$_host})) {
	for (sort split(/\n/, $server_info{$_host})) {
	  # Mask blanks and slashes
	  ($_service = $_) =~ tr / \//+!/;
                if ($_service =~ /FTP/) {
                  ($dum,$service) = split(/\+on\+port\+/,$_service);
                } 
                $service = "ftp" if (! $service);
                if ($_service =~ /Telnet/) {
                  ($dum,$service) = split(/\+on\+port\+/,$_service);
                }
                $service = "telnet" if (! $_service);
                $service = "ssh" if $_service =~ /SSH/i;
                $service = "domain" if $_service =~ /DNS/i;
                $service = "imap" if $_service =~ /IMAP/i;
                $service = "pop3" if $_service =~ /POP/i;
                $service = "netbios-ssn" if $_service =~ /SMB/i;
                if ($_service =~ /WWW/ && !/Secure/) {
                  ($dum,$service) = split(/\+on\+port\+/,$_service);
                }
                $service = "http" if (!$_service);
                $service = "https" if $_service =~ /WWW/i && $_service =~ /Secure/i;
                $service = "xdm" if $_service =~ /XDM/i; 
                $service = "gopher" if $_service =~ /Gopher/i;
                $service = "x11" if $_service =~ /X Windows/i;
                $ports = &svc2port($service);
                ($service,$dum) = split(/:/,$service);
                $csv_tmp .= "$_host,XXXX,$_ip,$_hosttype,$_service,GREEN,,$sara_data,$history_date,-,$_mac,$service,$ports\n";
                $xml_tmp .= "     <VULNERABILITY>\r\n";
                $xml_tmp .= "       <SEVERITY>GREEN</SEVERITY>\r\n";
                $xml_tmp .= "       <SERVICE>\r\n";
                $xml_tmp .= "         <NAME>$service</NAME>\r\n";
                $xml_tmp .= "         <PORT>$ports</PORT>\r\n";
                $xml_tmp .= "       </SERVICE>\r\n";
                $xml_tmp .= "       <CLASS></CLASS>\r\n";
                $xml_tmp .= "       <DATA></DATA>\r\n";
                $xml_tmp .= "       <FALSE_POSITIVE></FALSE_POSITIVE>\r\n";
                $xml_tmp .= "     </VULNERABILITY>\r\n";   
                $_dot = "greendot";
		$_alt = "GREEN";
                $_services .= "$_service|";
                if ($_service =~ "SMB") {
                  open (NB, $facts_file);
                  while (<NB>) {
                    chop;
                    if (/Netbios Name/i && /$_host\|/) {
                      @nb=split(/\|/,$_);
                      if ($nb[7]) {
                        $nbname="$nb[7]";
                        $xml_tmp =~ s/NBN Not Available/$nbname/;
                      }
                    }
                  }
                  close(NB);
                $_="SMB";
                }
         }
 }
if ($nbname) {
  $csv_tmp =~ s/XXXX/$nbname/g;
} else {
  $csv_tmp =~ s/XXXX//g;
} 
#print CSV "$csv_tmp";
$csv_output = $csv_tmp;
$xml_output .= $xml_tmp;
if (exists($severity_host_type_info{$_host})) {
	for (keys %{$severity_host_type_info{$_host}}) {
		($_tutorials = $_) =~ tr / /_/;
		for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_);
                         if($severity_red_type_info{$service_output}) {
                            if (sans10($service_output) == 1) {
                              $_fp ="-";
                              $_fp = "FP" if (correction("$_host|$text") == 1); 
                              $ports = &svc2port($service);
                              ($service,$dum) = split(/:/,$service);

#
# Define output strings
#
$http_output = "<TR><TD>$_host</TD><TD>$nbname</TD><TD>$_ip</TD><TD>$_hosttype</TD><TD>$service_output</TD><TD>RED</TD><TD>$text</TD><TD>$sara_data</TD><TD>$history_date</TD><TD>$_fp";
$http_output .= "<TD>$http$admin</TD>" if $get_admins == 1;
$http_output .= "</TR>\n";
$csv_output .= "$_host,$nbname,$_ip,$_hosttype,$service_output,RED,$text,$sara_data,$history_date,$_fp,$_mac,$service,$ports";
$csv_output .= ",$admin" if $get_admins == 1;
$csv_output .= "\n";
$xml_output .= "     <VULNERABILITY>\r\n";
$xml_output .= "       <SEVERITY>RED</SEVERITY>\r\n";
$xml_output .= "       <SERVICE>\r\n";
$xml_output .= "         <NAME>$service</NAME>\r\n";
$xml_output .= "         <PORT>$ports</PORT>\r\n";
$xml_output .= "       </SERVICE>\r\n";
$xml_output .= "       <CLASS>$service_output</CLASS>\r\n";
$xml_output .= "       <DATA>$text</DATA>\r\n";
$xml_output .= "       <FALSE_POSITIVE>$_fp</FALSE_POSITIVE>\r\n";
$xml_output .= "     </VULNERABILITY>\r\n";


print CLIENT "$http_output\n" if ($interactive eq "ON"); 
                            }
                          }
		}
	}
	$_exists = 1;

        for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                         &sara_split($_);
                         if ($severity_yellow_type_info{$service_output}) {
                            if (sans10($service_output) == 1) {
                              $_fp ="-";
                              $_fp = "FP" if (correction("$_host|$text") == 1);
                              $ports = &svc2port($service);
                              ($service,$dum) = split(/:/,$service);

$http_output = "<TR><TD>$_host</TD><TD>$nbname</TD><TD>$_ip</TD><TD>$_hosttype</TD><TD>$service_output</TD><TD>YELLOW</TD><TD>$text</TD><TD>$sara_data</TD><TD>$history_date</TD><TD>$_fp</TD>";
$http_output .= "<TD>$admin</TD>" if $get_admins == 1;
$http_output .= "</TR>\n";
$csv_output .= "$_host,$nbname,$_ip,$_hosttype,$service_output,YELLOW,$text,$sara_data,$history_date,$_fp,$_mac,$service,$ports";
$csv_output .= ",$admin" if $get_admins == 1;
$csv_output .= "\n";
$xml_output .= "   <VULNERABILITY>\r\n";
$xml_output .= "    <SEVERITY>YELLOW</SEVERITY>\r\n";
$xml_output .= "    <SERVICE>\r\n";
$xml_output .= "       <NAME>$service</NAME>\r\n";
$xml_output .= "       <PORT>$ports</PORT>\r\n";
$xml_output .= "    </SERVICE>\r\n";
$xml_output .= "    <CLASS>$service_output</CLASS>\r\n";
$xml_output .= "    <DATA>$text</DATA>\r\n";
$xml_output .= "    <ADMIN>$admin</ADMIN>\r\n" if $get_admins == 1;
$xml_output .= "    <FALSE_POSITIVE>$_fp</FALSE_POSITIVE>\r\n";
$xml_output .= "   </VULNERABILITY>\r\n";
print CLIENT "$http_output\n" if ($interactive eq "ON");
                          }

}
}
}
$exists = 1;
        for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                  &sara_split($_);
                  $_severity=$severity_brown_type_info{$service_output};
                  if ($_severity) {
                       if ($pro_filter) {
                         $_pro_filter="FALSE";
                         for (split(/\,/, $pro_filter)){
                           $_tmp = $_;
                           $_tmp =~ s/\ //;
                           if ( $_severity =~ "$_tmp") {
                             $_pro_filter = "TRUE";
                           }
                         }
                       }
                       if (sans10($service_output) == 1) {
                         if ($_pro_filter eq "FALSE") {
                           $_fp ="-";
                           $_fp = "FP" if (correction("$_host|$text") == 1);
                           $ports = &svc2port($service);
                           ($service,$dum) = split(/:/,$service);
$http_output = "<TR><TD>$_host</TD><TD>$nbname</TD><TD>$_ip</TD><TD>$_hosttype</TD><TD>$service_output</TD><TD>BROWN</TD><TD>$text</TD><TD>$sara_data</TD><TD>$history_date</TD><TD>$_fp</TD>";
$http_output .= "<TD>$admin</TD>" if $get_admins == 1;
$http_output .= "</TR>\n";
$csv_output .= "$_host,$nbname,$_ip,$_hosttype,$service_output,BROWN,$text,$sara_data,$history_date,$_fp,$_mac,$service,$ports";
$csv_output .= ",$admin" if $get_admins == 1;
$csv_output .= "\n";

$xml_output .= "   <VULNERABILITY>\r\n";
$xml_output .= "    <SEVERITY>BROWN</SEVERITY>\r\n";
$xml_output .= "    <SERVICE>\r\n";
$xml_output .= "       <NAME>$service</NAME>\r\n";
$xml_output .= "       <PORT>$ports</PORT>\r\n";
$xml_output .= "    </SERVICE>\r\n";
$xml_output .= "    <CLASS>$service_output</CLASS>\r\n";
$xml_output .= "    <DATA>$text</DATA>\r\n";
$xml_output .= "    <ADMIN>$admin</ADMIN>\r\n" if $get_admins == 1;
$xml_output .= "    <FALSE_POSITIVE>$_fp</FALSE_POSITIVE>\r\n";
$xml_output .= "   </VULNERABILITY>\r\n";

print CLIENT "$http_output\n" if ($interactive eq "ON");
                        }
                       }
}
}
}
$exists=1;
$xml_output .= "   </VULNERABILITIES>\r\n";
$xml_output .= "  </HOST>\r\n";
if (!$csv_output) {
$csv_output .= "$_host,,$_ip,,,GREY,,$sara_data,$history_date,-,$_mac,,\n"; 
}
print CSV "$csv_output";
}

