]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Rework CGI logic to download a new ruleset
[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
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/ids-functions.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings = ();
36 my %snortrules = ();
37 my %snortsettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
42
43 # Read-in main settings, for language, theme and colors.
44 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
45 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
46
47 # Get netsettings.
48 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
50 my $snortrulepath = "/etc/snort/rules";
51 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
52 my $errormessage;
53
54 &Header::showhttpheaders();
55
56 # Default settings for snort.
57 $snortsettings{'ENABLE_SNORT'} = 'off';
58 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
59 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
60 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
61 $snortsettings{'RULES'} = '';
62 $snortsettings{'OINKCODE'} = '';
63
64 #Get GUI values
65 &Header::getcgihash(\%cgiparams);
66
67 # Try to determine if oinkmaster is running.
68 my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
69
70 # If oinkmaster is running display output.
71 if ($oinkmaster_pid) {
72 &working("$Lang::tr{'snort working'}");
73 }
74
75 # Check if any error has been stored.
76 if (-e $IDS::storederrorfile) {
77 # Open file to read in the stored error message.
78 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
79
80 # Read the stored error message.
81 $errormessage = <FILE>;
82
83 # Close file.
84 close (FILE);
85
86 # Delete the file, which is now not longer required.
87 unlink($IDS::storederrorfile);
88 }
89
90
91 ## Grab all available snort rules and store them in the snortrules hash.
92 #
93 # Open snort rules directory and do a directory listing.
94 opendir(DIR, $snortrulepath) or die $!;
95 # Loop through the direcory.
96 while (my $file = readdir(DIR)) {
97
98 # We only want files.
99 next unless (-f "$snortrulepath/$file");
100
101 # Ignore empty files.
102 next if (-z "$snortrulepath/$file");
103
104 # Use a regular expression to find files ending in .rules
105 next unless ($file =~ m/\.rules$/);
106
107 # Ignore files which are not read-able.
108 next unless (-R "$snortrulepath/$file");
109
110 # Call subfunction to read-in rulefile and add rules to
111 # the snortrules hash.
112 &readrulesfile("$file");
113 }
114
115 closedir(DIR);
116
117 # Gather used rulefiles.
118 #
119 # Check if the file for activated rulefiles is not empty.
120 if(-f $snortusedrulefilesfile) {
121 # Open the file for used rulefile and read-in content.
122 open(FILE, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
123
124 # Read-in content.
125 my @lines = <FILE>;
126
127 # Close file.
128 close(FILE);
129
130 # Loop through the array.
131 foreach my $line (@lines) {
132 # Remove newlines.
133 chomp($line);
134
135 # Skip comments.
136 next if ($line =~ /\#/);
137
138 # Skip blank lines.
139 next if ($line =~ /^\s*$/);
140
141 # Gather rule sid and message from the ruleline.
142 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
143 my $rulefile = $1;
144
145 # Add the rulefile to the %snortrules hash.
146 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
147 }
148 }
149 }
150
151 # Save ruleset.
152 if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
153 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
154 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
155
156 # Arrays to store sid which should be added to the corresponding files.
157 my @enabled_sids;
158 my @disabled_sids;
159 my @enabled_rulefiles;
160
161 # Loop through the hash of snortrules.
162 foreach my $rulefile(keys %snortrules) {
163 # Check if the rulefile is enabled.
164 if ($cgiparams{$rulefile} eq "on") {
165 # Add rulefile to the array of enabled rulefiles.
166 push(@enabled_rulefiles, $rulefile);
167
168 # Drop item from cgiparams hash.
169 delete $cgiparams{$rulefile};
170 }
171 }
172
173 # Loop through the hash of snortrules.
174 foreach my $rulefile (keys %snortrules) {
175 # Loop through the single rules of the rulefile.
176 foreach my $sid (keys %{$snortrules{$rulefile}}) {
177 # Skip the current sid if it is not numeric.
178 next unless ($sid =~ /\d+/ );
179
180 # Check if there exists a key in the cgiparams hash for this sid.
181 if (exists($cgiparams{$sid})) {
182 # Look if the rule is disabled.
183 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
184 # Check if the state has been set to 'on'.
185 if ($cgiparams{$sid} eq "on") {
186 # Add the sid to the enabled_sids array.
187 push(@enabled_sids, $sid);
188
189 # Drop item from cgiparams hash.
190 delete $cgiparams{$rulefile}{$sid};
191 }
192 }
193 } else {
194 # Look if the rule is enabled.
195 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
196 # Check if the state is 'on' and should be disabled.
197 # In this case there is no entry
198 # for the sid in the cgiparams hash.
199 # Add it to the disabled_sids array.
200 push(@disabled_sids, $sid);
201
202 # Drop item from cgiparams hash.
203 delete $cgiparams{$rulefile}{$sid};
204 }
205 }
206 }
207 }
208
209 # Open enabled sid's file for writing.
210 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
211
212 # Write header to file.
213 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
214
215 # Check if the enabled_sids array contains any sid's.
216 if (@enabled_sids) {
217 # Loop through the array of enabled sids and write them to the file.
218 foreach my $sid (@enabled_sids) {
219 print FILE "enablesid $sid\n";
220 }
221 }
222
223 # Close file after writing.
224 close(FILE);
225
226 # Open disabled sid's file for writing.
227 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
228
229 # Write header to file.
230 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
231
232 # Check if the enabled_sids array contains any sid's.
233 if (@disabled_sids) {
234 # Loop through the array of disabled sids and write them to the file.
235 foreach my $sid (@disabled_sids) {
236 print FILE "disablesid $sid\n";
237 }
238 }
239
240 # Close file after writing.
241 close(FILE);
242
243 # Open file for used rulefiles.
244 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
245
246 # Write header to file.
247 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
248
249 # Check if the enabled_rulefiles array contains any entries.
250 if (@enabled_rulefiles) {
251 # Loop through the array of rulefiles which should be loaded and write the to the file.
252 foreach my $file (@enabled_rulefiles) {
253 print FILE "include \$RULE_PATH/$file\n";
254 }
255 }
256
257 # Close file after writing.
258 close(FILE);
259
260 # Call oinkmaster to alter the ruleset.
261 &oinkmaster();
262
263 # Download new ruleset.
264 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
265 # Check if the red device is active.
266 unless (-e "${General::swroot}/red/active") {
267 $errormessage = $Lang::tr{'could not download latest updates'};
268 }
269
270 # Check if enought free disk space is availabe.
271 $errormessage = &IDS::checkdiskspace();
272
273 # Check if any errors happend.
274 unless ($errormessage) {
275 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
276 &Header::openbigbox('100%', 'left', '', $errormessage);
277 &Header::openbox( 'Waiting', 1,);
278 print <<END;
279 <table>
280 <tr>
281 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
282 <td>$Lang::tr{'snort working'}</td>
283 </tr>
284 </table>
285 END
286 &Header::closebox();
287 &Header::closebigbox();
288 &Header::closepage();
289
290 # Call subfunction to download the ruleset.
291 $errormessage = &IDS::downloadruleset();
292
293 # Check if the downloader returned an error.
294 if ($errormessage) {
295 # Call function to store the errormessage.
296 &IDS::log_error($errormessage);
297
298 # Preform a reload of the page.
299 &reload();
300 } else {
301 # Call subfunction to launch oinkmaster.
302 &IDS::oinkmaster();
303
304 # Perform a reload of the page.
305 &reload();
306 }
307 }
308 # Save snort settings.
309 } elsif ($cgiparams{'SNORT'} eq $Lang::tr{'save'}) {
310 # Prevent form name from been stored in conf file.
311 delete $cgiparams{'SNORT'};
312
313 # Check if an oinkcode has been provided.
314 if ($cgiparams{'OINKCODE'}) {
315 # Check if the oinkcode contains unallowed chars.
316 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
317 $errormessage = $Lang::tr{'invalid input for oink code'};
318 }
319 }
320
321 # Go on if there are no error messages.
322 if (!$errormessage) {
323 # Store settings into settings file.
324 &General::writehash("${General::swroot}/snort/settings", \%cgiparams);
325
326 # Call snortctrl to restart snort
327 system('/usr/local/bin/snortctrl restart >/dev/null');
328 }
329 }
330
331 # Read-in snortsettings
332 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
333
334 $checked{'ENABLE_SNORT'}{'off'} = '';
335 $checked{'ENABLE_SNORT'}{'on'} = '';
336 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
337 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
338 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
339 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
340 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
341 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
342 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
343 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
344 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
345 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
346 $selected{'RULES'}{'nothing'} = '';
347 $selected{'RULES'}{'community'} = '';
348 $selected{'RULES'}{'emerging'} = '';
349 $selected{'RULES'}{'registered'} = '';
350 $selected{'RULES'}{'subscripted'} = '';
351 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
352
353 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
354
355 ### Java Script ###
356 print <<END
357 <script>
358 // Tiny java script function to show/hide the rules
359 // of a given category.
360 function showhide(tblname) {
361 \$("#" + tblname).toggle();
362 }
363 </script>
364 END
365 ;
366
367 &Header::openbigbox('100%', 'left', '', $errormessage);
368
369 if ($errormessage) {
370 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
371 print "<class name='base'>$errormessage\n";
372 print "&nbsp;</class>\n";
373 &Header::closebox();
374 }
375
376 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
377
378 my $rulesdate;
379
380 # Check if a ruleset allready has been downloaded.
381 if ( -f "$IDS::rulestarball"){
382 # Call stat on the filename to obtain detailed information.
383 my @Info = stat("$IDS::rulestarball");
384
385 # Grab details about the creation time.
386 $rulesdate = localtime($Info[9]);
387 }
388
389 print <<END
390 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
391 <table width='100%' border='0'>
392 <tr>
393 <td class='base' width='25%'>
394 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
395 </td>
396
397 <td class='base' width='25%'>
398 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
399 </td>
400
401 <td class='base' width='25%'>
402 END
403 ;
404
405 # Check if a blue device is configured.
406 if ($netsettings{'BLUE_DEV'}) {
407 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
408 }
409
410 print "</td>\n";
411
412 print "<td width='25%'>\n";
413
414 # Check if an orange device is configured.
415 if ($netsettings{'ORANGE_DEV'}) {
416 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
417 }
418
419 print <<END
420 </td>
421 </tr>
422
423 <tr>
424 <td colspan='4'><br><br></td>
425 </tr>
426
427 <tr>
428 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
429 </tr>
430
431 <tr>
432 <td colspan='4'><select name='RULES'>
433 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
434 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
435 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
436 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
437 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
438 </select>
439 </td>
440 </tr>
441
442 <tr>
443 <td colspan='4'>
444 <br>$Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}</br>
445 <br>$Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}</br>
446 </td>
447 </tr>
448
449 <tr>
450 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
451 </tr>
452
453 <tr>
454 <td colspan='4' align='left'><br>
455 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
456 </td>
457
458 </tr>
459 </table>
460
461 <br><br>
462
463 <table width='100%'>
464 <tr>
465 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
466 </tr>
467 </table>
468 </form>
469 END
470 ;
471
472 &Header::closebox();
473
474 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
475 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
476
477 # Output display table for rule files
478 print "<table width='100%'>\n";
479
480 # Local variable required for java script to show/hide
481 # rules of a rulefile.
482 my $rulesetcount = 1;
483
484 # Loop over each rule file
485 foreach my $rulefile (sort keys(%snortrules)) {
486 my $rulechecked = '';
487
488 # Check if rule file is enabled
489 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
490 $rulechecked = 'CHECKED';
491 }
492
493 # Table and rows for the rule files.
494 print"<tr>\n";
495 print"<td class='base' width='5%'>\n";
496 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
497 print"</td>\n";
498 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
499 print"<td class='base' width='5%' align='right'>\n";
500 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
501 print"</td>\n";
502 print"</tr>\n";
503
504 # Rows which will be hidden per default and will contain the single rules.
505 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
506 print"<td colspan='3'>\n";
507
508 # Local vars
509 my $lines;
510 my $rows;
511 my $col;
512
513 # New table for the single rules.
514 print "<table width='100%'>\n";
515
516 # Loop over rule file rules
517 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
518 # Local vars
519 my $ruledefchecked = '';
520
521 # Skip rulefile itself.
522 next if ($sid eq "Rulefile");
523
524 # If 2 rules have been displayed, start a new row
525 if (($lines % 2) == 0) {
526 print "</tr><tr>\n";
527
528 # Increase rows by once.
529 $rows++;
530 }
531
532 # Colour lines.
533 if ($rows % 2) {
534 $col="bgcolor='$color{'color20'}'";
535 } else {
536 $col="bgcolor='$color{'color22'}'";
537 }
538
539 # Set rule state
540 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
541 $ruledefchecked = 'CHECKED';
542 }
543
544 # Create rule checkbox and display rule description
545 print "<td class='base' width='5%' align='right' $col>\n";
546 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
547 print "</td>\n";
548 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
549
550 # Increment rule count
551 $lines++;
552 }
553
554 # If do not have a second rule for row, create empty cell
555 if (($lines % 2) != 0) {
556 print "<td class='base'></td>";
557 }
558
559 # Close display table
560 print "</tr></table></td></tr>";
561
562 # Finished whith the rule file, increase count.
563 $rulesetcount++;
564 }
565
566 # Close display table
567 print "</table>";
568
569 print <<END
570 <table width='100%'>
571 <tr>
572 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
573 &nbsp; <!-- space for future online help link -->
574 </td>
575 </tr>
576 </table>
577 </form>
578 END
579 ;
580 &Header::closebox();
581 &Header::closebigbox();
582 &Header::closepage();
583
584 #
585 ## A tiny function to perform a reload of the webpage after one second.
586 #
587 sub reload () {
588 print "<meta http-equiv='refresh' content='1'>\n";
589
590 # Stop the script.
591 exit;
592 }
593
594 #
595 ## Private function to read-in and parse rules of a given rulefile.
596 #
597 ## The given file will be read, parsed and all valid rules will be stored by ID,
598 ## message/description and it's state in the snortrules hash.
599 #
600 sub readrulesfile ($) {
601 my $rulefile = shift;
602
603 # Open rule file and read in contents
604 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
605
606 # Store file content in an array.
607 my @lines = <RULEFILE>;
608
609 # Close file.
610 close(RULEFILE);
611
612 # Loop over rule file contents
613 foreach my $line (@lines) {
614 # Remove whitespaces.
615 chomp $line;
616
617 # Skip blank lines.
618 next if ($line =~ /^\s*$/);
619
620 # Local vars.
621 my $sid;
622 my $msg;
623
624 # Gather rule sid and message from the ruleline.
625 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
626 $msg = $1;
627 $sid = $2;
628
629 # Check if a rule has been found.
630 if ($sid && $msg) {
631 # Add rule to the snortrules hash.
632 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
633
634 # Grab status of the rule. Check if ruleline starts with a "dash".
635 if ($line =~ /^\#/) {
636 # If yes, the rule is disabled.
637 $snortrules{$rulefile}{$sid}{'State'} = "off";
638 } else {
639 # Otherwise the rule is enabled.
640 $snortrules{$rulefile}{$sid}{'State'} = "on";
641 }
642 }
643 }
644 }
645 }