#  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.
#
# Report subnets
#
# generate the tables for looking subnets/hosts in:

&make_subnet_info();
$_subnets = keys %all_subnets;
sub sort_ip {
    local($aip,$bip);
    for $ip (split(/\./,$a)) { $aip = $aip * 256 + $ip; }
    for $ip (split(/\./,$b)) { $bip = $bip * 256 + $ip; }
    return $aip <=> $bip;
}
#
# 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;
$history_version =~ s/\n//;



print <<EOF;
<HTML>
<HEAD>
<title>Database by CSV </title>
<LINK REV="made" HREF="mailto:satan\@fish.com">
<LINK REV="made" HREF="mailto:sara\@arc.com">
</HEAD>
<H2>Security Auditor's Research Assistant (SARA) ReportWriter</H2>
<a href="$HTML_ROOT/$system.html"> Back to the $SYSTEM start page </a> |
<a href="$HTML_SERVER/reporting/analysis.pl"> Back to $SYSTEM Reporting and Analysis </a><HR>
EOF
#
#  Get History Information
#
$black_bar=0;
$green_bar=0;
$red_bar=0;
$yellow_bar=0;
$brown_bar=0;
$start_report=0;
$report="chart";
for $_net (sort sort_ip keys %all_subnets)
{
  @_hosts = split(/\s/, $all_subnets{$_net});
  do "$html_root/reporting/reportwriter/report_host_summary.pl";
}
$total_bar = $green_bar + $black_bar + $red_bar + $yellow_bar + $brown_bar;
$total_vulnerabilities = $red_bar + $yellow_bar + $brown_bar;

open (CSV, ">results/$sara_data/report.csv");
open (XML, ">results/$sara_data/report.xml");
if ( $interactive eq "ON") {
  print "<P><H3>CSV Results at ./results/$sara_data/report.csv</H3><P>";
  print '<TABLE WIDTH = 1000 BORDER = 3>';
  print '<TR><TD><B><CENTER>Host</Center></B></TD>';
  print '<TD><B><CENTER>NB</Center></B></TD>';
  print '<TD><B><CENTER>IP</Center></B></TD>';
  print '<TD><B><CENTER>OS</Center></B></TD>';
  print '<TD><B><CENTER>Vul Category</Center></B></TD>';
  print '<TD><B><CENTER>Vul Severity</Center></B></TD>';
  print '<TD><B><CENTER>Description</Center></B></TD>';
  print '<TD><B><CENTER>Database</Center></B></TD>';
  print '<TD><B><CENTER>Scan Date</Center></B></TD></TD>';
  print '<TD><B><CENTER>False Positive</Center></B></TD>';
  print '<TD><B><CENTER>Administrator</Center></B></TD>' if $get_admins == 1;
}
$history_mode =~ s/\&/ /;
$xml_output  = "<?xml version=\"1.0\" encoding =\"UTF-8\"?>\r\n";
$xml_output .= "<SARA_SCAN xmlns=\"www-arc.com/xml/sarascan\">\r\n";
$xml_output .= "  <SCAN_INFO>\r\n";
$xml_output .= "    <SCAN_ID>$sara_data</SCAN_ID>\r\n";
$xml_output .= "    <SCAN_DATE>$history_date</SCAN_DATE>\r\n";
$xml_output .= "    <SCAN_VERSION>$history_version</SCAN_VERSION>\r\n";
$xml_output .= "    <ATTACK_LEVEL>$history_mode</ATTACK_LEVEL>\r\n";
$xml_output .= "  </SCAN_INFO>\r\n";
$xml_output .= "  <VULNERABILITY_SUMMARY>\r\n";
$xml_output .= "    <VULNERABILITY_COUNT class=\"Total\">$total_bar</VULNERABILITY_COUNT>\r\n";
$xml_output .= "    <VULNERABILITY_COUNT class=\"Green\">$green_bar</VULNERABILITY_COUNT>\r\n"; 
$xml_output .= "    <VULNERABILITY_COUNT class=\"Grey\">$black_bar</VULNERABILITY_COUNT>\r\n"; 
$xml_output .= "    <VULNERABILITY_COUNT class=\"Red\">$red_bar</VULNERABILITY_COUNT>\r\n"; 
$xml_output .= "    <VULNERABILITY_COUNT class=\"Yellow\">$yellow_bar</VULNERABILITY_COUNT>\r\n"; 
$xml_output .= "    <VULNERABILITY_COUNT class=\"Brown\">$brown_bar</VULNERABILITY_COUNT>\r\n"; 
$xml_output .= "  </VULNERABILITY_SUMMARY>\r\n"; 
$xml_output .= "  <DETAILS>\r\n";

for $_net (sort sort_ip keys %all_subnets)
{
  @_hosts = split(/\s/, $all_subnets{$_net});
  if ($_net ne "unknown") {
   do "$html_root/reporting/reportwriter/report_csv.pl";
  }
}
print '</TABLE>/'if ($interactive = "ON");
$xml_output .= " </DETAILS>\r\n";
$xml_output .= "</SARA_SCAN>\r\n";
print XML "$xml_output";
close (CSV);
close (XML);
