Firewall-Log-Analyzer by Christian Schmidt

git-svn-id: http://svn.ipfire.org/svn/ipfire/trunk@447 ea5c0bd1-69bd-2848-81d8-4f18e57aeed8
This commit is contained in:
ms
2007-03-11 20:34:32 +00:00
parent c5568d645a
commit 76dd79d156
4 changed files with 1842 additions and 0 deletions

View File

@@ -0,0 +1,534 @@
#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# JC HERITIER
# page inspired from the initial firewalllog.dat
#
# Modified for IPFire by Christian Schmidt (www.ipfire.org)
use strict;
use Geo::IP::PurePerl;
use Getopt::Std;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
use POSIX();
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::table2colour} );
undef (@dummy);
my %cgiparams=();
my %settings=();
my $pienumber;
my $otherspie;
my $showpie;
my $sortcolumn;
my $errormessage = '';
$cgiparams{'pienumber'} = 10;
$cgiparams{'otherspie'} = 1;
$cgiparams{'showpie'} = 1;
$cgiparams{'sortcolumn'} = 1;
my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' );
my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
$Lang::tr{'december'} );
my @now = localtime();
my $dow = $now[6];
my $doy = $now[7];
my $tdoy = $now[7];
my $year = $now[5]+1900;
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
$cgiparams{'ACTION'} = '';
&General::readhash("${General::swroot}/fwlogs/ipsettings", \%settings);
if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
&Header::getcgihash(\%cgiparams);
if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
{
&General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
}
my $start = -1;
if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
{
my @temp = split(',',$ENV{'QUERY_STRING'});
$start = $temp[0];
$cgiparams{'MONTH'} = $temp[1];
$cgiparams{'DAY'} = $temp[2];
}
if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
!($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
{
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
}
elsif($cgiparams{'ACTION'} eq '>>')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
## Retrieve the same time on the next day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
elsif($cgiparams{'ACTION'} eq '<<')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
## Retrieve the same time on the previous day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
{
my @then = ();
if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
( $cgiparams{'MONTH'} > $now[4] ) ) {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
} else {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
}
$tdoy = $then[7];
my $lastleap=($year-1)%4;
if ($tdoy>$doy) {
if ($lastleap == 0 && $tdoy < 60) {
$doy=$tdoy+366;
} else {
$doy=$doy+365;
}
}
}
my $datediff=0;
my $dowd=0;
my $multifile=0;
if ($tdoy ne $doy) {
$datediff=int(($doy-$tdoy)/7);
$dowd=($doy-$tdoy)%7;
if (($dow-$dowd)<1) {
$datediff=$datediff+1;
}
if (($dow-$dowd)==0) {
$multifile=1;
}
}
my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
my $day = $cgiparams{'DAY'};
my $daystr='';
if ($day <= 9) {
$daystr = " $day"; }
else {
$daystr = $day;
}
my $skip=0;
my $filestr='';
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
# Note: This is in case the log does not exist for that date
}
my $lines = 0;
my @log=();
if (!$skip)
{
while (<FILE>)
{
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
$log[$lines] = $_;
$lines++;
}
}
close (FILE);
}
$skip=0;
if ($multifile) {
$datediff=$datediff-1;
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
}
if (!$skip) {
while (<FILE>) {
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
$log[$lines] = $_;
$lines++;
}
}
close (FILE);
}
}
my $MODNAME="fwlogs";
&Header::showhttpheaders();
&Header::openpage($Lang::tr{'firewall log'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
if ($errormessage) {
&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
print "<font class='base'>$errormessage&nbsp;</font>\n";
&Header::closebox();
}
&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
print <<END
<form method='post' action='$ENV{'SCRIPT_NAME'}'>
<table width='100%'>
<tr>
<td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
<td width='10%'>
<select name='MONTH'>
END
;
my $month;
for ($month = 0; $month < 12; $month++)
{
print "\t<option ";
if ($month == $cgiparams{'MONTH'}) {
print "selected='selected' "; }
print "value='$month'>$longmonths[$month]</option>\n";
}
print <<END
</select>
</td>
<td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
<td width='40%'>
<select name='DAY'>
END
;
for ($day = 1; $day <= 31; $day++)
{
print "\t<option ";
if ($day == $cgiparams{'DAY'}) {
print "selected='selected' "; }
print "value='$day'>$day</option>\n";
}
if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
print <<END
</select>
</td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
<td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
</tr>
</table>
<table width='100%'>
<tr><td width='20%' align='right' valign="center">$Lang::tr{'Number of IPs for the pie chart'}:</td>
<td width='10%' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
<td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
</table>
</form>
END
;
&Header::closebox();
&Header::openbox('100%', 'left', 'Firewall Logs');
print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
my $linesjc = 0;
my %tabjc;
if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
$lines = 0;
foreach $_ (@log)
{
if($_ =~ /SRC\=([\d\.]+)/){
$tabjc{$1} = $tabjc{$1} + 1 ;
if(($tabjc{$1} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
$linesjc++;
}
}
$pienumber = $lines;
my @keytabjc = keys %tabjc;
my @slice;
my $go;
my $nblinejc;
if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
if( $go != 1){ $nblinejc = 1000; }
my @key;
my @value;
my $indice=0;
my @tabjc2;
if ($sortcolumn == 1)
{
@tabjc2 = sort { $b <=> $a } values (%tabjc);
}
else
{
@tabjc2 = sort { $a <=> $b } keys (%tabjc);
}
my $colour=1;
##############################################
#pie chart generation
use GD::Graph::pie;
use GD::Graph::colour;
#ips sort by hits number
my $v;
if ($sortcolumn == 1)
{
for ($v=0;$v<$pienumber;$v++){
findkey($tabjc2[$v]);
}
}
else
{
foreach $v (@tabjc2) {
$key[$indice] = $v;
$value[$indice] = $tabjc{$v};
$indice++;
}
}
my @ips;
my @numb;
@ips = @key;
@numb = @value;
my $o;
if($cgiparams{'otherspie'} == 2 ){}
else{
my $numothers;
for($o=0;$o<$pienumber;$o++){
$numothers = $numothers + $numb[$o];
}
$numothers = $linesjc - $numothers;
if ($numothers > 0) {
$ips[$pienumber]="$Lang::tr{'otherip'}";
$numb[$pienumber] = $numothers;
}
}
my @data = (\@ips,\@numb);
use GD::Graph::colour qw( :files );
GD::Graph::colour::read_rgb( "/srv/web/ipfire/cgi-bin/logs.cgi/colours.txt" );
if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
my $mygraph = GD::Graph::pie->new(500, 350);
$mygraph->set(
'title' => '',
'pie_height' => 50,
'start_angle' => 89
) or warn $mygraph->error;
$mygraph->set_value_font(GD::gdMediumBoldFont);
$mygraph->set( dclrs => [ "colour1" , "colour2" , "colour3" , "colour4" , "colour5" , "colour6" , "colour7" , "colour8" , "colour9" , "colour10" ] );
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
my @filenames = glob("/srv/web/ipfire/html/fwlogs/ip*.png");
unlink(@filenames);
my $imagerandom = rand(1000000);
my $imagename = "/srv/web/ipfire/html/fwlogs/ip$imagerandom.png";
open(FILE,">$imagename");
print FILE $myimage->png;
close(FILE);
#####################################################
print "<table align='center'><tr><td>";
print "<img src='/fwlogs/ip$imagerandom.png'>";
print "</td></tr></table>";
}
print <<END
<TABLE WIDTH='100%'>
<TR>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'></TD>
<TD WIDTH='30%' ALIGN='CENTER' CLASS='boldbase'><B>IP</B></TD>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>Flag</B></TD>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>Count</B></TD>
<TD WIDTH='30%' ALIGN='CENTER' CLASS='boldbase'><B>Percent</B></TD>
</TR>
END
;
my $total=0;
my $show=0;
my @colour;
my $colour1;
my $colour2;
my $colour3;
my $colour4;
my $colour5;
my $colour6;
my $colour7;
my $colour8;
my $colour9;
my $colour10;
@colour = GD::Graph::colour::_rgb("colour1");
$colour1 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour2");
$colour2 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour3");
$colour3 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour4");
$colour4 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour5");
$colour5 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour6");
$colour6 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour7");
$colour7 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour8");
$colour8 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour9");
$colour9 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour10");
$colour10 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
my $s;
my $percent;
for($s=0;$s<$lines;$s++)
{
$show++;
$percent = $value[$s] * 100 / $linesjc;
$percent = sprintf("%.3f", $percent);
$total = $total + $value[$s];
if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
my $gi = Geo::IP::PurePerl->new();
my $ccode = $gi->country_code_by_name($key[$s]);
my $fcode = lc($ccode);
$colour++;
print "<TD ALIGN='CENTER'><form method='post' action='showrequestfromip.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='ip' value='$key[$s]'> <input type='submit' value='details'></form></TD>";
print "<TD ALIGN='CENTER'><a href='/cgi-bin/ipinfo.cgi?ip=$key[$s]'>$key[$s]</a></TD>";
print "<TD ALIGN='CENTER'><a href='/cgi-bin/country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></TD>";
print "<TD ALIGN='CENTER'>$value[$s]</TD>";
print "<TD ALIGN='CENTER'>$percent</TD>";
print "</TR>";
}
if($cgiparams{'otherspie'} == 2 ){}
else{
if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
my $dif;
$dif = $linesjc - $total;
$percent = $dif * 100 / $linesjc;
$percent = sprintf("%.3f", $percent);
print <<END
<TD ALIGN='CENTER'></TD>
<TD ALIGN='CENTER'>$Lang::tr{'otherip'}</TD>
<TD ALIGN='CENTER'></TD>
<TD ALIGN='CENTER'>$dif</TD>
<TD ALIGN='CENTER'>$percent</TD>
</TR>
END
;
}
print <<END
</TABLE>
END
;
&Header::closebox();
&Header::closebigbox();
&Header::closepage();
sub findkey {
my $v;
foreach $v (@keytabjc) {
if ($tabjc{$v} eq $_[0]) {
delete $tabjc{$v};
$key[$indice] = "$v";
$value[$indice] = $_[0];
$indice++;
last;
}
}
}sub checkversion {
#Automatic Updates is disabled
return "0","0";
}

View File

@@ -0,0 +1,525 @@
#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# JC HERITIER
# page inspired from the initial firewalllog.dat
#
# Modified for IPFire by Christian Schmidt (www.ipfire.org)
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
use POSIX();
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::table2colour} );
undef (@dummy);
my %cgiparams=();
my %settings=();
my $pienumber;
my $otherspie;
my $showpie;
my $sortcolumn;
my $errormessage = '';
$cgiparams{'pienumber'} = 10;
$cgiparams{'otherspie'} = 1;
$cgiparams{'showpie'} = 1;
$cgiparams{'sortcolumn'} = 1;
my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' );
my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
$Lang::tr{'december'} );
my @now = localtime();
my $dow = $now[6];
my $doy = $now[7];
my $tdoy = $now[7];
my $year = $now[5]+1900;
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
$cgiparams{'ACTION'} = '';
&General::readhash("${General::swroot}/fwlogs/portsettings", \%settings);
if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
&Header::getcgihash(\%cgiparams);
if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
{
&General::writehash("${General::swroot}/fwlogs/portsettings", \%settings);
}
my $start = -1;
if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
{
my @temp = split(',',$ENV{'QUERY_STRING'});
$start = $temp[0];
$cgiparams{'MONTH'} = $temp[1];
$cgiparams{'DAY'} = $temp[2];
}
if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
!($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
{
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
}
elsif($cgiparams{'ACTION'} eq '>>')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
## Retrieve the same time on the next day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
elsif($cgiparams{'ACTION'} eq '<<')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
## Retrieve the same time on the previous day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
{
my @then = ();
if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
( $cgiparams{'MONTH'} > $now[4] ) ) {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
} else {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
}
$tdoy = $then[7];
my $lastleap=($year-1)%4;
if ($tdoy>$doy) {
if ($lastleap == 0 && $tdoy < 60) {
$doy=$tdoy+366;
} else {
$doy=$doy+365;
}
}
}
my $datediff=0;
my $dowd=0;
my $multifile=0;
if ($tdoy ne $doy) {
$datediff=int(($doy-$tdoy)/7);
$dowd=($doy-$tdoy)%7;
if (($dow-$dowd)<1) {
$datediff=$datediff+1;
}
if (($dow-$dowd)==0) {
$multifile=1;
}
}
my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
my $day = $cgiparams{'DAY'};
my $daystr='';
if ($day <= 9) {
$daystr = " $day"; }
else {
$daystr = $day;
}
my $skip=0;
my $filestr='';
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
# Note: This is in case the log does not exist for that date
}
my $lines = 0;
my @log=();
if (!$skip)
{
while (<FILE>)
{
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
$log[$lines] = $_;
$lines++;
}
}
close (FILE);
}
$skip=0;
if ($multifile) {
$datediff=$datediff-1;
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
}
if (!$skip) {
while (<FILE>) {
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
$log[$lines] = $_;
$lines++;
}
}
close (FILE);
}
}
my $MODNAME="fwlogs";
&Header::showhttpheaders();
&Header::openpage($Lang::tr{'firewall log'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
if ($errormessage) {
&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
print "<font class='base'>$errormessage&nbsp;</font>\n";
&Header::closebox();
}
&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
print <<END
<form method='post' action='$ENV{'SCRIPT_NAME'}'>
<table width='100%'>
<tr>
<td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
<td width='10%'>
<select name='MONTH'>
END
;
my $month;
for ($month = 0; $month < 12; $month++)
{
print "\t<option ";
if ($month == $cgiparams{'MONTH'}) {
print "selected='selected' "; }
print "value='$month'>$longmonths[$month]</option>\n";
}
print <<END
</select>
</td>
<td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
<td width='40%'>
<select name='DAY'>
END
;
for ($day = 1; $day <= 31; $day++)
{
print "\t<option ";
if ($day == $cgiparams{'DAY'}) {
print "selected='selected' "; }
print "value='$day'>$day</option>\n";
}
if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
print <<END
</select>
</td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
<td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
</tr>
</table>
<table width='100%'>
<tr><td width='20%' align='right' valign="center">$Lang::tr{'Number of Ports for the pie chart'}:</td>
<td width='10%' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
<td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
</table>
</form>
END
;
&Header::closebox();
&Header::openbox('100%', 'left', 'Firewall Logs');
print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
my $linesjc = 0;
my %tabjc;
if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
$lines = 0;
foreach $_ (@log)
{
if($_ =~ /DPT\=([\d\.]+)/){
$tabjc{$1} = $tabjc{$1} + 1 ;
if(($tabjc{$1} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
$linesjc++;
}
}
$pienumber = $lines;
my @keytabjc = keys %tabjc;
my @slice;
my $go;
my $nblinejc;
if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
if( $go != 1){ $nblinejc = 1000; }
my @key;
my @value;
my $indice=0;
my @tabjc2;
if ($sortcolumn == 1)
{
@tabjc2 = sort { $b <=> $a } values (%tabjc);
}
else
{
@tabjc2 = sort { $a <=> $b } keys (%tabjc);
}
my $colour=1;
##############################################
#pie chart generation
use GD::Graph::pie;
use GD::Graph::colour;
#ports sort by hits number
#(port - number_of_hit) sorted by number_of_hit
my $v;
if ($sortcolumn == 1)
{
for ($v=0;$v<$pienumber;$v++){
findkey($tabjc2[$v]);
}
}
else
{
foreach $v (@tabjc2) {
$key[$indice] = $v;
$value[$indice] = $tabjc{$v};
$indice++;
}
}
my @ports;
my @numb;
@ports = @key;
@numb = @value;
my $o;
if($cgiparams{'otherspie'} == 2 ){}
else{
my $numothers;
for($o=0;$o<$pienumber;$o++){
$numothers = $numothers + $numb[$o];
}
$numothers = $linesjc - $numothers;
if ($numothers > 0) {
$ports[$pienumber]="$Lang::tr{'otherport'}";
$numb[$pienumber] = $numothers;
}
}
my @data = (\@ports,\@numb);
use GD::Graph::colour qw( :files );
GD::Graph::colour::read_rgb( "/srv/web/ipfire/cgi-bin/logs.cgi/colours.txt" );
if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
my $mygraph = GD::Graph::pie->new(500, 350);
$mygraph->set(
'title' => '',
'pie_height' => 50,
'start_angle' => 89
) or warn $mygraph->error;
$mygraph->set_value_font(GD::gdMediumBoldFont);
$mygraph->set( dclrs => [ "colour1" , "colour2" , "colour3" , "colour4" , "colour5" , "colour6" , "colour7" , "colour8" , "colour9" , "colour10" ] );
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
my @filenames = glob("/srv/web/ipfire/html/fwlogs/port*.png");
unlink(@filenames);
my $imagerandom = rand(1000000);
my $imagename = "/srv/web/ipfire/html/fwlogs/port$imagerandom.png";
open(FILE,">$imagename");
print FILE $myimage->png;
close(FILE);
#####################################################
print "<table align='center'><tr><td>";
print "<img src='/fwlogs/port$imagerandom.png'>";
print "</td></tr></table>";
}
print <<END
<TABLE WIDTH='100%'>
<TR>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'></TD>
<TD WIDTH='33%' ALIGN='CENTER' CLASS='boldbase'><B>Port</B></TD>
<TD WIDTH='33%' ALIGN='CENTER' CLASS='boldbase'><B>Count</B></TD>
<TD WIDTH='33%' ALIGN='CENTER' CLASS='boldbase'><B>Percent</B></TD>
</TR>
END
;
my $total=0;
my $show=0;
my @colour;
my $colour1;
my $colour2;
my $colour3;
my $colour4;
my $colour5;
my $colour6;
my $colour7;
my $colour8;
my $colour9;
my $colour10;
@colour = GD::Graph::colour::_rgb("colour1");
$colour1 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour2");
$colour2 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour3");
$colour3 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour4");
$colour4 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour5");
$colour5 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour6");
$colour6 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour7");
$colour7 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour8");
$colour8 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour9");
$colour9 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
@colour = GD::Graph::colour::_rgb("colour10");
$colour10 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
my $s;
my $percent;
for($s=0;$s<$lines;$s++)
{
$show++;
$percent = $value[$s] * 100 / $linesjc;
$percent = sprintf("%.3f", $percent);
$total = $total + $value[$s];
if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
$colour++;
print "<TD ALIGN='CENTER'><form method='post' action='showrequestfromport.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='port' value='$key[$s]'> <input type='submit' value='details'></form></TD>";
print "<TD ALIGN='CENTER'>$key[$s]</TD>";
print "<TD ALIGN='CENTER'>$value[$s]</TD>";
print "<TD ALIGN='CENTER'>$percent</TD>";
print "</TR>";
}
if($cgiparams{'otherspie'} == 2 ){}
else{
if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
my $dif;
$dif = $linesjc - $total;
$percent = $dif * 100 / $linesjc;
$percent = sprintf("%.3f", $percent);
print <<END
<TD ALIGN='CENTER'></TD>
<TD ALIGN='CENTER'>$Lang::tr{'otherport'}</TD>
<TD ALIGN='CENTER'>$dif</TD>
<TD ALIGN='CENTER'>$percent</TD>
</TR>
END
;
}
print <<END
</TABLE>
END
;
&Header::closebox();
&Header::closebigbox();
&Header::closepage();
sub findkey {
foreach $v (@keytabjc) {
if ($tabjc{$v} eq $_[0]) {
delete $tabjc{$v};
$key[$indice] = "$v";
$value[$indice] = $_[0];
$indice++;
last;
}
}
}
sub checkversion {
#Automatic Updates is disabled
return "0","0";
}

View File

@@ -0,0 +1,388 @@
#!/usr/bin/perl
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# JC HERITIER
# page inspired from the initial firewalllog.dat
#
# Modified for IPFire by Christian Schmidt (www.ipfire.org)
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
use POSIX();
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::table2colour} );
undef (@dummy);
my %cgiparams=();
my %logsettings=();
my $errormessage = '';
my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' );
my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
$Lang::tr{'december'} );
my @now = localtime();
my $dow = $now[6];
my $doy = $now[7];
my $tdoy = $now[7];
my $year = $now[5]+1900;
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
$cgiparams{'ACTION'} = '';
&Header::getcgihash(\%cgiparams);
$logsettings{'LOGVIEW_REVERSE'} = 'off';
&General::readhash("${General::swroot}/logging/settings", \%logsettings);
my $start = -1;
if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
{
my @temp = split(',',$ENV{'QUERY_STRING'});
$start = $temp[0];
$cgiparams{'MONTH'} = $temp[1];
$cgiparams{'DAY'} = $temp[2];
$cgiparams{ip} = $temp[3];
}
if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
!($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
{
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
}
elsif($cgiparams{'ACTION'} eq '>>')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
## Retrieve the same time on the next day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
elsif($cgiparams{'ACTION'} eq '<<')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
## Retrieve the same time on the previous day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
{
my @then = ();
if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
( $cgiparams{'MONTH'} > $now[4] ) ) {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
} else {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
}
$tdoy = $then[7];
my $lastleap=($year-1)%4;
if ($tdoy>$doy) {
if ($lastleap == 0 && $tdoy < 60) {
$doy=$tdoy+366;
} else {
$doy=$doy+365;
}
}
}
my $datediff=0;
my $dowd=0;
my $multifile=0;
if ($tdoy ne $doy) {
$datediff=int(($doy-$tdoy)/7);
$dowd=($doy-$tdoy)%7;
if (($dow-$dowd)<1) {
$datediff=$datediff+1;
}
if (($dow-$dowd)==0) {
$multifile=1;
}
}
my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
my $day = $cgiparams{'DAY'};
my $daystr='';
if ($day <= 9) {
$daystr = " $day"; }
else {
$daystr = $day;
}
my $skip=0;
my $filestr='';
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
# Note: This is in case the log does not exist for that date
}
my $lines = 0;
my @log=();
my $ip = $cgiparams{ip};
if (!$skip)
{
while (<FILE>)
{
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
if($_ =~ /SRC\=([\d\.]+)/){
if($1 eq $ip){
$log[$lines] = $_;
$lines++;
}
}
}
}
close (FILE);
}
$skip=0;
if ($multifile) {
$datediff=$datediff-1;
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
}
if (!$skip) {
while (<FILE>) {
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
if($_ =~ /SRC\=([\d\.]+)/){
if($1 eq $ip){
$log[$lines] = $_;
$lines++;
}
}
}
}
close (FILE);
}
}
&Header::showhttpheaders();
&Header::openpage($Lang::tr{'firewall log'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
if ($errormessage) {
&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
print "<font class='base'>$errormessage&nbsp;</font>\n";
&Header::closebox();
}
&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
print <<END
<form method='post' action='$ENV{'SCRIPT_NAME'}'>
<table width='100%'>
<tr>
<td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
<td width='10%'>
<select name='MONTH'>
END
;
my $month;
for ($month = 0; $month < 12; $month++)
{
print "\t<option ";
if ($month == $cgiparams{'MONTH'}) {
print "selected='selected' "; }
print "value='$month'>$longmonths[$month]</option>\n";
}
print <<END
</select>
</td>
<td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
<td width='40%'>
<select name='DAY'>
END
;
for ($day = 1; $day <= 31; $day++)
{
print "\t<option ";
if ($day == $cgiparams{'DAY'}) {
print "selected='selected' "; }
print "value='$day'>$day</option>\n";
}
print <<END
</select>
</td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
<tr><td width='15%'>$Lang::tr{'source ip'}</td><td><input type='text' name='ip' value='$cgiparams{ip}'size='15'></td></tr>
</tr>
</table>
</form>
END
;
&Header::closebox();
&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
if ($start == -1) {
$start = $lines - ${Header::viewsize}; }
if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
if ($start < 0) { $start = 0; }
my $prev = $start - ${Header::viewsize};
my $next = $start + ${Header::viewsize};
if ($prev < 0) { $prev = 0; }
if ($next >= $lines) { $next = -1 }
if ($start == 0) { $prev = -1; }
if ($lines != 0) { &oldernewer(); }
print <<END
<TABLE WIDTH='100%'>
<TR>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'time'}</B></TD>
<TD WIDTH='13%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'chain'}</B></TD>
<TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'iface'}</B></TD>
<TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'proto'}</B></TD>
<TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'source'}</B></TD>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'src port'}</B></TD>
<TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'destination'}</B></TD>
<TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'dst port'}</B></TD>
</TR>
END
;
my @slice = splice(@log, $start, ${Header::viewsize});
if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
$lines = 0;
foreach $_ (@slice)
{
$a = $_;
if($_ =~ /SRC\=([\d\.]+)/){
if($1 eq $ip){
my $chain = '';
my $in = '-'; my $out = '-';
my $srcaddr = ''; my $dstaddr = '';
my $protostr = '';
my $srcport = ''; my $dstport = '';
$_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
my $timestamp = $1; my $chain = $2; my $packet = $3;
$timestamp =~ /(...) (..) (..:..:..)/;
my $month = $1; my $day = $2; my $time = $3;
if ($a =~ /IN\=(\w+)/) { $in = $1; }
if ($a =~ /OUT\=(\w+)/) { $out = $1; }
if ($a =~ /SRC\=([\d\.]+)/) { $srcaddr = $1; }
if ($a =~ /DST\=([\d\.]+)/) { $dstaddr = $1; }
if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
my $protostrlc = lc($protostr);
if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
$a =~ /IN=(\w+)/; my $iface=$1;
if ($lines % 2) {
print "<tr bgcolor='${Header::table1colour}'>\n"; }
else {
print "<tr bgcolor='${Header::table2colour}'>\n"; }
print <<END
<TD ALIGN='CENTER'>$time</TD>
<TD ALIGN='CENTER'>$chain</TD>
<TD ALIGN='CENTER'>$iface</TD>
<TD ALIGN='CENTER'>$protostr</TD>
<td align='center'>
<table width='100%' cellpadding='0' cellspacing='0'><tr>
<td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
</tr></table>
</td>
<TD ALIGN='CENTER'>$srcport</TD>
<td align='center'>
<table width='100%' cellpadding='0' cellspacing='0'><tr>
<td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
</tr></table>
</td>
<TD ALIGN='CENTER'>$dstport</TD>
</TR>
END
;
$lines++;
}
}
}
print <<END
</TABLE>
END
;
&oldernewer();
&Header::closebox();
&Header::closebigbox();
&Header::closepage();
sub oldernewer
{
print <<END
<table width='100%'>
<tr>
END
;
print "<td align='center' width='50%'>";
if ($prev != -1) {
print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'older'}</a>"; }
else {
print "$Lang::tr{'older'}"; }
print "</td>\n";
print "<td align='center' width='50%'>";
if ($next != -1) {
print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'newer'}</a>"; }
else {
print "$Lang::tr{'newer'}"; }
print "</td>\n";
print <<END
</tr>
</table>
END
;
}

