<? /*
   Setup remote nmap scan against IP client is coming from.
   
  It functions by allowing user to select the type of scan they want and inserting that request into a database
  that will have a remote perl script run that will perform the scans and update the record

# Table structure for table `nmap`
#

CREATE TABLE `twig_nmap` (
  `id` int(11) NOT NULL auto_increment,
  `ipaddress` varchar(50) default NULL,
  `options` varchar(50) default NULL,
  `email` varchar(50) default NULL,
  `results` text,
  `thedate` varchar(14) default NULL,
  `groupid` int(11) NOT NULL default '0',
  `server` varchar(50) default NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=0 ;
    
In the background have the following perl script that will pull each non-scanned record, scan and update the record
-------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl

use Mysql;
use Mail::Mailer;
use MIME::Base64;

# lockfile to keep scan from running twice @ once...
$lockfile = "/tmp/nmapscan.lock";
$email_from = "user@<your email domain>";
$db_host = "<dbhost>";
$db_name = "<dbname>";
$db_user = "<dbuser>";
$db_pass = "<dbpass>";

@unlinklist = ($lockfile);

sub save_result {
  my $dbh = Mysql->Connect($db_host,$db_name,$db_user,$db_pass);
  $encoded_result = encode_base64($_[1]);
  my $sth = $dbh->Query("update nmap set results=\"$encoded_result\" where ipaddress='$_[0]'");

  my $mailer = new Mail::Mailer;

  $mailer->open({From=>$email_from,
                 To=>$_[2],
                 Subject=>"Results of DMZ Services NmapScan on host: $_[0]"}) or
    die "Unable to populate mailer object:$!\n";
  print $mailer $_[1];
  $mailer->close;
  $sth->finish;
}

if (-e $lockfile) {
  print "Scan in progress\n";
  exit 1;
}

open MYFILE, ">$lockfile";
print MYFILE "Scanning...\n";
close MYFILE;

$dbh = Mysql->Connect($datbase_host,$database,$database_user,$database_password);
$sth = $dbh->Query("select * from nmap where results is NULL");

while (@arr = $sth->FetchRow){
  $ipaddress = $arr[1];
  $options = $arr[2];
  $email = $arr[3];
  # i lock down nmap to sudo for the user that this script is running under
  $results = `/usr/bin/sudo /usr/bin/nmap --host_timeout 10000 -P0 $options $ipaddress`;
  save_result($ipaddress,$results,$email);
}
$sth->finish;

unlink @unlinklist;

-------------------------------------------------------------------------------------------------------------------
*/ 
?>
<head><title>Nmap Scan</title></head>
<body>
** Warning ** <br>
<a href="http://www.insecure.org/nmap/">NMAP</a>  is  designed to  allow  system  administrators  and curious individuals to scan large  networks  to  determine which  hosts  are  up and what services they are offering.  
<p>Warning NMap scans are a very intensive system scan.  These scans can, at times, cause disruption of services and is usually identifiable by network monitoring equipment. 
<p>All unauthorized uses of these services are prohibited.
<hr>

<?
  
include("mysql.php3"); //need to convert to adodb... 
  
$thisDb "nmap";  // database name
  
$startserver="<yourscanning server>"//server doing scan - for saving in db...not fully implemented yet 
  
$options="";
  switch (
$scantype) {
    case 
1:           
      
$options .= " -sT";
    break;               
    case 
2:
      
$options .= " -sS";
    break;               
    case 
3:
      
$options .= " -sF";
    break;               
    case 
4:
      
$options .= " -sX";
    break;               
    case 
5:
      
$options .= " -sN";
    break;               
    case 
6:
      
$options .= " -sU";
    break;
    case 
7:
      
$options .= " -sP";
    break;
    case 
8:
      
$options .= " -sR";
    break;
  }
  if (
$noping == "TRUE") { $options .= " -P0"; }
  if (
$tcpfingerprint == "TRUE") { $options .= " -O"; }
  if (
$fastscan == "TRUE") { $options .= " -F"; }
  if (
$ident == "TRUE") { $options .= " -I"; }
  if (
$nodns == "TRUE") { $options .= " -n"; }
  else if (
$nodns == "FALSE") { $options .= " -R"; }
  if (
$verbose == "TRUE") { $options .= " -v -v"; }
  if (
$fragment == "TRUE") { $options .= " -f"; }
  if (
$addscan == 1)
  {
    
$val_results[0] = true// was using an email validation function, may still want to...
    
if ($val_results[0] == true)
    {
      
$nmapserver=$REMOTE_ADDR;
      
$query="INSERT INTO "$thisDb " (ipaddress,options,email,thedate,server) VALUES ('" $nmapserver "','" $options "','" $email "','" time() . "','" $startserver "')";
      
//echo $query . "<br>";
      
$result dbQuery$query );
    }
    else
    {
      echo 
"<b>Error Invalid e-mail address</b><p>";
    }
  }
  
