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