View File

@@ -0,0 +1,395 @@
#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# JC HERITIER
# page inspired from the initial firewalllog.dat
#
# Modified for IPFire by Christian Schmidt (www.ipfire.org)
#Aug 28 14:54:30 shite kernel: IN=ppp0 OUT= MAC= SRC=213.48.150.1
#DST=213.208.115.234 LEN=110 TOS=0x00 PREC=0x00 TTL=60 ID=30699 DF PROTO=TCP
#SPT=6667 DPT=62593 WINDOW=2505 RES=0x00 ACK PSH URGP=0
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
use POSIX();
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::table2colour} );
undef (@dummy);
my %cgiparams=();
my %logsettings=();
my $errormessage = '';
my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' );
my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
$Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
$Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
$Lang::tr{'december'} );
my @now = localtime();
my $dow = $now[6];
my $doy = $now[7];
my $tdoy = $now[7];
my $year = $now[5]+1900;
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
$cgiparams{'ACTION'} = '';
&Header::getcgihash(\%cgiparams);
$logsettings{'LOGVIEW_REVERSE'} = 'off';
&General::readhash("${General::swroot}/logging/settings", \%logsettings);
my $start = -1;
if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
{
my @temp = split(',',$ENV{'QUERY_STRING'});
$start = $temp[0];
$cgiparams{'MONTH'} = $temp[1];
$cgiparams{'DAY'} = $temp[2];
$cgiparams{port} = $temp[3];
}
if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
!($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
{
$cgiparams{'DAY'} = $now[3];
$cgiparams{'MONTH'} = $now[4];
}
elsif($cgiparams{'ACTION'} eq '>>')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
## Retrieve the same time on the next day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
elsif($cgiparams{'ACTION'} eq '<<')
{
my @temp_then=();
my @temp_now = localtime(time);
$temp_now[4] = $cgiparams{'MONTH'};
$temp_now[3] = $cgiparams{'DAY'};
@temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
## Retrieve the same time on the previous day -
## 86400 seconds in a day
$cgiparams{'MONTH'} = $temp_then[4];
$cgiparams{'DAY'} = $temp_then[3];
}
if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
{
my @then = ();
if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
( $cgiparams{'MONTH'} > $now[4] ) ) {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
} else {
@then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
}
$tdoy = $then[7];
my $lastleap=($year-1)%4;
if ($tdoy>$doy) {
if ($lastleap == 0 && $tdoy < 60) {
$doy=$tdoy+366;
} else {
$doy=$doy+365;
}
}
}
my $datediff=0;
my $dowd=0;
my $multifile=0;
if ($tdoy ne $doy) {
$datediff=int(($doy-$tdoy)/7);
$dowd=($doy-$tdoy)%7;
if (($dow-$dowd)<1) {
$datediff=$datediff+1;
}
if (($dow-$dowd)==0) {
$multifile=1;
}
}
my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
my $day = $cgiparams{'DAY'};
my $daystr='';
if ($day <= 9) {
$daystr = " $day"; }
else {
$daystr = $day;
}
my $skip=0;
my $filestr='';
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
# Note: This is in case the log does not exist for that date
}
my $lines = 0;
my @log=();
my $port = $cgiparams{port};
if (!$skip)
{
while (<FILE>)
{
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
if($_ =~ /DPT\=([\d\.]+)/){
if($1 eq $port){
$log[$lines] = $_;
$lines++;
}
}
}
}
close (FILE);
}
$skip=0;
if ($multifile) {
$datediff=$datediff-1;
if ($datediff==0) {
$filestr="/var/log/messages";
} else {
$filestr="/var/log/messages.$datediff";
$filestr = "$filestr.gz" if -f "$filestr.gz";
}
if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
$errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
$skip=1;
}
if (!$skip) {
while (<FILE>) {
if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
if($_ =~ /DPT\=([\d\.]+)/){
if($1 eq $port){
$log[$lines] = $_;
$lines++;
}
}
}
}
close (FILE);
}
}
&Header::showhttpheaders();
&Header::openpage($Lang::tr{'firewall log'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
if ($errormessage) {
&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
print "<font class='base'>$errormessage&nbsp;</font>\n";
&Header::closebox();
}
&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
print <<END
<form method='post' action='$ENV{'SCRIPT_NAME'}'>
<table width='100%'>
<tr>
<td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
<td width='10%'>
<select name='MONTH'>
END
;
my $month;
for ($month = 0; $month < 12; $month++)
{
print "\t<option ";
if ($month == $cgiparams{'MONTH'}) {
print "selected='selected' "; }
print "value='$month'>$longmonths[$month]</option>\n";
}
print <<END
</select>
</td>
<td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
<td width='40%'>
<select name='DAY'>
END
;
for ($day = 1; $day <= 31; $day++)
{
print "\t<option ";
if ($day == $cgiparams{'DAY'}) {
print "selected='selected' "; }
print "value='$day'>$day</option>\n";
}
print <<END
</select>
</td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
<tr><td width='15%'>$Lang::tr{'destination port'}</td><td><input type='text' name='port' value='$cgiparams{port}'size='15'></td></tr>
</tr>
</table>
</form>
END
;
&Header::closebox();
&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
if ($start == -1) {
$start = $lines - ${Header::viewsize}; }
if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
if ($start < 0) { $start = 0; }
my $prev = $start - ${Header::viewsize};
my $next = $start + ${Header::viewsize};
if ($prev < 0) { $prev = 0; }
if ($next >= $lines) { $next = -1 }
if ($start == 0) { $prev = -1; }
if ($lines != 0) { &oldernewer(); }
print <<END
<TABLE WIDTH='100%'>
<TR>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'time'}</B></TD>
<TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'chain'}</B></TD>
<TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'iface'}</B></TD>
<TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'proto'}</B></TD>
<TD WIDTH='20%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'source'}</B></TD>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'src port'}</B></TD>
<TD WIDTH='20%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'destination'}</B></TD>
<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'dst port'}</B></TD>
</TR>
END
;
my @slice = splice(@log, $start, ${Header::viewsize});
if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
$lines = 0;
foreach $_ (@slice)
{
$a = $_;
if($_ =~ /DPT\=([\d\.]+)/){
if($1 eq $port){
my $chain = '';
my $in = '-'; my $out = '-';
my $srcaddr = ''; my $dstaddr = '';
my $protostr = '';
my $srcport = ''; my $dstport = '';
$_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
my $timestamp = $1; my $chain = $2; my $packet = $3;
$timestamp =~ /(...) (..) (..:..:..)/;
my $month = $1; my $day = $2; my $time = $3;
if ($a =~ /IN\=(\w+)/) { $in = $1; }
if ($a =~ /OUT\=(\w+)/) { $out = $1; }
if ($a =~ /SRC\=([\d\.]+)/) { $srcaddr = $1; }
if ($a =~ /DST\=([\d\.]+)/) { $dstaddr = $1; }
if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
my $protostrlc = lc($protostr);
if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
$a =~ /IN=(\w+)/; my $iface=$1;
if ($lines % 2) {
print "<tr bgcolor='${Header::table1colour}'>\n"; }
else {
print "<tr bgcolor='${Header::table2colour}'>\n"; }
print <<END
<TD ALIGN='CENTER'>$time</TD>
<TD ALIGN='CENTER'>$chain</TD>
<TD ALIGN='CENTER'>$iface</TD>
<TD ALIGN='CENTER'>$protostr</TD>
<td align='center'>
<table width='100%' cellpadding='0' cellspacing='0'><tr>
<td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
</tr></table>
</td>
<TD ALIGN='CENTER'>$srcport</TD>
<td align='center'>
<table width='100%' cellpadding='0' cellspacing='0'><tr>
<td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
</tr></table>
</td>
<TD ALIGN='CENTER'>$dstport</TD>
</TR>
END
;
$lines++;
}
}
}
print <<END
</TABLE>
END
;
&oldernewer();
&Header::closebox();
&Header::closebigbox();
&Header::closepage();
sub oldernewer
{
print <<END
<table width='100%'>
<tr>
END
;
print "<td align='center' width='50%'>";
if ($prev != -1) {
print "<a href='/cgi-bin/logs.cgi/showrequestfromport.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{port}'>$Lang::tr{'older'}</a>"; }
else {
print "$Lang::tr{'older'}"; }
print "</td>\n";
print "<td align='center' width='50%'>";
if ($next != -1) {
print "<a href='/cgi-bin/logs.cgi/showrequestfromport.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{port}'>$Lang::tr{'newer'}</a>"; }
else {
print "$Lang::tr{'newer'}"; }
print "</td>\n";
print <<END
</tr>
</table>
END
;
}