]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2015 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 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
34 my %mainsettings = ();
35 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
36 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
43 our $errormessage = '';
47 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
49 &Header
::showhttpheaders
();
51 $snortsettings{'ENABLE_SNORT'} = 'off';
52 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
53 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
54 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
55 $snortsettings{'ACTION'} = '';
56 $snortsettings{'RULES'} = '';
57 $snortsettings{'OINKCODE'} = '';
58 $snortsettings{'INSTALLDATE'} = '';
59 $snortsettings{'FILE'} = '';
62 &Header
::getcgihash
(\
%cgiparams);
64 my $snortrulepath = "/etc/snort/rules";
65 my $restartsnortrequired = 0;
68 my $table1colour = '';
69 my $table2colour = '';
73 my $linkedrulefile = '';
75 my $checkboxname = '';
77 ## Grab all available snort rules and store them in the snortrules hash.
79 # Open snort rules directory and do a directory listing.
80 opendir(DIR
, $snortrulepath) or die $!;
81 # Loop through the direcory.
82 while (my $file = readdir(DIR
)) {
85 next unless (-f
"$snortrulepath/$file");
88 next if (-z
"$snortrulepath/$file");
90 # Use a regular expression to find files ending in .rules
91 next unless ($file =~ m/\.rules$/);
93 # Ignore files which are not read-able.
94 next unless (-R
"$snortrulepath/$file");
96 # Call subfunction to read-in rulefile and add rules to
97 # the snortrules hash.
98 &readrulesfile
("$file");
104 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
105 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
106 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
108 # Arrays to store sid which should be added to the corresponding files.
112 # Loop through the hash of snortrules.
113 foreach my $rulefile(keys %snortrules) {
114 # Loop through the single rules of the rulefile.
115 foreach my $sid (keys %{$snortrules{$rulefile}}) {
116 # Check if there exists a key in the cgiparams hash for this sid.
117 if (exists($cgiparams{$sid})) {
118 # Look if the rule is disabled.
119 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
120 # Check if the state has been set to 'on'.
121 if ($cgiparams{$sid} eq "on") {
122 # Add the sid to the enabled_sids array.
123 push(@enabled_sids, $sid);
125 # Drop item from cgiparams hash.
126 delete $cgiparams{$sid};
130 # Look if the rule is enabled.
131 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
132 # Check if the state is 'on' and should be disabled.
133 # In this case there is no entry
134 # for the sid in the cgiparams hash.
135 # Add it to the disabled_sids array.
136 push(@disabled_sids, $sid);
138 # Drop item from cgiparams hash.
139 delete $cgiparams{$sid};
145 # Check if the enabled_sids array contains any sid's.
147 # Open enabled sid's file for writing.
148 open(FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
150 # Write header to file.
151 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
153 # Loop through the array of enabled sids and write them to the file.
154 foreach my $sid (@enabled_sids) {
155 print FILE
"enable_sid $sid\n";
158 # Close file after writing.
162 # Check if the enabled_sids array contains any sid's.
163 if (@disabled_sids) {
164 # Open disabled sid's file for writing.
165 open(FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
167 # Write header to file.
168 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
170 # Loop through the array of disabled sids and write them to the file.
171 foreach my $sid (@disabled_sids) {
172 print FILE
"disable_sid $sid\n";
175 # Close file after writing.
180 if ($snortsettings{'OINKCODE'} ne "") {
181 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
184 if (!$errormessage) {
185 if ($snortsettings{'RULES'} eq 'subscripted') {
186 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
187 } elsif ($snortsettings{'RULES'} eq 'registered') {
188 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
189 } elsif ($snortsettings{'RULES'} eq 'community') {
190 $url=" https://www.snort.org/rules/community";
192 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
195 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
196 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
197 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
199 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
201 unlink "${General::swroot}/snort/enable";
203 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
205 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
207 unlink "${General::swroot}/snort/enable_green";
209 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
211 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
213 unlink "${General::swroot}/snort/enable_blue";
215 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
217 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
219 unlink "${General::swroot}/snort/enable_orange";
221 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
223 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
225 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
228 system('/usr/local/bin/snortctrl restart >/dev/null');
231 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
232 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
234 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
235 my @df = `/bin/df -B M /var`;
236 foreach my $line (@df) {
237 next if $line =~ m/^Filesystem/;
240 if ($line =~ m/dev/ ) {
241 $line =~ m/^.* (\d+)M.*$/;
242 my @temp = split(/ +/,$line);
244 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
246 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
247 &downloadrulesfile
();
249 $return = `cat /var/tmp/log 2>/dev/null`;
253 if ($return =~ "ERROR") {
254 $errormessage = "<br /><pre>".$return."</pre>";
256 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
265 $checked{'ENABLE_SNORT'}{'off'} = '';
266 $checked{'ENABLE_SNORT'}{'on'} = '';
267 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
268 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
269 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
270 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
271 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
272 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
273 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
274 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
275 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
276 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
277 $selected{'RULES'}{'nothing'} = '';
278 $selected{'RULES'}{'community'} = '';
279 $selected{'RULES'}{'emerging'} = '';
280 $selected{'RULES'}{'registered'} = '';
281 $selected{'RULES'}{'subscripted'} = '';
282 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
284 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
289 // Tiny java script function to show/hide the rules
290 // of a given category.
291 function showhide(tblname) {
292 \$("#" + tblname).toggle();
298 &Header
::openbigbox
('100%', 'left', '', $errormessage);
302 # &Header::openbox('100%', 'left', 'DEBUG');
303 # my $debugCount = 0;
304 # foreach my $line (sort keys %snortsettings) {
305 # print "$line = $snortsettings{$line}<br />\n";
308 # print " Count: $debugCount\n";
309 # &Header::closebox();
314 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
315 print "<class name='base'>$errormessage\n";
316 print " </class>\n";
320 my $return = `pidof oinkmaster.pl -x`;
323 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
327 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
329 $Lang::tr{'snort working'}
330 <tr><td colspan='2' align='center'>
331 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
332 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
334 <tr><td colspan='2' align='left'><pre>
336 my @output = `tail -20 /var/tmp/log`;
345 &Header
::closebigbox
();
346 &Header
::closepage
();
351 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
353 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
354 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
357 if ($netsettings{'BLUE_DEV'} ne '') {
358 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
360 if ($netsettings{'ORANGE_DEV'} ne '') {
361 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
363 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
371 <td><b>$Lang::tr{'ids rules update'}</b></td>
374 <td><select name='RULES'>
375 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
376 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
377 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
378 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
379 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
385 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
386 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
390 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
393 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
396 if ( -e
"/var/tmp/snortrules.tar.gz"){
397 my @Info = stat("/var/tmp/snortrules.tar.gz");
398 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
400 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
408 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
415 if ($results ne '') {
421 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
422 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
424 # Output display table for rule files
425 print "<table width='100%'>\n";
427 # Local variable required for java script to show/hide
428 # rules of a rulefile.
429 my $rulesetcount = 1;
431 # Loop over each rule file
432 foreach my $rulefile (sort keys(%snortrules)) {
433 my $rulechecked = '';
435 # Check if rule file is enabled
436 if ($snortrules{$rulefile}{"State"} eq 'On') {
437 $rulechecked = 'CHECKED';
440 # Table and rows for the rule files.
442 print"<td class='base' width='5%'>\n";
443 print"<input type='checkbox' name='SNORT_RULE_$rulefile' $rulechecked>\n";
445 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
446 print"<td class='base' width='5%' align='right'>\n";
447 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
451 # Rows which will be hidden per default and will contain the single rules.
452 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
453 print"<td colspan='3'>\n";
460 # New table for the single rules.
461 print "<table width='100%'>\n";
463 # Loop over rule file rules
464 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
466 my $ruledefchecked = '';
468 # If 2 rules have been displayed, start a new row
469 if (($lines % 2) == 0) {
472 # Increase rows by once.
478 $col="bgcolor='$color{'color20'}'";
480 $col="bgcolor='$color{'color22'}'";
484 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
485 $ruledefchecked = 'CHECKED';
488 # Create rule checkbox and display rule description
489 print "<td class='base' width='5%' align='right' $col>\n";
490 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
492 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
494 # Increment rule count
498 # If do not have a second rule for row, create empty cell
499 if (($lines % 2) != 0) {
500 print "<td class='base'></td>";
503 # Close display table
504 print "</tr></table></td></tr>";
506 # Finished whith the rule file, increase count.
510 # Close display table
516 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
517 <!-- space for future online help link -->
526 &Header
::closebigbox
();
527 &Header
::closepage
();
530 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
531 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
535 sub downloadrulesfile
{
539 unlink("/var/tmp/log");
541 unless (-e
"${General::swroot}/red/active") {
542 $errormessage = $Lang::tr
{'could not download latest updates'};
546 my %proxysettings=();
547 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
549 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
550 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
554 system("wget -r --proxy=on --proxy-user=$proxysettings{'UPSTREAM_USER'} --proxy-passwd=$proxysettings{'UPSTREAM_PASSWORD'} -e http_proxy=http://$peer:$peerport/ -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
556 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
560 sub readrulesfile
($) {
561 my $rulefile = shift;
563 # Open rule file and read in contents
564 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
566 # Store file content in an array.
567 my @lines = <RULEFILE
>;
572 # Loop over rule file contents
573 foreach my $line (@lines) {
574 # Remove whitespaces.
578 next if ($line =~ /^\s*$/);
584 # Gather rule sid and message from the ruleline.
585 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
589 # Check if a rule has been found.
591 # Add rule to the snortrules hash.
592 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
594 # Grab status of the rule. Check if ruleline starts with a "dash".
595 if ($line =~ /^\#/) {
596 # If yes, the rule is disabled.
597 $snortrules{$rulefile}{$sid}{'State'} = "off";
599 # Otherwise the rule is enabled.
600 $snortrules{$rulefile}{$sid}{'State'} = "on";