]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Colourize network zones
[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 %idsrules = ();
36 my %idssettings=();
37 my %rulessettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
42 my %ignored=();
43
44 # Read-in main settings, for language, theme and colors.
45 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
46 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
47
48 # Get the available network zones, based on the config type of the system and store
49 # the list of zones in an array.
50 my @network_zones = &IDS::get_available_network_zones();
51
52 # File where the used rulefiles are stored.
53 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
54
55 # File where the addresses of the homenet are stored.
56 my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
57
58 # File which contains the enabled sids.
59 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
60
61 # File which contains the disabled sids.
62 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
63
64 # File which contains wheater the rules should be changed.
65 my $modify_sids_file = "$IDS::settingsdir/oinkmaster-modify-sids.conf";
66
67 # File which stores the configured IPS settings.
68 my $idssettingsfile = "$IDS::settingsdir/settings";
69
70 # File which stores the configured rules-settings.
71 my $rulessettingsfile = "$IDS::settingsdir/rules-settings";
72
73 # File which stores the configured settings for whitelisted addresses.
74 my $ignoredfile = "$IDS::settingsdir/ignored";
75
76 # File which contains the rules to whitelist addresses on suricata.
77 my $whitelistfile = "$IDS::rulespath/whitelist.rules";
78
79 my $errormessage;
80
81 # Create files if they does not exist yet.
82 unless (-f "$enabled_sids_file") { &IDS::create_empty_file($enabled_sids_file); }
83 unless (-f "$disabled_sids_file") { &IDS::create_empty_file($disabled_sids_file); }
84 unless (-f "$modify_sids_file") { &IDS::create_empty_file($modify_sids_file); }
85 unless (-f "$idsusedrulefilesfile") { &IDS::create_empty_file($idsusedrulefilesfile); }
86 unless (-f "$idssettingsfile") { &IDS::create_empty_file($idssettingsfile); }
87 unless (-f "$rulessettingsfile") { &IDS::create_empty_file($rulessettingsfile); }
88 unless (-f "$ignoredfile") { &IDS::create_empty_file($ignoredfile); }
89 unless (-f "$whitelistfile" ) { &IDS::create_empty_file($whitelistfile); }
90
91 # Hash which contains the colour code of a network zone.
92 my %colourhash = (
93 'red' => $Header::colourred,
94 'green' => $Header::colourgreen,
95 'blue' => $Header::colourblue,
96 'orange' => $Header::colourorange
97 );
98
99 &Header::showhttpheaders();
100
101 #Get GUI values
102 &Header::getcgihash(\%cgiparams);
103
104 ## Add/edit an entry to the ignore file.
105 #
106 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
107
108 # Check if any input has been performed.
109 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
110
111 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
112 if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
113 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
114 }
115 } else {
116 $errormessage = "$Lang::tr{'guardian empty input'}";
117 }
118
119 # Go further if there was no error.
120 if ($errormessage eq '') {
121 my %ignored = ();
122 my $id;
123 my $status;
124
125 # Assign hash values.
126 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
127 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
128
129 # Read-in ignoredfile.
130 &General::readhasharray($ignoredfile, \%ignored);
131
132 # Check if we should edit an existing entry and got an ID.
133 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
134 # Assin the provided id.
135 $id = $cgiparams{'ID'};
136
137 # Undef the given ID.
138 undef($cgiparams{'ID'});
139
140 # Grab the configured status of the corresponding entry.
141 $status = $ignored{$id}[2];
142 } else {
143 # Each newly added entry automatically should be enabled.
144 $status = "enabled";
145
146 # Generate the ID for the new entry.
147 #
148 # Sort the keys by their ID and store them in an array.
149 my @keys = sort { $a <=> $b } keys %ignored;
150
151 # Reverse the key array.
152 my @reversed = reverse(@keys);
153
154 # Obtain the last used id.
155 my $last_id = @reversed[0];
156
157 # Increase the last id by one and use it as id for the new entry.
158 $id = ++$last_id;
159 }
160
161 # Add/Modify the entry to/in the ignored hash.
162 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
163
164 # Write the changed ignored hash to the ignored file.
165 &General::writehasharray($ignoredfile, \%ignored);
166
167 # Regenerate the ignore file.
168 &GenerateIgnoreFile();
169 }
170
171 # Check if the IDS is running.
172 if(&IDS::ids_is_running()) {
173 # Call suricatactrl to perform a reload.
174 &IDS::call_suricatactrl("reload");
175 }
176
177 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
178 #
179
180 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
181 my %ignored = ();
182
183 # Only go further, if an ID has been passed.
184 if ($cgiparams{'ID'}) {
185 # Assign the given ID.
186 my $id = $cgiparams{'ID'};
187
188 # Undef the given ID.
189 undef($cgiparams{'ID'});
190
191 # Read-in ignoredfile.
192 &General::readhasharray($ignoredfile, \%ignored);
193
194 # Grab the configured status of the corresponding entry.
195 my $status = $ignored{$id}[2];
196
197 # Switch the status.
198 if ($status eq "disabled") {
199 $status = "enabled";
200 } else {
201 $status = "disabled";
202 }
203
204 # Modify the status of the existing entry.
205 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
206
207 # Write the changed ignored hash to the ignored file.
208 &General::writehasharray($ignoredfile, \%ignored);
209
210 # Regenerate the ignore file.
211 &GenerateIgnoreFile();
212
213 # Check if the IDS is running.
214 if(&IDS::ids_is_running()) {
215 # Call suricatactrl to perform a reload.
216 &IDS::call_suricatactrl("reload");
217 }
218 }
219
220 ## Remove entry from ignore list.
221 #
222 } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
223 my %ignored = ();
224
225 # Read-in ignoredfile.
226 &General::readhasharray($ignoredfile, \%ignored);
227
228 # Drop entry from the hash.
229 delete($ignored{$cgiparams{'ID'}});
230
231 # Undef the given ID.
232 undef($cgiparams{'ID'});
233
234 # Write the changed ignored hash to the ignored file.
235 &General::writehasharray($ignoredfile, \%ignored);
236
237 # Regenerate the ignore file.
238 &GenerateIgnoreFile();
239
240 # Check if the IDS is running.
241 if(&IDS::ids_is_running()) {
242 # Call suricatactrl to perform a reload.
243 &IDS::call_suricatactrl("reload");
244 }
245 }
246
247 # Check if any error has been stored.
248 if (-e $IDS::storederrorfile) {
249 # Open file to read in the stored error message.
250 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
251
252 # Read the stored error message.
253 $errormessage = <FILE>;
254
255 # Close file.
256 close (FILE);
257
258 # Delete the file, which is now not longer required.
259 unlink($IDS::storederrorfile);
260 }
261
262
263 ## Grab all available snort rules and store them in the idsrules hash.
264 #
265 # Open snort rules directory and do a directory listing.
266 opendir(DIR, $IDS::rulespath) or die $!;
267 # Loop through the direcory.
268 while (my $file = readdir(DIR)) {
269
270 # We only want files.
271 next unless (-f "$IDS::rulespath/$file");
272
273 # Ignore empty files.
274 next if (-z "$IDS::rulespath/$file");
275
276 # Use a regular expression to find files ending in .rules
277 next unless ($file =~ m/\.rules$/);
278
279 # Ignore files which are not read-able.
280 next unless (-R "$IDS::rulespath/$file");
281
282 # Skip whitelist rules file.
283 next if( $file eq "whitelist.rules");
284
285 # Call subfunction to read-in rulefile and add rules to
286 # the idsrules hash.
287 &readrulesfile("$file");
288 }
289
290 closedir(DIR);
291
292 # Gather used rulefiles.
293 #
294 # Check if the file for activated rulefiles is not empty.
295 if(-f $idsusedrulefilesfile) {
296 # Open the file for used rulefile and read-in content.
297 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
298
299 # Read-in content.
300 my @lines = <FILE>;
301
302 # Close file.
303 close(FILE);
304
305 # Loop through the array.
306 foreach my $line (@lines) {
307 # Remove newlines.
308 chomp($line);
309
310 # Skip comments.
311 next if ($line =~ /\#/);
312
313 # Skip blank lines.
314 next if ($line =~ /^\s*$/);
315
316 # Gather rule sid and message from the ruleline.
317 if ($line =~ /.*- (.*)/) {
318 my $rulefile = $1;
319
320 # Check if the current rulefile exists in the %idsrules hash.
321 # If not, the file probably does not exist anymore or contains
322 # no rules.
323 if($idsrules{$rulefile}) {
324 # Add the rulefile state to the %idsrules hash.
325 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
326 }
327 }
328 }
329 }
330
331 # Save ruleset configuration.
332 if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
333 my %oldsettings;
334
335 # Read-in current (old) IDS settings.
336 &General::readhash("$rulessettingsfile", \%oldsettings);
337
338 # Prevent form name from been stored in conf file.
339 delete $cgiparams{'RULESET'};
340
341 # Check if an oinkcode has been provided.
342 if ($cgiparams{'OINKCODE'}) {
343 # Check if the oinkcode contains unallowed chars.
344 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
345 $errormessage = $Lang::tr{'invalid input for oink code'};
346 }
347 }
348
349 # Go on if there are no error messages.
350 if (!$errormessage) {
351 # Store settings into settings file.
352 &General::writehash("$rulessettingsfile", \%cgiparams);
353 }
354
355 # Check if the the automatic rule update hass been touched.
356 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
357 # Call suricatactrl to set the new interval.
358 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
359 }
360
361 # Save ruleset.
362 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
363 # Arrays to store which rulefiles have been enabled and will be used.
364 my @enabled_rulefiles;
365
366 # Hash to store the user-enabled and disabled sids.
367 my %enabled_disabled_sids;
368
369 # Loop through the hash of idsrules.
370 foreach my $rulefile(keys %idsrules) {
371 # Check if the rulefile is enabled.
372 if ($cgiparams{$rulefile} eq "on") {
373 # Add rulefile to the array of enabled rulefiles.
374 push(@enabled_rulefiles, $rulefile);
375
376 # Drop item from cgiparams hash.
377 delete $cgiparams{$rulefile};
378 }
379 }
380
381 # Read-in the files for enabled/disabled sids.
382 # This will be done by calling the read_enabled_disabled_sids_file function two times
383 # and merge the returned hashes together into the enabled_disabled_sids hash.
384 %enabled_disabled_sids = (
385 &read_enabled_disabled_sids_file($disabled_sids_file),
386 &read_enabled_disabled_sids_file($enabled_sids_file));
387
388 # Loop through the hash of idsrules.
389 foreach my $rulefile (keys %idsrules) {
390 # Loop through the single rules of the rulefile.
391 foreach my $sid (keys %{$idsrules{$rulefile}}) {
392 # Skip the current sid if it is not numeric.
393 next unless ($sid =~ /\d+/ );
394
395 # Check if there exists a key in the cgiparams hash for this sid.
396 if (exists($cgiparams{$sid})) {
397 # Look if the rule is disabled.
398 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
399 # Check if the state has been set to 'on'.
400 if ($cgiparams{$sid} eq "on") {
401 # Add/Modify the sid to/in the enabled_disabled_sids hash.
402 $enabled_disabled_sids{$sid} = "enabled";
403
404 # Drop item from cgiparams hash.
405 delete $cgiparams{$rulefile}{$sid};
406 }
407 }
408 } else {
409 # Look if the rule is enabled.
410 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
411 # Check if the state is 'on' and should be disabled.
412 # In this case there is no entry
413 # for the sid in the cgiparams hash.
414 # Add/Modify it to/in the enabled_disabled_sids hash.
415 $enabled_disabled_sids{$sid} = "disabled";
416
417 # Drop item from cgiparams hash.
418 delete $cgiparams{$rulefile}{$sid};
419 }
420 }
421 }
422 }
423
424 # Open enabled sid's file for writing.
425 open(ENABLED_FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
426
427 # Open disabled sid's file for writing.
428 open(DISABLED_FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
429
430 # Write header to the files.
431 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
432 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
433
434 # Check if the hash for enabled/disabled files contains any entries.
435 if (%enabled_disabled_sids) {
436 # Loop through the hash.
437 foreach my $sid (keys %enabled_disabled_sids) {
438 # Check if the sid is enabled.
439 if ($enabled_disabled_sids{$sid} eq "enabled") {
440 # Print the sid to the enabled_sids file.
441 print ENABLED_FILE "enablesid $sid\n";
442 # Check if the sid is disabled.
443 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
444 # Print the sid to the disabled_sids file.
445 print DISABLED_FILE "disablesid $sid\n";
446 # Something strange happende - skip the current sid.
447 } else {
448 next;
449 }
450 }
451 }
452
453 # Close file for enabled_sids after writing.
454 close(ENABLED_FILE);
455
456 # Close file for disabled_sids after writing.
457 close(DISABLED_FILE);
458
459 # Open file for used rulefiles.
460 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
461
462 # Write yaml header to the file.
463 print FILE "%YAML 1.1\n";
464 print FILE "---\n\n";
465
466 # Write header to file.
467 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
468
469 # Allways load the whitelist.
470 print FILE " - whitelist.rules\n";
471
472 # Check if the enabled_rulefiles array contains any entries.
473 if (@enabled_rulefiles) {
474 # Loop through the array of rulefiles which should be loaded and write them to the file.
475 foreach my $file (@enabled_rulefiles) {
476 print FILE " - $file\n";
477 }
478 }
479
480 # Close file after writing.
481 close(FILE);
482
483 # Lock the webpage and print message.
484 &working_notice("$Lang::tr{'snort working'}");
485
486 # Call oinkmaster to alter the ruleset.
487 &IDS::oinkmaster();
488
489 # Check if the IDS is running.
490 if(&IDS::ids_is_running()) {
491 # Call suricatactrl to perform a reload.
492 &IDS::call_suricatactrl("reload");
493 }
494
495 # Reload page.
496 &reload();
497
498 # Download new ruleset.
499 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
500 # Check if the red device is active.
501 unless (-e "${General::swroot}/red/active") {
502 $errormessage = $Lang::tr{'could not download latest updates'};
503 }
504
505 # Check if enought free disk space is availabe.
506 if(&IDS::checkdiskspace()) {
507 $errormessage = "$Lang::tr{'not enough disk space'}";
508 }
509
510 # Check if any errors happend.
511 unless ($errormessage) {
512 # Lock the webpage and print notice about downloading
513 # a new ruleset.
514 &working_notice("$Lang::tr{'snort working'}");
515
516 # Call subfunction to download the ruleset.
517 if(&IDS::downloadruleset()) {
518 $errormessage = $Lang::tr{'could not download latest updates'};
519
520 # Call function to store the errormessage.
521 &IDS::_store_error_message($errormessage);
522
523 # Preform a reload of the page.
524 &reload();
525 } else {
526 # Call subfunction to launch oinkmaster.
527 &IDS::oinkmaster();
528
529 # Check if the IDS is running.
530 if(&IDS::ids_is_running()) {
531 # Call suricatactrl to perform a reload.
532 &IDS::call_suricatactrl("reload");
533 }
534
535 # Perform a reload of the page.
536 &reload();
537 }
538 }
539 # Save snort settings.
540 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
541 my %oldidssettings;
542 my $reload_page;
543 my $monitored_zones = 0;
544
545 # Read-in current (old) IDS settings.
546 &General::readhash("$idssettingsfile", \%oldidssettings);
547
548 # Prevent form name from been stored in conf file.
549 delete $cgiparams{'IDS'};
550
551 # Check if the IDS should be enabled.
552 if ($cgiparams{'ENABLE_IDS'} eq "on") {
553 # Check if any ruleset is available. Otherwise abort and display an error.
554 unless(%idsrules) {
555 $errormessage = $Lang::tr{'ids no ruleset available'};
556 }
557
558 # Loop through the array of available interfaces.
559 foreach my $zone (@network_zones) {
560 # Convert interface name into upper case.
561 my $zone_upper = uc($zone);
562
563 # Check if the IDS is enabled for this interaces.
564 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
565 # Increase count.
566 $monitored_zones++;
567 }
568 }
569
570 # Check if at least one zone should be monitored, or show an error.
571 unless ($monitored_zones >= 1) {
572 $errormessage = $Lang::tr{'ids no network zone'};
573 }
574 }
575
576 # Go on if there are no error messages.
577 if (!$errormessage) {
578 # Store settings into settings file.
579 &General::writehash("$idssettingsfile", \%cgiparams);
580 }
581
582 # Generate file to store the home net.
583 &generate_home_net_file();
584
585 # Open modify sid's file for writing.
586 open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
587
588 # Write file header.
589 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
590
591 # Check if the traffic only should be monitored.
592 unless($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
593 # Tell oinkmaster to switch all rules from alert to drop.
594 print FILE "modifysid \* \"alert\" \| \"drop\"\n";
595 }
596
597 # Close file handle.
598 close(FILE);
599
600 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
601 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
602 # Check if a ruleset exists.
603 if (%idsrules) {
604 # Lock the webpage and print message.
605 &working_notice("$Lang::tr{'snort working'}");
606
607 # Call oinkmaster to alter the ruleset.
608 &IDS::oinkmaster();
609
610 # Set reload_page to "True".
611 $reload_page="True";
612 }
613 }
614
615 # Check if the IDS currently is running.
616 if(&IDS::ids_is_running()) {
617 # Check if ENABLE_IDS is set to on.
618 if($cgiparams{'ENABLE_IDS'} eq "on") {
619 # Call suricatactrl to perform a reload of suricata.
620 &IDS::call_suricatactrl("reload");
621 } else {
622 # Call suricatactrl to stop suricata.
623 &IDS::call_suricatactrl("stop");
624 }
625 } else {
626 # Call suricatactrl to start suricata.
627 &IDS::call_suricatactrl("start");
628 }
629
630 # Check if the page should be reloaded.
631 if ($reload_page) {
632 # Perform a reload of the page.
633 &reload();
634 }
635 }
636
637 # Read-in idssettings and rulesetsettings
638 &General::readhash("$idssettingsfile", \%idssettings);
639 &General::readhash("$rulessettingsfile", \%rulessettings);
640
641 # If no autoupdate intervall has been configured yet, set default value.
642 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
643 # Set default to "weekly".
644 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
645 }
646
647 # Read-in ignored hosts.
648 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
649
650 $checked{'ENABLE_IDS'}{'off'} = '';
651 $checked{'ENABLE_IDS'}{'on'} = '';
652 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
653 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
654 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
655 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
656 $selected{'RULES'}{'nothing'} = '';
657 $selected{'RULES'}{'community'} = '';
658 $selected{'RULES'}{'emerging'} = '';
659 $selected{'RULES'}{'registered'} = '';
660 $selected{'RULES'}{'subscripted'} = '';
661 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
662 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
663 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
664 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
665 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
666
667 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
668
669 ### Java Script ###
670 print <<END
671 <script>
672 // Tiny java script function to show/hide the rules
673 // of a given category.
674 function showhide(tblname) {
675 \$("#" + tblname).toggle();
676 }
677 </script>
678 END
679 ;
680
681 &Header::openbigbox('100%', 'left', '', $errormessage);
682
683 if ($errormessage) {
684 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
685 print "<class name='base'>$errormessage\n";
686 print "&nbsp;</class>\n";
687 &Header::closebox();
688 }
689
690 # Draw current state of the IDS
691 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
692
693 # Check if the IDS is running and obtain the process-id.
694 my $pid = &IDS::ids_is_running();
695
696 # Display some useful information, if suricata daemon is running.
697 if ($pid) {
698 # Gather used memory.
699 my $memory = &get_memory_usage($pid);
700
701 print <<END;
702 <table width='95%' cellspacing='0' class='tbl'>
703 <tr>
704 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
705 </tr>
706
707 <tr>
708 <td class='base'>$Lang::tr{'guardian daemon'}</td>
709 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
710 </tr>
711
712 <tr>
713 <td class='base'></td>
714 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
715 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
716 </tr>
717
718 <tr>
719 <td class='base'></td>
720 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
721 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
722 </tr>
723 </table>
724 END
725 } else {
726 # Otherwise display a hint that the service is not launched.
727 print <<END;
728 <table width='95%' cellspacing='0' class='tbl'>
729 <tr>
730 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
731 </tr>
732
733 <tr>
734 <td class='base'>$Lang::tr{'guardian daemon'}</td>
735 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
736 </tr>
737 </table>
738 END
739 }
740 &Header::closebox();
741
742 my $rulesdate;
743
744 # Check if a ruleset allready has been downloaded.
745 if ( -f "$IDS::rulestarball"){
746 # Call stat on the filename to obtain detailed information.
747 my @Info = stat("$IDS::rulestarball");
748
749 # Grab details about the creation time.
750 $rulesdate = localtime($Info[9]);
751 }
752
753 # Draw elements for IDS configuration.
754 &Header::openbox('100%', 'center', $Lang::tr{'settings'});
755
756 print <<END
757 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
758 <table width='100%' border='0'>
759 <tr>
760 <td class='base' colspan='2'>
761 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
762 </td>
763
764 <td class='base' colspan='2'>
765 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
766 </td>
767 </tr>
768
769 <tr>
770 <td><br><br></td>
771 <td><br><br></td>
772 <td><br><br></td>
773 <td><br><br></td>
774 </tr>
775
776 <tr>
777 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
778 </tr>
779
780 <tr>
781 END
782 ;
783
784 # Loop through the array of available networks and print config options.
785 foreach my $zone (@network_zones) {
786 my $checked_input;
787 my $checked_forward;
788
789 # Convert current zone name to upper case.
790 my $zone_upper = uc($zone);
791
792 # Grab checkbox status from settings hash.
793 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
794 $checked_input = "checked = 'checked'";
795 }
796
797 print "<td class='base' width='25%'>\n";
798 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
799 print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone}</font>\n";
800 print "</td>\n";
801 }
802
803 print <<END
804 </tr>
805 </table>
806
807 <br><br>
808
809 <table width='100%'>
810 <tr>
811 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
812 </tr>
813 </table>
814 </form>
815 END
816 ;
817
818 &Header::closebox();
819
820 # Draw elements for ruleset configuration.
821 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
822
823 print <<END
824 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
825 <table width='100%' border='0'>
826 <tr>
827 <td><b>$Lang::tr{'ids rules update'}</b></td>
828 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
829 </tr>
830
831 <tr>
832 <td><select name='RULES'>
833 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
834 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
835 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
836 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
837 </select>
838 </td>
839
840 <td>
841 <select name='AUTOUPDATE_INTERVAL'>
842 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
843 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
844 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
845 </select>
846 </td>
847 </tr>
848
849 <tr>
850 <td colspan='2'>
851 <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>
852 <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>
853 </td>
854 </tr>
855
856 <tr>
857 <td colspan='2' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
858 </tr>
859
860 <tr>
861 <td>&nbsp;</td>
862
863 <td align='right'>
864 END
865 ;
866 # Check if a ruleset source has been configured yet.
867 unless($rulessettings{'RULES'}) {
868 # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
869 print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
870 } else {
871 # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
872 if (%idsrules) {
873 # Allow to press the button and show it as "update ruleset".
874 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
875 } else {
876 # Also allow to press the button, but show it as "download new ruleset".
877 print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
878 }
879 }
880 print <<END;
881 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
882 </td>
883
884 </tr>
885 </table>
886 </form>
887 END
888 ;
889
890 &Header::closebox();
891
892 #
893 # Whitelist / Ignorelist
894 #
895 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
896
897 print <<END;
898 <table width='100%'>
899 <tr>
900 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
901 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
902 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
903 </tr>
904 END
905 # Check if some hosts have been added to be ignored.
906 if (keys (%ignored)) {
907 my $col = "";
908
909 # Loop through all entries of the hash.
910 while( (my $key) = each %ignored) {
911 # Assign data array positions to some nice variable names.
912 my $address = $ignored{$key}[0];
913 my $remark = $ignored{$key}[1];
914 my $status = $ignored{$key}[2];
915
916 # Check if the key (id) number is even or not.
917 if ($cgiparams{'ID'} eq $key) {
918 $col="bgcolor='${Header::colouryellow}'";
919 } elsif ($key % 2) {
920 $col="bgcolor='$color{'color22'}'";
921 } else {
922 $col="bgcolor='$color{'color20'}'";
923 }
924
925 # Choose icon for the checkbox.
926 my $gif;
927 my $gdesc;
928
929 # Check if the status is enabled and select the correct image and description.
930 if ($status eq 'enabled' ) {
931 $gif = 'on.gif';
932 $gdesc = $Lang::tr{'click to disable'};
933 } else {
934 $gif = 'off.gif';
935 $gdesc = $Lang::tr{'click to enable'};
936 }
937
938 print <<END;
939 <tr>
940 <td width='20%' class='base' $col>$address</td>
941 <td width='65%' class='base' $col>$remark</td>
942
943 <td align='center' $col>
944 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
945 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
946 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
947 <input type='hidden' name='ID' value='$key' />
948 </form>
949 </td>
950
951 <td align='center' $col>
952 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
953 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
954 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
955 <input type='hidden' name='ID' value='$key' />
956 </form>
957 </td>
958
959 <td align='center' $col>
960 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
962 <input type='hidden' name='ID' value='$key'>
963 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
964 </form>
965 </td>
966 </tr>
967 END
968 }
969 } else {
970 # Print notice that currently no hosts are ignored.
971 print "<tr>\n";
972 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
973 print "</tr>\n";
974 }
975
976 print "</table>\n";
977
978 # Section to add new elements or edit existing ones.
979 print <<END;
980 <br>
981 <hr>
982 <br>
983
984 <div align='center'>
985 <table width='100%'>
986 END
987
988 # Assign correct headline and button text.
989 my $buttontext;
990 my $entry_address;
991 my $entry_remark;
992
993 # Check if an ID (key) has been given, in this case an existing entry should be edited.
994 if ($cgiparams{'ID'} ne '') {
995 $buttontext = $Lang::tr{'update'};
996 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
997
998 # Grab address and remark for the given key.
999 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1000 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1001 } else {
1002 $buttontext = $Lang::tr{'add'};
1003 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1004 }
1005
1006 print <<END;
1007 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1008 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1009 <tr>
1010 <td width='30%'>$Lang::tr{'ip address'}: </td>
1011 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1012
1013 <td width='30%'>$Lang::tr{'remark'}: </td>
1014 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1015 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1016 </tr>
1017 </form>
1018 </table>
1019 </div>
1020 END
1021
1022 &Header::closebox();
1023
1024 # Only show the section for configuring the ruleset if one is present.
1025 if (%idsrules) {
1026 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
1027
1028 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1029
1030 # Output display table for rule files
1031 print "<table width='100%'>\n";
1032
1033 # Loop over each rule file
1034 foreach my $rulefile (sort keys(%idsrules)) {
1035 my $rulechecked = '';
1036
1037 # Check if rule file is enabled
1038 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1039 $rulechecked = 'CHECKED';
1040 }
1041
1042 # Convert rulefile name into category name.
1043 my $categoryname = &_rulefile_to_category($rulefile);
1044
1045 # Table and rows for the rule files.
1046 print"<tr>\n";
1047 print"<td class='base' width='5%'>\n";
1048 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1049 print"</td>\n";
1050 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1051 print"<td class='base' width='5%' align='right'>\n";
1052 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
1053 print"</td>\n";
1054 print"</tr>\n";
1055
1056 # Rows which will be hidden per default and will contain the single rules.
1057 print"<tr style='display:none' id='$categoryname'>\n";
1058 print"<td colspan='3'>\n";
1059
1060 # Local vars
1061 my $lines;
1062 my $rows;
1063 my $col;
1064
1065 # New table for the single rules.
1066 print "<table width='100%'>\n";
1067
1068 # Loop over rule file rules
1069 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1070 # Local vars
1071 my $ruledefchecked = '';
1072
1073 # Skip rulefile itself.
1074 next if ($sid eq "Rulefile");
1075
1076 # If 2 rules have been displayed, start a new row
1077 if (($lines % 2) == 0) {
1078 print "</tr><tr>\n";
1079
1080 # Increase rows by once.
1081 $rows++;
1082 }
1083
1084 # Colour lines.
1085 if ($rows % 2) {
1086 $col="bgcolor='$color{'color20'}'";
1087 } else {
1088 $col="bgcolor='$color{'color22'}'";
1089 }
1090
1091 # Set rule state
1092 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1093 $ruledefchecked = 'CHECKED';
1094 }
1095
1096 # Create rule checkbox and display rule description
1097 print "<td class='base' width='5%' align='right' $col>\n";
1098 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1099 print "</td>\n";
1100 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1101
1102 # Increment rule count
1103 $lines++;
1104 }
1105
1106 # If do not have a second rule for row, create empty cell
1107 if (($lines % 2) != 0) {
1108 print "<td class='base'></td>";
1109 }
1110
1111 # Close display table
1112 print "</tr></table></td></tr>";
1113 }
1114
1115 # Close display table
1116 print "</table>";
1117
1118 print <<END
1119 <table width='100%'>
1120 <tr>
1121 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
1122 &nbsp; <!-- space for future online help link -->
1123 </td>
1124 </tr>
1125 </table>
1126 </form>
1127 END
1128 ;
1129 &Header::closebox();
1130 }
1131
1132 &Header::closebigbox();
1133 &Header::closepage();
1134
1135 #
1136 ## A function to display a notice, to lock the webpage and
1137 ## tell the user which action currently will be performed.
1138 #
1139 sub working_notice ($) {
1140 my ($message) = @_;
1141
1142 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1143 &Header::openbigbox('100%', 'left', '', $errormessage);
1144 &Header::openbox( 'Waiting', 1,);
1145 print <<END;
1146 <table>
1147 <tr>
1148 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1149 <td>$message</td>
1150 </tr>
1151 </table>
1152 END
1153 &Header::closebox();
1154 &Header::closebigbox();
1155 &Header::closepage();
1156 }
1157
1158 #
1159 ## A tiny function to perform a reload of the webpage after one second.
1160 #
1161 sub reload () {
1162 print "<meta http-equiv='refresh' content='1'>\n";
1163
1164 # Stop the script.
1165 exit;
1166 }
1167
1168 #
1169 ## Private function to read-in and parse rules of a given rulefile.
1170 #
1171 ## The given file will be read, parsed and all valid rules will be stored by ID,
1172 ## message/description and it's state in the idsrules hash.
1173 #
1174 sub readrulesfile ($) {
1175 my $rulefile = shift;
1176
1177 # Open rule file and read in contents
1178 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1179
1180 # Store file content in an array.
1181 my @lines = <RULEFILE>;
1182
1183 # Close file.
1184 close(RULEFILE);
1185
1186 # Loop over rule file contents
1187 foreach my $line (@lines) {
1188 # Remove whitespaces.
1189 chomp $line;
1190
1191 # Skip blank lines.
1192 next if ($line =~ /^\s*$/);
1193
1194 # Local vars.
1195 my $sid;
1196 my $msg;
1197
1198 # Gather rule sid and message from the ruleline.
1199 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1200 $msg = $1;
1201 $sid = $2;
1202
1203 # Check if a rule has been found.
1204 if ($sid && $msg) {
1205 # Add rule to the idsrules hash.
1206 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1207
1208 # Grab status of the rule. Check if ruleline starts with a "dash".
1209 if ($line =~ /^\#/) {
1210 # If yes, the rule is disabled.
1211 $idsrules{$rulefile}{$sid}{'State'} = "off";
1212 } else {
1213 # Otherwise the rule is enabled.
1214 $idsrules{$rulefile}{$sid}{'State'} = "on";
1215 }
1216 }
1217 }
1218 }
1219 }
1220
1221 #
1222 ## Function to get the used memory of a given process-id.
1223 #
1224 sub get_memory_usage($) {
1225 my ($pid) = @_;
1226
1227 my $memory = 0;
1228
1229 # Try to open the status file for the given process-id on the pseudo
1230 # file system proc.
1231 if (open(FILE, "/proc/$pid/status")) {
1232 # Loop through the entire file.
1233 while (<FILE>) {
1234 # Splitt current line content and store them into variables.
1235 my ($key, $value) = split(":", $_, 2);
1236
1237 # Check if the current key is the one which contains the memory usage.
1238 # The wanted one is VmRSS which contains the Real-memory (resident set)
1239 # of the entire process.
1240 if ($key eq "VmRSS") {
1241 # Found the memory usage add it to the memory variable.
1242 $memory += $value;
1243
1244 # Break the loop.
1245 last;
1246 }
1247 }
1248
1249 # Close file handle.
1250 close(FILE);
1251
1252 # Return memory usage.
1253 return $memory;
1254 }
1255
1256 # If the file could not be open, return nothing.
1257 return;
1258 }
1259
1260 #
1261 ## Function to generate the file which contains the home net information.
1262 #
1263 sub generate_home_net_file() {
1264 my %netsettings;
1265
1266 # Read-in network settings.
1267 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1268
1269 # Get available network zones.
1270 my @network_zones = &IDS::get_available_network_zones();
1271
1272 # Temporary array to store network address and prefix of the configured
1273 # networks.
1274 my @networks;
1275
1276 # Loop through the array of available network zones.
1277 foreach my $zone (@network_zones) {
1278 # Skip the red network - It never can be part to the home_net!
1279 next if($zone eq "red");
1280
1281 # Convert current zone name into upper case.
1282 $zone = uc($zone);
1283
1284 # Generate key to access the required data from the netsettings hash.
1285 my $zone_netaddress = $zone . "_NETADDRESS";
1286 my $zone_netmask = $zone . "_NETMASK";
1287
1288 # Obtain the settings from the netsettings hash.
1289 my $netaddress = $netsettings{$zone_netaddress};
1290 my $netmask = $netsettings{$zone_netmask};
1291
1292 # Convert the subnetmask into prefix notation.
1293 my $prefix = &Network::convert_netmask2prefix($netmask);
1294
1295 # Generate full network string.
1296 my $network = join("/", $netaddress,$prefix);
1297
1298 # Check if the network is valid.
1299 if(&Network::check_subnet($network)) {
1300 # Add the generated network to the array of networks.
1301 push(@networks, $network);
1302 }
1303 }
1304
1305 # Format home net declaration.
1306 my $line = "\"\[";
1307
1308 # Loop through the array of networks.
1309 foreach my $network (@networks) {
1310 # Add the network to the line.
1311 $line = "$line" . "$network";
1312
1313 # Check if the current network was the last in the array.
1314 if ($network eq $networks[-1]) {
1315 # Close the line.
1316 $line = "$line" . "\]\"";
1317 } else {
1318 # Add "," for the next network.
1319 $line = "$line" . "\,";
1320 }
1321 }
1322
1323 # Open file to store the addresses of the home net.
1324 open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1325
1326 # Print yaml header.
1327 print FILE "%YAML 1.1\n";
1328 print FILE "---\n\n";
1329
1330 # Print notice about autogenerated file.
1331 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1332
1333 # Print the generated and required HOME_NET declaration to the file.
1334 print FILE "HOME_NET:\t$line\n";
1335
1336 # Close file handle.
1337 close(FILE);
1338
1339 }
1340
1341 #
1342 ## Function to generate the rules file with whitelisted addresses.
1343 #
1344 sub GenerateIgnoreFile() {
1345 my %ignored = ();
1346
1347 # SID range 1000000-1999999 Reserved for Local Use
1348 # Put your custom rules in this range to avoid conflicts
1349 my $sid = 1500000;
1350
1351 # Read-in ignoredfile.
1352 &General::readhasharray($ignoredfile, \%ignored);
1353
1354 # Open ignorefile for writing.
1355 open(FILE, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1356
1357 # Config file header.
1358 print FILE "# Autogenerated file.\n";
1359 print FILE "# All user modifications will be overwritten.\n\n";
1360
1361 # Add all user defined addresses to the whitelist.
1362 #
1363 # Check if the hash contains any elements.
1364 if (keys (%ignored)) {
1365 # Loop through the entire hash and write the host/network
1366 # and remark to the ignore file.
1367 while ( (my $key) = each %ignored) {
1368 my $address = $ignored{$key}[0];
1369 my $remark = $ignored{$key}[1];
1370 my $status = $ignored{$key}[2];
1371
1372 # Check if the status of the entry is "enabled".
1373 if ($status eq "enabled") {
1374 # Check if the address/network is valid.
1375 if ((&General::validip($address)) || (&General::validipandmask($address))) {
1376 # Write rule line to the file to pass any traffic from this IP
1377 print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1378
1379 # Increment sid.
1380 $sid++;
1381 }
1382 }
1383 }
1384 }
1385
1386 close(FILE);
1387 }
1388
1389 #
1390 ## Function to read-in the given enabled or disables sids file.
1391 #
1392 sub read_enabled_disabled_sids_file($) {
1393 my ($file) = @_;
1394
1395 # Temporary hash to store the sids and their state. It will be
1396 # returned at the end of this function.
1397 my %temphash;
1398
1399 # Open the given filename.
1400 open(FILE, "$file") or die "Could not open $file. $!\n";
1401
1402 # Loop through the file.
1403 while(<FILE>) {
1404 # Remove newlines.
1405 chomp $_;
1406
1407 # Skip blank lines.
1408 next if ($_ =~ /^\s*$/);
1409
1410 # Skip coments.
1411 next if ($_ =~ /^\#/);
1412
1413 # Splitt line into sid and state part.
1414 my ($state, $sid) = split(" ", $_);
1415
1416 # Skip line if the sid is not numeric.
1417 next unless ($sid =~ /\d+/ );
1418
1419 # Check if the sid was enabled.
1420 if ($state eq "enablesid") {
1421 # Add the sid and its state as enabled to the temporary hash.
1422 $temphash{$sid} = "enabled";
1423 # Check if the sid was disabled.
1424 } elsif ($state eq "disablesid") {
1425 # Add the sid and its state as disabled to the temporary hash.
1426 $temphash{$sid} = "disabled";
1427 # Invalid state - skip the current sid and state.
1428 } else {
1429 next;
1430 }
1431 }
1432
1433 # Close filehandle.
1434 close(FILE);
1435
1436 # Return the hash.
1437 return %temphash;
1438 }
1439
1440 #
1441 ## Private function to convert a given rulefile to a category name.
1442 ## ( No file extension anymore and if the name contained a dot, it
1443 ## would be replaced by a underline sign.)
1444 #
1445 sub _rulefile_to_category($) {
1446 my ($filename) = @_;
1447
1448 # Splitt the filename into single chunks and store them in a
1449 # temorary array.
1450 my @parts = split(/\./, $filename);
1451
1452 # Return / Remove last element of the temporary array.
1453 # This removes the file extension.
1454 pop @parts;
1455
1456 # Join together the single elements of the temporary array.
1457 # If these are more than one, use a "underline" for joining.
1458 my $category = join '_', @parts;
1459
1460 # Return the converted filename.
1461 return $category;
1462 }