#  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;
}

print <<EOF;
<HTML>
<HEAD>
<LINK REV="made" HREF="mailto:satan\@fish.com">
<LINK REV="made" HREF="mailto:sara\@arc.com">
</HEAD>
EOF
#
#  Do Bar Chart
#
$black_bar=0;
$green_bar=0;
$red_bar=0;
$yellow_bar=0;
$brown_bar=0;
$start_report=0;
$report="chart";
$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_mode .= " (SANS TOP 10 only)" if $sans eq "on" && $sans_version == 1;
$history_mode .= " (SANS TOP 20 only)" if $sans eq "on" && $sans_version == 2;
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;

if ($total_bar >= 1) {
  $green_per = int(((500 * $green_bar) / $total_bar) +.5);
  $black_per = int(((500 * $black_bar) / $total_bar) +.5);
  $red_per =   int(((500 * $red_bar) / $total_bar) +.5);
  $yellow_per= int(((500 * $yellow_bar) / $total_bar) +.5);
  $brown_per = int(((500 * $brown_bar) / $total_bar) +.5);
} else {
  $green_per = 0;
  $black_per = 500;
  $red_per   = 0;
  $yellow_per= 0;
  $brown_per = 0;
}
$todays_date = `date +"%d %B %Y"`;
if ( -f "$html_root/reporting/reportwriter/report.prefix" ) {
  open(TMP, "$html_root/reporting/reportwriter/report.prefix");
while (<TMP>) {
  chomp;
  if (/TOTAL_HOSTS/  && $total_bar == 0) {
    print <<EOF;
    SARA detected no hosts with services running.  One of the following
    may be true:
    <P>
    <UL>
    <LI>System may be behind a firewall
    <LI>System may not offer any services
    <LI>System may be off-line
    </UL>
    <P>
EOF
    exit;
   }
    
  $_ =~ s/DATE/$history_date/;
  $_ =~ s/REPORT_D/$todays_date/;
  $_ =~ s/CUSTOMER/$sara_data/g;
  $_ =~ s/MODE/$history_mode/;
  $_ =~ s/VERSION/$history_version/;
  $_ =~ s/TOTAL_HOSTS/$total_bar/;
  $_ =~ s/VULNERABILITY_COUNT/$total_vulnerabilities/;
  print <<EOF;
$_

EOF
}
  close(TMP);
 }
