<?php
 $capture = "/home/suck.syn";
 $fingerprints = "/home/httpd/www.rbeverly.net/research/finger/p0f.fp";
 $condprob = "/home/httpd/www.rbeverly.net/research/finger/condprob.pl";
 $p0f = "/home/httpd/www.rbeverly.net/research/finger/p0f";
 $log = "/home/httpd/www.rbeverly.net/research/finger/finger.log";
 $host = getenv("REMOTE_HOST");
 $ip = getenv("REMOTE_ADDR");
 $http_agent = getenv("HTTP_USER_AGENT");
?>

<?php
  // send email to owner on error
  function mailErr($message) {
    $mailto = "rbeverly@rbeverly.net";
    $from = "From: Finger <finger@ralph.rbeverly.net>\r\n";
    $subject = "rbeverly.net/research/finger/finger.php error notification";
    $mailer = "X-Mailer: PHP/".phpversion();
    if (@mail($mailto, $subject, $message, $from . $mailer)) {
       echo("<p><b>** admin notified.  check back in a few hours.</b><br>");
    } else {
       echo("<p><b>mailto error.</b><br></p>");
    }
  }
?>

<?php
 function parse_agent ($http_agent) {
   $all_agents = array("Windows", "Linux", "Solaris", "Mac", "BSD", "SunOS");
   foreach($all_agents as $try) {
     if (stristr($http_agent, $try)) {
       $agent = $try;
       break;
     }
   }
   if ($agent == "") $agent = "Other";
   if ($agent == "SunOS") $agent = "Solaris";
   return $agent;
 }
?>

<HTML>
<HEAD>
<TITLE>Robust Classifier for TCP/IP Fingerprinting</TITLE>
</HEAD>
<BODY BGCOLOR="ffffff">
<FONT SIZE="+2">Robust Classifier for TCP/IP Fingerprinting</FONT>
<P> 
This web page attempts to determine your host's operating
system based on the TCP/IP "signature."  
It analyzes the packet headers of the
HTTP GET request from your host.
This technique is known as TCP/IP fingerprinting and has
applications in intrusion detection, NAT detection and 
Internet modeling.
In contrast to rule-based approaches, we use a 
Bayesian Classifier to probabilistically determine the most
likely operating system.  By accessing this page, you are helping
us better train our classifier.
Additional <A HREF="background.html">background</A>.
<P> 
<?php
 // Let suck.c catch up
 date_default_timezone_set('America/Los_Angeles');
 sleep(1);
 $agent = parse_agent($http_agent);
?>

HTTP Information:
<BLOCKQUOTE>

<?php
 echo "Request from: $ip <BR>\n";
 echo "Time: ".date("r")."<BR>\n";;
 echo "Agent: $http_agent <BR>\n";
 echo "Agent OS: $agent <P>\n";
?>

</BLOCKQUOTE>
Identifying (Rule-based):
<BLOCKQUOTE>

<?php
 $cmd = "/usr/sbin/tcpdump -nr $capture -w - src $ip | $p0f -f $fingerprints -s - | grep $ip | tail -1";
 echo "doing: $cmd\n";
 $line1 = exec($cmd, $output, $error);
 echo "got: $line1\n";
 while (list(,$line) = each($output)){
   echo $line, "<BR>\n";
   $p0fout = $line;
   if (stristr($line, "unknown")) $notfound = 1;
   if (stristr($line, $agent)) $foundagent = 1;
 }
 if ($error){
  echo "Unix error code: $error<BR>\n";
  mailErr("Unix error code: $error \n");
 } 
 if ($notfound) {
   echo "<BR>Rule-based method fails to identify your machine!\n";
   $rule_correct = 0;
 } else if ($foundagent) {
   echo "Correct Guess (Consistent with HTTP agent).\n";
   $rule_correct = 1;
 } else {
   echo "<BR>Guess inconsistent with HTTP agent.\n";
   echo "<BR>Please send us your host information below.\n";
   $rule_correct = 0;
 }
?>

</BLOCKQUOTE>
Identifying (Bayesian Classifier):
<BLOCKQUOTE>

<?php
 $cmd = "/usr/sbin/tcpdump -nr $capture -w - src $ip | $condprob $fingerprints - | tail -1";
 //echo "doing: $cmd\n";
 $line1 = exec($cmd, $output, $error);
 $nomatch = 1;
 while (list(,$line) = each($output)){
   $nomatch = 0;
   echo $line, "<BR>\n";
 }
 if ($error){
  echo "Unix error code: $error<BR>\n";
  mailErr("Unix error code: $error \n");
 } 
 if ($nomatch) {
   echo "<STRONG>Error:</STRONG> I couldn't find the SYN from $ip.<BR>\n";
   mailErr("Couldn't find the SYN from $ip \n");
 } else if (stristr($line1, $agent)) {
   echo "Correct Guess (Consistent with HTTP agent).\n";
   $class_correct = 1;
 } else {
   echo "<BR>Guess inconsistent with HTTP agent.\n";
   echo "<BR>Please send us your host information below.\n";
   $class_correct = 0;
 }
?>
</BLOCKQUOTE>
Feedback:
<BLOCKQUOTE>
<FORM ACTION="send_feedback.php" METHOD="POST">
<P>
We are using this data to train our classifier.  If the classifier 
incorrectly identified your system, please help us by filling in the 
following fields.  Thanks!
<P>
<TABLE>
<TD>Operating System:</TD><TD><INPUT TYPE="TEXT" NAME="os" SIZE="30"></TD><TR>
<TD>OS Version:</TD><TD><INPUT TYPE="TEXT" NAME="osver" SIZE="30"></TD><TR>
<TD>OS/TCP Modifications:</TD><TD><TEXTAREA NAME="additional" ROWS="3" COLS="30" WRAP></TEXTAREA></TD>
</TABLE>
<P>
<INPUT TYPE="HIDDEN" NAME="formtime" VALUE="<?echo time();?>">
<INPUT TYPE="SUBMIT" VALUE="Send">
<INPUT TYPE="RESET" VALUE="Clear">
</BLOCKQUOTE>
<P>
<FONT SIZE="-1">
2004-01-24
</BODY>
</HTML>

<?php
// write log
 #$handle = fopen($log, "a+");
 #$output = time().",\t$ip,\t$class_correct,\t$rule_correct,\t$agent,\t$http_agent,\t$line1,\t$p0fout\n";
 #fwrite($handle, $output);
 #fclose($handle);
?>
