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