]>
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, '');
210 // Tiny java script function to show/hide the rules
211 // of a given category.
212 function showhide(tblname) {
213 \$("#" + tblname).toggle();
220 <style type="text/css">
227 background-color: $color{'color22'};
231 background-color: $color{'color20'};
234 border: double #FF0000;
235 background-color: #DCDCDC;
242 &Header
::openbigbox
('100%', 'left', '', $errormessage);
246 # &Header::openbox('100%', 'left', 'DEBUG');
247 # my $debugCount = 0;
248 # foreach my $line (sort keys %snortsettings) {
249 # print "$line = $snortsettings{$line}<br />\n";
252 # print " Count: $debugCount\n";
253 # &Header::closebox();
258 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
259 print "<class name='base'>$errormessage\n";
260 print " </class>\n";
264 my $return = `pidof oinkmaster.pl -x`;
267 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
271 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
273 $Lang::tr{'snort working'}
274 <tr><td colspan='2' align='center'>
275 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
276 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
278 <tr><td colspan='2' align='left'><pre>
280 my @output = `tail -20 /var/tmp/log`;
289 &Header
::closebigbox
();
290 &Header
::closepage
();
295 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
297 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
298 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
301 if ($netsettings{'BLUE_DEV'} ne '') {
302 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
304 if ($netsettings{'ORANGE_DEV'} ne '') {
305 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
307 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
315 <td><b>$Lang::tr{'ids rules update'}</b></td>
318 <td><select name='RULES'>
319 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
320 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
321 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
322 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
323 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
329 $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 />
330 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
334 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
337 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
340 if ( -e
"/var/tmp/snortrules.tar.gz"){
341 my @Info = stat("/var/tmp/snortrules.tar.gz");
342 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
344 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
352 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
359 if ($results ne '') {
365 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
366 # Output display table for rule files
367 print "<table width='100%'>\n";
369 # Local variable required for java script to show/hide
370 # rules of a rulefile.
371 my $rulesetcount = 1;
373 # Loop over each rule file
374 foreach my $rulefile (sort keys(%snortrules)) {
375 my $rulechecked = '';
377 # Check if rule file is enabled
378 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
379 $rulechecked = 'CHECKED';
382 # Table and rows for the rule files.
384 print"<td class='base' width='5%'>\n";
385 print"<input type='checkbox' name='SNORT_RULE_$rulefile' $rulechecked>\n";
387 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
388 print"<td class='base' width='5%' align='right'>\n";
389 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
393 # Rows which will be hidden per default and will contain the single rules.
394 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
395 print"<td colspan='3'>\n";
402 # New table for the single rules.
403 print "<table width='100%'>\n";
405 # Loop over rule file rules
406 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
408 my $ruledefchecked = '';
410 # If 2 rules have been displayed, start a new row
411 if (($lines % 2) == 0) {
414 # Increase rows by once.
420 $col="bgcolor='$color{'color20'}'";
422 $col="bgcolor='$color{'color22'}'";
426 if ($snortrules{$rulefile}{$sid}{'State'} eq 'Enabled') {
427 $ruledefchecked = 'CHECKED';
430 # Create rule checkbox and display rule description
431 print "<td class='base' width='5%' align='right' $col>\n";
432 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
434 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
436 # Increment rule count
440 # If do not have a second rule for row, create empty cell
441 if (($lines % 2) != 0) {
442 print "<td class='base'></td>";
445 # Close display table
446 print "</tr></table></td></tr>";
448 # Finished whith the rule file, increase count.
452 # Close display table
458 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}'>
459 <!-- space for future online help link -->
467 &Header
::closebigbox
();
468 &Header
::closepage
();
471 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
472 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
476 sub downloadrulesfile
{
480 unlink("/var/tmp/log");
482 unless (-e
"${General::swroot}/red/active") {
483 $errormessage = $Lang::tr
{'could not download latest updates'};
487 my %proxysettings=();
488 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
490 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
491 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
495 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");
497 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
501 sub readrulesfile
($) {
502 my $rulefile = shift;
504 # Open rule file and read in contents
505 open(RULEFILE
, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
507 # Store file content in an array.
508 my @lines = <RULEFILE
>;
513 # Loop over rule file contents
514 foreach my $line (@lines) {
515 # Remove whitespaces.
519 next if ($line =~ /^\s*$/);
525 # Gather rule sid and message from the ruleline.
526 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
530 # Check if a rule has been found.
532 # Add rule to the snortrules hash.
533 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
535 # Grab status of the rule. Check if ruleline starts with a "dash".
536 if ($line =~ /^\#/) {
537 # If yes, the rule is disabled.
538 $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
540 # Otherwise the rule is enabled.
541 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";