]>
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 ####################### Added for snort rules control #################################
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");
103 ####################### End added for snort rules control #################################
105 if ($snortsettings{'OINKCODE'} ne "") {
106 $errormessage = $Lang::tr
{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
109 if (!$errormessage) {
110 if ($snortsettings{'RULES'} eq 'subscripted') {
111 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
112 } elsif ($snortsettings{'RULES'} eq 'registered') {
113 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
114 } elsif ($snortsettings{'RULES'} eq 'community') {
115 $url=" https://www.snort.org/rules/community";
117 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
120 if ($snortsettings{'ACTION'} eq $Lang::tr
{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
121 &General
::writehash
("${General::swroot}/snort/settings", \
%snortsettings);
122 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
124 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
126 unlink "${General::swroot}/snort/enable";
128 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
130 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
132 unlink "${General::swroot}/snort/enable_green";
134 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
136 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
138 unlink "${General::swroot}/snort/enable_blue";
140 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
142 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
144 unlink "${General::swroot}/snort/enable_orange";
146 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
148 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
150 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
153 system('/usr/local/bin/snortctrl restart >/dev/null');
156 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
157 &General
::readhash
("${General::swroot}/snort/settings", \
%snortsettings);
159 if ($snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
160 my @df = `/bin/df -B M /var`;
161 foreach my $line (@df) {
162 next if $line =~ m/^Filesystem/;
165 if ($line =~ m/dev/ ) {
166 $line =~ m/^.* (\d+)M.*$/;
167 my @temp = split(/ +/,$line);
169 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
171 if ( $snortsettings{'ACTION'} eq $Lang::tr
{'download new ruleset'}) {
172 &downloadrulesfile
();
174 $return = `cat /var/tmp/log 2>/dev/null`;
178 if ($return =~ "ERROR") {
179 $errormessage = "<br /><pre>".$return."</pre>";
181 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 &");
190 $checked{'ENABLE_SNORT'}{'off'} = '';
191 $checked{'ENABLE_SNORT'}{'on'} = '';
192 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
193 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
194 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
195 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
196 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
197 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
198 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
199 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
200 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
201 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
202 $selected{'RULES'}{'nothing'} = '';
203 $selected{'RULES'}{'community'} = '';
204 $selected{'RULES'}{'emerging'} = '';
205 $selected{'RULES'}{'registered'} = '';
206 $selected{'RULES'}{'subscripted'} = '';
207 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
209 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
211 ####################### Added for snort rules control #################################
212 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
214 <style type="text/css">
221 background-color: $color{'color22'};
225 background-color: $color{'color20'};
228 border: double #FF0000;
229 background-color: #DCDCDC;
235 ####################### End added for snort rules control #################################
237 &Header
::openbigbox
('100%', 'left', '', $errormessage);
241 # &Header::openbox('100%', 'left', 'DEBUG');
242 # my $debugCount = 0;
243 # foreach my $line (sort keys %snortsettings) {
244 # print "$line = $snortsettings{$line}<br />\n";
247 # print " Count: $debugCount\n";
248 # &Header::closebox();
253 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
254 print "<class name='base'>$errormessage\n";
255 print " </class>\n";
259 my $return = `pidof oinkmaster.pl -x`;
262 &Header
::openbox
( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
266 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />
268 $Lang::tr{'snort working'}
269 <tr><td colspan='2' align='center'>
270 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
271 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
273 <tr><td colspan='2' align='left'><pre>
275 my @output = `tail -20 /var/tmp/log`;
284 &Header
::closebigbox
();
285 &Header
::closepage
();
290 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
292 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
293 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
296 if ($netsettings{'BLUE_DEV'} ne '') {
297 print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
299 if ($netsettings{'ORANGE_DEV'} ne '') {
300 print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
302 print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
310 <td><b>$Lang::tr{'ids rules update'}</b></td>
313 <td><select name='RULES'>
314 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
315 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
316 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
317 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
318 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
324 $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 />
325 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
329 <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
332 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
335 if ( -e
"/var/tmp/snortrules.tar.gz"){
336 my @Info = stat("/var/tmp/snortrules.tar.gz");
337 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
339 print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
347 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
354 if ($results ne '') {
360 ####################### Added for snort rules control #################################
361 if ( -e
"${General::swroot}/snort/enable" || -e
"${General::swroot}/snort/enable_green" || -e
"${General::swroot}/snort/enable_blue" || -e
"${General::swroot}/snort/enable_orange" ) {
362 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
363 # Output display table for rule files
364 print "<table width='100%'><tr><td valign='top'><table>";
366 print "<form method='post'>";
369 my $ruledisplaycnt = 1;
370 my $rulecnt = keys %snortrules;
372 $rulecnt = $rulecnt / 2;
374 # Loop over each rule file
375 foreach my $rulefile (sort keys(%snortrules)) {
376 my $rulechecked = '';
378 # Hide inkompatible Block rules
379 if ($rulefile =~'-BLOCK.rules') {
383 # Check if reached half-way through rule file rules to start new column
384 if ($ruledisplaycnt > $rulecnt) {
385 print "</table></td><td valign='top'><table>";
389 # Check if rule file is enabled
390 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
391 $rulechecked = 'CHECKED';
394 # Create rule file link, vars array, and display flag
395 my $rulefilelink = "?RULEFILE=$rulefile";
396 my $rulefiletoclose = '';
398 my $displayrulefilerules = 0;
400 # Check for passed in query string
401 if ($ENV{'QUERY_STRING'}) {
403 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
406 foreach $value (@queryvars) {
407 # Split out var pairs
408 ($var, $linkedrulefile) = split(/=/, $value);
410 # Check if var is 'RULEFILE'
411 if ($var eq 'RULEFILE') {
412 # Check if rulefile equals linkedrulefile
413 if ($rulefile eq $linkedrulefile) {
415 $displayrulefilerules = 1;
417 # Strip out rulefile from rulefilelink
418 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
420 # Add linked rule file to rulefilelink
421 $rulefilelink .= "&RULEFILE=$linkedrulefile";
427 # Strip out extra & & ? from rulefilelink
428 $rulefilelink =~ s/^\?\&/\?/i;
430 # Check for a single '?' and replace with page for proper link display
431 if ($rulefilelink eq '?') {
432 $rulefilelink = "ids.cgi";
435 # Output rule file name and checkbox
436 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
437 print "<tr><td class='base' valign='top'>";
439 # Check for empty 'Description'
440 if ($snortrules{$rulefile}{'Description'} eq '') {
441 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
443 # Output rule file 'Description'
444 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
447 # Check for display flag
448 if ($displayrulefilerules) {
449 # Rule file definition rule display
450 print "<tr><td class='base' valign='top'><table border='0'><tr>";
453 my $ruledefdisplaycnt = 0;
454 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
456 $ruledefcnt = $ruledefcnt / 2;
458 # Loop over rule file rules
459 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
461 my $ruledefchecked = '';
463 # If have display 2 rules, start new row
464 if (($ruledefdisplaycnt % 2) == 0) {
466 $ruledefdisplaycnt = 0;
469 # Check for rules state
470 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
471 $ruledefchecked = 'CHECKED';
474 # Create rule file rule's checkbox
475 $checkboxname = "SNORT_RULE_$rulefile";
476 $checkboxname .= "_$ruledef";
477 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
480 $ruledefdisplaycnt++;
483 # If do not have second rule for row, create empty cell
484 if (($ruledefdisplaycnt % 2) != 0) {
485 print "<td class='base'></td>";
488 # Close display table
489 print "</tr></table></td></tr>";
492 # Close display table
495 # Increment ruledisplaycnt
498 print "</td></tr></table></td></tr></table>";
502 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
503 <!-- space for future online help link -->
513 ####################### End added for snort rules control #################################
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'} = "Disabled";
587 # Otherwise the rule is enabled.
588 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";