Source for file PortScan.php

Documentation is available at PortScan.php

  1. <?php
  2. /**
  3.  * PortScan.php
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE: This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License as published
  9.  * by the Free Software Foundation.
  10.  *
  11.  * @package                     FreeNAC
  12.  * @author                      Seiler Thomas (contributer)
  13.  * @author                      Hector Ortiz (FreeNAC Core Team)
  14.  * @copyright                   2007 FreeNAC
  15.  * @license                     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 2
  16.  * @version                     SVN: $Id$
  17.  * @link                        http://www.freenac.net
  18.  */
  19.  
  20. /**
  21.  * This class represents a row in the systems table in the database.
  22.  * Additionaly, it retrieves information about open ports for this end device.
  23.  * This class extends the {@link EndDevice} class
  24.  */
  25. class PortScan extends EndDevice
  26. {
  27.  
  28.    /**
  29.    * Retrieve a row from the systems table for this system and also port_scan information
  30.    * @param object $object    Required to construct our object
  31.    */
  32.    public function __construct($object)
  33.    {
  34.       #Call parent constructor to retrieve a row from the systems table
  35.       parent::__construct($object);
  36.  
  37.       #And now retrieve port_scan information for this system
  38.       if ($this->inDB())
  39.       {
  40.          #Check if this system is in the nac_hostscanned table
  41.          $query=<<<EOF
  42.             SELECT ip AS port_scan_ip, 
  43.             hostname AS port_scan_hostname, 
  44.             os AS port_scan_os, 
  45.             timestamp AS port_scan_lastscanned 
  46.             FROM nac_hostscanned 
  47.             WHERE sid='{$this->getEndDeviceID()}';
  48. EOF;
  49.          $this->logger->debug($query,3);
  50.          $res=mysql_query($query);
  51.          if ($res)
  52.          {
  53.             #If so, assign all properties retrieved to our internal array
  54.             $row=mysql_fetch_assoc($res);
  55.             if ($res && (mysql_num_rows($res)>0))
  56.             {
  57.                foreach ($row as $k => $v)
  58.                   $this->db_row[$k]=$v;
  59.             
  60.                #Now, get the list of open ports for this device
  61.                $query=<<<EOF
  62.                   SELECT s.port, p.name AS protocol, s.name AS service, o.banner, o.timestamp
  63.                   FROM nac_openports o INNER JOIN services s ON o.service=s.id
  64.                   INNER JOIN protocols p on p.protocol=s.protocol WHERE o.sid='{$this->getEndDeviceID()}';
  65. EOF;
  66.                $this->logger->debug($query,3);
  67.                $res=mysql_query($query);
  68.                $num_rows=mysql_num_rows($res);
  69.                if ($res && ($num_rows>0))
  70.                {
  71.                   $open_ports=mysql_num_rows($res);
  72.                   #How many open ports?
  73.                   $this->db_row['open_ports']=$open_ports;
  74.                   if ($open_ports 0)
  75.                   {
  76.                      #And store those open ports in our internal array
  77.                      while ($row=mysql_fetch_assoc($res))
  78.                         $this->db_row['ports'][]=$row;
  79.                   }
  80.                }
  81.             }
  82.          }
  83.       }
  84.    }
  85.  
  86.    /**
  87.    * Get the number of open ports for this device
  88.    * @return mixed    Number of ports open or false if the device is not in de database
  89.    */
  90.    public function openPorts()
  91.    {
  92.       if ($this->inDB())
  93.       {
  94.          return $this->open_ports;
  95.       }
  96.       else
  97.       {
  98.          return false;
  99.       }      
  100.    }
  101.  
  102.    /**
  103.    * Get all open ports for this system
  104.    * @return array        List of open ports according to the nac_openports table
  105.    */ 
  106.    public function getPorts()
  107.    {
  108.       if ($this->inDB(&& $this->openPorts())
  109.       {
  110.          return $this->ports;
  111.       }
  112.    }
  113.  
  114.    /**
  115.    * Tell if a specific port has been open during the last days
  116.    * @param integer $port    Port number
  117.    * @param mixed $protocol    Protocol for this port
  118.    * @param integer $days    Number of days to check if the port has been open for
  119.    * @return boolean        True if port has been open during the last days, false otherwise
  120.    */
  121.    public function isPortOpen($port 0$protocol 'TCP'$days 7)
  122.    {
  123.       if ($days && !is_numeric($days))
  124.          $days=7;
  125.       #Convert number of days in seconds
  126.       #This is done so because our time_diff function returns the difference between two dates in seconds
  127.       $days_in_seconds $days*24*3600;
  128.       if ($port && is_numeric($port&& $this->openPorts())
  129.       {
  130.          foreach($this->ports as $temp_port)
  131.             #Check if this port is the one we are refering to
  132.             if (($temp_port['port'== $port&& (strcasecmp($temp_port['protocol']trim($protocol)) == 0))
  133.                #If so, check if this port was open in the last $days days
  134.                if time_diff($this->port_scan_lastscanneddate('Y-m-d H:i:s')) $days_in_seconds)
  135.                {
  136.                   return true;
  137.                }
  138.          return false;
  139.       }
  140.       else
  141.       {
  142.          return false;
  143.       }
  144.    }
  145.  
  146.    public function scan()
  147.    {
  148.       $query="UPDATE systems SET scannow='1' where id='{$this->sid}';";
  149.       $res = mysql_query($query);
  150.       if ($res)
  151.       {
  152.          return true;
  153.       }
  154.       else
  155.       {
  156.          $this->logger->logit(mysql_error(),LOG_ERR);
  157.          return false;
  158.       }
  159.    }
  160. }
  161.  

Documentation generated on Mon, 17 Nov 2008 01:10:45 +0100 by phpDocumentor 1.4.0