]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
Updated snort to stable 2.8.5.3.
[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) 2007 Michael Tremer & Christian Schmidt #
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
23 use LWP::UserAgent;
24 use File::Copy;
25 use File::Temp qw/ tempfile tempdir /;
26 use strict;
27
28 # enable only the following on debugging purpose
29 use warnings;
30 use CGI::Carp 'fatalsToBrowser';
31
32 require '/var/ipfire/general-functions.pl';
33 require "${General::swroot}/lang.pl";
34 require "${General::swroot}/header.pl";
35
36 my %color = ();
37 my %mainsettings = ();
38 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
39 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
40
41 my %snortsettings=();
42 my %checked=();
43 my %selected=();
44 my %netsettings=();
45 our $errormessage = '';
46 our $md5 = '0';# not '' to avoid displaying the wrong message when INSTALLMD5 not set
47 our $realmd5 = '';
48 our $results = '';
49 our $tempdir = '';
50 our $url='';
51 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
52
53 &Header::showhttpheaders();
54
55 $snortsettings{'ENABLE_SNORT'} = 'off';
56 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
57 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
58 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
59 $snortsettings{'ENABLE_GUARDIAN'} = 'off';
60 $snortsettings{'GUARDIAN_INTERFACE'} = `cat /var/ipfire/red/iface`;
61 $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'} = '1';
62 $snortsettings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
63 $snortsettings{'GUARDIAN_ALERTFILE'} = '/var/log/snort/alert';
64 $snortsettings{'GUARDIAN_IGNOREFILE'} = '/var/ipfire/guardian/guardian.ignore';
65 $snortsettings{'GUARDIAN_TARGETFILE'} = '/var/ipfire/guardian/guardian.target';
66 $snortsettings{'GUARDIAN_TIMELIMIT'} = '86400';
67 $snortsettings{'ACTION'} = '';
68 $snortsettings{'ACTION2'} = '';
69 $snortsettings{'RULES'} = '';
70 $snortsettings{'OINKCODE'} = '';
71 $snortsettings{'INSTALLDATE'} = '';
72 $snortsettings{'INSTALLMD5'} = '';
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 =~ /^\#/) {
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://dl.snort.org/sub-rules/snortrules-snapshot-2853_s.tar.gz?oink_code=$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://dl.snort.org/reg-rules/snortrules-snapshot-2853.tar.gz?oink_code=$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://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.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
283 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
284 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
285 {
286 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
287 } else {
288 unlink "${General::swroot}/snort/enable";
289 }
290 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
291 {
292 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
293 } else {
294 unlink "${General::swroot}/snort/enable_green";
295 }
296 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
297 {
298 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
299 } else {
300 unlink "${General::swroot}/snort/enable_blue";
301 }
302 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
303 {
304 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
305 } else {
306 unlink "${General::swroot}/snort/enable_orange";
307 }
308 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
309 {
310 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
311 } else {
312 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
313 }
314 if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on')
315 {
316 system ('/usr/bin/touch', "${General::swroot}/guardian/enable");
317 } else {
318 unlink "${General::swroot}/guardian/enable";
319 }
320
321 system('/usr/local/bin/snortctrl restart >/dev/null');
322
323 } elsif ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "guardian" ){
324 open(IGNOREFILE, ">$snortsettings{'GUARDIAN_IGNOREFILE'}") or die "Unable to write guardian ignore file $snortsettings{'GUARDIAN_IGNOREFILE'}";
325 print IGNOREFILE $snortsettings{'IGNOREFILE_CONTENT'};
326 close(IGNOREFILE);
327 open(GUARDIAN, ">/var/ipfire/guardian/guardian.conf") or die "Unable to write guardian conf /var/ipfire/guardian/guardian.conf";
328 print GUARDIAN <<END
329 Interface $snortsettings{'GUARDIAN_INTERFACE'}
330 HostGatewayByte $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'}
331 LogFile $snortsettings{'GUARDIAN_LOGFILE'}
332 AlertFile $snortsettings{'GUARDIAN_ALERTFILE'}
333 IgnoreFile $snortsettings{'GUARDIAN_IGNOREFILE'}
334 TargetFile $snortsettings{'GUARDIAN_TARGETFILE'}
335 TimeLimit $snortsettings{'GUARDIAN_TIMELIMIT'}
336 END
337 ;
338 close(GUARDIAN);
339 system('/usr/local/bin/snortctrl restart >/dev/null');
340 }
341 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
342 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
343
344 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
345 $md5 = &getmd5;
346 if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
347 chomp($md5);
348 my $filename = &downloadrulesfile();
349 if (defined $filename) {
350 # Check MD5sum
351 $realmd5 = `/usr/bin/md5sum $filename`;
352 chomp ($realmd5);
353 $realmd5 =~ s/^(\w+)\s.*$/$1/;
354 if ( $md5 ne $realmd5 ) {
355 $errormessage = "$Lang::tr{'invalid md5sum'} - $md5 - $realmd5";
356 } else {
357 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
358 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`;
359 $results .= "</pre>";
360 }
361 unlink ($filename);
362 }
363 }
364 }
365
366 $checked{'ENABLE_SNORT'}{'off'} = '';
367 $checked{'ENABLE_SNORT'}{'on'} = '';
368 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
369 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
370 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
371 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
372 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
373 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
374 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
375 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
376 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
377 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
378 $checked{'ENABLE_GUARDIAN'}{'off'} = '';
379 $checked{'ENABLE_GUARDIAN'}{'on'} = '';
380 $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'";
381 $selected{'RULES'}{'nothing'} = '';
382 $selected{'RULES'}{'community'} = '';
383 $selected{'RULES'}{'registered'} = '';
384 $selected{'RULES'}{'subscripted'} = '';
385 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
386
387 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
388
389 ####################### Added for snort rules control #################################
390 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
391 print <<END
392 <STYLE TYPE="text/css">
393 <!--
394 .section {
395 border: groove;
396 }
397 .row1color {
398 border: ridge;
399 background-color: $color{'color22'};
400 }
401 .row2color {
402 border: ridge;
403 background-color: $color{'color20'};
404 }
405 .rowselected {
406 border: double #FF0000;
407 background-color: #DCDCDC;
408 }
409 -->
410 </STYLE>
411 END
412 ;
413 ####################### End added for snort rules control #################################
414
415 &Header::openbigbox('100%', 'left', '', $errormessage);
416
417 if ($errormessage) {
418 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
419 print "<class name='base'>$errormessage\n";
420 print "&nbsp;</class>\n";
421 &Header::closebox();
422 }
423
424 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'});
425 print <<END
426 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
427 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
428 END
429 ;
430 if ($netsettings{'BLUE_DEV'} ne '') {
431 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
432 }
433 if ($netsettings{'ORANGE_DEV'} ne '') {
434 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
435 }
436 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
437 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
438 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian";
439 }
440
441 print <<END
442 </td></tr>
443 <tr>
444 <td><hr /></td>
445 </tr>
446 <tr>
447 <td><b>$Lang::tr{'ids rules update'}</b></td>
448 </tr>
449 <tr>
450 <td><select name='RULES'>
451 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
452 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
453 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
454 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
455 </select>
456 </td>
457 </tr>
458 <tr>
459 <td><br />
460 $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 />
461 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
462 </td>
463 </tr>
464 <tr>
465 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
466 </tr>
467 <tr>
468 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
469 END
470 ;
471
472 if ($snortsettings{'INSTALLMD5'} eq $md5) {
473 print "&nbsp;$Lang::tr{'rules already up to date'}</td>";
474 } else {
475 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} && $md5 eq $realmd5 ) {
476 $snortsettings{'INSTALLMD5'} = $realmd5;
477 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
478 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
479 }
480 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
481 }
482 print <<END
483 </tr>
484 </table>
485 <hr />
486 <table width='100%'>
487 <tr>
488 <td align='center'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
489 </tr>
490 </table>
491 </form>
492 END
493 ;
494
495 if ($results ne '') {
496 print "$results";
497 }
498
499 &Header::closebox();
500
501 ####################### Added for guardian control ####################################
502 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
503 &Header::openbox('100%', 'LEFT', $Lang::tr{'guardian configuration'});
504 print <<END
505 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
506 <tr><td align='left' width='40%'>$Lang::tr{'guardian interface'}</td><td align='left'><input type='text' name='INTERFACE' value='$snortsettings{'GUARDIAN_INTERFACE'}' size="30" /></td></tr>
507 <tr><td align='left' width='40%'>$Lang::tr{'guardian timelimit'}</td><td align='left'><input type='text' name='TIMELIMIT' value='$snortsettings{'GUARDIAN_TIMELIMIT'}' size="30" /></td></tr>
508 <tr><td align='left' width='40%'>$Lang::tr{'guardian logfile'}</td><td align='left'><input type='text' name='LOGFILE' value='$snortsettings{'GUARDIAN_LOGFILE'}' size="30" /></td></tr>
509 <tr><td align='left' width='40%'>$Lang::tr{'guardian alertfile'}</td><td align='left'><input type='text' name='ALERTFILE' value='$snortsettings{'GUARDIAN_ALERTFILE'}' size="30" /></td></tr>
510 <tr><td align='left' width='40%'>$Lang::tr{'guardian ignorefile'}</td><td align='left'><textarea name='IGNOREFILE_CONTENT' cols='32' rows='6' wrap='off'>
511 END
512 ;
513 print `cat /var/ipfire/guardian/guardian.ignore`;
514 print <<END
515 </textarea></td></tr>
516 <tr><td align='center' colspan='2'><input type='hidden' name='ACTION2' value='guardian' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
517 </table>
518 </form>
519 END
520 ;
521 &Header::closebox();
522 }
523
524
525
526
527 ####################### Added for snort rules control #################################
528 if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
529 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
530 # Output display table for rule files
531 print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
532
533 print "<form method='post'>";
534
535 # Local vars
536 my $ruledisplaycnt = 1;
537 my $rulecnt = keys %snortrules;
538 $rulecnt++;
539 $rulecnt = $rulecnt / 2;
540
541 # Loop over each rule file
542 foreach my $rulefile (sort keys(%snortrules)) {
543 my $rulechecked = '';
544
545 # Check if reached half-way through rule file rules to start new column
546 if ($ruledisplaycnt > $rulecnt) {
547 print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
548 $ruledisplaycnt = 0;
549 }
550
551 # Check if rule file is enabled
552 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
553 $rulechecked = 'CHECKED';
554 }
555
556 # Create rule file link, vars array, and display flag
557 my $rulefilelink = "?RULEFILE=$rulefile";
558 my $rulefiletoclose = '';
559 my @queryvars = ();
560 my $displayrulefilerules = 0;
561
562 # Check for passed in query string
563 if ($ENV{'QUERY_STRING'}) {
564 # Split out vars
565 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
566
567 # Loop over values
568 foreach $value (@queryvars) {
569 # Split out var pairs
570 ($var, $linkedrulefile) = split(/=/, $value);
571
572 # Check if var is 'RULEFILE'
573 if ($var eq 'RULEFILE') {
574 # Check if rulefile equals linkedrulefile
575 if ($rulefile eq $linkedrulefile) {
576 # Set display flag
577 $displayrulefilerules = 1;
578
579 # Strip out rulefile from rulefilelink
580 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
581 } else {
582 # Add linked rule file to rulefilelink
583 $rulefilelink .= "&RULEFILE=$linkedrulefile";
584 }
585 }
586 }
587 }
588
589 # Strip out extra & & ? from rulefilelink
590 $rulefilelink =~ s/^\?\&/\?/i;
591
592 # Check for a single '?' and replace with page for proper link display
593 if ($rulefilelink eq '?') {
594 $rulefilelink = "ids.cgi";
595 }
596
597 # Output rule file name and checkbox
598 print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
599 print "<TR><TD CLASS='base' VALIGN='TOP'>";
600
601 # Check for empty 'Description'
602 if ($snortrules{$rulefile}{'Description'} eq '') {
603 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
604 } else {
605 # Output rule file 'Description'
606 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
607 }
608
609 # Check for display flag
610 if ($displayrulefilerules) {
611 # Rule file definition rule display
612 print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
613
614 # Local vars
615 my $ruledefdisplaycnt = 0;
616 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
617 $ruledefcnt++;
618 $ruledefcnt = $ruledefcnt / 2;
619
620 # Loop over rule file rules
621 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
622 # Local vars
623 my $ruledefchecked = '';
624
625 # If have display 2 rules, start new row
626 if (($ruledefdisplaycnt % 2) == 0) {
627 print "</TR><TR>";
628 $ruledefdisplaycnt = 0;
629 }
630
631 # Check for rules state
632 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
633 $ruledefchecked = 'CHECKED';
634 }
635
636 # Create rule file rule's checkbox
637 $checkboxname = "SNORT_RULE_$rulefile";
638 $checkboxname .= "_$ruledef";
639 print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
640
641 # Increment count
642 $ruledefdisplaycnt++;
643 }
644
645 # If do not have second rule for row, create empty cell
646 if (($ruledefdisplaycnt % 2) != 0) {
647 print "<TD CLASS='base'></TD>";
648 }
649
650 # Close display table
651 print "</TR></TABLE></TD></TR>";
652 }
653
654 # Close display table
655 print "</TABLE>";
656
657 # Increment ruledisplaycnt
658 $ruledisplaycnt++;
659 }
660 print "</TD></TR></TABLE></TD></TR></TABLE>";
661 print <<END
662 <table width='100%'>
663 <tr>
664 <td width='33%'>&nbsp;</td>
665 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
666 <td width='33%'>
667 &nbsp; <!-- space for future online help link -->
668 </td>
669 </tr>
670 </table>
671 </form>
672 END
673 ;
674 &Header::closebox();
675 }
676
677 ####################### End added for snort rules control #################################
678 &Header::closebigbox();
679 &Header::closepage();
680
681 sub getmd5 {
682 # Retrieve MD5 sum from $url.md5 file
683
684 my $md5buf;
685 if ($snortsettings{'RULES'} eq 'subscripted') {
686 $md5buf = &geturl("http://dl.snort.org/reg-rules/snortrules-snapshot-2.8_s.tar.gz.md5?oink_code=$snortsettings{'OINKCODE'}");
687 } elsif ($snortsettings{'RULES'} eq 'registered') {
688 $md5buf = &geturl("http://dl.snort.org/reg-rules/snortrules-snapshot-2.8.tar.gz.md5?oink_code=$snortsettings{'OINKCODE'}");
689 } else {
690 $md5buf = &geturl("http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz.md5");
691 }
692
693 return undef unless $md5buf;
694
695 if (0) { # 1 to debug
696 my $filename='';
697 my $fh='';
698 ($fh, $filename) = tempfile('/var/tmp/XXXXXXXX',SUFFIX => '.md5' );
699 binmode ($fh);
700 syswrite ($fh, $md5buf->content);
701 close($fh);
702 }
703
704 return $md5buf->content;
705 }
706 sub downloadrulesfile {
707 my $return = &geturl($url);
708 return undef unless $return;
709
710 if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
711 $errormessage = $Lang::tr{'invalid loaded file'};
712 return undef;
713 }
714
715 my $filename='';
716 my $fh='';
717 ($fh, $filename) = tempfile('/var/tmp/XXXXXXXX',SUFFIX => '.tar.gz' );#oinkmaster work only with this extension
718 binmode ($fh);
719 syswrite ($fh, $return->content);
720 close($fh);
721 return $filename;
722 }
723
724 sub geturl ($) {
725 my $url=$_[0];
726
727 unless (-e "${General::swroot}/red/active") {
728 $errormessage = $Lang::tr{'could not download latest updates'};
729 return undef;
730 }
731
732 my $downloader = LWP::UserAgent->new;
733 $downloader->timeout(5);
734
735 my %proxysettings=();
736 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
737
738 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
739 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
740 if ($proxysettings{'UPSTREAM_USER'}) {
741 $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
742 } else {
743 $downloader->proxy("http","http://$peer:$peerport/");
744 }
745 }
746
747 my $return = $downloader->get($url,'Cache-Control','no-cache');
748
749 if ($return->code == 403) {
750 $errormessage = $Lang::tr{'access refused with this oinkcode'};
751 return undef;
752 } elsif (!$return->is_success()) {
753 $errormessage = $Lang::tr{'could not download latest updates'};
754 return undef;
755 }
756
757 return $return;
758
759 }