#
require 'config/xml.cf';
require 'config/version.pl';
require 'perl/misc.pl';
require 'perllib/getopts.pl';
require 'perl/subnets.pl';
sub save_xml {
        local($path) = @_;
        $_level   = $attack_level;
        $_version = $sara_version;
        $_date    = &yymmdd(time());
        if ($regenerate eq "on") {
          open(HISTORY, "results/$sara_data/history");
          while (<HISTORY>) {
            chomp;
            ($dum, $_date, $_level, $dum, $_version) = split(/\|/);
          }
        }
        close (HISTORY);
        ($_date,$dum) = split(/ /, $_date);
        open(XML, ">$path") || die "cannot save xml to $path: $!";
        $xml_header   = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
        $xml_header  .= "<$tag_scan $tag_scan_info>\n";
        $xml_header  .= "  <$tag_info>\n";
        $xml_header  .= "    <$tag_id>$sara_data</$tag_id>\n";
        $xml_header  .= "    <$tag_date>$_date</$tag_date>\n";
        $xml_header  .= "    <$tag_version>$_version</$tag_version>\n";
        $xml_header  .= "    <$tag_level>$attack_name[$_level]</$tag_level>\n";
        if ($dont_smb_guess == 1) {
          $xml_header .= "    <$tag_level_option>SMB password guessing disabled</$tag_level_option>\n";
        }
        $xml_header  .= "  </$tag_info>\n";
        $xml_header1  = "  <$tag_details>\n";

        $_host        = 0;
        $_net         = "";
        @_hosts       = "";
        @rhosts       = "";
        @yhosts       = "";
        @bhosts       = "";
        @ghosts       = "";
        $found_host   = "";
        &make_subnet_info();
        for $net (sort sort_ip keys %all_subnets) {
         @subnet_hosts = split(/\s/, $all_subnets{$net});
         for $host (sort @subnet_hosts) {
           $ip      = $all_hosts{$host}{'IP'};
           $mac       = $all_hosts{$host}{'MAC'};
           $fw        = $all_hosts{$host}{'FW'};
           $fw        = "none" if $fw =~ /alive/;
           $proximity = $all_hosts{$host}{'proximity'};
           $level     = $all_hosts{$host}{'attack'};
           $expand    = $all_hosts{$host}{'expand'};
           $time      = $all_hosts{$host}{'time'};
           $os        = "";
           @reds      = "";
           @yellows   = "";
           @browns    = "";
           @greens    = "";
           @greens0   = "";
           $red       = $red_cnt[$_host]    = $_red_cnt[$_host]    = 0;
           $yellow    = $yellow_cnt[$_host] = $_yellow_cnt[$_host] = 0;
           $brown     = $brown_cnt[$_host]  = $_brown_cnt[$_host]  = 0;
           $gray      = $gray_cnt[$_host]   = $_gray_cnt[$_host]   = 0;
           $green     = $green_cnt[$_host]  = 0;
           $green0    = 0;
           if ($time) {
             $found_host = "YES";
             if ($net ne $_net) {
               $_hosts[$_host]  = "  </$tag_subnet>\n" if ($_net);
               $_hosts[$_host] .= "  <$tag_subnet>\n";
               $_hosts[$_host] .= "    <$tag_sub_info>$net</$tag_sub_info>\n";
               $_net = $net;
             }
             $_hosts[$_host] .= "    <$tag_host>\n";
             $_hosts[$_host] .= "      <$tag_host_IP>$ip</$tag_host_IP>\n";
             $_hosts[$_host] .= "      <$tag_host_mac>$mac</$tag_host_mac>\n";
             $_hosts[$_host] .= "      <$tag_host_fw>$fw</$tag_host_fw>\n";
             $_hosts[$_host] .= "      <$tag_host_desc>\n";
             $_hosts[$_host] .= "        <$tag_host_name>$host</$tag_host_name>\n";



             for (grep (/^$host/, keys %old_facts)) {
               ($tgt,$svc,$stat,$sev,$d,$d,$svc_output,$text)=split(/\|/);
               if ($text =~ /\&/ && $text !~ /\&[a-z]+;/) {
                  $text =~ s/\&/\&amp;/g;
               }
               if ($host eq "$tgt") {
                 if (!$os) {
                   if ($svc eq "host") {
                     ($d, $os) = split(/\s+/,$svc_output,2);         
                     $_hosts[$_host] .= "        <$tag_host_os>$os</$tag_host_os>\n"; 
                   }
                 }
                 if ($svc_output =~ /Netbios Name/) {
                   $_hosts[$_host] .= "        <$tag_host_nb>$text</$tag_host_nb>\n";
                 } elsif ($svc_output =~ /Netbios Domain/) {
                   $_hosts[$_host] .= "        <$tag_host_domain>$text</$tag_host_domain>\n";
                 } elsif($sev eq "rs"||$sev eq "us"||$sev eq"ns"||$sev eq "uw") {
                   @reds[$red] = $_;
                   $port   = &svc2port($svc);
                   $rhost  = "        <$tag_vul>\n";
                   $rhost .= "          <$tag_severity>RED</$tag_severity>\n";
                   $rhost .= "           <$tag_service>\n";
                   $rhost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                   $rhost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                   $rhost .= "           </$tag_service>\n";
                   $rhost .= "           <$tag_class>$svc_output</$tag_class>\n";
                   $rhost .= "           <$tag_data>$text</$tag_data>\n";

                   $cve = &get_cve($svc_output,$text);
                   if ($cve) {
                     $nvd = &get_nvd($cve);
                     ($_cve, $sys, $pub, $cvss, $severity, $desc, $ref, $range, $vuln, $loss) = split(/\|/, $nvd);
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>$cve</$tag_cve>\n";
                     $rhost .= "             <$tag_nvd_cvss>$cvss</$tag_nvd_cvss>\n";
                     $rhost .= "             <$tag_nvd_severity>$severity</$tag_nvd_severity>\n";
                     if ($range) {
                       $rhost .= "             <$tag_nvd_range>$range</$tag_nvd_range>\n";
                     }
                     if ($vuln) {
                       $rhost .= "             <$tag_nvd_vuln>$vuln</$tag_nvd_vuln>\n";
                     }
                     if ($loss) {
                       $rhost .= "             <$tag_nvd_loss>$loss</$tag_nvd_loss>\n";
                     }
                     $rhost .= "             <$tag_nvd_desc>$desc</$tag_nvd_desc>\n";
                     if ($ref) {
                       $rhost .= "             <$tag_nvd_refs>\n";
                       (my @refs) = split(/\,/, $ref);
                       for my $_ref (@refs) {
                         $rhost .= "               <$tag_nvd_ref>$_ref</$tag_nvd_ref>\n";
                       }
                       $rhost .= "             </$tag_nvd_refs>\n";
                     }
                     $rhost .= "           </$tag_nvd>\n";
                   } else {
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>NOCVE</$tag_cve>\n";
                     $rhost .= "           </$tag_nvd>\n";
                   }

                   $sans = &get_sans($cve);
                   if ($cve) {
                     $rhost .= "           <$tag_sans>$sans</$tag_sans>\n";
                   }

                   if (correction("$host|$text") == 1) {
                     $rhost .= "           <$tag_fp>FP</$tag_fp>\n";
                     $_gray_cnt[$_host]++;
                   } else {
                     $_red_cnt[$_host]++;
                     $rhost .= "           <$tag_fp>-</$tag_fp>\n";
                   }
                   $rhost .= "         </$tag_vul>\n";
                   $rhosts[$_host]  .= "$rhost";
                   $red_cnt[$_host] = $red;
                   $red++;
                 } elsif ($sev eq "ur"||$sev eq "nw"||$sev eq"nfs") {
                   @reds[$red] = $_;
                   $port   = &svc2port($svc);
                   $rhost  = "        <$tag_vul>\n";
                   $rhost .= "          <$tag_severity>RED</$tag_severity>\n";
                   $rhost .= "           <$tag_service>\n";
                   $rhost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                   $rhost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                   $rhost .= "           </$tag_service>\n";
                   $rhost .= "           <$tag_class>$svc_output</$tag_class>\n";
                   $rhost .= "           <$tag_data>$text</$tag_data>\n";

                   $cve = &get_cve($svc_output,$text);
                   if ($cve) {
                     $nvd = &get_nvd($cve);
                     ($_cve, $sys, $pub, $cvss, $severity, $desc, $ref, $range, $vuln, $loss) = split(/\|/, $nvd);
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>$cve</$tag_cve>\n";
                     $rhost .= "             <$tag_nvd_cvss>$cvss</$tag_nvd_cvss>\n";
                     $rhost .= "             <$tag_nvd_severity>$severity</$tag_nvd_severity>\n";
                     if ($range) {
                       $rhost .= "             <$tag_nvd_range>$range</$tag_nvd_range>\n";
                     }
                     if ($vuln) {
                       $rhost .= "             <$tag_nvd_vuln>$vuln</$tag_nvd_vuln>\n";
                     }
                     if ($loss) {
                       $rhost .= "             <$tag_nvd_loss>$loss</$tag_nvd_loss>\n";
                     }
                     $rhost .= "             <$tag_nvd_desc>$desc</$tag_nvd_desc>\n";
                     if ($ref) {
                       $rhost .= "             <$tag_nvd_refs>\n";
                       (my @refs) = split(/\,/, $ref);
                       for my $_ref (@refs) {
                         $rhost .= "               <$tag_nvd_ref>$_ref</$tag_nvd_ref>\n";
                       }
                       $rhost .= "             </$tag_nvd_refs>\n";
                     }
                     $rhost .= "           </$tag_nvd>\n";
                   } else {
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>NOCVE</$tag_cve>\n";
                     $rhost .= "           </$tag_nvd>\n";
                   }
                   $sans = &get_sans($cve);
                   if ($cve) {
                     $rhost .= "           <$tag_sans>$sans</$tag_sans>\n";
                   }

                   if (correction("$host|$text") == 1) {
                     $rhost .= "           <$tag_fp>FP</$tag_fp>\n";
                     $_gray_cnt[$_host]++;
                   } else {
                     $_red_cnt[$_host]++;
                     $rhost .= "           <$tag_fp>-</$tag_fp>\n";

                   }

                   $rhost .= "      </$tag_vul>\n";
                   $rhosts[$_host]  .= "$rhost";
                   $red_cnt[$_host] = $red;
                   $red++;
                 } elsif ($sev eq "ht"||$sev eq "bo") {
                   @reds[$red] = $_;
                   $port   = &svc2port($svc);
                   $rhost  = "        <$tag_vul>\n";
                   $rhost .= "          <$tag_severity>RED</$tag_severity>\n";
                   $rhost .= "           <$tag_service>\n";
                   $rhost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                   $rhost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                   $rhost .= "           </$tag_service>\n";
                   $rhost .= "           <$tag_class>$svc_output</$tag_class>\n";
                   $rhost .= "           <$tag_data>$text</$tag_data>\n";

                   $cve = &get_cve($svc_output,$text);
                   if ($cve) {
                     $nvd = &get_nvd($cve);
                     ($_cve, $sys, $pub, $cvss, $severity, $desc, $ref, $range, $vuln, $loss) = split(/\|/, $nvd);
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>$cve</$tag_cve>\n";
                     $rhost .= "             <$tag_nvd_cvss>$cvss</$tag_nvd_cvss>\n";
                     $rhost .= "             <$tag_nvd_severity>$severity</$tag_nvd_severity>\n";
                     if ($range) {
                       $rhost .= "             <$tag_nvd_range>$range</$tag_nvd_range>\n";
                     }
                     if ($vuln) {
                       $rhost .= "             <$tag_nvd_vuln>$vuln</$tag_nvd_vuln>\n";
                     }
                     if ($loss) {
                       $rhost .= "             <$tag_nvd_loss>$loss</$tag_nvd_loss>\n";
                     }
                     $rhost .= "             <$tag_nvd_desc>$desc</$tag_nvd_desc>\n";
                     if ($ref) {
                       $rhost .= "             <$tag_nvd_refs>\n";
                       (my @refs) = split(/\,/, $ref);
                       for my $_ref (@refs) {
                         $rhost .= "               <$tag_nvd_ref>$_ref</$tag_nvd_ref>\n";
                       }
                       $rhost .= "             </$tag_nvd_refs>\n";
                     }
                     $rhost .= "           </$tag_nvd>\n";
                   } else {
                     $rhost .= "           <$tag_nvd>\n";
                     $rhost .= "             <$tag_cve>NOCVE</$tag_cve>\n";
                     $rhost .= "           </$tag_nvd>\n";
                   }
                   $sans = &get_sans($cve);
                   if ($cve) {
                     $rhost .= "           <$tag_sans>$sans</$tag_sans>\n";
                   }


                   if (correction("$host|$text") == 1) {
                     $rhost .= "           <$tag_fp>FP</$tag_fp>\n";
                     $_gray_cnt[$_host]++;
                   } else {
                     $_red_cnt[$_host]++;
                     $rhost .= "           <$tag_fp>-</$tag_fp>\n";

                   }

                   $rhost .= "      </$tag_vul>\n";
                   $rhosts[$_host]  .= "$rhost";
                   $red_cnt[$_host] = $red;
                   $red++;
                 } elsif ($sev eq "yus" || $sev eq "ycio") {
                   @yellows[$yellow] = $_;
                   $port   = &svc2port($svc);
                   $yhost  = "        <$tag_vul>\n";
                   $yhost .= "          <$tag_severity>YELLOW</$tag_severity>\n";
                   $yhost .= "           <$tag_service>\n";
                   $yhost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                   $yhost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                   $yhost .= "           </$tag_service>\n";
                   $yhost .= "           <$tag_class>$svc_output</$tag_class>\n";
                   $yhost .= "           <$tag_data>$text</$tag_data>\n";

                   $cve = &get_cve($svc_output,$text);
                   if ($cve) {
                     $nvd = &get_nvd($cve);
                     ($_cve, $sys, $pub, $cvss, $severity, $desc, $ref, $range, $vuln, $loss) = split(/\|/, $nvd);
                     $yhost .= "           <$tag_nvd>\n";
                     $yhost .= "             <$tag_cve>$cve</$tag_cve>\n";
                     $yhost .= "             <$tag_nvd_cvss>$cvss</$tag_nvd_cvss>\n";
                     $yhost .= "             <$tag_nvd_severity>$severity</$tag_nvd_severity>\n";
                     if ($range) {
                       $yhost .= "             <$tag_nvd_range>$range</$tag_nvd_range>\n";
                     }
                     if ($vuln) {
                       $yhost .= "             <$tag_nvd_vuln>$vuln</$tag_nvd_vuln>\n";
                     }
                     if ($loss) {
                       $yhost .= "             <$tag_nvd_loss>$loss</$tag_nvd_loss>\n";
                     }
                     $yhost .= "             <$tag_nvd_desc>$desc</$tag_nvd_desc>\n";
                     if ($ref) {
                       $yhost .= "             <$tag_nvd_refs>\n";
                       (my @refs) = split(/\,/, $ref);
                       for my $_ref (@refs) {
                         $yhost .= "               <$tag_nvd_ref>$_ref</$tag_nvd_ref>\n";
                       }
                       $yhost .= "             </$tag_nvd_refs>\n";
                     }
                     $yhost .= "           </$tag_nvd>\n";
                   } else {
                     $yhost .= "           <$tag_nvd>\n";
                     $yhost .= "             <$tag_cve>NOCVE</$tag_cve>\n";
                     $yhost .= "           </$tag_nvd>\n";
                   }

                   if ($cve) {
                     $sans = &get_sans($cve);
                     if ($sans) {
                        $yhost .= "           <$tag_sans>$sans</$tag_sans>\n";
                     }
                   }

                   if (correction("$host|$text") == 1) {
                     $yhost .= "           <$tag_fp>FP</$tag_fp>\n";
                     $_gray_cnt[$_host]++;
                   } else {
                     $_yellow_cnt[$_host]++;
                     $yhost .= "           <$tag_fp>-</$tag_fp>\n";

                   }
                   $yhost .= "      </$tag_vul>\n";
                   $yhosts[$_host]  .= "$yhost";
                   $yellow_cnt[$_host] = $yellow;
                   $yellow++;

                 } elsif ($sev eq "zcio" || $sev eq "zwoi") {
                   @browns[$brown] = $_;
                   $port   = &svc2port($svc);
                   $bhost  = "        <$tag_vul>\n";
                   $bhost .= "          <$tag_severity>BROWN</$tag_severity>\n";
                   $bhost .= "           <$tag_service>\n";
                   $bhost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                   $bhost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                   $bhost .= "           </$tag_service>\n";
                   $bhost .= "           <$tag_class>$svc_output</$tag_class>\n";
                   $bhost .= "           <$tag_data>$text</$tag_data>\n";

                   $cve = &get_cve($svc_output,$text);
                   if ($cve) {
                     $nvd = &get_nvd($cve);
                     ($_cve, $sys, $pub, $cvss, $severity, $desc, $ref, $range, $vuln, $loss) = split(/\|/, $nvd);
                     $bhost .= "           <$tag_nvd>\n";
                     $bhost .= "             <$tag_cve>$cve</$tag_cve>\n";
                     $bhost .= "             <$tag_nvd_cvss>$cvss</$tag_nvd_cvss>\n";
                     $bhost .= "             <$tag_nvd_severity>$severity</$tag_nvd_severity>\n";
                     if ($range) {
                       $bhost .= "             <$tag_nvd_range>$range</$tag_nvd_range>\n";
                     }
                     if ($vuln) {
                       $bhost .= "             <$tag_nvd_vuln>$vuln</$tag_nvd_vuln>\n";
                     }
                     if ($loss) {
                       $bhost .= "             <$tag_nvd_loss>$loss</$tag_nvd_loss>\n";
                     }
                     $bhost .= "             <$tag_nvd_desc>$desc</$tag_nvd_desc>\n";
                     if ($ref) {
                       $bhost .= "             <$tag_nvd_refs>\n";
                       (my @refs) = split(/\,/, $ref);
                       for my $_ref (@refs) {
                         $bhost .= "               <$tag_nvd_ref>$_ref</$tag_nvd_ref>\n";
                       }
                       $bhost .= "             </$tag_nvd_refs>\n";
                     }
                     $bhost .= "           </$tag_nvd>\n";
                   } else {
                     $bhost .= "           <$tag_nvd>\n";
                     $bhost .= "             <$tag_cve>NOCVE</$tag_cve>\n";
                     $bhost .= "           </$tag_nvd>\n";
                   }
                   $sans = &get_sans($cve);
                   if ($cve) {
                     $bhost .= "           <$tag_sans>$sans</$tag_sans>\n";
                   }

                   if (correction("$host|$text") == 1) {
                     $bhost .= "           <$tag_fp>FP</$tag_fp>\n";
                    $gray_cnt[$_host]++;
                   } else {
                     $_brown_cnt[$_host]++;
                     $bhost .= "           <$tag_fp>-</$tag_fp>\n";

                   }
                   $bhost .= "      </$tag_vul>\n";
                   $bhosts[$_host]  .= "$bhost";
                   $brown_cnt[$_host] = $brown;
                   $brown++;           
                 } elsif ($sev eq "" || $sev eq "x" || $sev eq "g") {
                   if ($svc =~ /^[a-z]/ && $svc ne "host" && $svc ne "users" && $svc ne "tftp" && $stat eq "a") {
                     @greens0[$green0] = $_;
                     $green0++;
                   }
                 }
               }
             }  
             $green  = 0;
             for (@greens0) {
               $tmp = $_;
               $ignore = "";
               ($dum, $svc,$dum) = split(/\|/);
               for (@reds) {
                 ($dum,$service,$dum) = split(/\|/);
                 $ignore = "yes" if ($svc eq $service);
               }
               if (! $ignore) {
                 for (@yellows) {
                   ($dum,$service,$dum) = split(/\|/);
                   $ignore = "yes" if ($svc eq $service);
                 }
               }
               if (! $ignore) {
                 for (@browns) {
                   ($dum,$service,$dum) = split(/\|/);
                   $ignore = "yes" if ($svc eq $service);
                 }
               }
               if (! $ignore) {
                 for (@greens) {
                   ($dum,$service,$dum) = split(/\|/);
                   $ignore = "yes" if ($svc eq $service);
                 }
               }

               if (!$ignore) {
                @greens[$green] = $tmp;
               ($tgt,$svc,$stat,$sev,$d,$d,$svc_output,$text)=split(/\|/,$tmp);
                $port   = &svc2port($svc);
		if ($port != 0) {
                  $ghost  = "        <$tag_vul>\n";
                  $ghost .= "          <$tag_severity>GREEN</$tag_severity>\n";
                  $ghost .= "           <$tag_service>\n";
                  $ghost .= "             <$tag_svc_name>$svc</$tag_svc_name>\n";
                  $ghost .= "             <$tag_svc_port>$port</$tag_svc_port>\n";
                  $ghost .= "           </$tag_service>\n";
                  $ghost .= "         </$tag_vul>\n";
                  $ghosts[$_host]  .= "$ghost";
                  $green++;
                  $green_cnt[$_host] = $green;
		}  
            }
            if (!$os) {
              &infer_hosttype($host);
              $os = $hosttype{$host};
              if ($os) {
                $_hosts[$_host] .= "        <$tag_host_os>$os</$tag_host_os>\n";
              }
            }
          }
         $_host++;
        }
    }
}
       $_host = 0;
       if ($found_host) {
         $grey = $green = $red = $yellow = $brown = 0;
         for (@_hosts) {
           if ($_red_cnt[$_host] != 0) {
             $red++;
           } elsif($_yellow_cnt[$_host] != 0) {
             $yellow++;
           } elsif($_brown_cnt[$_host] != 0) {
             $brown++;
           } elsif($green_cnt[$_host] != 0) {
             $green++;
           } else {
             $grey++;
           }
           $_hosts[$_host] .= "        <$tag_host_green>$green_cnt[$_host]</$tag_host_green>\n";
           $_hosts[$_host] .= "        <$tag_host_red>$_red_cnt[$_host]</$tag_host_red>\n";
           $_hosts[$_host] .= "        <$tag_host_yellow>$_yellow_cnt[$_host]</$tag_host_yellow>\n";
           $_hosts[$_host] .= "        <$tag_host_brown>$_brown_cnt[$_host]</$tag_host_brown>\n";
           $_hosts[$_host] .= "        <$tag_host_gray>$_gray_cnt[$_host]</$tag_host_gray>\n";
           $_host++;
         }
         $counts  = "    <$tag_vul_sum>\n";
         $counts .= "      <$tag_vul_cnt class=\"Total\">$_host</$tag_vul_cnt>\n";
         $counts .= "      <$tag_vul_cnt class=\"Green\">$green</$tag_vul_cnt>\n";
         $counts .= "      <$tag_vul_cnt class=\"Gray\">$grey</$tag_vul_cnt>\n";
         $counts .= "      <$tag_vul_cnt class=\"Red\">$red</$tag_vul_cnt>\n";
         $counts .= "      <$tag_vul_cnt class=\"Yellow\">$yellow</$tag_vul_cnt>\n";
         $counts .= "      <$tag_vul_cnt class=\"Brown\">$brown</$tag_vul_cnt>\n";
         $counts .= "    </$tag_vul_sum>\n";

         $_host   = 0; 
         print XML "$xml_header";
         print XML "$xml_vuls";
         print XML "$xml_header1";
         print XML "$counts";
         for (@_hosts) {
           print XML "$_";
           print XML "      </$tag_host_desc>\n";
           print XML "       <$tag_vuls>\n";
           print XML " $ghosts[$_host]";
           print XML " $rhosts[$_host]";
           print XML " $yhosts[$_host]";
           print XML " $bhosts[$_host]";
           print XML "    </$tag_vuls>\n      </$tag_host>\n";
           $_host++;
         }
         print XML "    </$tag_subnet>\n" if ($_net);
         print XML "  </$tag_details>\n";
         print XML "</$tag_scan>\n";
       }
       close (XML);
}
1;
#
# Builds vulnerability record IAW with config/xml.cf format
#
#