$query "SELECT * FROM $thisDb WHERE (ipaddress='"$REMOTE_ADDR "')"
  
$result dbQuery$query );
  
$j dbNumRows($result);
  
$nmapserver=$REMOTE_ADDR;

  echo 
"<a NAME=\"top\"></a>";

  if (
$j == 0) {
    echo 
"Your host " .  $nmapserver " has not had any scans run.  Please fill in the below information to have a nmap scan generated.<p>";
  }
  else {
    echo 
"There have been " $j " scans run.<br>";

  for (
$z=1;$z<=$j;$z++) { echo "<a href=\"#" $z "\">" $z "</a> ";}

  echo 
"<br> To have your host Scanned again, fill out the below form. <p>";
  }

  echo 
"<FORM ACTION=\"".$PHP_SELF."\" METHOD=\"post\">
  to:
    <INPUT TYPE=\"hidden\" NAME=\"nmapserver\" value="
.$REMOTE_ADDR.">".$REMOTE_ADDR."
  <INPUT TYPE=\"hidden\" NAME=\"addscan\" value=1>
  <br>Scan type:
  <dd><input type=\"radio\" name=\"scantype\" value=\"1\" CHECKED>tcp connect() port scan
  <dd><input type=\"radio\" name=\"scantype\" value=\"2\">tcp SYN stealth port scan
  <dd><input type=\"radio\" name=\"scantype\" value=\"3\">Stealth FIN scan (UNIX only)
  <dd><input type=\"radio\" name=\"scantype\" value=\"4\">Xmas scan (UNIX only)
  <dd><input type=\"radio\" name=\"scantype\" value=\"5\">Null Scan (UNIX only)
  <dd><input type=\"radio\" name=\"scantype\" value=\"6\">UDP port scan
  <dd><input type=\"radio\" name=\"scantype\" value=\"7\">ping \"scan\", Find which hosts on specified network(s) are up but don't port scan them
  <dd><input type=\"radio\" name=\"scantype\" value=\"8\">RPC scan
  <br>With the following options:
  <dd><input type=\"checkbox\" name=\"fragment\" value=\"TRUE\">use tiny fragmented packets for SYN, FIN, Xmas, or NULL scan
  <dd><input type=\"checkbox\" name=\"noping\" value=\"TRUE\">Don't ping hosts (needed to scan www.microsoft.com and others)
  <dd><input type=\"checkbox\" name=\"tcpfingerprint\" value=\"TRUE\" CHECKED>Use TCP/IP fingerprinting to guess what OS the remote host is running
  <dd><input type=\"checkbox\" name=\"fastscan\" value=\"TRUE\">fast scan. Only scans ports in /etc/services, a la strobe
  <dd><input type=\"checkbox\" name=\"ident\" value=\"TRUE\">Get identd (rfc 1413) info on listening TCP processes
  <dd><input type=\"radio\" name=\"nodns\" value=\"TRUE\">Don't DNS resolve anything unless we have to (makes ping scans faster)
  <dd><input type=\"radio\" name=\"nodns\" value=\"FALSE\" CHECKED>Try to resolve all hosts, even down ones (can take a lot of time)
  <dd><input type=\"checkbox\" name=\"verbose\" value=\"TRUE\" CHECKED>Verbose. Its use is recommended
  <br>Email address to send result to:
  <INPUT TYPE=\"text\" NAME=\"email\" SIZE=\"40\" MAXLENGTH=\"100\">
  <INPUT TYPE=submit VALUE=\"Nmap\"><INPUT TYPE=reset VALUE=\"Reset\">
</FORM>
<HR>"
;
  for (
$i 0$i $j$i++)
  {
    
$z=$i+1;
    echo 
"<A NAME=\"" $z "\">Scan #:" $z "</a><br>";
    
$data dbResultArray($result,$i);                      
    echo 
"Date: " date("F j, Y, g:i a" $data["thedate"]) . "<br>Host: " $data["ipaddress"] . "<br>Scanned from: " $data["server"];
    echo 
"<br>Results ";                                                                      
    if (
strlen($data["results"]) < 1) echo "will be ";
    echo 
"sent to: your e-mail address specified<br>";       
    echo 
"Results: ";                          
    if (
strlen($data["results"]) < 1) echo "Scan in progress..(note: scans take place every 15 minutes)";
    else echo 
"<pre>".base64_decode($data["results"])."</pre>";                                          
    echo 
"<a href=\"#top\">Top</a>";                           
    if (
$i $j-1) { echo "<HR>"; } 
  }
?>