]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
corrected speed footer not working with dhcp and static
[people/teissler/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{'GUARDIAN_IGNOREFILE_CONTENT'} = `cat $snortsettings{'GUARDIAN_IGNOREFILE'}`;
68 $snortsettings{'ACTION'} = '';
69 $snortsettings{'ACTION2'} = '';
70 $snortsettings{'RULES'} = '';
71 $snortsettings{'OINKCODE'} = '';
72 $snortsettings{'INSTALLDATE'} = '';
73 $snortsettings{'INSTALLMD5'} = '';
74
75 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
76
77 ####################### Added for snort rules control #################################
78 my $snortrulepath; # change to "/etc/snort/rules" - maniac
79 my @snortconfig;
80 my $restartsnortrequired = 0;
81 my %snortrules;
82 my $rule = '';
83 my $table1colour = '';
84 my $table2colour = '';
85 my $var = '';
86 my $value = '';
87 my $tmp = '';
88 my $linkedrulefile = '';
89 my $border = '';
90 my $checkboxname = '';
91
92 if (-e "/etc/snort/snort.conf") {
93
94
95 # Open snort.conf file, read it in, close it, and re-open for writing
96 open(FILE, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
97 @snortconfig = <FILE>;
98 close(FILE);
99 open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
100
101 my @rules = `cd /etc/snort/rules/ && ls *.rules`; # With this loop the rule might be display with correct rulepath set
102 foreach (@rules) {
103 chomp $_;
104 my $temp = join(";",@snortconfig);
105 if ( $temp =~ /$_/ ){next;}
106 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
107 }
108
109 # Loop over each line
110 foreach my $line (@snortconfig) {
111 # Trim the line
112 chomp $line;
113
114 # Check for a line with .rules
115 if ($line =~ /\.rules$/) {
116 # Parse out rule file name
117 $rule = $line;
118 $rule =~ s/\$RULE_PATH\///i;
119 $rule =~ s/ ?include ?//i;
120 $rule =~ s/\#//i;
121 my $snortrulepathrule = "$snortrulepath/$rule";
122
123 # Open rule file and read in contents
124 open(RULEFILE, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
125 my @snortrulefile = <RULEFILE>;
126 close(RULEFILE);
127 open(RULEFILE, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
128
129 # Local vars
130 my $dashlinecnt = 0;
131 my $desclook = 1;
132 my $snortruledesc = '';
133 my %snortruledef = ();
134 my $rulecnt = 1;
135
136 # Loop over rule file contents
137 foreach my $ruleline (@snortrulefile) {
138 chomp $ruleline;
139
140 # If still looking for a description
141 if ($desclook) {
142 # If line does not start with a # anymore, then done looking for a description
143 if ($ruleline !~ /^\#/) {
144 $desclook = 0;
145 }
146
147 # If see more than one dashed line, (start to) create rule file description
148 if ($dashlinecnt > 1) {
149 # Check for a line starting with a #
150 if ($ruleline =~ /^\#/) {
151 # Create tempruleline
152 my $tempruleline = $ruleline;
153
154 # Strip off # and clean up line
155 $tempruleline =~ s/\# ?//i;
156
157 # Check for part of a description
158 if ($snortruledesc eq '') {
159 $snortruledesc = $tempruleline;
160 } else {
161 $snortruledesc .= " $tempruleline";
162 }
163 } else {
164 # Must be done
165 $desclook = 0;
166 }
167 }
168
169 # If have a dashed line, increment count
170 if ($ruleline =~ /\# ?\-+/) {
171 $dashlinecnt++;
172 }
173 } else {
174 # Parse out rule file rule's message for display
175 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
176 my $msg = '';
177 $msg = $1;
178 $msg =~ s/msg\:\"//i;
179 $msg =~ s/\";//i;
180 $snortruledef{$rulecnt}{'Description'} = $msg;
181
182 # Check for 'Save' and rule file displayed in query string
183 if (($snortsettings{'ACTION'} eq $Lang::tr{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
184 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
185 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
186 $restartsnortrequired = 1;
187 }
188
189 # Strip out leading # from rule line
190 $ruleline =~ s/\# ?//i;
191
192 # Check if it does not exists (which means it is disabled), append a #
193 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
194 $ruleline = "#"." $ruleline";
195 }
196 }
197
198 # Check if ruleline does not begin with a #, so it is enabled
199 if ($ruleline !~ /^\#/) {
200 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
201 } else {
202 # Otherwise it is disabled
203 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
204 }
205 }
206 }
207
208 # Print ruleline to RULEFILE
209 print RULEFILE "$ruleline\n";
210 }
211
212 # Close RULEFILE
213 close(RULEFILE);
214
215 # Check for 'Save'
216 if ($snortsettings{'ACTION'} eq $Lang::tr{'update'}) {
217 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
218 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
219 $restartsnortrequired = 1;
220 }
221
222 # Strip out leading # from rule line
223 $line =~ s/\# ?//i;
224
225 # Check if it does not exists (which means it is disabled), append a #
226 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
227 $line = "# $line";
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://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.6_s.tar.gz";
267 } elsif ($snortsettings{'RULES'} eq 'registered') {
268 $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.6.tar.gz";
269 } else {
270 $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz";
271 }
272
273 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" )
274 {
275 $errormessage = $Lang::tr{'invalid input for oink code'} unless (
276 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
277 ($snortsettings{'RULESTYPE'} eq 'nothing' ) );
278
279 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
280 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
281 {
282 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
283 } else {
284 unlink "${General::swroot}/snort/enable";
285 }
286 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
287 {
288 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
289 } else {
290 unlink "${General::swroot}/snort/enable_green";
291 }
292 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
293 {
294 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
295 } else {
296 unlink "${General::swroot}/snort/enable_blue";
297 }
298 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
299 {
300 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
301 } else {
302 unlink "${General::swroot}/snort/enable_orange";
303 }
304 if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on')
305 {
306 system ('/usr/bin/touch', "${General::swroot}/guardian/enable");
307 } else {
308 unlink "${General::swroot}/guardian/enable";
309 }
310
311 system('/usr/local/bin/snortctrl restart >/dev/null');
312
313 } elsif ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "guardian" ){
314 open(IGNOREFILE, ">$snortsettings{'GUARDIAN_IGNOREFILE'}") or die "Unable to write guardian ignore file $snortsettings{'GUARDIAN_IGNOREFILE'}";
315 print IGNOREFILE $snortsettings{'GUARDIAN_IGNOREFILE_CONTENT'};
316 close(IGNOREFILE);
317 open(GUARDIAN, ">/var/ipfire/guardian/guardian.conf") or die "Unable to write guardian conf /var/ipfire/guardian/guardian.conf";
318 print GUARDIAN <<END
319 Interface $snortsettings{'GUARDIAN_INTERFACE'}
320 HostGatewayByte $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'}
321 LogFile $snortsettings{'GUARDIAN_LOGFILE'}
322 AlertFile $snortsettings{'GUARDIAN_ALERTFILE'}
323 IgnoreFile $snortsettings{'GUARDIAN_IGNOREFILE'}
324 TargetFile $snortsettings{'GUARDIAN_TARGETFILE'}
325 TimeLimit $snortsettings{'GUARDIAN_TIMELIMIT'}
326 END
327 ;
328 close(GUARDIAN);
329 system('/usr/local/bin/snortctrl restart >/dev/null');
330 }
331 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
332 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
333
334 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
335 $md5 = &getmd5;
336 if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) {
337 chomp($md5);
338 my $filename = &downloadrulesfile();
339 if (defined $filename) {
340 # Check MD5sum
341 $realmd5 = `/usr/bin/md5sum $filename`;
342 chomp ($realmd5);
343 $realmd5 =~ s/^(\w+)\s.*$/$1/;
344 if ($md5 ne $realmd5) {
345 $errormessage = "$Lang::tr{'invalid md5sum'}";
346 } else {
347 $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>";
348 $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`;
349 $results .= "</pre>";
350 }
351 unlink ($filename);
352 }
353 }
354 }
355
356 $checked{'ENABLE_SNORT'}{'off'} = '';
357 $checked{'ENABLE_SNORT'}{'on'} = '';
358 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
359 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
360 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
361 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
362 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
363 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
364 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
365 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
366 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
367 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
368 $checked{'ENABLE_GUARDIAN'}{'off'} = '';
369 $checked{'ENABLE_GUARDIAN'}{'on'} = '';
370 $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'";
371 $selected{'RULES'}{'nothing'} = '';
372 $selected{'RULES'}{'community'} = '';
373 $selected{'RULES'}{'registered'} = '';
374 $selected{'RULES'}{'subscripted'} = '';
375 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
376
377 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
378
379 ####################### Added for snort rules control #################################
380 print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>";
381 print <<END
382 <STYLE TYPE="text/css">
383 <!--
384 .section {
385 border: groove;
386 }
387 .row1color {
388 border: ridge;
389 background-color: $color{'color22'};
390 }
391 .row2color {
392 border: ridge;
393 background-color: $color{'color20'};
394 }
395 .rowselected {
396 border: double #FF0000;
397 background-color: #DCDCDC;
398 }
399 -->
400 </STYLE>
401 END
402 ;
403 ####################### End added for snort rules control #################################
404
405 &Header::openbigbox('100%', 'left', '', $errormessage);
406
407 if ($errormessage) {
408 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
409 print "<class name='base'>$errormessage\n";
410 print "&nbsp;</class>\n";
411 &Header::closebox();
412 }
413
414 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'});
415 print <<END
416 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
417 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
418 END
419 ;
420 if ($netsettings{'BLUE_DEV'} ne '') {
421 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
422 }
423 if ($netsettings{'ORANGE_DEV'} ne '') {
424 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
425 }
426 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
427 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
428 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian";
429 }
430
431 print <<END
432 </td></tr>
433 <tr>
434 <td><hr /></td>
435 </tr>
436 <tr>
437 <td><b>$Lang::tr{'ids rules update'}</b></td>
438 </tr>
439 <tr>
440 <td><select name='RULES'>
441 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
442 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
443 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
444 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
445 </select>
446 </td>
447 </tr>
448 <tr>
449 <td><br />
450 $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br />
451 <br />
452 $Lang::tr{'ids rules license2'} <a href='http://www.snort.org/reg-bin/userprefs.cgi' target='_blank'>USER PREFERENCES</a>, $Lang::tr{'ids rules license3'}<br />
453 </td>
454 </tr>
455 <tr>
456 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
457 </tr>
458 <tr>
459 <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
460 END
461 ;
462
463 if ($snortsettings{'INSTALLMD5'} eq $md5) {
464 print "&nbsp;$Lang::tr{'rules already up to date'}</td>";
465 } else {
466 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} && $md5 eq $realmd5 ) {
467 $snortsettings{'INSTALLMD5'} = $realmd5;
468 $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`;
469 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
470 }
471 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
472 }
473 print <<END
474 </tr>
475 </table>
476 <hr />
477 <table width='100%'>
478 <tr>
479 <td align='center'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
480 </tr>
481 </table>
482 </form>
483 END
484 ;
485
486 if ($results ne '') {
487 print "$results";
488 }
489
490 &Header::closebox();
491
492 ####################### Added for guardian control ####################################
493 if ( -e "/var/ipfire/guardian/guardian.conf" ) {
494 &Header::openbox('100%', 'LEFT', $Lang::tr{'guardian configuration'});
495 print <<END
496 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
497 <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>
498 <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>
499 <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>
500 <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>
501 <tr><td align='left' width='40%'>$Lang::tr{'guardian ignorefile'}</td><td align='left'><textarea name='IGNOREFILE_CONTENT' cols='32' rows='6' wrap='off'></textarea></td></tr>
502 <tr><td align='center' colspan='2'><input type='hidden' name='ACTION2' value='guardian' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
503 </table>
504 </form>
505 END
506 ;
507 &Header::closebox();
508 }
509
510
511
512
513 ####################### Added for snort rules control #################################
514 if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
515 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
516 # Output display table for rule files
517 print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>";
518
519 print "<form method='post'>";
520
521 # Local vars
522 my $ruledisplaycnt = 1;
523 my $rulecnt = keys %snortrules;
524 $rulecnt++;
525 $rulecnt = $rulecnt / 2;
526
527 # Loop over each rule file
528 foreach my $rulefile (sort keys(%snortrules)) {
529 my $rulechecked = '';
530
531 # Check if reached half-way through rule file rules to start new column
532 if ($ruledisplaycnt > $rulecnt) {
533 print "</TABLE></TD><TD VALIGN='TOP'><TABLE>";
534 $ruledisplaycnt = 0;
535 }
536
537 # Check if rule file is enabled
538 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
539 $rulechecked = 'CHECKED';
540 }
541
542 # Create rule file link, vars array, and display flag
543 my $rulefilelink = "?RULEFILE=$rulefile";
544 my $rulefiletoclose = '';
545 my @queryvars = ();
546 my $displayrulefilerules = 0;
547
548 # Check for passed in query string
549 if ($ENV{'QUERY_STRING'}) {
550 # Split out vars
551 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
552
553 # Loop over values
554 foreach $value (@queryvars) {
555 # Split out var pairs
556 ($var, $linkedrulefile) = split(/=/, $value);
557
558 # Check if var is 'RULEFILE'
559 if ($var eq 'RULEFILE') {
560 # Check if rulefile equals linkedrulefile
561 if ($rulefile eq $linkedrulefile) {
562 # Set display flag
563 $displayrulefilerules = 1;
564
565 # Strip out rulefile from rulefilelink
566 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
567 } else {
568 # Add linked rule file to rulefilelink
569 $rulefilelink .= "&RULEFILE=$linkedrulefile";
570 }
571 }
572 }
573 }
574
575 # Strip out extra & & ? from rulefilelink
576 $rulefilelink =~ s/^\?\&/\?/i;
577
578 # Check for a single '?' and replace with page for proper link display
579 if ($rulefilelink eq '?') {
580 $rulefilelink = "ids.cgi";
581 }
582
583 # Output rule file name and checkbox
584 print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>";
585 print "<TR><TD CLASS='base' VALIGN='TOP'>";
586
587 # Check for empty 'Description'
588 if ($snortrules{$rulefile}{'Description'} eq '') {
589 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>";
590 } else {
591 # Output rule file 'Description'
592 print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>";
593 }
594
595 # Check for display flag
596 if ($displayrulefilerules) {
597 # Rule file definition rule display
598 print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>";
599
600 # Local vars
601 my $ruledefdisplaycnt = 0;
602 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
603 $ruledefcnt++;
604 $ruledefcnt = $ruledefcnt / 2;
605
606 # Loop over rule file rules
607 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
608 # Local vars
609 my $ruledefchecked = '';
610
611 # If have display 2 rules, start new row
612 if (($ruledefdisplaycnt % 2) == 0) {
613 print "</TR><TR>";
614 $ruledefdisplaycnt = 0;
615 }
616
617 # Check for rules state
618 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
619 $ruledefchecked = 'CHECKED';
620 }
621
622 # Create rule file rule's checkbox
623 $checkboxname = "SNORT_RULE_$rulefile";
624 $checkboxname .= "_$ruledef";
625 print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>";
626
627 # Increment count
628 $ruledefdisplaycnt++;
629 }
630
631 # If do not have second rule for row, create empty cell
632 if (($ruledefdisplaycnt % 2) != 0) {
633 print "<TD CLASS='base'></TD>";
634 }
635
636 # Close display table
637 print "</TR></TABLE></TD></TR>";
638 }
639
640 # Close display table
641 print "</TABLE>";
642
643 # Increment ruledisplaycnt
644 $ruledisplaycnt++;
645 }
646 print "</TD></TR></TABLE></TD></TR></TABLE>";
647 print <<END
648 <table width='100%'>
649 <tr>
650 <td width='33%'>&nbsp;</td>
651 <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
652 <td width='33%'>
653 &nbsp; <!-- space for future online help link -->
654 </td>
655 </tr>
656 </table>
657 </form>
658 END
659 ;
660 &Header::closebox();
661 }
662
663 ####################### End added for snort rules control #################################
664 &Header::closebigbox();
665 &Header::closepage();
666
667 sub getmd5 {
668 # Retrieve MD5 sum from $url.md5 file
669 #
670 my $md5buf = &geturl("$url.md5");
671 return undef unless $md5buf;
672
673 if (0) { # 1 to debug
674 my $filename='';
675 my $fh='';
676 ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.md5' );
677 binmode ($fh);
678 syswrite ($fh, $md5buf->content);
679 close($fh);
680 }
681 return $md5buf->content;
682 }
683 sub downloadrulesfile {
684 my $return = &geturl($url);
685 return undef unless $return;
686
687 if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning
688 $errormessage = $Lang::tr{'invalid loaded file'};
689 return undef;
690 }
691
692 my $filename='';
693 my $fh='';
694 ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.tar.gz' );#oinkmaster work only with this extension
695 binmode ($fh);
696 syswrite ($fh, $return->content);
697 close($fh);
698 return $filename;
699 }
700
701 sub geturl ($) {
702 my $url=$_[0];
703
704 unless (-e "${General::swroot}/red/active") {
705 $errormessage = $Lang::tr{'could not download latest updates'};
706 return undef;
707 }
708
709 my $downloader = LWP::UserAgent->new;
710 $downloader->timeout(5);
711
712 my %proxysettings=();
713 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
714
715 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
716 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
717 if ($proxysettings{'UPSTREAM_USER'}) {
718 $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/");
719 } else {
720 $downloader->proxy("http","http://$peer:$peerport/");
721 }
722 }
723
724 my $return = $downloader->get($url,'Cache-Control','no-cache');
725
726 if ($return->code == 403) {
727 $errormessage = $Lang::tr{'access refused with this oinkcode'};
728 return undef;
729 } elsif (!$return->is_success()) {
730 $errormessage = $Lang::tr{'could not download latest updates'};
731 return undef;
732 }
733
734 return $return;
735
736 }