]>
git.ipfire.org Git - people/stevee/ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
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
} );
44 # Path to the guardian.ignore file.
45 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
47 our %netsettings = ();
48 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
51 our %mainsettings = ();
52 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
53 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
57 $settings{'GUARDIAN_ENABLED'} = 'off';
58 $settings{'GUARDIAN_ENABLE_SNORT'} = 'on';
59 $settings{'GUARDIAN_ENABLE_SSH'} = 'on';
60 $settings{'GUARDIAN_ENABLE_HTTPD'} = 'on';
61 $settings{'GUARDIAN_LOGLEVEL'} ='info';
62 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
63 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
64 $settings{'GUARDIAN_SNORT_ALERTFILE'} = '/var/log/snort/alert';
66 $settings{'ACTION'} = '';
68 my $errormessage = '';
70 &Header
::showhttpheaders
();
73 &Header
::getcgihash
(\
%settings);
74 &Header
::getcgihash
(\
%cgiparams);
76 ## Perform input checks and save settings.
78 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
80 # Check for valid blocktime.
81 if ($settings{'GUARDIAN_BLOCKTIME'} ne '') {
82 if (($settings{'GUARDIAN_BLOCKTIME'} !~ /^[0-9]+$/) || ($settings{'GUARDIAN_BLOCKTIME'} le '0')) {
83 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
88 if ($settings{'GUARDIAN_LOGFILE'} ne '') {
89 if ($settings{'GUARDIAN_LOGFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
90 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
94 # Check input for snort alert file.
95 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} ne '') {
96 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
97 $errormessage = "$Lang::tr{'guardian invalid alertfile'}";
101 # Only continue if no error message has been set.
102 if ($errormessage eq '') {
103 # Write configuration settings to file.
104 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
106 # Update configuration files.
107 &BuildConfiguration
();
110 ## Add a new entry to the ignore file.
112 } elsif ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
114 # Check if any input has been performed.
115 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
117 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
118 if ((!&General
::validip
($settings{'NEW_IGNORE_ENTRY'})) && (!&General
::validipandmask
($settings{'NEW_IGNORE_ENTRY'}))) {
119 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
122 $errormessage = "$Lang::tr{'guardian empty input'}";
125 # Go further if there was no error.
126 if ($errormessage eq '') {
127 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
129 # Open file for appending the new entry.
130 open (FILE
, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
132 # Write the new entry to the ignore file.
133 print FILE
"$new_entry\n";
137 ## Remove entry from ignore list.
139 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
142 # Open ignorefile and read content.
143 open(FILE
, "<$ignorefile") or die "Unable to open $ignorefile.";
144 my @current = <FILE
>;
147 # Re-open ignorefile for writing.
148 open(FILE
, ">$ignorefile") or die "Unable to open $ignorefile for writing";
151 # Read line by line from ignorefile and write them back except the line with the given ID.
152 # So this line is missing in the new file and the entry has been deleted.
153 foreach my $line (@current) {
155 unless ($cgiparams{'ID'} eq $id) {
161 ## Block a user given address or subnet.
163 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
165 # Check if no empty input has been performed.
166 if ($settings{'ADDRESS_BLOCK'} ne '') {
168 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
169 if ((!&General
::validip
($settings{'ADDRESS_BLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_BLOCK'}))) {
170 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
174 $errormessage = "$Lang::tr{'guardian empty input'}";
177 # Go further if there was no error.
178 if ($errormessage eq '') {
179 my $block = $settings{'ADDRESS_BLOCK'};
181 # Call helper to unblock address.
182 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
185 ## Unblock address or subnet.
187 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
189 # Check if no empty input has been performed.
190 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
192 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
193 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
194 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
198 $errormessage = "$Lang::tr{'guardian empty input'}";
201 # Go further if there was no error.
202 if ($errormessage eq '') {
203 my $unblock = $settings{'ADDRESS_UNBLOCK'};
205 # Call helper to unblock address.
206 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
211 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
213 # Call helper to flush iptables chain from guardian.
214 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
217 # Load settings from file.
218 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
222 # Call functions to generate whole page.
226 # Display area only if guardian is running.
227 if ( ($memory != 0) && (@pid[0] ne "///") ) {
231 # Function to display the status of guardian and allow base configuration.
236 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
237 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
238 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
239 $checked{'GUARDIAN_ENABLE_SNORT'}{'off'} = '';
240 $checked{'GUARDIAN_ENABLE_SNORT'}{'on'} = '';
241 $checked{'GUARDIAN_ENABLE_SNORT'}{$settings{'GUARDIAN_ENABLE_SNORT'}} = "checked='checked'";
242 $checked{'GUARDIAN_ENABLE_SSH'}{'off'} = '';
243 $checked{'GUARDIAN_ENABLE_SSH'}{'on'} = '';
244 $checked{'GUARDIAN_ENABLE_SSH'}{$settings{'GUARDIAN_ENABLE_SSH'}} = "checked='checked'";
245 $checked{'GUARDIAN_ENABLE_HTTPD'}{'off'} = '';
246 $checked{'GUARDIAN_ENABLE_HTTPD'}{'on'} = '';
247 $checked{'GUARDIAN_ENABLE_HTTPD'}{$settings{'GUARDIAN_ENABLE_HTTPD'}} = "checked='checked'";
249 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}}= 'selected';
251 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
252 &Header
::openbigbox
('100%', 'left', '', $errormessage);
254 # Print errormessage if there is one.
256 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
257 print "<font class='base'>$errormessage </font>\n";
262 # Draw current guardian state.
263 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
265 # Check if guardian is running and grab some stats.
268 # Display some useful information related to guardian, if daemon is running.
269 if ( ($memory != 0) && (@pid[0] ne "///") ){
271 <table width='95%' cellspacing='0' class='tbl'>
273 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
276 <td class='base'>$Lang::tr{'guardian daemon'}</td>
277 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
280 <td class='base'></td>
281 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
282 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
285 <td class='base'></td>
286 <td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>
287 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
292 # Otherwise display a hint that the service is not launched.
294 <table width='95%' cellspacing='0' class='tbl'>
296 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
299 <td class='base'>$Lang::tr{'guardian daemon'}</td>
300 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
308 # Draw elements for guardian configuration.
309 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
312 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
316 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
319 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
320 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
323 <td colspan='2'><br></td>
326 <td width='20%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
327 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_SNORT' value='on' $checked{'GUARDIAN_ENABLE_SNORT'}{'on'} /> /
328 <input type='radio' name='GUARDIAN_ENABLE_SNORT' value='off' $checked{'GUARDIAN_ENABLE_SNORT'}{'off'} /> off</td>
331 <td width='20%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
332 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_SSH' value='on' $checked{'GUARDIAN_ENABLE_SSH'}{'on'} /> /
333 <input type='radio' name='GUARDIAN_ENABLE_SSH' value='off' $checked{'GUARDIAN_ENABLE_SSH'}{'off'} /> off</td>
336 <td width='20%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
337 <td align='left'>on <input type='radio' name='GUARDIAN_ENABLE_HTTPD' value='on' $checked{'GUARDIAN_ENABLE_HTTPD'}{'on'} /> /
338 <input type='radio' name='GUARDIAN_ENABLE_HTTPD' value='off' $checked{'GUARDIAN_ENABLE_HTTPD'}{'off'} /> off</td>
341 <td colspan='2'><br></td>
344 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}</td>
345 <td><select name='GUARDIAN_LOGLEVEL'>
346 <option value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>off</option>
347 <option value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>info</option>
348 <option value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>debug</option>
353 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
354 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
357 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
358 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
361 <td width='20%' class='base'>$Lang::tr{'guardian snort alertfile'}:</td>
362 <td><input type='text' name='GUARDIAN_SNORT_ALERTFILE' value='$settings{'GUARDIAN_SNORT_ALERTFILE'}' size='30' /></td>
373 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
383 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
384 sub showIgnoreBox
() {
385 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
390 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
393 # Check if the guardian ignore file contains any content.
394 if (-s
$ignorefile) {
396 # Open file and print contents.
397 open FILE
, $ignorefile or die "Could not open $ignorefile";
402 # Read file line by line and print out the elements.
403 while( my $ignored_element = <FILE
> ) {
405 # Increase id number for each element in the ignore file.
408 # Check if the id number is even or not.
410 $col="bgcolor='$color{'color22'}'";
412 $col="bgcolor='$color{'color20'}'";
417 <td width='80%' class='base' $col>$ignored_element</td>
418 <td width='20%' align='center' $col>
419 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
420 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
421 <input type='hidden' name='ID' value='$id'>
422 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
430 # Print notice that currently no elements are stored in the ignore file.
432 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
438 # Section to add new elements to the ignore list.
443 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
445 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
446 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
447 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
457 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
458 sub showBlockedBox
() {
459 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
464 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
468 # Lauch function to get the currently blocked hosts.
469 my @blocked_hosts = &GetBlockedHosts
();
474 # Loop through our blocked hosts array.
475 foreach my $blocked_host (@blocked_hosts) {
477 # Increase id number for each element in the ignore file.
480 # Check if the id number is even or not.
482 $col="bgcolor='$color{'color22'}'";
484 $col="bgcolor='$color{'color20'}'";
489 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
490 <td width='20%' align='center' $col>
491 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
492 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
493 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
494 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
501 # If the loop only has been runs once the id still is "0", which means there are no
502 # additional entries (blocked hosts) in the iptables chain.
505 # Print notice that currently no hosts are blocked.
507 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
513 # Section for a manual block of an IP-address.
517 <table width='60%' border='0'>
518 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
520 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
521 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
522 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
523 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
533 &Header
::closebigbox
();
534 &Header
::closepage
();
536 # Function to check if guardian has been started.
537 # Grab process id and consumed memory if the daemon is running.
541 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
544 $string = join("", @guardian);
545 $string =~ s/[a-z_]//gi;
546 $string =~ s/\[[0-1]\;[0-9]+//gi;
547 $string =~ s/[\(\)\.]//gi;
550 @pid = split(/\s/,$string);
551 if (open(FILE
, "/proc/$pid[0]/statm")){
553 @memory = split(/ /,$temp);
559 sub GetBlockedHosts
() {
561 # Create new, empty array.
564 # Lauch helper to get chains from iptables.
565 open(FILE
, "/usr/local/bin/guardianctrl get-chain |");
567 # Read file line by line and print out the elements.
568 foreach my $line (<FILE
>) {
570 # Skip descriptive lines.
571 next if ($line =~ /^Chain/);
572 next if ($line =~ /^ pkts/);
574 # Generate array, based on the line content (seperator is a single or multiple space's)
575 my @comps = split(/\s{1,}/, $line);
576 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
578 # Assign different variable names.
579 my $blocked_host = $source;
581 # Add host to our hosts array.
582 push(@hosts, $blocked_host);
585 # Convert entries, sort them, write back and store the sorted entries into new array.
586 my @sorted = map { $_->[0] }
587 sort { $a->[1] <=> $b->[1] }
588 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
591 # Return our sorted list.
595 sub BuildConfiguration
() {
597 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
599 my $configfile = "${General::swroot}/guardian/guardian.conf";
601 # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway.
602 my $HostGatewayByte = "1";
604 # Open configfile for writing.
605 open(FILE
, ">$configfile");
607 print FILE
"EnableSnortMonitoring $settings{'GUARDIAN_ENABLE_SNORT'}\n";
608 print FILE
"EnableSSHMonitoring $settings{'GUARDIAN_ENABLE_SSH'}\n";
609 print FILE
"EnableHTTPDMonitoring $settings{'GUARDIAN_ENABLE_HTTPD'}\n";
610 print FILE
"LogLevel $settings{'GUARDIAN_LOGLEVEL'}\n";
611 print FILE
"HostGatewayByte $HostGatewayByte\n";
612 print FILE
"LogFile $settings{'GUARDIAN_LOGFILE'}\n";
613 print FILE
"AlertFile $settings{'GUARDIAN_SNORT_ALERTFILE'}\n";
614 print FILE
"IgnoreFile $ignorefile\n";
615 print FILE
"TimeLimit $settings{'GUARDIAN_BLOCKTIME'}\n";
619 # Restart the service.
620 if ($settings{'GUARDIAN_ENABLED'} eq 'on') {
621 system("/usr/local/bin/guardianctrl restart &>/dev/null");
623 system("/usr/local/bin/guardianctrl stop &>/dev/null");