Location: PHPKode > projects > IPTables log analyzer > iptablelog/to_port.php
<?php
/////////////////////////////////////////////////////////////////////////////////////
// IPTable log analyzer
// Copyright (C) 2002 Gerald GARCIA
//
// 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; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Plac<B2>e - Suite 330, Boston, MA  02111-1307, USA.
//
// Contact author : hide@address.com
/////////////////////////////////////////////////////////////////////////////////////

// $Id: to_port.php,v 1.6 2007/03/30 22:39:18 tarbuck Exp $


require_once("conf/config.php");
require_once("$file_base/utils/Tabulator.php");

$tabulator = new Tabulator();

pageHeader();

$port=$_REQUEST["port"];
$port_where = " AND (u.udp_dport=$port OR u.tcp_dport=$port)";

if ($chain != 'ALL') {
        $chain_where = " AND u.oob_prefix='$chain'";
}
if ($date < 1000) {
        $date_where = " AND u.local_time > UNIX_TIMESTAMP(ADDDATE(CURDATE(),INTERVAL -$date DAY))";
        $date_displayString = ", younger than " . $dateToString[$date];
}
if ($srcsubnet != "0.0.0.0/0") {
        list($net,$mask) = parsesubnet($srcsubnet,1);
        $subnet_where  .= " AND (u.ip_saddr & $mask) = $net";
        $subnet_displayString = ", matching source " . $srcsubnet;
}

if ($destsubnet != "0.0.0.0/0") {
      list($net,$mask) = parsesubnet($destsubnet,1);
      $subnet_where  .= " AND (u.ip_daddr & $mask) = $net";
      if (!isset($subnet_displayString)) {
          $subnet_displayString = ", matching destination $destsubnet";
      } else {
          $subnet_displayString .= " and destination " . $destsubnet;
      }
}


#$query = "SELECT name,description FROM ports where port=$port ORDER BY custom DESC limit 1";
#$result = db_query ($query) or db_handleError("Unable to query log table");
#if ($res=db_nextobject($result)) {
#  $port_name=$res->name;
#  $port_description=$res->description;
#}
$port_name= findPortNameFromNumber($port);
$port_proto= findPortProtoFromNumber($port);
$port_description= findPortDescFromNumber($port);

$query = "SELECT count(id) FROM ulog u WHERE 1=1 $date_where $chain_where $subnet_where $port_where";
$result = db_query ($query) or db_handleError("Unable to query log table");
$nb_records=db_nextvalue($result);

if ($first > ($nb_records - $number)) { $first = ($nb_records - $number); }
if ($first < 0) { $first = 0; }

$display_cols = 3;

?>

<b>Filtered packets to port <?php print "$port $port_proto $port_name ($port_description)"; ?> for chain <?php print $chain; ?> younger than <?php print $dateToString[$date]; ?> :</b><br><br>


<table cellspacing="0" cellpadding="0" width="100%"><tr bgcolor="#DDDDDD"><td>
<table cellspacing="1" cellpadding="2" width="100%">
<tr><td><center>Chain</center></td><td><center>Date</center></td>
<?php 
if ($display_netfilter_host) { 
	echo "<td><center>Host&nbsp;<a href='?_display_netfilter_host=0&port=$port' class='DelButton'>[X]</a></center></td>";
	$display_cols++;
}
if ($display_netfilter_interface) { 
	echo "<td><center>Interf.&nbsp;<a href='?_display_netfilter_interface=0&port=$port' class='DelButton'>[X]</a></center></td>";
	$display_cols++;
}
echo "<td><center>Source IP</center></td>\n";
if ($display_netfilter_srcport) { 
	echo "<td nowrap><center>S. port <a href='?_display_netfilter_srcport=0' class='DelButton'>[X]</a></center></td>\n";
	$display_cols++;
}
if ($display_netfilter_destination) { 
	echo "<td><center>Dest. IP&nbsp;<a href='?_display_netfilter_destination=0&port=$port' class='DelButton'>[X]</a></center></td>\n";
	$display_cols++;
}
?>
</tr>
<?php

$query = "SELECT FROM_UNIXTIME(u.local_time) as date,local_hostname AS host,u.oob_prefix AS chain,u.oob_in AS interface_in,u.oob_out AS interface_out,INET_NTOA(u.ip_saddr) AS ip_src,p.name AS proto,IFNULL(shc.host,'Unresolved') AS name_src,IFNULL(u.tcp_dport,IFNULL(u.udp_dport,0)) AS port_dest,IFNULL(u.tcp_sport,IFNULL(u.udp_sport,0)) AS port_src,IFNULL(dhc.host,'Unresolved') AS name_dest,INET_NTOA(u.ip_daddr) AS ip_dest FROM ulog u LEFT OUTER JOIN host_cache shc ON u.ip_saddr = shc.ip LEFT OUTER JOIN host_cache dhc ON u.ip_daddr = dhc.ip LEFT OUTER JOIN protos p ON u.ip_protocol = p.num WHERE 1=1 $date_where $chain_where $subnet_where $port_where ORDER BY date DESC LIMIT $first,$number";

$result = db_query ($query);
if ( db_numrows($result) == 0 ) {
  print "<tr bgcolor=\"#FFFFFF\"><td colspan=$display_cols>No packets match the request</td></tr>";
  print "<tr bgcolor=\"#FFFFFF\"><td colspan=$display_cols>$query</td></tr>";
} else {

while($line = db_nextobject($result)){
   print "<tr bgcolor=\"#FFFFFF\">";
   print "<td>$line->chain</td>"; 
   print "<td>$line->date</td>"; 
   if ($display_netfilter_host) { print "<td>$line->host</td>"; }
   if ($display_netfilter_interface) { 
        if ($line->interface_in) {
                print "<td>$line->interface_in&lt;-</td>";
        } else {
                print "<td>$line->interface_out-&gt;</td>";
        }
    }


   print "<td><a href=\"from_host.php?ip=$line->ip_src\">";
   if ($line->name_src=="Unresolved") { print "$line->ip_src"; } else { print "$line->name_src"; }
   print "</a></td>";

   if ($display_netfilter_srcport) {
	if (strcmp($line->proto,"ICMP")) { print "<td>$line->port_src</td>"; } else { echo "<td>&nbsp;</td>"; }
   }

   if ($display_netfilter_destination) {
     print "<td><a href=\"to_host.php?ip=$line->ip_dest\">";
     if ($line->name_dest=="Unresolved") { print "$line->ip_dest"; } else { print "$line->name_dest"; }
     print "</a></td>";
   }

   print "</tr>";
}

} // nb packet != 0

?>

</table>
</td></tr></table>


<?php
$tabulator->display($nb_records); 
?>

<?php if (!$display_netfilter_host) { ?><a href="?_display_netfilter_host=1&port=<?php print $port; ?>" class="AddButton">Show Host</a><br> <?php } ?>
<?php if (!$display_netfilter_interface) { ?><a href="?_display_netfilter_interface=1&port=<?php print $port; ?>" class="AddButton">Show Interface</a><br> <?php } ?>
<?php if (!$display_netfilter_srcport) { ?><a href="?_display_netfilter_srcport=1&port=<?php print $port; ?>" class="AddButton">Show Source Port</a><br><?php } ?>
<?php if (!$display_netfilter_destination) { ?><a href="?_display_netfilter_destination=1&port=<?php print $port; ?>" class="AddButton">Show Destination IP</a><br><?php } ?>


<?php pageFooter(); ?>
Return current item: IPTables log analyzer