####################################################################### #nqt.php last revised 03/01/02 (www.shat.net/php/nqt/) # # Initial release 03/11/01 # # # #Version 1.2 # #03/01: The behavior of the gethostbyaddr() function appears to have # #changed in PHP 4.12, it no longer accepts hostnames in fqdn format. # #(This is how it really should work anyway, I think the old behavior # #was a bug.) Previous versions of NQT relied upon the fact that the # #gethostbyaddr() function would accept a hostname. If you're getting # #errors about addresses not being in a.b.c.d format, please download # #and install this version of the script. # # # #Version 1.1 # #10/30: Security update, thanks Dmitry Frolov, Mathijs Futselaar. # #Previous versions of script allowed possible execution of arbitrary # #commands on the host system. Updated lines marked with #bugfix, code # #courtesy Dmitry Frolov. # # # #Copyright shaun@shat.net - If you use this script, the copyright # #notice appended to query results MUST remain INTACT and UNALTERED, # #including the link to the distribution site http://shat.net/php/bs/ # #This script is free, but you may NOT claim it as your own. # # # #Requires: PHP3+, unix server preferred # # # #This script takes a given hostname or IP address and attempts to # #look up all sorts of information about that address. Basically # #it does what network-tools.com does, without all the ads and ASP :) # # # #The following steps can be performed separately or all at once: # # # # reverse DNS lookup, DNS query (dig), WWW whois, ARIN whois, # # open-port check, ping, traceroute # # # #As you can probably guess this script is intended for unix machines. # #If you use this script under win32, DNS query (dig) will not work. # # # #NO INITIAL CONFIGURATION IS REQUIRED. THERE ARE NO VARIABLES TO SET. # # # #If you encounter problems with traceroute, replace the default path # #/usr/sbin/traceroute with the correct path in the tr() function. # ######################################################################$ #First output the HTML page ?>
DNS Query Results:
"); #$target = gethostbyaddr($target); #if (! eregi("[a-zA-Z]", ($target = gethostbyaddr($target))) ) if( (!eregi("[a-zA-Z]", $target) && (!eregi("[a-zA-Z]", $ntarget)))) $msg .= "Can't do a DNS query without a hostname."; else{ if(!eregi("[a-zA-Z]", $target)) $target = $ntarget; if (! $msg .= trim(nl2br(`dig any '$target'`))) #bugfix $msg .= "The dig command is not working on your system."; } #TODO: Clean up output, remove ;;'s and DiG headers $msg .= ""; message($msg); } function wwwhois($target){ global $ntarget; $server = "whois.crsnic.net"; message("
WWWhois Results:
"); #$target = gethostbyaddr($target); if((!eregi("[a-zA-Z]", $target)) && (!eregi("[a-zA-Z]", $ntarget))){ $msg .= "Can't WWWhois without a domain name."; return; } if (!eregi("\.com|\.net|\.org|\.edu", $target)) if (!eregi("\.com|\.net|\.org|\.edu", $ntarget)){ $msg .= "I currently only support .com, .net, .org, and .edu."; return; } else $target = $ntarget; message("Connecting to $server..."; message($msg); } function arin($target){ $server = "whois.arin.net"; message("
"); if (! $sock = fsockopen($server, 43, &$num, &$error, 10)){ unset($sock); $msg .= "Timed-out connecting to $server (port 43)"; } else{ fputs($sock, "$target\n"); while (!feof($sock)) $buffer .= fgets($sock, 10240); } fclose($sock); if(! eregi("Whois Server:", $buffer)){ if(eregi("no match", $buffer)) message("NOT FOUND: No match for $target
"); else message("Ambiguous query, multiple matches for $target:
"); } else{ $buffer = split("\n", $buffer); for ($i=0; $i
"); if(! $sock = fsockopen($nextServer, 43, &$num, &$error, 10)){ unset($sock); $msg .= "Timed-out connecting to $nextServer (port 43)"; } else{ fputs($sock, "$target\n"); while (!feof($sock)) $buffer .= fgets($sock, 10240); fclose($sock); } } $msg .= nl2br($buffer); $msg .= "
IP Whois Results:
"); if (!$target = gethostbyname($target)) $msg .= "Can't IP Whois without an IP address."; else{ message("Connecting to $server..."; message($msg); } function checkp($target,$portNum){ message("
"); if (! $sock = fsockopen($server, 43, &$num, &$error, 20)){ unset($sock); $msg .= "Timed-out connecting to $server (port 43)"; } else{ fputs($sock, "$target\n"); while (!feof($sock)) $buffer .= fgets($sock, 10240); fclose($sock); } if (eregi("RIPE.NET", $buffer)) $nextServer = "whois.ripe.net"; else if (eregi("whois.apnic.net", $buffer)) $nextServer = "whois.apnic.net"; else if (eregi("nic.ad.jp", $buffer)){ $nextServer = "whois.nic.ad.jp"; #/e suppresses Japanese character output from JPNIC $extra = "/e"; } else if (eregi("whois.registro.br", $buffer)) $nextServer = "whois.registro.br"; if($nextServer){ $buffer = ""; message("Deferred to specific whois server: $nextServer...
"); if(! $sock = fsockopen($nextServer, 43, &$num, &$error, 10)){ unset($sock); $msg .= "Timed-out connecting to $nextServer (port 43)"; } else{ fputs($sock, "$target$extra\n"); while (!feof($sock)) $buffer .= fgets($sock, 10240); fclose($sock); } } $buffer = str_replace(" ", " ", $buffer); $msg .= nl2br($buffer); } $msg .= "
Checking Port $portNum...
"); if (! $sock = fsockopen($target, $portNum, &$num, &$error, 5)) $msg .= "Port $portNum does not appear to be open."; else{ $msg .= "Port $portNum is open and accepting connections."; fclose($sock); } $msg .= ""; message($msg); } function p($target){ message("
Ping Results:
"); if (! $msg .= trim(nl2br(`ping -c5 '$target'`))) #bugfix $msg .= "Ping failed. Host may not be active."; $msg .= ""; message($msg); } function tr($target){ message("
Traceroute Results:
"); if (! $msg .= trim(nl2br(`/usr/sbin/traceroute '$target'`))) #bugfix $msg .= "Traceroute failed. Host may not be active."; $msg .= ""; message($msg); } #If the form has been posted, process the query, otherwise there's #nothing to do yet foreach($_POST as $key=>$value) ${$key}=$value; if(!$queryType) exit; #Make sure the target appears valid if( (!$target) || (!preg_match("/^[\w\d\.\-]+\.[\w\d]{1,3}$/i",$target)) ){ #bugfix message("Error: You did not specify a valid target host or IP."); exit; } #Figure out which tasks to perform, and do them if( ($queryType=="all") || ($queryType=="lookup") ) lookup($target); if( ($queryType=="all") || ($queryType=="dig") ) dig($target); if( ($queryType=="all") || ($queryType=="wwwhois") ) wwwhois($target); if( ($queryType=="all") || ($queryType=="arin") ) arin($target); if( ($queryType=="all") || ($queryType=="checkp") ) checkp($target,$portNum); if( ($queryType=="all") || ($queryType=="p") ) p($target); if( ($queryType=="all") || ($queryType=="tr") ) tr($target); ?>
Network Query Tool
1.2
Copyright 2002 shaun@shat.net