Changed snort last update time to perl function.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2005-2010 IPFire Team #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27 use File::Copy;
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();}
34
35 $a = new CGI;
36
37 my %color = ();
38 my %mainsettings = ();
39 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
40 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
41
42 my %snortsettings=();
43 my %checked=();
44 my %selected=();
45 my %netsettings=();
46 our $errormessage = '';
47 our $results = '';
48 our $tempdir = '';
49 our $url='';
50 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
51
52 &Header::showhttpheaders();
53
54 $snortsettings{'ENABLE_SNORT'} = 'off';
55 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
56 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
57 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
58 $snortsettings{'ENABLE_GUARDIAN'} = 'off';
59 $snortsettings{'GUARDIAN_INTERFACE'} = `cat /var/ipfire/red/iface`;
60 $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'} = '1';
61 $snortsettings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
62 $snortsettings{'GUARDIAN_ALERTFILE'} = '/var/log/snort/alert';
63 $snortsettings{'GUARDIAN_IGNOREFILE'} = '/var/ipfire/guardian/guardian.ignore';
64 $snortsettings{'GUARDIAN_TARGETFILE'} = '/var/ipfire/guardian/guardian.target';
65 $snortsettings{'GUARDIAN_TIMELIMIT'} = '86400';
66 $snortsettings{'ACTION'} = '';
67 $snortsettings{'ACTION2'} = '';
68 $snortsettings{'RULES'} = '';
69 $snortsettings{'OINKCODE'} = '';
70 $snortsettings{'INSTALLDATE'} = '';
71 $snortsettings{'FILE'} = '';
72 $snortsettings{'UPLOAD'} = '';
73
74 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
75
76 ####################### Added for snort rules control #################################
77 my $snortrulepath; # change to "/etc/snort/rules" - maniac
78 my @snortconfig;
79 my $restartsnortrequired = 0;
80 my %snortrules;
81 my $rule = '';
82 my $table1colour = '';
83 my $table2colour = '';
84 my $var = '';
85 my $value = '';
86 my $tmp = '';
87 my $linkedrulefile = '';
88 my $border = '';
89 my $checkboxname = '';
90
91 if (-e "/etc/snort/snort.conf") {
92
93
94 # Open snort.conf file, read it in, close it, and re-open for writing
95 open(FILE, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
96 @snortconfig = <FILE>;
97 close(FILE);
98 open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
99
100 my @rules = `cd /etc/snort/rules/ && ls *.rules`; # With this loop the rule might be display with correct rulepath set
101 foreach (@rules) {
102 chomp $_;
103 my $temp = join(";",@snortconfig);
104 if ( $temp =~ /$_/ ){next;}
105 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
106 }
107
108 # Loop over each line
109 foreach my $line (@snortconfig) {
110 # Trim the line
111 chomp $line;
112
113 # Check for a line with .rules
114 if ($line =~ /\.rules$/) {
115 # Parse out rule file name
116 $rule = $line;
117 $rule =~ s/\$RULE_PATH\///i;
118 $rule =~ s/ ?include ?//i;
119 $rule =~ s/\#//i;
120 my $snortrulepathrule = "$snortrulepath/$rule";
121
122 # Open rule file and read in contents
123 open(RULEFILE, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
124 my @snortrulefile = <RULEFILE>;
125 close(RULEFILE);
126 open(RULEFILE, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
127
128 # Local vars
129 my $dashlinecnt = 0;
130 my $desclook = 1;
131 my $snortruledesc = '';
132 my %snortruledef = ();
133 my $rulecnt = 1;
134
135 # Loop over rule file contents
136 foreach my $ruleline (@snortrulefile) {
137 chomp $ruleline;
138
139 # If still looking for a description
140 if ($desclook) {
141 # If line does not start with a # anymore, then done looking for a description
142 if ($ruleline !~ /^\#/) {
143 $desclook = 0;
144 }
145
146 # If see more than one dashed line, (start to) create rule file description
147 if ($dashlinecnt > 1) {
148 # Check for a line starting with a #
149 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
150 # Create tempruleline
151 my $tempruleline = $ruleline;
152
153 # Strip off # and clean up line
154 $tempruleline =~ s/\# ?//i;
155
156 # Check for part of a description
157 if ($snortruledesc eq '') {
158 $snortruledesc = $tempruleline;
159 } else {
160 $snortruledesc .= " $tempruleline";
161 }
162 } else {
163 # Must be done
164 $desclook = 0;
165 }
166 }
167
168 # If have a dashed line, increment count
169 if ($ruleline =~ /\# ?\-+/) {
170 $dashlinecnt++;
171 }
172 } else {
173 # Parse out rule file rule's message for display
174 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
175 my $msg = '';
176 $msg = $1;
177 $msg =~ s/msg\:\"//i;
178 $msg =~ s/\";//i;
179 $snortruledef{$rulecnt}{'Description'} = $msg;
180
181 # Check for 'Save' and rule file displayed in query string
182 if (($snortsettings{'ACTION'} eq $Lang::tr{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
183 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
184 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
185 $restartsnortrequired = 1;
186 }
187
188 # Strip out leading # from rule line
189 $ruleline =~ s/\# ?//i;
190
191 # Check if it does not exists (which means it is disabled), append a #
192 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
193 $ruleline = "#"." $ruleline";
194 }
195 }
196
197 # Check if ruleline does not begin with a #, so it is enabled
198 if ($ruleline !~ /^\#/) {
199 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
200 } else {
201 # Otherwise it is disabled
202 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
203 }
204 }
205 }
206
207 # Print ruleline to RULEFILE
208 print RULEFILE "$ruleline\n";
209 }
210
211 # Close RULEFILE
212 close(RULEFILE);
213
214 # Check for 'Save'
215 if ($snortsettings{'ACTION'} eq $Lang::tr{'update'}) {
216 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
217 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
218 $restartsnortrequired = 1;
219 }
220
221 # Strip out leading # from rule line
222 $line =~ s/\# ?//i;
223
224 # Check if it does not exists (which means it is disabled), append a #
225 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
226 $line = "# $line";
227 }
228
229 }
230
231 # Check for rule state
232 if ($line =~ /^\#/) {
233 $snortrules{$rule}{"State"} = "Disabled";
234 } else {
235 $snortrules{$rule}{"State"} = "Enabled";
236 }
237
238 # Set rule description
239 $snortrules{$rule}{"Description"} = $snortruledesc;
240
241 # Loop over sorted rules
242 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
243 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
244 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
245 }
246
247 $snortruledesc = '';
248 print FILE "$line\n";
249 } elsif ($line =~ /var RULE_PATH/) {
250 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
251 print FILE "$line\n";
252 } else {
253 print FILE "$line\n";
254 }
255 }
256 close(FILE);
257
258 if ($restartsnortrequired) {
259 system('/usr/local/bin/snortctrl restart >/dev/null');
260 }
261 }
262
263 ####################### End added for snort rules control #################################
264
265 if ($snortsettings{'RULES'} eq 'subscripted') {
266 #$url="http://dl.snort.org/sub-rules/snortrules-snapshot-2.8_s.tar.gz?oink_code=$snortsettings{'OINKCODE'}";
267 $url=" http://www.snort.org/reg-rules/snortrules-snapshot-2902_s.tar.gz/$snortsettings{'OINKCODE'}";
268 #$url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.8_s.tar.gz";
269 } elsif ($snortsettings{'RULES'} eq 'registered') {
270 #$url="http://dl.snort.org/reg-rules/snortrules-snapshot-2.8.tar.gz?oink_code=$snortsettings{'OINKCODE'}";
271 $url=" http://www.snort.org/reg-rules/snortrules-snapshot-2902.tar.gz/$snortsettings{'OINKCODE'}";
272 #$url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.8.tar.gz";
273 } else {
274 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
275 }
276
277 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" )
278 {
279 $errormessage = $Lang::tr{'invalid input for oink code'} unless (
280 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
281 ($snortsettings{'RULESTYPE'} eq 'nothing' ) ||
282 ($snortsettings{'RULESTYPE'} eq 'community' ));
283
284 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
285 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
286 {
287 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
288 } else {
289 unlink "${General::swroot}/snort/enable";
290 }
291 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
292 {
293 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
294 } else {
295 unlink "${General::swroot}/snort/enable_green";
296 }
297 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
298 {
299 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
300 } else {
301 unlink "${General::swroot}/snort/enable_blue";
302 }
303 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
304 {
305 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
306 } else {
307 unlink "${General::swroot}/snort/enable_orange";
308 }
309 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
310 {
311 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
312 } else {
313 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
314 }
315 if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on')
316 {
317 system ('/usr/bin/touch', "${General::swroot}/guardian/enable");
318 } else {
319 unlink "${General::swroot}/guardian/enable";
320 }
321
322 system('/usr/local/bin/snortctrl restart >/dev/null');
323
324 } elsif ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "guardian" ){
325 foreach my $key (keys %snortsettings){
326 if ( $key !~ /^GUARDIAN/ ){
327 delete $snortsettings{$key};
328 }
329 }
330 &General::writehashpart("${General::swroot}/snort/settings", \%snortsettings);
331 open(IGNOREFILE, ">$snortsettings{'GUARDIAN_IGNOREFILE'}") or die "Unable to write guardian ignore file $snortsettings{'GUARDIAN_IGNOREFILE'}";
332 print IGNOREFILE $snortsettings{'GUARDIAN_IGNOREFILE_CONTENT'};
333 close(IGNOREFILE);
334 open(GUARDIAN, ">/var/ipfire/guardian/guardian.conf") or die "Unable to write guardian conf /var/ipfire/guardian/guardian.conf";
335 print GUARDIAN <<END
336 Interface $snortsettings{'GUARDIAN_INTERFACE'}
337 HostGatewayByte $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'}
338 LogFile $snortsettings{'GUARDIAN_LOGFILE'}
339 AlertFile $snortsettings{'GUARDIAN_ALERTFILE'}
340 IgnoreFile $snortsettings{'GUARDIAN_IGNOREFILE'}
341 TargetFile $snortsettings{'GUARDIAN_TARGETFILE'}
342 TimeLimit $snortsettings{'GUARDIAN_TIMELIMIT'}
343 END
344 ;
345 close(GUARDIAN);
346 system('/usr/local/bin/snortctrl restart >/dev/null');
347 }
348 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
349 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
350
351 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} || $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'}) {
352
353 my @df = `/bin/df -B M /var`;
354 foreach my $line (@df) {
355 next if $line =~ m/^Filesystem/;
356 my $return;
357
358 if ($line =~ m/dev/ ) {
359 $line =~ m/^.* (\d+)M.*$/;
360 my @temp = split(/ +/,$line);
361 if ($1<300) {
362 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
363 } else {
364
365 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} ){
366
367 &downloadrulesfile();
368 sleep(3);
369 $return = `cat /var/tmp/log 2>/dev/null`;
370
371 } elsif ( $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'} ) {
372 my $upload = $a->param("UPLOAD");
373 open UPLOADFILE, ">/var/tmp/snortrules.tar.gz";
374 binmode $upload;
375 while ( <$upload> ) {
376 print UPLOADFILE;
377 }
378 close UPLOADFILE;
379 }
380
381 if ($return =~ "ERROR"){
382 $errormessage = "<br /><pre>".$return."</pre>";
383 } else {
384 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 &");
385 sleep(2);
386 }
387 }
388 }
389 }
390 }
391
392 $checked{'ENABLE_SNORT'}{'off'} = '';
393 $checked{'ENABLE_SNORT'}{'on'} = '';
394 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
395 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
396 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
397 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
398 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
399 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
400 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
401 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
402 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
403 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
404 $checked{'ENABLE_GUARDIAN'}{'off'} = '';
405 $checked{'ENABLE_GUARDIAN'}{'on'} = '';
406 $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'";
407 $selected{'RULES'}{'nothing'} = '';
408 $selected{'RULES'}{'community'} = '';
409 $selected{'RULES'}{'registered'} = '';
410 $selected{'RULES'}{'subscripted'} = '';
411 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
412
413 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
414
415 ####################### Added for snort rules control #################################
416 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
417 print <<END
418 <STYLE TYPE="text/css">
419 <!--
420 .section {
421 border: groove;
422 }
423 .row1color {
424 border: ridge;
425 background-color: $color{'color22'};
426 }
427 .row2color {
428 border: ridge;
429 background-color: $color{'color20'};
430 }
431 .rowselected {
432 border: double #FF0000;
433 background-color: #DCDCDC;
434 }
435 -->
436 </STYLE>
437 END
438 ;
439 ####################### End added for snort rules control #################################
440
441 &Header::openbigbox('100%', 'left', '', $errormessage);
442
443 ###############
444 # DEBUG DEBUG
445 # &Header::openbox('100%', 'left', 'DEBUG');
446 # my $debugCount = 0;
447 # foreach my $line (sort keys %snortsettings) {
448 # print "$line = $snortsettings{$line}<br />\n";
449 # $debugCount++;
450 # }
451 # print "&nbsp;Count: $debugCount\n";
452 # &Header::closebox();
453 # DEBUG DEBUG
454 ###############
455
456 if ($errormessage) {
457 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
458 print "<class name='base'>$errormessage\n";
459 print "&nbsp;</class>\n";
460 &Header::closebox();
461 }
462
463 my $return = `pidof oinkmaster.pl -x`;
464 chomp($return);
465 if ($return) {
466 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
467 print <<END;
468 <table>
469 <tr><td>
470 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
471 <td>
472 $Lang::tr{'snort working'}
473 <tr><td colspan='2' align='center'>
474 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
475 <input type='image' alt='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
476 </form>
477 <tr><td colspan='2' align='left'><pre>
478 END
479 my @output = `tail -20 /var/tmp/log`;
480 foreach (@output) {
481 print "$_";
482 }
483 print <<END;
484 </pre>
485 </table>
486 END
487 &Header::closebox();
488 &Header::closebigbox();
489 &Header::closepage();
490 exit;
491 refreshpage();
492 }
493
494 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'});
495 print <<END
496 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
497 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
498 END
499 ;
500 if ($netsettings{'BLUE_DEV'} ne '') {
501 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
502 }
503 if ($netsettings{'ORANGE_DEV'} ne '') {
504 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
505 }
506 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
507 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
508 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian";
509 }
510
511 print <<END
512 </td></tr>
513 <tr>
514 <td><hr /></td>
515 </tr>
516 <tr>
517 <td><b>$Lang::tr{'ids rules update'}</b></td>
518 </tr>
519 <tr>
520 <td><select name='RULES'>
521 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
522 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
523 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
524 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
525 </select>
526 </td>
527 </tr>
528 <tr>
529 <td><br />
530 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/signup' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
531 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
532 </td>
533 </tr>
534 <tr>
535 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
536 </tr>
537 <tr>
538 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
539 END
540 ;
541 if ( -e "/var/tmp/snortrules.tar.gz"){
542 my @Info = stat("/var/tmp/snortrules.tar.gz");
543 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
544 }
545 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
546
547 print <<END
548 </tr>
549 </table>
550 <hr />
551 <table width='100%'>
552 <tr>
553 <td align='center'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
554 </tr>
555 </table>
556 </form>
557 END
558 ;
559
560 if ($results ne '') {
561 print "$results";
562 }
563
564 &Header::closebox();
565
566 ####################### Added for guardian control ####################################
567 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
568 &Header::openbox('100%', 'LEFT', $Lang::tr{'guardian configuration'});
569 print <<END
570 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
571 <tr><td align='left' width='40%'>$Lang::tr{'guardian interface'}</td><td align='left'><input type='text' name='GUARDIAN_INTERFACE' value='$snortsettings{'GUARDIAN_INTERFACE'}' size="30" /></td></tr>
572 <tr><td align='left' width='40%'>$Lang::tr{'guardian timelimit'}</td><td align='left'><input type='text' name='GUARDIAN_TIMELIMIT' value='$snortsettings{'GUARDIAN_TIMELIMIT'}' size="30" /></td></tr>
573 <tr><td align='left' width='40%'>$Lang::tr{'guardian logfile'}</td><td align='left'><input type='text' name='GUARDIAN_LOGFILE' value='$snortsettings{'GUARDIAN_LOGFILE'}' size="30" /></td></tr>
574 <tr><td align='left' width='40%'>$Lang::tr{'guardian alertfile'}</td><td align='left'><input type='text' name='GUARDIAN_ALERTFILE' value='$snortsettings{'GUARDIAN_ALERTFILE'}' size="30" /></td></tr>
575 <tr><td align='left' width='40%'>$Lang::tr{'guardian ignorefile'}</td><td align='left'><textarea name='GUARDIAN_IGNOREFILE_CONTENT' cols='32' rows='6' wrap='off'>
576 END
577 ;
578 print `cat /var/ipfire/guardian/guardian.ignore`;
579 print <<END
580 </textarea></td></tr>
581 <tr><td align='center' colspan='2'><input type='hidden' name='ACTION2' value='guardian' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
582 </table>
583 </form>
584 END
585 ;
586 &Header::closebox();
587 }
588
589
590
591
592 ####################### Added for snort rules control #################################
593 if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
594 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
595 # Output display table for rule files
596 print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
597
598 print "<form method='post'>";
599
600 # Local vars
601 my $ruledisplaycnt = 1;
602 my $rulecnt = keys %snortrules;
603 $rulecnt++;
604 $rulecnt = $rulecnt / 2;
605
606 # Loop over each rule file
607 foreach my $rulefile (sort keys(%snortrules)) {
608 my $rulechecked = '';
609
610 # Hide inkompatible Block rules
611 if ($rulefile =~'-BLOCK.rules') {
612 next;
613 }
614
615 # Check if reached half-way through rule file rules to start new column
616 if ($ruledisplaycnt > $rulecnt) {
617 print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
618 $ruledisplaycnt = 0;
619 }
620
621 # Check if rule file is enabled
622 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
623 $rulechecked = 'CHECKED';
624 }
625
626 # Create rule file link, vars array, and display flag
627 my $rulefilelink = "?RULEFILE=$rulefile";
628 my $rulefiletoclose = '';
629 my @queryvars = ();
630 my $displayrulefilerules = 0;
631
632 # Check for passed in query string
633 if ($ENV{'QUERY_STRING'}) {
634 # Split out vars
635 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
636
637 # Loop over values
638 foreach $value (@queryvars) {
639 # Split out var pairs
640 ($var, $linkedrulefile) = split(/=/, $value);
641
642 # Check if var is 'RULEFILE'
643 if ($var eq 'RULEFILE') {
644 # Check if rulefile equals linkedrulefile
645 if ($rulefile eq $linkedrulefile) {
646 # Set display flag
647 $displayrulefilerules = 1;
648
649 # Strip out rulefile from rulefilelink
650 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
651 } else {
652 # Add linked rule file to rulefilelink
653 $rulefilelink .= "&RULEFILE=$linkedrulefile";
654 }
655 }
656 }
657 }
658
659 # Strip out extra & & ? from rulefilelink
660 $rulefilelink =~ s/^\?\&/\?/i;
661
662 # Check for a single '?' and replace with page for proper link display
663 if ($rulefilelink eq '?') {
664 $rulefilelink = "ids.cgi";
665 }
666
667 # Output rule file name and checkbox
668 print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
669 print "<TR><TD CLASS='base' VALIGN='TOP'>";
670
671 # Check for empty 'Description'
672 if ($snortrules{$rulefile}{'Description'} eq '') {
673 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
674 } else {
675 # Output rule file 'Description'
676 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
677 }
678
679 # Check for display flag
680 if ($displayrulefilerules) {
681 # Rule file definition rule display
682 print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
683
684 # Local vars
685 my $ruledefdisplaycnt = 0;
686 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
687 $ruledefcnt++;
688 $ruledefcnt = $ruledefcnt / 2;
689
690 # Loop over rule file rules
691 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
692 # Local vars
693 my $ruledefchecked = '';
694
695 # If have display 2 rules, start new row
696 if (($ruledefdisplaycnt % 2) == 0) {
697 print "</TR><TR>";
698 $ruledefdisplaycnt = 0;
699 }
700
701 # Check for rules state
702 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
703 $ruledefchecked = 'CHECKED';
704 }
705
706 # Create rule file rule's checkbox
707 $checkboxname = "SNORT_RULE_$rulefile";
708 $checkboxname .= "_$ruledef";
709 print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
710
711 # Increment count
712 $ruledefdisplaycnt++;
713 }
714
715 # If do not have second rule for row, create empty cell
716 if (($ruledefdisplaycnt % 2) != 0) {
717 print "<TD CLASS='base'></TD>";
718 }
719
720 # Close display table
721 print "</TR></TABLE></TD></TR>";
722 }
723
724 # Close display table
725 print "</TABLE>";
726
727 # Increment ruledisplaycnt
728 $ruledisplaycnt++;
729 }
730 print "</TD></TR></TABLE></TD></TR></TABLE>";
731 print <<END
732 <table width='100%'>
733 <tr>
734 <td width='33%'>&nbsp;</td>
735 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
736 <td width='33%'>
737 &nbsp; <!-- space for future online help link -->
738 </td>
739 </tr>
740 </table>
741 </form>
742 END
743 ;
744 &Header::closebox();
745 }
746
747 ####################### End added for snort rules control #################################
748 &Header::closebigbox();
749 &Header::closepage();
750
751 sub downloadrulesfile {
752 my $peer;
753 my $peerport;
754
755 unlink("/var/tmp/log");
756
757 unless (-e "${General::swroot}/red/active") {
758 $errormessage = $Lang::tr{'could not download latest updates'};
759 return undef;
760 }
761
762 my %proxysettings=();
763 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
764
765 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
766 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
767 }
768
769 if ($peer) {
770 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 --no-check-certificate --output-document=/var/tmp/snortrules.tar.gz $url");
771 } else {
772 system("wget -r --no-check-certificate -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
773 }
774 }