Source for file port_scan.php
Documentation is available at port_scan.php
* Long description for file:
* Learn open ports from equipments allowed
* Currently it has 3 modes of operation:
* Normal: Without parameters
* Will get ips from devices allowed in the network and will scan them
* Scannow: With the parameter "scannow"
* Will get ips which have the scannow value set to 1 no matter if
* they are allowed in the network or not
* Every parameter passed through the command line will be taken
* as an ip to be scanned. No to be used in combination with the
* Important: You have to define first networks to scan in the
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation.
* @author Héctor Ortiz (FreeNAC Core Team)
* @copyright 2006 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* @link http://www.freenac.net
require_once "funcs.inc.php";
$logger->setDebugLevel(0);
$logger->setLogToStdOut(false);
#Compatibility with old vars
if (!$conf->scan_directory &&
$conf->nmap_scan_directory)
$scan_directory=
$conf->nmap_scan_directory;
$scan_directory=
$conf->scan_directory;
if (!$conf->what_units_time &&
$conf->nmap_what_units_time)
$what_units_time=
$conf->nmap_what_units_time;
$what_units_time=
$conf->what_units_time;
if (!$conf->time_threshold &&
$conf->nmap_time_threshold)
$time_threshold=
$conf->nmap_time_threshold;
$time_threshold=
$conf->time_threshold;
if (!$conf->which_nmap &&
$conf->nmap_path)
$which_nmap=
$conf->nmap_path;
$which_nmap=
$conf->which_nmap;
//If we get up to this point, life is good
if ($argv[$i]==
"--scannow")
else if ($argv[$i]==
"--verbose")
$logger->setDebugLevel(1);
message("Doing port_scan to sytems... Please wait...\n");
$logger->debug("port_scan started");
$file_timestamp=
date('Y-m-d H:i:s');
$scan_results=
$scan_directory.
"/scan-$file_timestamp.xml"; //Scan file
$logger->debug("Scan file: $scan_results"); //Parameters from port_scan.inc
$logger->debug("Nmap flags: ".
$conf->nmap_flags);
if (($what_units_time<
0)||
($what_units_time>
6)||
($what_units_time==
2))
$string=
$time_threshold.
" hours";
else if ($what_units_time==
0)
$string=
$time_threshold.
" seconds";
else if ($what_units_time==
1)
$string=
$time_threshold.
" minutes";
else if ($what_units_time==
3)
$string=
$time_threshold.
" days";
else if ($what_units_time==
4)
$string=
$time_threshold.
" weeks";
else if ($what_units_time==
5)
$string=
$time_threshold.
" months";
else if ($what_units_time==
6)
$string=
$time_threshold.
" years";
$logger->debug("Last_seen threshold: $string");
if ($argc==
1) //Running mode
$logger->debug("Running mode: Normal");
$logger->debug("Running mode: Scannow");
else $logger->debug("Running mode: Manual");
$list=
scan($scan_results,$conf->nmap_flags); //Scan network with those flags
if ($var['equipments']>
0)
message("port_scan finished normally. ".
$var['equipments'].
" hosts scanned\n");
$logger->debug("port_scan finished normally. ".
$var['equipments'].
" hosts scanned\n");
log2db('info',"port_scan finished normally. ".
$var['equipments'].
" hosts scanned");
global $conf,$what_units_time, $scan_directory, $time_threshold, $which_nmap;
$functions=
$functions['user']; //A little bit of paranoia :)
if (!in_array('normalise_mac',$functions))
$query=
"describe systems;";
check_and_abort("Please make sure you have properly installed FreeNAC\n",$res);
$query=
"describe nac_hostscanned;";
check_and_abort("Please make sure you have properly followed the doc file README.port_scan\n",$res);
$query=
"describe nac_openports;";
check_and_abort("Please make sure you have properly followed the doc file README.port_scan\n",$res);
$query=
"describe subnets;";
check_and_abort("Please make sure you have properly followed the doc file README.port_scan\n",$res);
$query=
"describe services;";
check_and_abort("Please make sure you have properly followed the doc file README.port_scan\n",$res);
$query=
"describe protocols;";
check_and_abort("Please make sure you have properly followed the doc file README.port_scan\n",$res);
$tmp=
syscall($which_nmap.
" --version | grep -i nmap");
if (isset
($nmap_string)&&
(strcasecmp($nmap_string,"nmap")!=
0))
#if (isset($nmap_version)&&($nmap_version<4.11))
if (isset
($nmap_version)&&
($nmap_version<
4.10))
global $output,$logger,$output_to_syslog;
if (($output===
TRUE)&&
(!$logger->getDebugLevel()))
if ($output_to_syslog===
TRUE)
//log2db('info',$string);
$logger->setLogToStdOut();
$logger->setLogToStdOut(false);
function do_something($query) //Let's do something with our structure
if ( isset
($query['number']) )
$queries=
$query['number']; //How many queries we have?
if ( isset
($query['messages']) )
$messages=
$query['messages']; //How many messages?
for ($i=
0;$i<
$queries;$i++
)
if ( isset
($query['query'][$i]) )
for ($i=
0;$i<
$messages;$i++
)
if ( isset
($query['message'][$i]) )
$logger->debug($query['message'][$i]); //And display the messages
global $queries; //Here we hold messages and queries
if ( ! isset
($queries['query'][$queries['number']]) )
$queries['query'][$queries['number']]=
$mesg; //This is a query
if ( ! isset
($queries['number']) )
$queries['number']++
; //Count queries
if ( ! isset
($queries['message'][$queries['messages']]) )
$queries['message'][$queries['messages']]=
$mesg; //This is a message
if ( ! isset
($queries['messages']) )
$queries['messages'] =
0;
$queries['messages']++
; //Count messages
for ($i=
0;$i<
$data_from_xml['equipments'];$i++
) //How many hosts scanned
if ( isset
($data_from_xml[$i]['ip']) )
$ip=
$data_from_xml[$i]['ip']; //Get ip of one host
if ( isset
($data_from_xml[$i]['sid']) )
$id=
$data_from_xml[$i]['sid'];
add_entry($data_from_xml[$i]);//Host not found in database
check_existent($data_from_xml[$i]); //Host in database, let's see if something has changed
function check_existent($data) //This function will check info concerning one host scanned against its info in the database
global $logger, $queries;
$timestamp=
date('Y-m-d H:i:s');
if ((!isset
($data))||
(!is_array($data)))
check_and_abort("There was a problem parsing the XML file. Make sure you have the right version of PHP and libXML in your system",0);
if ( isset
($data['ip']) )
if ( isset
($data['ports']) )
$ports=
$data['ports']; //Number of open ports this time
if ( isset
($data['hostname']) )
if ( isset
($data['os']) )
$os=
$data['os']; //OS system this time
if ( isset
($data['sid']) )
$db_ip=
$result['ip']; //Same IP from last time?
$db_hostname=
strtolower($result['hostname']); //Same hostname from last time?
$db_os=
$result['os']; //Same OS from last time?
$db_timestamp=
$result['timestamp']; //If it changed, since when?
$host_changed=
$os_changed=
$mac_changed=
0; //To control if we need to update its record in the database
if (!empty($hostname)&&
!empty($db_hostname)&&
(strcasecmp($hostname,$db_hostname)!=
0)) //Info about its hostname
update_queries("Host $ip has its hostname resolved now. $ip is $hostname\n",'m');
update_queries("Host $ip has its hostname resolved now. $ip is $hostname\n",'m');
update_queries("Unable to resolve $ip this time, old hostname $db_hostname preserved\n",'m');
update_queries("Old hostname $db_hostname no longer valid. Renamed to $hostname\n",'m');
if (!empty($os)&&
!empty($db_os)&&
(strcasecmp($os,$db_os)!=
0)) //Info about its OS
//update_queries("No OS info yet for $ip this time\n",'m');
update_queries("$ip has changed its OS since $db_timestamp. Now is using $os\n",'m');
$changes=
$host_changed+
$os_changed+
$mac_changed;
$query=
sprintf("update nac_hostscanned set hostname='%s',os='%s',timestamp='%s' where sid='%d' and ip='%s';",$hostname,$os,$timestamp,$id,$ip);
$query=
sprintf("select o.banner as banner,o.timestamp as timestamp, p.name as protocol, s.port as port from nac_openports o inner join services s on o.service=s.id inner join protocols p on s.protocol=p.protocol and o.sid='%s';",mysql_real_escape_string($id));
if ($res1) //Let's check info about ports
$db_tmp_port_tcp[$db_tcp]['port']=
$result['port'];
$db_tmp_port_tcp[$db_tcp]['timestamp']=
$result['timestamp'];
$db_tmp_port_tcp[$db_tcp]['banner']=
$result['banner'];
else if (strcasecmp($result['protocol'],'udp')==
0)
$db_tmp_port_udp[$db_udp]['port']=
$result['port'];
$db_tmp_port_udp[$db_udp]['timestamp']=
$result['timestamp'];
$db_tmp_port_udp[$db_udp]['banner']=
$result['banner'];
if ((isset
($db_tmp_port_tcp))&&
(is_array($db_tmp_port_tcp)))
if ((isset
($db_tmp_port_udp))&&
(is_array($db_tmp_port_udp)))
if (($db_tcp==
0)&&
($db_udp==
0)) //In case we have no info in the db
$db_porttstmp[0]=
'0000-00-00 00:00:00';
for ($i=
0;$i<
$db_tcp;$i++
)
$db_port[$i]=
$db_tmp_port_tcp[$i]['port'];
$db_porttstmp[$i]=
$db_tmp_port_tcp[$i]['timestamp'];
$db_banner[$i]=
$db_tmp_port_tcp[$i]['banner'];
for (;$i<
($db_tcp+
$db_udp);$i++
)
$db_port[$i]=
$db_tmp_port_udp[($i-
$db_tcp)]['port'];
$db_porttstmp[$i]=
$db_tmp_port_udp[($i-
$db_tcp)]['timestamp'];
$db_banner[$i]=
$db_tmp_port_udp[($i-
$db_tcp)]['banner'];
for ($i=
0;$i<
$ports;$i++
)
if ($data['port'][$i]['protocol']==
'tcp')
$tmp_port_tcp[$tcp]['port']=
$data['port'][$i]['portid'];
$tmp_port_tcp[$tcp]['banner']=
$data['port'][$i]['description'];
else if ($data['port'][$i]['protocol']==
'udp')