]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
86a469cb2b54cea26b7bc20f4b4670183a83ab27
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 = ();
41 # Read-in main settings, for language, theme and colors.
42 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
43 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
46 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
48 &Header
::showhttpheaders
();
50 # Default settings for snort.
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'} = '';
61 &Header
::getcgihash
(\
%cgiparams);
63 my $snortrulepath = "/etc/snort/rules";
64 my $restartsnortrequired = 0;
69 ## Grab all available snort rules and store them in the snortrules hash.
71 # Open snort rules directory and do a directory listing.
72 opendir(DIR
, $snortrulepath) or die $!;
73 # Loop through the direcory.
74 while (my $file = readdir(DIR
)) {
77 next unless (-f
"$snortrulepath/$file");
80 next if (-z
"$snortrulepath/$file");
82 # Use a regular expression to find files ending in .rules
83 next unless ($file =~ m/\.rules$/);
85 # Ignore files which are not read-able.
86 next unless (-R
"$snortrulepath/$file");
88 # Call subfunction to read-in rulefile and add rules to
89 # the snortrules hash.
90 &readrulesfile
("$file");
96 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
97 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
98 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
100 # Arrays to store sid which should be added to the corresponding files.
104 # Loop through the hash of snortrules.
105 foreach my $rulefile(keys %snortrules) {
106 # Loop through the single rules of the rulefile.
107 foreach my $sid (keys %{$snortrules{$rulefile}}) {
108 # Check if there exists a key in the cgiparams hash for this sid.
109 if (exists($cgiparams{$sid})) {
110 # Look if the rule is disabled.
111 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
112 # Check if the state has been set to 'on'.
113 if ($cgiparams{$sid} eq "on") {
114 # Add the sid to the enabled_sids array.
115 push(@enabled_sids, $sid);
117 # Drop item from cgiparams hash.
118 delete $cgiparams{$sid};
122 # Look if the rule is enabled.
123 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
124 # Check if the state is 'on' and should be disabled.
125 # In this case there is no entry
126 # for the sid in the cgiparams hash.
127 # Add it to the disabled_sids array.
128 push(@disabled_sids, $sid);
130 # Drop item from cgiparams hash.
131 delete $cgiparams{$sid};
137 # Check if the enabled_sids array contains any sid's.
139 # Open enabled sid's file for writing.
140 open(FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
142 # Write header to file.
143 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
145 # Loop through the array of enabled sids and write them to the file.
146 foreach my $sid (@enabled_sids) {
147 print FILE
"enable_sid $sid\n";
150 # Close file after writing.
154 # Check if the enabled_sids array contains any sid's.
155 if (@disabled_sids) {
156 # Open disabled sid's file for writing.
157 open(FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
159 # Write header to file.
160 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
162 # Loop through the array of disabled sids and write them to the file.
163 foreach my $sid (@disabled_sids) {
164 print FILE
"disable_sid $sid\n";
167 # Close file after writing.
172 if ($snortsettings{'OINKCODE'} ne "") {
173 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
176 if (!$errormessage) {
177 if ($snortsettings{'RULES'} eq 'subscripted') {
178 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
179 } elsif ($snortsettings{'RULES'} eq 'registered') {
180 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
181 } elsif ($snortsettings{'RULES'} eq 'community') {
182 $url=" https://www.snort.org/rules/community";
184 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
187 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
188 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
189 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
191 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
193 unlink "${General::swroot}/snort/enable";
195 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
197 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
199 unlink "${General::swroot}/snort/enable_green";
201 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
203 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
205 unlink "${General::swroot}/snort/enable_blue";
207 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
209 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
211 unlink "${General::swroot}/snort/enable_orange";
213 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
215 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
217 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
220 system('/usr/local/bin/snortctrl restart >/dev/null');
223 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
224 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
226 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
227 my @df = `/bin/df -B M /var`;
228 foreach my $line (@df) {
229 next if $line =~ m/^Filesystem/;
232 if ($line =~ m/dev/ ) {
233 $line =~ m/^.* (\d+)M.*$/;
234 my @temp = split(/ +/,$line);
236 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
238 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
239 &downloadrulesfile
();
241 $return = `cat /var/tmp/log 2>/dev/null`;
245 if ($return =~ "ERROR") {
246 $errormessage = "<br /><pre>".$return."</pre>";
248 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 &");
257 $checked{'ENABLE_SNORT'}{'off'} = '';
258 $checked{'ENABLE_SNORT'}{'on'} = '';
259 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
260 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
261 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
262 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
263 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
264 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
265 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
266 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
267 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
268 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
269 $selected{'RULES'}{'nothing'} = '';
270 $selected{'RULES'}{'community'} = '';
271 $selected{'RULES'}{'emerging'} = '';
272 $selected{'RULES'}{'registered'} = '';
273 $selected{'RULES'}{'subscripted'} = '';
274 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
276 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
281 // Tiny java script function to show/hide the rules
282 // of a given category.
283 function showhide(tblname) {
284 \$("#" + tblname).toggle();
290 &Header
::openbigbox
('100%', 'left', '', $errormessage);
294 # &Header::openbox('100%', 'left', 'DEBUG');
295 # my $debugCount = 0;
296 # foreach my $line (sort keys %snortsettings) {
297 # print "$line = $snortsettings{$line}<br />\n";
300 # print " Count: $debugCount\n";
301 # &Header::closebox();
306 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
307 print "<class name='base'>$errormessage\n";
308 print " </class>\n";
312 my $return = `pidof oinkmaster.pl -x`;
315 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
319 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
321 $Lang::tr{'snort working'}
322 <tr><td colspan='2' align='center'>
323 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
324 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
326 <tr><td colspan='2' align='left'><pre>
328 my @output = `tail -20 /var/tmp/log`;
337 &Header
::closebigbox
();
338 &Header
::closepage
();
343 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
345 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
346 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
349 if ($netsettings{'BLUE_DEV'} ne '') {
350 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
352 if ($netsettings{'ORANGE_DEV'} ne '') {
353 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
355 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
363 <td><b>$Lang::tr{'ids rules update'}</b></td>
366 <td><select name='RULES'>
367 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
368 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
369 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
370 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
371 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
377 $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 />
378 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
382 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
385 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
388 if ( -e
"/var/tmp/snortrules.tar.gz"){
389 my @Info = stat("/var/tmp/snortrules.tar.gz");
390 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
392 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
400 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
409 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
410 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
412 # Output display table for rule files
413 print "<table width='100%'>\n";
415 # Local variable required for java script to show/hide
416 # rules of a rulefile.
417 my $rulesetcount = 1;
419 # Loop over each rule file
420 foreach my $rulefile (sort keys(%snortrules)) {
421 my $rulechecked = '';
423 # Check if rule file is enabled
424 if ($snortrules{$rulefile}{"State"} eq 'On') {
425 $rulechecked = 'CHECKED';
428 # Table and rows for the rule files.
430 print"<td class='base' width='5%'>\n";
431 print"<input type='checkbox' name='SNORT_RULE_$rulefile' $rulechecked>\n";
433 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
434 print"<td class='base' width='5%' align='right'>\n";
435 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
439 # Rows which will be hidden per default and will contain the single rules.
440 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
441 print"<td colspan='3'>\n";
448 # New table for the single rules.
449 print "<table width='100%'>\n";
451 # Loop over rule file rules
452 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
454 my $ruledefchecked = '';
456 # If 2 rules have been displayed, start a new row
457 if (($lines % 2) == 0) {
460 # Increase rows by once.
466 $col="bgcolor='$color{'color20'}'";
468 $col="bgcolor='$color{'color22'}'";
472 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
473 $ruledefchecked = 'CHECKED';
476 # Create rule checkbox and display rule description
477 print "<td class='base' width='5%' align='right' $col>\n";
478 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
480 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
482 # Increment rule count
486 # If do not have a second rule for row, create empty cell
487 if (($lines % 2) != 0) {
488 print "<td class='base'></td>";
491 # Close display table
492 print "</tr></table></td></tr>";
494 # Finished whith the rule file, increase count.
498 # Close display table
504 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
505 <!-- space for future online help link -->
514 &Header
::closebigbox
();
515 &Header
::closepage
();
518 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
519 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
523 sub downloadrulesfile
{
527 unlink("/var/tmp/log");
529 unless (-e
"${General::swroot}/red/active") {
530 $errormessage = $Lang::tr
{'could not download latest updates'};
534 my %proxysettings=();
535 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
537 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
538 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
542 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");
544 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
548 sub readrulesfile
($) {
549 my $rulefile = shift;
551 # Open rule file and read in contents
552 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
554 # Store file content in an array.
555 my @lines = <RULEFILE
>;
560 # Loop over rule file contents
561 foreach my $line (@lines) {
562 # Remove whitespaces.
566 next if ($line =~ /^\s*$/);
572 # Gather rule sid and message from the ruleline.
573 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
577 # Check if a rule has been found.
579 # Add rule to the snortrules hash.
580 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
582 # Grab status of the rule. Check if ruleline starts with a "dash".
583 if ($line =~ /^\#/) {
584 # If yes, the rule is disabled.
585 $snortrules{$rulefile}{$sid}{'State'} = "off";
587 # Otherwise the rule is enabled.
588 $snortrules{$rulefile}{$sid}{'State'} = "on";