sub save_csv {
local($path) = @_;
$xml         = "results/$sara_data/$report_xml";
@_facts      = "";
@_all_hosts  = "";
@csv_str     = "";
@level       = "";
@csv_rec     = "";
$level_num   = 0;
$_csv        = 0;
$csv_cnt     = 0;
open(CSV, ">$path") || die "cannot save csv to $path: $!";
for (@csv_def) {
  if ($_ eq $tag_severity) {
    $csv_str[$_csv] = "GREY";
    $_csv++;
  } elsif ($_ eq $tag_fp) {
    $csv_str[$_csv] = "-";
    $_csv++;
  } else {
    $csv_str[$_csv] = "";
    $_csv++;
  }
}
open (XML,$xml);
while (<XML>) {
#
# Find next start tag
# 
   if (/<[a-zA-Z0-9\_\-\=\" ][a-zA-Z0-9\_\-\=\"\/\. ]+>/) {
    $class = "";
    ($dum,$tag_start) = split(/</);
    ($tag_start,$dum) = split(/>/,$tag_start);
    if ($tag_start =~ /class\=/) {
      ($tag_start,$class) = split(/\s+/,$tag_start);
      ($dum,$class) = split(/\=/,$class);
      $class  =~ s/\"//g;
      $class .= "=";
    } elsif ($tag_start =~ /xmlns\=/) {
      ($tag_start,$dum) = split(/\s+/,$tag_start);
    }
    $_ =~ s/<[a-zA-Z0-9\_\-\=\" ][a-zA-Z0-9\_\-\=\"\/\. ]+>/%$class/;
    next if ($tag_start =~ /xml version/);
    $level[$level_num] = $tag_start;    
    if ($tag_start eq "$tag_host") {
      $_csv  = 0;
      $_data = 0;
    }
    $level_num++;
  }
#
#  Find next stop tag
#
   if (/<\/[a-zA-Z0-9\_\-\=\" ]+>/) {
    ($_data,$tag_stop) = split(/</);
    $_data =~ s/^\s+%//;
    $_data =~ s/\s+$//;
    $_data =~ s/\n//g;
    ($tag_stop,$dum) = split(/>/,$tag_stop);
    ($dum,$tag_stop) = split(/\//,$tag_stop);
    $level_num--;
    if ($level[$level_num] eq $tag_stop) {
      $_csv = 0;
      for (@csv_def) {
        if ($_ eq "$tag_stop") {
          if ($tag_stop eq $tag_cve) {
            $csv_str[$_csv] = "$_data"; 
          } elsif ($tag_stop eq $tag_sans) {
            $csv_str[$_csv] = "$_data";
          } else {
            $csv_str[$_csv] = "$_data";
          }

        }
        $_csv++;
      }
      if ($tag_stop eq "$tag_vul") {
        $csv_record = "";
        for (@csv_str) {
          $csv_record .= "$_";
          if (/SMB: User list/) {
            $csv_record .= " (dsg)" if $dont_smb_guess == 1;
          }
          $csv_record .= ",";
        }
        $csv_record =~ s/\,$//;
        $csv_rec[$csv_cnt] .= "$csv_record";
        $csv_cnt++;
        $_csv = 0;
        for (@csv_def) {
          if ($_ eq $tag_class) {
            $csv_str[$_csv] = "";
            $_csv++;
          } elsif($_ eq $tag_data) {
           $csv_str[$_csv] = "";
            $_csv++;
          } elsif ($_ eq $tag_fp) {
            $csv_str[$_csv] = "-";
            $_csv++;
          } elsif($_ eq $tag_cve) {
           $csv_str[$_csv] = "";
            $_csv++;
          } elsif($_ eq $tag_sans) {
           $csv_str[$_csv] = "";
            $_csv++;
          } elsif($_ eq $tag_nvd_cvss) {
            $csv_str[$_csv] = "";
            $_csv++;
          } else {
            $_csv++;
          } 
        }
        $_csv = 0;
      } elsif ($tag_stop eq "$tag_host") {
       $csv_record ="";
       for (@csv_str) {
          $csv_record .= "$_,";
        }
        $csv_record =~ s/\,$//;
        if ($csv_record =~ /GREY/) {
          $csv_rec[$csv_cnt] .= "$csv_record";
          $csv_cnt++;
        }
        $_csv = 0;
        for (@csv_def) {
          if ($_ eq $tag_id) {
            $_csv++;
          } elsif($_ eq $tag_date) {
            $_csv++;
          } elsif ($_ eq $tag_severity) {
            $csv_str[$_csv] = "GREY";
            $_csv++;
          } elsif ($_ eq $tag_fp) {
            $csv_str[$_csv] = "-";
            $_csv++;
          } else {
            $csv_str[$_csv] = "";
            $_csv++;
          }
        }
      }
    } else {
       die "error: start tag $level[$level_num] and stop tag ($tag_stop) mismatch\n";
  } 
 }
}
close(XML);
#open(CSV, ">$path") || die "cannot save csv to $path: $!";
for (@csv_rec) {
  print CSV "$_\n";
}
close(CSV);
}
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;
}

1;
