]>
git.ipfire.org Git - people/stevee/ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
1ccb619ba773b51936cdcdc3ac0702b57ee680fc
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
25 # enable only the following on debugging purpose
27 use CGI
::Carp
'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header
::colouryellow
} );
43 # Path to the guardian.ignore file.
44 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
46 our %netsettings = ();
47 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
50 our %mainsettings = ();
51 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
52 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
56 $settings{'GUARDIAN_ENABLED'} = 'off';
57 $settings{'GUARDIAN_ENABLE_SNORT'} = 'on';
58 $settings{'GUARDIAN_ENABLE_SSH'} = 'on';
59 $settings{'GUARDIAN_ENABLE_HTTPD'} = 'on';
60 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
61 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
62 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
63 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
64 $settings{'GUARDIAN_SNORT_ALERTFILE'} = '/var/log/snort/alert';
65 $settings{'GUARDIAN_PRIORITY_LEVEL'} = '3';
67 $settings{'ACTION'} = '';
69 my $errormessage = '';
71 &Header
::showhttpheaders
();
74 &Header
::getcgihash
(\
%settings);
76 # Check if guardian is running and grab some stats.
80 ## Perform input checks and save settings.
82 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
83 # Check for valid blocktime.
84 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
85 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
88 # Check if the bloccount is valid.
89 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
90 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
94 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
95 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
98 # Check input for snort alert file.
99 unless($settings{'GUARDIAN_SNORT_ALERTFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
100 $errormessage = "$Lang::tr{'guardian invalid alertfile'}";
103 # Only continue if no error message has been set.
104 if($errormessage eq '') {
105 # Write configuration settings to file.
106 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
108 # Update configuration files.
109 &BuildConfiguration
();
112 ## Add a new entry to the ignore file.
114 } elsif ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
116 # Check if any input has been performed.
117 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
119 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
120 if ((!&General
::validip
($settings{'NEW_IGNORE_ENTRY'})) && (!&General
::validipandmask
($settings{'NEW_IGNORE_ENTRY'}))) {
121 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
124 $errormessage = "$Lang::tr{'guardian empty input'}";
127 # Go further if there was no error.
128 if ($errormessage eq '') {
129 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
131 # Open file for appending the new entry.
132 open (FILE
, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
134 # Write the new entry to the ignore file.
135 print FILE
"$new_entry\n";
139 # Check if guardian is running.
141 # Call guardianctrl to perform a reload.
142 system("/usr/local/bin/guardianctrl reload &>/dev/null");
145 ## Remove entry from ignore list.
147 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
150 # Open ignorefile and read content.
151 open(FILE
, "<$ignorefile") or die "Unable to open $ignorefile.";
152 my @current = <FILE
>;
155 # Re-open ignorefile for writing.
156 open(FILE
, ">$ignorefile") or die "Unable to open $ignorefile for writing";
159 # Read line by line from ignorefile and write them back except the line with the given ID.
160 # So this line is missing in the new file and the entry has been deleted.
161 foreach my $line (@current) {
163 unless ($settings{'ID'} eq $id) {
169 # Check if guardian is running.
171 # Call guardianctrl to perform a reload.
172 system("/usr/local/bin/guardianctrl reload &>/dev/null");
175 ## Block a user given address or subnet.
177 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
179 # Check if no empty input has been performed.
180 if ($settings{'ADDRESS_BLOCK'} ne '') {
182 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
183 if ((!&General
::validip
($settings{'ADDRESS_BLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_BLOCK'}))) {
184 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
188 $errormessage = "$Lang::tr{'guardian empty input'}";
191 # Go further if there was no error.
192 if ($errormessage eq '') {
193 my $block = $settings{'ADDRESS_BLOCK'};
195 # Call helper to unblock address.
196 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
199 ## Unblock address or subnet.
201 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
203 # Check if no empty input has been performed.
204 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
206 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
207 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
208 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
212 $errormessage = "$Lang::tr{'guardian empty input'}";
215 # Go further if there was no error.
216 if ($errormessage eq '') {
217 my $unblock = $settings{'ADDRESS_UNBLOCK'};
219 # Call helper to unblock address.
220 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
225 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
227 # Call helper to flush iptables chain from guardian.
228 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
231 # Load settings from file.
232 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
236 # Call functions to generate whole page.
240 # Display area only if guardian is running.
241 if ( ($memory != 0) && ($pid > 0) ) {
245 # Function to display the status of guardian and allow base configuration.
250 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
251 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
252 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
253 $checked{'GUARDIAN_ENABLE_SNORT'}{'off'} = '';
254 $checked{'GUARDIAN_ENABLE_SNORT'}{'on'} = '';
255 $checked{'GUARDIAN_ENABLE_SNORT'}{$settings{'GUARDIAN_ENABLE_SNORT'}} = "checked='checked'";
256 $checked{'GUARDIAN_ENABLE_SSH'}{'off'} = '';
257 $checked{'GUARDIAN_ENABLE_SSH'}{'on'} = '';
258 $checked{'GUARDIAN_ENABLE_SSH'}{$settings{'GUARDIAN_ENABLE_SSH'}} = "checked='checked'";
259 $checked{'GUARDIAN_ENABLE_HTTPD'}{'off'} = '';
260 $checked{'GUARDIAN_ENABLE_HTTPD'}{'on'} = '';
261 $checked{'GUARDIAN_ENABLE_HTTPD'}{$settings{'GUARDIAN_ENABLE_HTTPD'}} = "checked='checked'";
263 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
264 $selected{'GUARDIAN_PRIORITY_LEVEL'}{$settings{'GUARDIAN_PRIORITY_LEVEL'}} = 'selected';
266 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
267 &Header
::openbigbox
('100%', 'left', '', $errormessage);
269 # Print errormessage if there is one.
271 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
272 print "<font class='base'>$errormessage </font>\n";
277 # Draw current guardian state.
278 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
280 # Get current status of guardian.
284 # Display some useful information related to guardian, if daemon is running.
285 if ( ($memory != 0) && ($pid > 0) ){
287 <table width='95%' cellspacing='0' class='tbl'>
289 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
292 <td class='base'>$Lang::tr{'guardian daemon'}</td>
293 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
296 <td class='base'></td>
297 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
298 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
301 <td class='base'></td>
302 <td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>
303 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
308 # Otherwise display a hint that the service is not launched.
310 <table width='95%' cellspacing='0' class='tbl'>
312 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
315 <td class='base'>$Lang::tr{'guardian daemon'}</td>
316 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
324 # Draw elements for guardian configuration.
325 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
328 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
332 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
335 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
336 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
339 <td colspan='2'><br></td>
342 <td width='20%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
343 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_SNORT' value='on' $checked{'GUARDIAN_ENABLE_SNORT'}{'on'} /> /
344 <input type='radio' name='GUARDIAN_ENABLE_SNORT' value='off' $checked{'GUARDIAN_ENABLE_SNORT'}{'off'} /> off</td>
347 <td width='20%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
348 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_SSH' value='on' $checked{'GUARDIAN_ENABLE_SSH'}{'on'} /> /
349 <input type='radio' name='GUARDIAN_ENABLE_SSH' value='off' $checked{'GUARDIAN_ENABLE_SSH'}{'off'} /> off</td>
352 <td width='20%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
353 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_HTTPD' value='on' $checked{'GUARDIAN_ENABLE_HTTPD'}{'on'} /> /
354 <input type='radio' name='GUARDIAN_ENABLE_HTTPD' value='off' $checked{'GUARDIAN_ENABLE_HTTPD'}{'off'} /> off</td>
357 <td colspan='2'><br></td>
360 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
361 <td><select name='GUARDIAN_LOGLEVEL'>
362 <option value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>off</option>
363 <option value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>info</option>
364 <option value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>debug</option>
368 <td colspan='2'><br></td>
371 <td align='left' width='20%'>$Lang::tr{'guardian priority level'}:</td>
372 <td><select name='GUARDIAN_PRIORITY_LEVEL'>
373 <option value='1' $selected{'GUARDIAN_PRIORITY_LEVEL'}{'1'}>1</option>
374 <option value='2' $selected{'GUARDIAN_PRIORITY_LEVEL'}{'2'}>2</option>
375 <option value='3' $selected{'GUARDIAN_PRIORITY_LEVEL'}{'3'}>3</option>
376 <option value='4' $selected{'GUARDIAN_PRIORITY_LEVEL'}{'4'}>4</option>
380 <td colspan='2'><br></td>
383 <td width='20%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
384 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
387 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
388 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
391 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
392 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
395 <td width='20%' class='base'>$Lang::tr{'guardian snort alertfile'}:</td>
396 <td><input type='text' name='GUARDIAN_SNORT_ALERTFILE' value='$settings{'GUARDIAN_SNORT_ALERTFILE'}' size='30' /></td>
407 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
417 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
418 sub showIgnoreBox
() {
419 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
424 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
427 # Check if the guardian ignore file contains any content.
428 if (-s
$ignorefile) {
430 # Open file and print contents.
431 open FILE
, $ignorefile or die "Could not open $ignorefile";
436 # Read file line by line and print out the elements.
437 while( my $ignored_element = <FILE
> ) {
439 # Increase id number for each element in the ignore file.
442 # Check if the id number is even or not.
444 $col="bgcolor='$color{'color22'}'";
446 $col="bgcolor='$color{'color20'}'";
451 <td width='80%' class='base' $col>$ignored_element</td>
452 <td width='20%' align='center' $col>
453 <form method='post' name='$id' action='$ENV{'SCRIPT_NAME'}'>
454 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
455 <input type='hidden' name='ID' value='$id'>
456 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
464 # Print notice that currently no elements are stored in the ignore file.
466 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
472 # Section to add new elements to the ignore list.
477 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
479 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
480 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
481 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
491 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
492 sub showBlockedBox
() {
493 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
498 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
502 # Lauch function to get the currently blocked hosts.
503 my @blocked_hosts = &GetBlockedHosts
();
508 # Loop through our blocked hosts array.
509 foreach my $blocked_host (@blocked_hosts) {
511 # Increase id number for each element in the ignore file.
514 # Check if the id number is even or not.
516 $col="bgcolor='$color{'color22'}'";
518 $col="bgcolor='$color{'color20'}'";
523 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
524 <td width='20%' align='center' $col>
525 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
526 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
527 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
528 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
535 # If the loop only has been runs once the id still is "0", which means there are no
536 # additional entries (blocked hosts) in the iptables chain.
539 # Print notice that currently no hosts are blocked.
541 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
547 # Section for a manual block of an IP-address.
551 <table width='60%' border='0'>
552 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
554 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
555 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
556 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
557 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
567 &Header
::closebigbox
();
568 &Header
::closepage
();
570 # Function to check if guardian has been started.
571 # Grab process id and consumed memory if the daemon is running.
575 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
578 $string = join("", @guardian);
579 $string =~ s/[a-z_]//gi;
580 $string =~ s/\[[0-1]\;[0-9]+//gi;
581 $string =~ s/[\(\)\.]//gi;
584 @pid = split(/\s/,$string);
585 if (open(FILE
, "/proc/$pid[0]/statm")){
587 @memory = split(/ /,$temp);
593 sub GetBlockedHosts
() {
595 # Create new, empty array.
598 # Lauch helper to get chains from iptables.
599 open(FILE
, "/usr/local/bin/guardianctrl get-chain |");
601 # Read file line by line and print out the elements.
602 foreach my $line (<FILE
>) {
604 # Skip descriptive lines.
605 next if ($line =~ /^Chain/);
606 next if ($line =~ /^ pkts/);
608 # Generate array, based on the line content (seperator is a single or multiple space's)
609 my @comps = split(/\s{1,}/, $line);
610 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
612 # Assign different variable names.
613 my $blocked_host = $source;
615 # Add host to our hosts array.
616 push(@hosts, $blocked_host);
619 # Convert entries, sort them, write back and store the sorted entries into new array.
620 my @sorted = map { $_->[0] }
621 sort { $a->[1] <=> $b->[1] }
622 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
625 # Return our sorted list.
629 sub BuildConfiguration
() {
631 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
633 my $configfile = "${General::swroot}/guardian/guardian.conf";
635 # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway.
636 my $HostGatewayByte = "1";
638 # Open configfile for writing.
639 open(FILE
, ">$configfile");
641 print FILE
"EnableSnortMonitoring\t\t$settings{'GUARDIAN_ENABLE_SNORT'}\n";
642 print FILE
"EnableSSHMonitoring\t\t$settings{'GUARDIAN_ENABLE_SSH'}\n";
643 print FILE
"EnableHTTPDMonitoring\t\t$settings{'GUARDIAN_ENABLE_HTTPD'}\n";
644 print FILE
"LogLevel\t\t\t$settings{'GUARDIAN_LOGLEVEL'}\n";
645 print FILE
"BlockCount\t\t\t$settings{'GUARDIAN_BLOCKCOUNT'}\n";
646 print FILE
"HostGatewayByte\t\t\t$HostGatewayByte\n";
647 print FILE
"LogFile\t\t\t\t$settings{'GUARDIAN_LOGFILE'}\n";
648 print FILE
"AlertFile\t\t\t$settings{'GUARDIAN_SNORT_ALERTFILE'}\n";
649 print FILE
"IgnoreFile\t\t\t$ignorefile\n";
650 print FILE
"TimeLimit\t\t\t$settings{'GUARDIAN_BLOCKTIME'}\n";
651 print FILE
"PriorityLevel\t\t\t$settings{'GUARDIAN_PRIORITY_LEVEL'}\n";
655 # Check if guardian should be started or stopped.
656 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
658 # Call guardianctl to perform a reload.
659 system("/usr/local/bin/guardianctrl reload &>/dev/null");
662 system("/usr/local/bin/guardianctrl start &>/dev/null");
666 system("/usr/local/bin/guardianctrl stop &>/dev/null");