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