print <<EOF;
<TABLE BORDER=0><TR><TD width="50"><Font size=2>Green</Font></TD><TD width="$green_per" BGCOLOR="Green"><Font Size=2>&nbsp;</Font></TD><TD><Font size=2>$green_bar</Font></TD></TR></TABLE>
<TABLE BORDER=0><TR><TD width="50"><Font size=2>Grey</Font></TD><TD width="$black_per" BGCOLOR="#808080"><Font size=2>&nbsp;</Font></TD><TD><Font size=2>$black_bar</Font></TD></TR></TABLE>
<TABLE BORDER=0><TR><TD width="50"><Font size=2>Red</Font></TD><TD width="$red_per" BGCOLOR="RED"><Font size=2>&nbsp;</Font></TD><TD><Font size=2>$red_bar</Font></TD></TR></TABLE>
<TABLE BORDER=0><TR><TD width="50"><Font size=2>Yellow</Font></TD><TD width="$yellow_per" BGCOLOR="YELLOW"><Font size=2>&nbsp;</Font></TD><TD><Font size=2>$yellow_bar</Font></TD></TR></TABLE>
<TABLE BORDER=0><TR><TD width="50"><Font size=2>Brown</Font></TD><TD width="$brown_per" BGCOLOR="#800000"><Font size=2>&nbsp;</Font></TD><TD><Font size=2>$brown_bar</Font size=2></TD></TR></TABLE>
<P>
<B>Figure 1  Host Summary by Color</B>
</UL></UL></UL>
EOF
if ($report_type eq "full" ) {
 if(($total_vulnerabilities + $green_bar) != 0) {
    $post_type ="$html_root/reporting/reportwriter/report.postfix";
 }
}  elsif ($report_type eq "full_subnet") {
  $post_type = "$html_root/reporting/reportwriter/report.bysubnet.postfix";
}
if ( -f "$post_type" ) {
  open(TMP, "$post_type");
while (<TMP>) {
  chomp;
  if (/<!--/) {
   ($d0,$tmp,$d3) = split(/\s+/,$_);
   @appendices = split(/\|/,$tmp);
    for $appendices (@appendices) {
   }
  } else {
   print $_;
  }
 }
if ($report_type =~ /full/) {
  @appendices_labels = ('A', 'B', 'C', 'D', 'E', 'F');
  $label_cnt = 0;
  for $appendices (@appendices) {
   if ($appendices eq "TABLE") {
     print <<EOF;
    <CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
EOF
   if ($report_type eq "full_subnet") {
    print <<EOF;
   <CENTER><B><A NAME="appendix$appendices_labels[$label_cnt]">
   Appendix $appendices_labels[$label_cnt]</A></B></CENTER>
   <CENTER><B>SARA Scan Information by Subnet</B></CENTER>
   <P>
EOF
   } else {
   print <<EOF; 
   <CENTER><B><A NAME="appendix$appendices_labels[$label_cnt]">
   Appendix $appendices_labels[$label_cnt]</A></B></CENTER>
   <CENTER><B>SARA Scan Summary</B></CENTER>
EOF
   }
    $net_cnt = 0;
    $report = "table";
    $scanned=0;
    $host_found="FALSE";
      for $_net (sort sort_ip keys %all_subnets) {
        $scanned=0;
        $host_found="FALSE";
        @_hosts = split(/\s/, $all_subnets{$_net});
        do "$html_root/reporting/reportwriter/report_host_check.pl";
        if ($host_found eq "TRUE") {
          $net_cnt = $net_cnt+1;
          if ($_net ne "unknown") {
    if ($report_type eq "full_subnet") {
      if ($net_cnt >= 2) {
        print <<EOF;
    <CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
EOF
      }
      print  <<EOF;
    <CENTER><B>ANNEX $net_cnt Scan Results of Subnet $_net</B></CENTER>
    <CENTER><B>TAB 1: Subnet ($_net) Scan Summary</B></CENTER>
    <P>
EOF
    }
    print <<EOF;
    <CENTER><TABLE BORDER=1><TR><TD width=140><Font Size=2><B>Host Name</B></Font></TD><TD width=70><Font size=2><B>IP Address</B></Font></TD><TD width=80><Font size=2><B>Host Type</B></Font></TD><TD width=40><Font size=2><B>Green</B></Font></TD><TD width=30><Font size=2><B>Red</B></Font></TD><TD width=45><Font size=2><B>Yellow</B></Font></TD><TD width=45><Font size=2><B>Brown</B></Font></TD><TD width=25><Font Size=2><B>FP</B></Font size=2></TD></TR>

EOF
        @_hosts = split(/\s/, $all_subnets{$_net});
        do "$html_root/reporting/reportwriter/report_host_summary.pl";
        print <<EOF;
</TABLE></CENTER>
<CENTER><B>Table 1  Hosts on Sub-net $_net</B></CENTER>
<PRE> </PRE>
EOF
        if ($report_type eq "full_subnet") {
         print <<EOF;
       <HR><CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER><P>
       <CENTER><B>TAB 2: Subnet ($_net) Scan Details</B></CENTER>
EOF
         do "$html_root/reporting/reportwriter/report_hosts.pl";
        }
       }
      }
    }
   } elsif ($appendices eq "DETAILS") {
      if ($report_type eq "full") {
        print <<EOF;
     <HR><CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
     <CENTER>
     <B><A NAME="appendix$appendices_labels[$label_cnt]">
     Appendix  $appendices_labels[$label_cnt]</A></B>
     <B>$SYSTEM Scan Details</B>
     </CENTER>
EOF
       for $_net (sort sort_ip keys %all_subnets) {
         @_hosts = split(/\s/, $all_subnets{$_net});
         do "$html_root/reporting/reportwriter/report_hosts.pl";
       }
     }else {
      $label_cnt = $label_cnt -1;
     }
   } elsif ($appendices eq "VULNERABILITY") {
     if($total_vulnerabilities != 0) {
      print <<EOF;
  <CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
  <CENTER>
  <B><A NAME="appendix$appendices_labels[$label_cnt]">
  Appendix $appendices_labels[$label_cnt]</A></B>
  <B>Vulnerability List by Severity</B>
  <P>
  </CENTER>
EOF
do "$html_root/reporting/reportwriter/sara_report_danger.pl";
    }

   } elsif ($appendices eq "TUTORIAL") {
    if($total_vulnerabilities != 0) {
    print <<EOF;
  <HR><CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
  <CENTER>
  <B><A NAME="appendix$appendices_labels[$label_cnt]">Appendix $appendices_labels[$label_cnt]</A></B>
  <B>$SYSTEM Vulnerability Tutorials</B>
  </CENTER>
EOF
     if ( $notut ne "on" ) {
       do "$html_root/reporting/reportwriter/output_tutorial.pl" if ($system eq "sara");
       print <<EOF;
<CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
EOF
     }
    }
   } elsif ($appendices eq "SANS20") {
     if($total_vulnerabilities != 0) {
#       if ($sans eq "on" && -f "config/sans10.r2") {
       print <<EOF;
  <HR><CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
  <CENTER>
  <B><A NAME="appendix$appendices_labels[$label_cnt]">Appendix $appendices_labels[$label_cnt]</A></B>
  <B>SANS Top 20</B>
  </CENTER>
EOF
      $sans_text = `cat "html/cve/Top_20_cve.html"`; 
      print "$sans_text";
     }
   } elsif ($appendices eq "BLANK") {
      print <<EOF;
  <HR><CENTER><FONT Color = "#C0C0C0">END OF SECTION</FONT></CENTER>
  <CENTER>
  <B><A NAME="appendix$appendices_labels[$label_cnt]">
  Appendix $appendices_labels[$label_cnt]</A></B>
  </CENTER>
EOF
   }
    $label_cnt = $label_cnt +1;
  }
} else {
   do $html/reporting/reportwriter/sara_report_csv.pl;
print <<EOF;
<H1>Not Supported Yet</H1>
EOF
}
  close(TMP);
} else {
  if(($total_vulnerabilities + $green_bar) != 0) {
    print <<EOF;
  <H3> Missing postfix file at $post_type</H3> 
  <HR>
EOF
  }
}
