home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / SATAN11.ZIP / PERL / HOSTTYPE.PL < prev    next >
Text File  |  1995-04-11  |  5KB  |  191 lines

  1. #
  2. # infer_hosttype - classify host by banner, the dns HINFO record may be wrong.
  3. # Output to: 
  4. #
  5. # $hosttype{$target}        operating system type per host
  6. # $sysclass{$type}        OS class that an OS type belongs to
  7. # $hosttype_flag        (*)
  8. # $systype_counts{type}        number of hosts per OS type
  9. # $systype_severities{type}    ditto, with at least one vulnerability
  10. # $sysclass_counts{class}    number of hosts per OS class
  11. # $sysclass_severities{class}    ditto, with at least one vulnerability
  12. # $hosts_by_systype{type}    string with hosts per OS type
  13. # $systypes_by_class{class}    string with OS types per OS class
  14. #
  15. # (*) Is reset whenever the $hosttype etc. tables are updated. To recalculate,
  16. # invoke make_hosttype_info_flag().
  17. #
  18. # Standalone usage: perl hosttype.pl [satan_record_files...]
  19.  
  20. $hosttype_files = "rules/hosttype";
  21. $hosttype_unknown = "unknown type";
  22. $hosttype_notprobed = "not scanned";
  23.  
  24. sub build_infer_hosttype {
  25.     local($files) = @_;
  26.     local($type, $code, $file, $cond, $type, $class);
  27.  
  28.     $code = "sub infer_hosttype {\n";
  29.     $code .= "\tlocal(\$type);\n";
  30.     $code .= "\tif (\$service !~ /^(smtp|telnet|ftp)\$/) {\n\t\treturn;\n\t}\n";
  31.     $class = "other";
  32.  
  33.     foreach $file (split(/\s+/, $files)) {
  34.     open(RULES, $file) || die "cannot open $file: $!";
  35.     while (<RULES>) {
  36.         chop;
  37.         while (/\\$/) {
  38.         chop;
  39.         $_ .= <RULES>;
  40.         chop;
  41.         }
  42.         s/#.*$//;
  43.         s/\s+$//;
  44.         next if /^$/;
  45.         if (/^CLASS\s+(.+)/i) {
  46.         $class = $1;
  47.         } else {
  48.         s/\bUNKNOWN\b/(HOSTTYPE eq "" || HOSTTYPE eq \"$hosttype_unknown\" || HOSTTYPE eq \"$hosttype_notprobed\")/;
  49.         s/\bHOSTTYPE\b/\$hosttype{\$target}/g;
  50.         s/@/\\@/g;
  51.         ($cond, $type) = split(/\t+/, $_, 2);
  52.         $type = "\$1" if ($type eq "");
  53.         $code .= "\
  54.     if ($cond) {
  55.         \$type = $type;
  56.         \$hosttype{\$target} = \$type; 
  57.         \$sysclass{\$type} = \"$class\";
  58.         \$hosttype_flag = 0;
  59.         return;
  60.     }
  61. ";
  62.         }
  63.     }
  64.     close(RULES);
  65.     }
  66.     $code .= "\t\$hosttype{\$target} = \"\"
  67.         if !exists(\$hosttype{\$target});\n}";
  68.     return $code;
  69. }
  70.  
  71. #
  72. # Generate hosttype-dependent statistics.
  73. #
  74. sub make_hosttype_info {
  75.     local($host, $class, $type, $count);
  76.  
  77.     if ($hosttype_flag > 0) {
  78.     return;
  79.     }
  80.     $hosttype_flag = time();
  81.  
  82.     print "Rebuild host type statistics...\n" if $debug;
  83.     &make_severity_info();
  84.  
  85.     %hosts_by_systype = ();
  86.     %systypes_by_class = ();
  87.     %systype_severities = ();
  88.     %systype_counts = ();
  89.     %sysclass_severities = ();
  90.     %sysclass_counts = ();
  91.  
  92.     for $host (keys %all_hosts) {
  93.     $hosttype{$host} = $hosttype_unknown 
  94.         if ($host && $hosttype{$host} eq "" && &get_host_time($host) > 1);
  95.     $hosttype{$host} = $hosttype_notprobed 
  96.         if ($host && $hosttype{$host} eq "");
  97.     }
  98.     $sysclass{$hosttype_unknown} = "other";
  99.     $sysclass{$hosttype_notprobed} = "other";
  100.  
  101.     for $type (sort keys %sysclass) {
  102.     $systype_severities{$type} = 0;
  103.     $sysclass_severities{$sysclass{$type}} = 0;
  104.     }
  105.     for $host (sort keys %hosttype) {
  106.     $type = $hosttype{$host};
  107.     if ($type eq "") {
  108.         $type = $hosttype{$host} = $hosttype_unknown;
  109.     }
  110.     if (exists($severity_host_type_info{$host})) {
  111.          $systype_severities{$type}++;
  112.     }
  113.     $hosts_by_systype{$type} .= $host . "\n";
  114.     $systype_counts{$type}++;
  115.     }
  116.  
  117.     for $type (sort keys %sysclass) {
  118.     if (($count = $systype_counts{$type}) > 0) {
  119.         $class = $sysclass{$type};
  120.         $systypes_by_class{$class} .= $type . "\n";
  121.         $sysclass_counts{$class} += $count;
  122.         if ($systype_severities{$type}) {
  123.         $sysclass_severities{$class} += $systype_severities{$type};
  124.         }
  125.     } else {
  126.         delete($sysclass{$type});
  127.     }
  128.     }
  129. }
  130.  
  131. #
  132. # Reset the host type tables.
  133. #
  134. sub clear_hosttype_info {
  135.     %hosttype = ();
  136.     %systype = ();
  137.     %hosts_by_systype = ();
  138.     %systypes_by_class = ();
  139.     %systype_severities = ();
  140.     %systype_counts = ();
  141.     %sysclass_severities = ();
  142.     %sysclass_counts = ();
  143.     $hosttype_flag = 0;
  144. }
  145.  
  146. #
  147. # Some scaffolding for stand-alone operation
  148. #
  149. if ($running_under_satan) {
  150.     eval &build_infer_hosttype($hosttype_files);
  151.     die "error in $hosttype_files: $@" if $@;
  152. } else {
  153.     $running_under_satan = -1;
  154.     $debug = 1;
  155.  
  156.     require 'perl/misc.pl';
  157.  
  158.     #
  159.     # Generate code from rules files.
  160.     #
  161.     $code = &build_infer_hosttype($hosttype_files);
  162.     print "Code generated from $hosttype_files:\n\n";
  163.     print $code;
  164.     eval $code; 
  165.     die "error in $hosttype_files: $@" if $@;
  166.  
  167.     #
  168.     # Apply rules.
  169.     #
  170.     print "\nApplying rules to all SATAN records...\n";
  171.     while (<>) {
  172.     chop;
  173.     if (&satan_split($_) == 0) {
  174.         &infer_hosttype();
  175.     }
  176.     }
  177.     &make_hosttype_info();
  178.     for (sort keys %hosttype) {
  179.     print "$hosttype{$_}    $_\n";
  180.     }
  181.     print "Host class information:\n";
  182.     for $class (sort keys %systypes_by_class) {
  183.     print "$class    ";
  184.     print join('    ', split(/\n/, $systypes_by_class{$class}));
  185.     print "\n";
  186.     }
  187. }
  188.  
  189. 1;
  190.