]>
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);
42 our $errormessage = '';
46 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
48 &Header
::showhttpheaders
();
50 $snortsettings{'ENABLE_SNORT'} = 'off';
51 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
52 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
53 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
54 $snortsettings{'ACTION'} = '';
55 $snortsettings{'RULES'} = '';
56 $snortsettings{'OINKCODE'} = '';
57 $snortsettings{'INSTALLDATE'} = '';
58 $snortsettings{'FILE'} = '';
60 &Header
::getcgihash
(\
%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
62 my $snortrulepath = "/etc/snort/rules";
63 my $restartsnortrequired = 0;
66 my $table1colour = '';
67 my $table2colour = '';
71 my $linkedrulefile = '';
73 my $checkboxname = '';
75 ## Grab all available snort rules and store them in the snortrules hash.
77 # Open snort rules directory and do a directory listing.
78 opendir(DIR
, $snortrulepath) or die $!;
79 # Loop through the direcory.
80 while (my $file = readdir(DIR
)) {
83 next unless (-f
"$snortrulepath/$file");
86 next if (-z
"$snortrulepath/$file");
88 # Use a regular expression to find files ending in .rules
89 next unless ($file =~ m/\.rules$/);
91 # Ignore files which are not read-able.
92 next unless (-R
"$snortrulepath/$file");
94 # Call subfunction to read-in rulefile and add rules to
95 # the snortrules hash.
96 &readrulesfile
("$file");
101 if ($snortsettings{'OINKCODE'} ne "") {
102 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
105 if (!$errormessage) {
106 if ($snortsettings{'RULES'} eq 'subscripted') {
107 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
108 } elsif ($snortsettings{'RULES'} eq 'registered') {
109 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
110 } elsif ($snortsettings{'RULES'} eq 'community') {
111 $url=" https://www.snort.org/rules/community";
113 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
116 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
117 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
118 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
120 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
122 unlink "${General::swroot}/snort/enable";
124 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
126 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
128 unlink "${General::swroot}/snort/enable_green";
130 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
132 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
134 unlink "${General::swroot}/snort/enable_blue";
136 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
138 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
140 unlink "${General::swroot}/snort/enable_orange";
142 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
144 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
146 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
149 system('/usr/local/bin/snortctrl restart >/dev/null');
152 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
153 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
155 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
156 my @df = `/bin/df -B M /var`;
157 foreach my $line (@df) {
158 next if $line =~ m/^Filesystem/;
161 if ($line =~ m/dev/ ) {
162 $line =~ m/^.* (\d+)M.*$/;
163 my @temp = split(/ +/,$line);
165 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
167 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
168 &downloadrulesfile
();
170 $return = `cat /var/tmp/log 2>/dev/null`;
174 if ($return =~ "ERROR") {
175 $errormessage = "<br /><pre>".$return."</pre>";
177 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 &");
186 $checked{'ENABLE_SNORT'}{'off'} = '';
187 $checked{'ENABLE_SNORT'}{'on'} = '';
188 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
189 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
190 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
191 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
192 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
193 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
194 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
195 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
196 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
197 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
198 $selected{'RULES'}{'nothing'} = '';
199 $selected{'RULES'}{'community'} = '';
200 $selected{'RULES'}{'emerging'} = '';
201 $selected{'RULES'}{'registered'} = '';
202 $selected{'RULES'}{'subscripted'} = '';
203 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
205 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
207 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
209 <style type="text/css">
216 background-color: $color{'color22'};
220 background-color: $color{'color20'};
223 border: double #FF0000;
224 background-color: #DCDCDC;
231 &Header
::openbigbox
('100%', 'left', '', $errormessage);
235 # &Header::openbox('100%', 'left', 'DEBUG');
236 # my $debugCount = 0;
237 # foreach my $line (sort keys %snortsettings) {
238 # print "$line = $snortsettings{$line}<br />\n";
241 # print " Count: $debugCount\n";
242 # &Header::closebox();
247 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
248 print "<class name='base'>$errormessage\n";
249 print " </class>\n";
253 my $return = `pidof oinkmaster.pl -x`;
256 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
260 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
262 $Lang::tr{'snort working'}
263 <tr><td colspan='2' align='center'>
264 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
265 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
267 <tr><td colspan='2' align='left'><pre>
269 my @output = `tail -20 /var/tmp/log`;
278 &Header
::closebigbox
();
279 &Header
::closepage
();
284 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
286 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
287 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
290 if ($netsettings{'BLUE_DEV'} ne '') {
291 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
293 if ($netsettings{'ORANGE_DEV'} ne '') {
294 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
296 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
304 <td><b>$Lang::tr{'ids rules update'}</b></td>
307 <td><select name='RULES'>
308 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
309 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
310 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
311 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
312 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
318 $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 />
319 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
323 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
326 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
329 if ( -e
"/var/tmp/snortrules.tar.gz"){
330 my @Info = stat("/var/tmp/snortrules.tar.gz");
331 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
333 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
341 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
348 if ($results ne '') {
354 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
355 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
356 # Output display table for rule files
357 print "<table width='100%'><tr><td valign='top'><table>";
359 print "<form method='post'>";
362 my $ruledisplaycnt = 1;
363 my $rulecnt = keys %snortrules;
365 $rulecnt = $rulecnt / 2;
367 # Loop over each rule file
368 foreach my $rulefile (sort keys(%snortrules)) {
369 my $rulechecked = '';
371 # Hide inkompatible Block rules
372 if ($rulefile =~'-BLOCK.rules') {
376 # Check if reached half-way through rule file rules to start new column
377 if ($ruledisplaycnt > $rulecnt) {
378 print "</table></td><td valign='top'><table>";
382 # Check if rule file is enabled
383 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
384 $rulechecked = 'CHECKED';
387 # Create rule file link, vars array, and display flag
388 my $rulefilelink = "?RULEFILE=$rulefile";
389 my $rulefiletoclose = '';
391 my $displayrulefilerules = 0;
393 # Check for passed in query string
394 if ($ENV{'QUERY_STRING'}) {
396 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
399 foreach $value (@queryvars) {
400 # Split out var pairs
401 ($var, $linkedrulefile) = split(/=/, $value);
403 # Check if var is 'RULEFILE'
404 if ($var eq 'RULEFILE') {
405 # Check if rulefile equals linkedrulefile
406 if ($rulefile eq $linkedrulefile) {
408 $displayrulefilerules = 1;
410 # Strip out rulefile from rulefilelink
411 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
413 # Add linked rule file to rulefilelink
414 $rulefilelink .= "&RULEFILE=$linkedrulefile";
420 # Strip out extra & & ? from rulefilelink
421 $rulefilelink =~ s/^\?\&/\?/i;
423 # Check for a single '?' and replace with page for proper link display
424 if ($rulefilelink eq '?') {
425 $rulefilelink = "ids.cgi";
428 # Output rule file name and checkbox
429 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
430 print "<tr><td class='base' valign='top'>";
432 # Check for empty 'Description'
433 if ($snortrules{$rulefile}{'Description'} eq '') {
434 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
436 # Output rule file 'Description'
437 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
440 # Check for display flag
441 if ($displayrulefilerules) {
442 # Rule file definition rule display
443 print "<tr><td class='base' valign='top'><table border='0'><tr>";
446 my $ruledefdisplaycnt = 0;
447 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
449 $ruledefcnt = $ruledefcnt / 2;
451 # Loop over rule file rules
452 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
454 my $ruledefchecked = '';
456 # If have display 2 rules, start new row
457 if (($ruledefdisplaycnt % 2) == 0) {
459 $ruledefdisplaycnt = 0;
462 # Check for rules state
463 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
464 $ruledefchecked = 'CHECKED';
467 # Create rule file rule's checkbox
468 $checkboxname = "SNORT_RULE_$rulefile";
469 $checkboxname .= "_$ruledef";
470 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
473 $ruledefdisplaycnt++;
476 # If do not have second rule for row, create empty cell
477 if (($ruledefdisplaycnt % 2) != 0) {
478 print "<td class='base'></td>";
481 # Close display table
482 print "</tr></table></td></tr>";
485 # Close display table
488 # Increment ruledisplaycnt
491 print "</td></tr></table></td></tr></table>";
495 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
496 <!-- space for future online help link -->
506 &Header
::closebigbox
();
507 &Header
::closepage
();
510 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
511 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
515 sub downloadrulesfile
{
519 unlink("/var/tmp/log");
521 unless (-e
"${General::swroot}/red/active") {
522 $errormessage = $Lang::tr
{'could not download latest updates'};
526 my %proxysettings=();
527 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
529 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
530 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
534 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");
536 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
540 sub readrulesfile
($) {
541 my $rulefile = shift;
543 # Open rule file and read in contents
544 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
546 # Store file content in an array.
547 my @lines = <RULEFILE
>;
552 # Loop over rule file contents
553 foreach my $line (@lines) {
554 # Remove whitespaces.
558 next if ($line =~ /^\s*$/);
564 # Gather rule sid and message from the ruleline.
565 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
569 # Check if a rule has been found.
571 # Add rule to the snortrules hash.
572 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
574 # Grab status of the rule. Check if ruleline starts with a "dash".
575 if ($line =~ /^\#/) {
576 # If yes, the rule is disabled.
577 $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
579 # Otherwise the rule is enabled.
580 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";