]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Remove comment lines for snort rules control
[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-2015 IPFire Team <info@ipfire.org> #
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 my %color = ();
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);
37
38 my %snortsettings=();
39 my %checked=();
40 my %selected=();
41 my %netsettings=();
42 our $errormessage = '';
43 our $results = '';
44 our $tempdir = '';
45 our $url='';
46 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
47
48 &Header::showhttpheaders();
49
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'} = '';
59
60 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
61
62 my $snortrulepath = "/etc/snort/rules";
63 my $restartsnortrequired = 0;
64 my %snortrules;
65 my $rule = '';
66 my $table1colour = '';
67 my $table2colour = '';
68 my $var = '';
69 my $value = '';
70 my $tmp = '';
71 my $linkedrulefile = '';
72 my $border = '';
73 my $checkboxname = '';
74
75 ## Grab all available snort rules and store them in the snortrules hash.
76 #
77 # Open snort rules directory and do a directory listing.
78 opendir(DIR, $snortrulepath) or die $!;
79 # Loop through the direcory.
80 while (my $file = readdir(DIR)) {
81
82 # We only want files.
83 next unless (-f "$snortrulepath/$file");
84
85 # Ignore empty files.
86 next if (-z "$snortrulepath/$file");
87
88 # Use a regular expression to find files ending in .rules
89 next unless ($file =~ m/\.rules$/);
90
91 # Ignore files which are not read-able.
92 next unless (-R "$snortrulepath/$file");
93
94 # Call subfunction to read-in rulefile and add rules to
95 # the snortrules hash.
96 &readrulesfile("$file");
97 }
98
99 closedir(DIR);
100
101 if ($snortsettings{'OINKCODE'} ne "") {
102 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
103 }
104
105 if (!$errormessage) {
106 if ($snortsettings{'RULES'} eq 'subscripted') {
107 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
108 } elsif ($snortsettings{'RULES'} eq 'registered') {
109 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
110 } elsif ($snortsettings{'RULES'} eq 'community') {
111 $url=" https://www.snort.org/rules/community";
112 } else {
113 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
114 }
115
116 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
117 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
118 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
119 {
120 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
121 } else {
122 unlink "${General::swroot}/snort/enable";
123 }
124 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
125 {
126 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
127 } else {
128 unlink "${General::swroot}/snort/enable_green";
129 }
130 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
131 {
132 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
133 } else {
134 unlink "${General::swroot}/snort/enable_blue";
135 }
136 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
137 {
138 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
139 } else {
140 unlink "${General::swroot}/snort/enable_orange";
141 }
142 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
143 {
144 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
145 } else {
146 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
147 }
148
149 system('/usr/local/bin/snortctrl restart >/dev/null');
150 }
151
152 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
153 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
154
155 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
156 my @df = `/bin/df -B M /var`;
157 foreach my $line (@df) {
158 next if $line =~ m/^Filesystem/;
159 my $return;
160
161 if ($line =~ m/dev/ ) {
162 $line =~ m/^.* (\d+)M.*$/;
163 my @temp = split(/ +/,$line);
164 if ($1<300) {
165 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
166 } else {
167 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
168 &downloadrulesfile();
169 sleep(3);
170 $return = `cat /var/tmp/log 2>/dev/null`;
171
172 }
173
174 if ($return =~ "ERROR") {
175 $errormessage = "<br /><pre>".$return."</pre>";
176 } else {
177 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
178 sleep(2);
179 }
180 }
181 }
182 }
183 }
184 }
185
186 $checked{'ENABLE_SNORT'}{'off'} = '';
187 $checked{'ENABLE_SNORT'}{'on'} = '';
188 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
189 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
190 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
191 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
192 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
193 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
194 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
195 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
196 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
197 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
198 $selected{'RULES'}{'nothing'} = '';
199 $selected{'RULES'}{'community'} = '';
200 $selected{'RULES'}{'emerging'} = '';
201 $selected{'RULES'}{'registered'} = '';
202 $selected{'RULES'}{'subscripted'} = '';
203 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
204
205 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
206
207 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
208 print <<END
209 <style type="text/css">
210 <!--
211 .section {
212 border: groove;
213 }
214 .row1color {
215 border: ridge;
216 background-color: $color{'color22'};
217 }
218 .row2color {
219 border: ridge;
220 background-color: $color{'color20'};
221 }
222 .rowselected {
223 border: double #FF0000;
224 background-color: #DCDCDC;
225 }
226 -->
227 </style>
228 END
229 ;
230
231 &Header::openbigbox('100%', 'left', '', $errormessage);
232
233 ###############
234 # DEBUG DEBUG
235 # &Header::openbox('100%', 'left', 'DEBUG');
236 # my $debugCount = 0;
237 # foreach my $line (sort keys %snortsettings) {
238 # print "$line = $snortsettings{$line}<br />\n";
239 # $debugCount++;
240 # }
241 # print "&nbsp;Count: $debugCount\n";
242 # &Header::closebox();
243 # DEBUG DEBUG
244 ###############
245
246 if ($errormessage) {
247 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
248 print "<class name='base'>$errormessage\n";
249 print "&nbsp;</class>\n";
250 &Header::closebox();
251 }
252
253 my $return = `pidof oinkmaster.pl -x`;
254 chomp($return);
255 if ($return) {
256 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
257 print <<END;
258 <table>
259 <tr><td>
260 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
261 <td>
262 $Lang::tr{'snort working'}
263 <tr><td colspan='2' align='center'>
264 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
265 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
266 </form>
267 <tr><td colspan='2' align='left'><pre>
268 END
269 my @output = `tail -20 /var/tmp/log`;
270 foreach (@output) {
271 print "$_";
272 }
273 print <<END;
274 </pre>
275 </table>
276 END
277 &Header::closebox();
278 &Header::closebigbox();
279 &Header::closepage();
280 exit;
281 refreshpage();
282 }
283
284 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
285 print <<END
286 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
287 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
288 END
289 ;
290 if ($netsettings{'BLUE_DEV'} ne '') {
291 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
292 }
293 if ($netsettings{'ORANGE_DEV'} ne '') {
294 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
295 }
296 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
297
298 print <<END
299 </td></tr>
300 <tr>
301 <td><br><br></td>
302 </tr>
303 <tr>
304 <td><b>$Lang::tr{'ids rules update'}</b></td>
305 </tr>
306 <tr>
307 <td><select name='RULES'>
308 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
309 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
310 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
311 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
312 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
313 </select>
314 </td>
315 </tr>
316 <tr>
317 <td><br />
318 $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 />
319 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
320 </td>
321 </tr>
322 <tr>
323 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
324 </tr>
325 <tr>
326 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
327 END
328 ;
329 if ( -e "/var/tmp/snortrules.tar.gz"){
330 my @Info = stat("/var/tmp/snortrules.tar.gz");
331 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
332 }
333 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
334
335 print <<END
336 </tr>
337 </table>
338 <br><br>
339 <table width='100%'>
340 <tr>
341 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
342 </tr>
343 </table>
344 </form>
345 END
346 ;
347
348 if ($results ne '') {
349 print "$results";
350 }
351
352 &Header::closebox();
353
354 if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
355 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
356 # Output display table for rule files
357 print "<table width='100%'><tr><td valign='top'><table>";
358
359 print "<form method='post'>";
360
361 # Local vars
362 my $ruledisplaycnt = 1;
363 my $rulecnt = keys %snortrules;
364 $rulecnt++;
365 $rulecnt = $rulecnt / 2;
366
367 # Loop over each rule file
368 foreach my $rulefile (sort keys(%snortrules)) {
369 my $rulechecked = '';
370
371 # Hide inkompatible Block rules
372 if ($rulefile =~'-BLOCK.rules') {
373 next;
374 }
375
376 # Check if reached half-way through rule file rules to start new column
377 if ($ruledisplaycnt > $rulecnt) {
378 print "</table></td><td valign='top'><table>";
379 $ruledisplaycnt = 0;
380 }
381
382 # Check if rule file is enabled
383 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
384 $rulechecked = 'CHECKED';
385 }
386
387 # Create rule file link, vars array, and display flag
388 my $rulefilelink = "?RULEFILE=$rulefile";
389 my $rulefiletoclose = '';
390 my @queryvars = ();
391 my $displayrulefilerules = 0;
392
393 # Check for passed in query string
394 if ($ENV{'QUERY_STRING'}) {
395 # Split out vars
396 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
397
398 # Loop over values
399 foreach $value (@queryvars) {
400 # Split out var pairs
401 ($var, $linkedrulefile) = split(/=/, $value);
402
403 # Check if var is 'RULEFILE'
404 if ($var eq 'RULEFILE') {
405 # Check if rulefile equals linkedrulefile
406 if ($rulefile eq $linkedrulefile) {
407 # Set display flag
408 $displayrulefilerules = 1;
409
410 # Strip out rulefile from rulefilelink
411 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
412 } else {
413 # Add linked rule file to rulefilelink
414 $rulefilelink .= "&RULEFILE=$linkedrulefile";
415 }
416 }
417 }
418 }
419
420 # Strip out extra & & ? from rulefilelink
421 $rulefilelink =~ s/^\?\&/\?/i;
422
423 # Check for a single '?' and replace with page for proper link display
424 if ($rulefilelink eq '?') {
425 $rulefilelink = "ids.cgi";
426 }
427
428 # Output rule file name and checkbox
429 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
430 print "<tr><td class='base' valign='top'>";
431
432 # Check for empty 'Description'
433 if ($snortrules{$rulefile}{'Description'} eq '') {
434 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
435 } else {
436 # Output rule file 'Description'
437 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
438 }
439
440 # Check for display flag
441 if ($displayrulefilerules) {
442 # Rule file definition rule display
443 print "<tr><td class='base' valign='top'><table border='0'><tr>";
444
445 # Local vars
446 my $ruledefdisplaycnt = 0;
447 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
448 $ruledefcnt++;
449 $ruledefcnt = $ruledefcnt / 2;
450
451 # Loop over rule file rules
452 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
453 # Local vars
454 my $ruledefchecked = '';
455
456 # If have display 2 rules, start new row
457 if (($ruledefdisplaycnt % 2) == 0) {
458 print "</tr><tr>";
459 $ruledefdisplaycnt = 0;
460 }
461
462 # Check for rules state
463 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
464 $ruledefchecked = 'CHECKED';
465 }
466
467 # Create rule file rule's checkbox
468 $checkboxname = "SNORT_RULE_$rulefile";
469 $checkboxname .= "_$ruledef";
470 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
471
472 # Increment count
473 $ruledefdisplaycnt++;
474 }
475
476 # If do not have second rule for row, create empty cell
477 if (($ruledefdisplaycnt % 2) != 0) {
478 print "<td class='base'></td>";
479 }
480
481 # Close display table
482 print "</tr></table></td></tr>";
483 }
484
485 # Close display table
486 print "</table>";
487
488 # Increment ruledisplaycnt
489 $ruledisplaycnt++;
490 }
491 print "</td></tr></table></td></tr></table>";
492 print <<END
493 <table width='100%'>
494 <tr>
495 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
496 &nbsp; <!-- space for future online help link -->
497 </td>
498 </tr>
499 </table>
500 </form>
501 END
502 ;
503 &Header::closebox();
504 }
505
506 &Header::closebigbox();
507 &Header::closepage();
508
509 sub refreshpage {
510 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
511 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
512 &Header::closebox();
513 }
514
515 sub downloadrulesfile {
516 my $peer;
517 my $peerport;
518
519 unlink("/var/tmp/log");
520
521 unless (-e "${General::swroot}/red/active") {
522 $errormessage = $Lang::tr{'could not download latest updates'};
523 return undef;
524 }
525
526 my %proxysettings=();
527 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
528
529 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
530 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
531 }
532
533 if ($peer) {
534 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");
535 } else {
536 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
537 }
538 }
539
540 sub readrulesfile ($) {
541 my $rulefile = shift;
542
543 # Open rule file and read in contents
544 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
545
546 # Store file content in an array.
547 my @lines = <RULEFILE>;
548
549 # Close file.
550 close(RULEFILE);
551
552 # Loop over rule file contents
553 foreach my $line (@lines) {
554 # Remove whitespaces.
555 chomp $line;
556
557 # Skip blank lines.
558 next if ($line =~ /^\s*$/);
559
560 # Local vars.
561 my $sid;
562 my $msg;
563
564 # Gather rule sid and message from the ruleline.
565 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
566 $msg = $1;
567 $sid = $2;
568
569 # Check if a rule has been found.
570 if ($sid && $msg) {
571 # Add rule to the snortrules hash.
572 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
573
574 # Grab status of the rule. Check if ruleline starts with a "dash".
575 if ($line =~ /^\#/) {
576 # If yes, the rule is disabled.
577 $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
578 } else {
579 # Otherwise the rule is enabled.
580 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";
581 }
582 }
583 }
584 }
585 }