]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllog.dat
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllog.dat
CommitLineData
cd1a2927 1#!/usr/bin/perl
ac225ec6
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2023 IPFire Team <info@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
cd1a2927
MT
22use strict;
23
dace10b5
MT
24use Getopt::Std;
25
cd1a2927
MT
26# enable only the following on debugging purpose
27#use warnings;
28#use CGI::Carp 'fatalsToBrowser';
29
986e08d9 30require '/var/ipfire/general-functions.pl';
e4f1e36c 31require "${General::swroot}/location-functions.pl";
cd1a2927
MT
32require "${General::swroot}/lang.pl";
33require "${General::swroot}/header.pl";
34
f2fdd0c1
CS
35my %color = ();
36my %mainsettings = ();
37&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 38&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
f2fdd0c1 39
cd1a2927
MT
40use POSIX();
41
42#workaround to suppress a warning when a variable is used only once
43my @dummy = ( ${Header::table2colour} );
44undef (@dummy);
45
46my %cgiparams=();
47my %logsettings=();
48my $errormessage = '';
49
50my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43638be5 51 'Sep', 'Oct', 'Nov', 'Dec' );
cd1a2927 52my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43638be5
ME
53 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
54 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
55 $Lang::tr{'december'} );
cd1a2927
MT
56
57my @now = localtime();
58my $dow = $now[6];
59my $doy = $now[7];
60my $tdoy = $now[7];
61my $year = $now[5]+1900;
62
63$cgiparams{'DAY'} = $now[3];
64$cgiparams{'MONTH'} = $now[4];
65$cgiparams{'ACTION'} = '';
66
67&Header::getcgihash(\%cgiparams);
68$logsettings{'LOGVIEW_REVERSE'} = 'off';
69&General::readhash("${General::swroot}/logging/settings", \%logsettings);
70${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
71
72my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
73
74if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
75{
43638be5
ME
76 my @temp = split(',',$ENV{'QUERY_STRING'});
77 $start = $temp[0];
78 $cgiparams{'MONTH'} = $temp[1];
79 $cgiparams{'DAY'} = $temp[2];
cd1a2927
MT
80}
81
82if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
43638be5 83 !($cgiparams{'DAY'} =~ /^(0|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)$/))
cd1a2927 84{
43638be5
ME
85 $cgiparams{'DAY'} = $now[3];
86 $cgiparams{'MONTH'} = $now[4];
cd1a2927
MT
87}
88elsif($cgiparams{'ACTION'} eq '>>')
89{
43638be5
ME
90 my @temp_then=();
91 my @temp_now = localtime(time);
92 $temp_now[4] = $cgiparams{'MONTH'};
93 $temp_now[3] = $cgiparams{'DAY'};
94 if ($cgiparams{'DAY'}) {
95 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
96 ## Retrieve the same time on the next day +
97 ## 86400 seconds in a day
98 } else {
99 $temp_now[3] = 1;
100 $temp_now[4] = ($temp_now[4]+1) %12;
101 @temp_then = localtime(POSIX::mktime(@temp_now) );
102 $temp_then[3] = 0;
103 }
104 $cgiparams{'MONTH'} = $temp_then[4];
105 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
106}
107elsif($cgiparams{'ACTION'} eq '<<')
108{
43638be5
ME
109 my @temp_then=();
110 my @temp_now = localtime(time);
111 $temp_now[4] = $cgiparams{'MONTH'};
112 $temp_now[3] = $cgiparams{'DAY'};
113 if ($cgiparams{'DAY'}) {
114 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
115 ## Retrieve the same time on the next day -
116 ## 86400 seconds in a day
117 } else {
118 $temp_now[3] = 1;
119 $temp_now[4] = ($temp_now[4]-1) %12;
120 @temp_then = localtime(POSIX::mktime(@temp_now) );
121 $temp_then[3] = 0;
122 }
123 $cgiparams{'MONTH'} = $temp_then[4];
124 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
125}
126
127# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
128my $gzindex;
129my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
130
131{
43638be5 132 my $xday;
66c36198 133
43638be5
ME
134 # Calculate time. If future date, calculate for past year !!!
135 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
136 ( $cgiparams{'MONTH'} > $now[4] ) ) {
137 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
138 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
139 } else {
140 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
141 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
142 }
143
144 # calculate end of active week (saturday 23H59)
145 my @then = ();
146 @then = localtime(time());
147 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
148 $sunday += (6-$then[6]) * 86400;
149
150 # Convert delta in second to full weeks
151 $gzindex = int (($sunday-$xday)/604800 );
cd1a2927 152}
43638be5 153
cd1a2927
MT
154my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
155my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
43638be5 156
cd1a2927
MT
157my $lines = 0;
158my @log=();
159
160my $loop = 1;
161my $filestr = 0;
43638be5 162my $lastdatetime; # for debug
cd1a2927 163my $search_for_end = 0;
43638be5 164
cd1a2927 165while ($gzindex >=0 && $loop) {
43638be5
ME
166 # calculate file name
167 if ($gzindex == 0) {
168 $filestr = "/var/log/messages";
169 } else {
170 $filestr = "/var/log/messages.$gzindex";
171 $filestr = "$filestr.gz" if -f "$filestr.gz";
172 }
173 # now read file if existing
174 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
175 #&General::log("reading $filestr");
176 READ:while (<FILE>) {
177 my $line = $_;
178 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
179 # when standart viewing, just keep in memory the correct slice
180 # it starts a '$start' and size is $viewport
181 # If export, then keep all lines...
182 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
183 $log[$lines++] = "$line";
184 } else {
185 if ($lines++ < ($start + $Header::viewsize)) {
186 push(@log,"$line");
187 if (@log > $Header::viewsize) {
188 shift (@log);
189 }
190 #} else { dont do this optimisation, need to count lines !
191 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
192 # last READ; # exit read file
193 }
194 }
195 $search_for_end = 1; # we find the start of slice, can look for end now
196 } else {
197 if ($search_for_end == 1) {
198 #finish read files when date is over (test month equality only)
199 $line =~ /^(...) (..) ..:..:..*$/;
200 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
201 }
202 }
203 }
204 close (FILE);
205 }
206 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
207 # for a long time
cd1a2927
MT
208}# while
209
210# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
211
212if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
213{
43638be5
ME
214 print "Content-type: text/plain\n\n";
215 print "IPFire firewall log\r\n";
216 print "$Lang::{'date'}: $date\r\n\r\n";
217
218 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
219
220 foreach $_ (@log)
221 {
f02adfbc 222 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
43638be5
ME
223 my $day = $1;
224 $day =~ tr / /0/;
225 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
f02adfbc
AB
226 my $chain = $3;
227 print "$time $chain $4\r\n";
66c36198 228
43638be5
ME
229 }
230 exit 0;
cd1a2927
MT
231}
232
233&Header::showhttpheaders();
234
235&Header::openpage($Lang::tr{'firewall log'}, 1, '');
236
237&Header::openbigbox('100%', 'left', '', $errormessage);
238
239if ($errormessage) {
43638be5
ME
240 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
241 print "<font class='base'>$errormessage&nbsp;</font>\n";
242 &Header::closebox();
cd1a2927
MT
243}
244
73ede29f 245&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
cd1a2927
MT
246
247print <<END
248<form method='post' action='$ENV{'SCRIPT_NAME'}'>
249<table width='100%'>
250<tr>
43638be5
ME
251 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
252 <td width='10%'>
253 <select name='MONTH'>
cd1a2927
MT
254END
255;
256for (my $month = 0; $month < 12; $month++)
257{
43638be5
ME
258 print "\t<option ";
259 if ($month == $cgiparams{'MONTH'}) {
66c36198 260 print "selected='selected' ";
43638be5
ME
261 }
262 print "value='$month'>$longmonths[$month]</option>\n";
cd1a2927
MT
263}
264print <<END
43638be5
ME
265 </select>
266 </td>
267 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
268 <td width='40%'>
269 <select name='DAY'>
cd1a2927
MT
270END
271;
272print "<option value='0'>$Lang::tr{'all'}</option>\n";
66c36198 273for (my $day = 1; $day <= 31; $day++)
cd1a2927 274{
43638be5
ME
275 print "\t<option ";
276 if ($day == $cgiparams{'DAY'}) {
66c36198 277 print "selected='selected' ";
43638be5
ME
278 }
279 print "value='$day'>$day</option>\n";
cd1a2927
MT
280}
281print <<END
282</select>
283</td>
284<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
285<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
286<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
287<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
288</tr>
289</table>
290</form>
291END
292;
293
294&Header::closebox();
295
296&Header::openbox('100%', 'left', $Lang::tr{'log'});
297print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
298
f69446d2
AH
299my $lastPageIndex = $lines - ${Header::viewsize};
300$start = $lastPageIndex if ($start >= $lastPageIndex);
cd1a2927
MT
301$start = 0 if ($start < 0);
302
303my $prev;
43638be5
ME
304if ($start == 0) {
305 $prev = -1;
306} else {
307 $prev = $start - ${Header::viewsize};
308 $prev = 0 if ( $prev < 0);
309}
66c36198 310
cd1a2927 311my $next;
43638be5
ME
312if ($start == $lastPageIndex) {
313 $next = -1;
314} else {
315 $next = $start + ${Header::viewsize};
316 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
317}
cd1a2927
MT
318
319if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
320if ($lines != 0) { &oldernewer(); }
321
322print <<END
fda1f52f 323<table width='100%' class='tbl'>
cd1a2927 324<tr>
43638be5
ME
325 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
326 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
327 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
328 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
329 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
330 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
331 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
332 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
cd1a2927
MT
333</tr>
334END
335;
336
ca1f4a07
SS
337# Generate hash to translate protocol numbers into protocol names.
338my %protocols = &General::generateProtoTransHash();
cd1a2927
MT
339
340$lines = 0;
341foreach $_ (@log)
342{
9877d28d
ME
343 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
344 # otherwise use IN=
43638be5
ME
345 my $packet = '';
346 if ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) { $packet = $5; }
347 elsif ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) { $packet = $4; }
348 my $day = $1;
349 $day =~ tr / /0/;
350 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
66c36198 351 my $chain = $3;
43638be5
ME
352
353 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
354 if ($packet =~ /PHYSIN=(\w+)/) { $iface=$1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
355 # Identify whether ipv4 or ipv6. Both are mutally exclusive.
356 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr=$1; }
357 if ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr=$1; }
358 if ($packet =~ /DST\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $dstaddr=$1; }
359 if ($packet =~ /DST\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $dstaddr=$1; }
360 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
361 $proto=$1 if $packet =~ /PROTO=(\w+)/;
362 $srcport=$1 if $packet =~ /SPT=(\d+)/;
363 $dstport=$1 if $packet =~ /DPT=(\d+)/;
364
07e42be9
SS
365 # Get the country code.
366 my $ccode = &Location::Functions::lookup_country_code($srcaddr);
43638be5 367
ca1f4a07
SS
368 # Lookup if the grabbed protocol is part of the protocols hash.
369 if (exists ($protocols{$proto})) {
370 # Translate protocol number into protocol name.
371 $proto = $protocols{$proto};
372 }
373
43638be5
ME
374 my $servi = uc(getservbyport($srcport, lc($proto)));
375 if ($servi ne '' && $srcport < 1024) {
66c36198 376 $srcport = "$srcport($servi)";
43638be5
ME
377 }
378 $servi = uc(getservbyport($dstport, lc($proto)));
379 if ($servi ne '' && $dstport < 1024) {
380 $dstport = "$dstport($servi)";
381 }
382 my @mactemp = split(/:/,$macaddr);
383 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
384 my $col="";
385 if ($lines % 2) {
386 print "<tr>";
66c36198 387 $col="bgcolor='$color{'color20'}'";
43638be5
ME
388 }
389 else {
390 print "<tr>";
66c36198 391 $col="bgcolor='$color{'color22'}'";
43638be5
ME
392 }
393 print <<END
394
395 <td align='center' $col>$time</td>
396 <td align='center' $col>$chain</td>
397 <td align='center' $col>$iface</td>
398 <td align='center' $col>$proto</td>
399 <td align='center' $col><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a><br /><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
400 <td align='center' $col>$srcport<br/>$dstport</td>
8c384f97 401END
43638be5 402 ;
3ea01fe0 403 # Get flag icon for of the country.
e4f1e36c 404 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
3ea01fe0 405
43638be5 406 if ( $flag_icon) {
00793c27 407 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
3ea01fe0
SS
408 } else {
409 print "<td align='center' $col></td>";
410 }
411
43638be5
ME
412 print <<END
413 <td align='center' $col>$macaddr</td>
414 </tr>
cd1a2927 415END
43638be5
ME
416 ;
417 $lines++;
cd1a2927
MT
418}
419
420print "</table>";
421
422&oldernewer();
423
424&Header::closebox();
425
426&Header::closebigbox();
427
428&Header::closepage();
429
430sub oldernewer
431{
43638be5
ME
432 print <<END
433 <table width='100%'>
434 <tr>
cd1a2927 435END
43638be5 436 ;
cd1a2927 437
43638be5
ME
438 print "<td align='center' width='50%'>";
439 if ($prev != -1) {
66c36198
PM
440 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
441 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>";
43638be5
ME
442 }
443 else {
66c36198 444 print "$Lang::tr{'first'} $Lang::tr{'older'}";
43638be5
ME
445 }
446 print "</td>\n";
cd1a2927 447
43638be5
ME
448 print "<td align='center' width='50%'>";
449 if ($next >= 0) {
450 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
66c36198 451 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>";
43638be5
ME
452 }
453 else {
66c36198 454 print "$Lang::tr{'newer'} $Lang::tr{'last'} ";
43638be5
ME
455 }
456 print "</td>\n";
457
458 print <<END
459 </tr>
460 </table>
cd1a2927 461END
43638be5 462 ;
cd1a2927 463}
71dfc4b7 464