]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids.cgi: Prevent from starting suricata without ruleset or selected network zone
[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
8353c3fd
SS
422 # Allways load the whitelist.
423 print FILE " - whitelist.rules\n";
424
e5738079
SS
425 # Check if the enabled_rulefiles array contains any entries.
426 if (@enabled_rulefiles) {
b7e29743 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;
ebdd0f9a 496 my $monitored_zones = 0;
bbb6efae
SS
497
498 # Read-in current (old) IDS settings.
499 &General::readhash("$IDS::settingsdir/settings", \%oldidssettings);
500
a232b58c 501 # Prevent form name from been stored in conf file.
e0bfd338 502 delete $cgiparams{'IDS'};
a232b58c
SS
503
504 # Check if an oinkcode has been provided.
505 if ($cgiparams{'OINKCODE'}) {
506 # Check if the oinkcode contains unallowed chars.
507 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
508 $errormessage = $Lang::tr{'invalid input for oink code'};
509 }
f9c2147d 510 }
ac1cfefa 511
ebdd0f9a
SS
512 # Check if the IDS should be enabled.
513 if ($cgiparams{'ENABLE_IDS'} eq "on") {
514 # Check if any ruleset is available. Otherwise abort and display an error.
515 unless(%idsrules) {
516 $errormessage = $Lang::tr{'ids no ruleset available'};
517 }
518
519 # Loop through the array of available interfaces.
520 foreach my $zone (@network_zones) {
521 # Convert interface name into upper case.
522 my $zone_upper = uc($zone);
523
524 # Check if the IDS is enabled for this interaces.
525 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
526 # Increase count.
527 $monitored_zones++;
528 }
529 }
530
531 # Check if at least one zone should be monitored, or show an error.
532 unless ($monitored_zones >= 1) {
533 $errormessage = $Lang::tr{'ids no network zone'};
534 }
535 }
536
a232b58c
SS
537 # Go on if there are no error messages.
538 if (!$errormessage) {
539 # Store settings into settings file.
02844177 540 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
a9a91e5f 541 }
8d2f6b0b
SS
542
543 # Generate file to store the home net.
544 &generate_home_net_file();
e2e7880d 545
eadad5fd
SS
546 # Check if the the automatic rule update hass been touched.
547 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
548 # Call suricatactrl to set the new interval.
549 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
550 }
551
dae534f2
SS
552 # Check if the runmode has been changed.
553 if($cgiparams{'RUN_MODE'} ne $oldidssettings{'RUN_MODE'}) {
554 # Open modify sid's file for writing.
555 open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
bbb6efae 556
dae534f2
SS
557 # Write file header.
558 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
bbb6efae 559
dae534f2
SS
560 # Check if the configured runmode is IPS.
561 if ($cgiparams{'RUN_MODE'} eq 'IPS') {
562 # Tell oinkmaster to switch all rules from alert to drop.
563 print FILE "modifysid \* \"alert\" \| \"drop\"\n";
564 }
bbb6efae 565
dae534f2
SS
566 # Close file handle.
567 close(FILE);
bbb6efae 568
bbb6efae
SS
569 # Check if a ruleset exists.
570 if (%idsrules) {
571 # Lock the webpage and print message.
572 &working_notice("$Lang::tr{'snort working'}");
573
574 # Call oinkmaster to alter the ruleset.
575 &IDS::oinkmaster();
576
577 # Set reload_page to "True".
578 $reload_page="True";
579 }
580 }
581
e2e7880d
SS
582 # Check if the IDS currently is running.
583 if(&IDS::ids_is_running()) {
584 # Check if ENABLE_IDS is set to on.
585 if($cgiparams{'ENABLE_IDS'} eq "on") {
586 # Call suricatactrl to perform a reload of suricata.
587 &IDS::call_suricatactrl("reload");
588 } else {
589 # Call suricatactrl to stop suricata.
590 &IDS::call_suricatactrl("stop");
591 }
592 } else {
593 # Call suricatactrl to start suricata.
594 &IDS::call_suricatactrl("start");
595 }
bbb6efae
SS
596
597 # Check if the page should be reloaded.
598 if ($reload_page) {
599 # Perform a reload of the page.
600 &reload();
601 }
ac1cfefa
MT
602}
603
1286e0d4
SS
604# Read-in idssettings
605&General::readhash("$IDS::settingsdir/settings", \%idssettings);
606
a4ccfcbb
SS
607# If the runmode has not been configured yet, set default value.
608unless(exists($idssettings{'RUN_MODE'})) {
609 # Set default to IPS.
610 $idssettings{'RUN_MODE'} = 'IPS';
611}
612
b7e29743
SS
613# Read-in ignored hosts.
614&General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
615
1286e0d4
SS
616$checked{'ENABLE_IDS'}{'off'} = '';
617$checked{'ENABLE_IDS'}{'on'} = '';
618$checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
a4ccfcbb
SS
619$checked{'RUN_MODE'}{'IDS'} = '';
620$checked{'RUN_MODE'}{'IPS'} = '';
621$checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
5a3e0dca 622$selected{'RULES'}{'nothing'} = '';
5a3e0dca 623$selected{'RULES'}{'community'} = '';
a0fa489f 624$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
625$selected{'RULES'}{'registered'} = '';
626$selected{'RULES'}{'subscripted'} = '';
1286e0d4 627$selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
eadad5fd
SS
628$selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
629$selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
630$selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
631$selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
ac1cfefa
MT
632
633&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
634
17726644
SS
635### Java Script ###
636print <<END
637<script>
638 // Tiny java script function to show/hide the rules
639 // of a given category.
640 function showhide(tblname) {
641 \$("#" + tblname).toggle();
642 }
643</script>
644END
645;
646
ac1cfefa
MT
647&Header::openbigbox('100%', 'left', '', $errormessage);
648
649if ($errormessage) {
650 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
651 print "<class name='base'>$errormessage\n";
652 print "&nbsp;</class>\n";
653 &Header::closebox();
654}
655
87660964 656# Draw current state of the IDS
7cc8a0e5 657&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 658
87660964
SS
659# Check if the IDS is running and obtain the process-id.
660my $pid = &IDS::ids_is_running();
661
662# Display some useful information, if suricata daemon is running.
663if ($pid) {
664 # Gather used memory.
665 my $memory = &get_memory_usage($pid);
666
667 print <<END;
668 <table width='95%' cellspacing='0' class='tbl'>
669 <tr>
670 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
671 </tr>
672
673 <tr>
674 <td class='base'>$Lang::tr{'guardian daemon'}</td>
675 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
676 </tr>
677
678 <tr>
679 <td class='base'></td>
680 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
681 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
682 </tr>
683
684 <tr>
685 <td class='base'></td>
686 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
687 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
688 </tr>
689 </table>
690END
691} else {
692 # Otherwise display a hint that the service is not launched.
693 print <<END;
694 <table width='95%' cellspacing='0' class='tbl'>
695 <tr>
696 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
697 </tr>
698
699 <tr>
700 <td class='base'>$Lang::tr{'guardian daemon'}</td>
701 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
702 </tr>
703 </table>
87660964
SS
704END
705}
706&Header::closebox();
707
708# Draw elements for IDS configuration.
709&Header::openbox('100%', 'center', $Lang::tr{'settings'});
710
1504a375
SS
711my $rulesdate;
712
713# Check if a ruleset allready has been downloaded.
a69b96d2 714if ( -f "$IDS::rulestarball"){
1504a375 715 # Call stat on the filename to obtain detailed information.
a69b96d2 716 my @Info = stat("$IDS::rulestarball");
1504a375
SS
717
718 # Grab details about the creation time.
719 $rulesdate = localtime($Info[9]);
720}
721
ac1cfefa 722print <<END
1504a375
SS
723<form method='post' action='$ENV{'SCRIPT_NAME'}'>
724 <table width='100%' border='0'>
725 <tr>
a4ccfcbb 726 <td class='base' colspan='4'>
1286e0d4
SS
727 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
728 </td>
a4ccfcbb 729 </tr>
1286e0d4 730
a4ccfcbb
SS
731 <tr>
732 <td colspan='4'><br><br></td>
1286e0d4
SS
733 </tr>
734
735 <tr>
a4ccfcbb 736 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
1286e0d4 737 </tr>
1504a375 738
1286e0d4 739 <tr>
a4ccfcbb
SS
740 <td class='base' colspan='4'>
741 <input type='radio' name='RUN_MODE' value='IDS' $checked{'RUN_MODE'}{'IDS'}>$Lang::tr{'intrusion detection system2'} &nbsp&nbsp&nbsp
742 <input type='radio' name='RUN_MODE' value='IPS' $checked{'RUN_MODE'}{'IPS'}>$Lang::tr{'intrusion prevention system'}
1504a375 743 </td>
a4ccfcbb 744 </tr>
1504a375 745
a4ccfcbb
SS
746 <tr>
747 <td colspan='4'><br></td>
1286e0d4 748 </tr>
a4ccfcbb
SS
749
750 <tr>
751 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
752 </tr>
753
754 <tr>
ac1cfefa
MT
755END
756;
1504a375 757
1286e0d4
SS
758# Loop through the array of available networks and print config options.
759foreach my $zone (@network_zones) {
760 my $checked_input;
761 my $checked_forward;
1504a375 762
1286e0d4
SS
763 # Convert current zone name to upper case.
764 my $zone_upper = uc($zone);
1504a375 765
1286e0d4 766 # Grab checkbox status from settings hash.
a4ccfcbb 767 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1286e0d4
SS
768 $checked_input = "checked = 'checked'";
769 }
1504a375 770
1286e0d4 771 print "<td class='base' width='25%'>\n";
a4ccfcbb 772 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
1286e0d4 773 print "</td>\n";
ac1cfefa 774}
1b73b07e 775
ac1cfefa 776print <<END
1504a375
SS
777 </tr>
778
779 <tr>
780 <td colspan='4'><br><br></td>
781 </tr>
782
783 <tr>
eadad5fd
SS
784 <td colspan='2'><b>$Lang::tr{'ids rules update'}</b></td>
785 <td colspan='2'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1504a375
SS
786 </tr>
787
788 <tr>
eadad5fd 789 <td colspan='2'><select name='RULES'>
5a3e0dca 790 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 791 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
792 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
793 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
794 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
795 </select>
1504a375 796 </td>
eadad5fd
SS
797
798 <td colspan='2'>
799 <select name='AUTOUPDATE_INTERVAL'>
800 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
801 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
802 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
803 </select>
804 </td>
1504a375
SS
805 </tr>
806
807 <tr>
808 <td colspan='4'>
809 <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>
810 <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>
811 </td>
812 </tr>
813
814 <tr>
1286e0d4 815 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
1504a375
SS
816 </tr>
817
818 <tr>
819 <td colspan='4' align='left'><br>
820 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
821 </td>
822
823 </tr>
824 </table>
825
826 <br><br>
827
828 <table width='100%'>
829 <tr>
e0bfd338 830 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1504a375
SS
831 </tr>
832 </table>
ac1cfefa
MT
833</form>
834END
835;
836
ac1cfefa 837&Header::closebox();
fbfdb241 838
b7e29743
SS
839#
840# Whitelist / Ignorelist
841#
842&Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
843
844print <<END;
845 <table width='100%'>
846 <tr>
847 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
848 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
849 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
850 </tr>
851END
852 # Check if some hosts have been added to be ignored.
853 if (keys (%ignored)) {
854 my $col = "";
855
856 # Loop through all entries of the hash.
857 while( (my $key) = each %ignored) {
858 # Assign data array positions to some nice variable names.
859 my $address = $ignored{$key}[0];
860 my $remark = $ignored{$key}[1];
861 my $status = $ignored{$key}[2];
862
863 # Check if the key (id) number is even or not.
864 if ($cgiparams{'ID'} eq $key) {
865 $col="bgcolor='${Header::colouryellow}'";
866 } elsif ($key % 2) {
867 $col="bgcolor='$color{'color22'}'";
868 } else {
869 $col="bgcolor='$color{'color20'}'";
870 }
871
872 # Choose icon for the checkbox.
873 my $gif;
874 my $gdesc;
875
876 # Check if the status is enabled and select the correct image and description.
877 if ($status eq 'enabled' ) {
878 $gif = 'on.gif';
879 $gdesc = $Lang::tr{'click to disable'};
880 } else {
881 $gif = 'off.gif';
882 $gdesc = $Lang::tr{'click to enable'};
883 }
884
885print <<END;
886 <tr>
887 <td width='20%' class='base' $col>$address</td>
888 <td width='65%' class='base' $col>$remark</td>
889
890 <td align='center' $col>
891 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
892 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
893 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
894 <input type='hidden' name='ID' value='$key' />
895 </form>
896 </td>
897
898 <td align='center' $col>
899 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
900 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
901 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
902 <input type='hidden' name='ID' value='$key' />
903 </form>
904 </td>
905
906 <td align='center' $col>
907 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
908 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
909 <input type='hidden' name='ID' value='$key'>
910 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
911 </form>
912 </td>
913 </tr>
914END
915 }
916 } else {
917 # Print notice that currently no hosts are ignored.
918 print "<tr>\n";
919 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
920 print "</tr>\n";
921 }
922
923 print "</table>\n";
924
925 # Section to add new elements or edit existing ones.
926print <<END;
927 <br>
928 <hr>
929 <br>
930
931 <div align='center'>
932 <table width='100%'>
933END
934
935 # Assign correct headline and button text.
936 my $buttontext;
937 my $entry_address;
938 my $entry_remark;
939
940 # Check if an ID (key) has been given, in this case an existing entry should be edited.
941 if ($cgiparams{'ID'} ne '') {
942 $buttontext = $Lang::tr{'update'};
943 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
944
945 # Grab address and remark for the given key.
946 $entry_address = $ignored{$cgiparams{'ID'}}[0];
947 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
948 } else {
949 $buttontext = $Lang::tr{'add'};
950 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
951 }
952
953print <<END;
954 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
955 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
956 <tr>
957 <td width='30%'>$Lang::tr{'ip address'}: </td>
958 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
959
960 <td width='30%'>$Lang::tr{'remark'}: </td>
961 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
962 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
963 </tr>
964 </form>
965 </table>
966 </div>
967END
968
969&Header::closebox();
970
80bcd4dd
SS
971# Only show the section for configuring the ruleset if one is present.
972if (%idsrules) {
973 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9 974
80bcd4dd 975 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
ce0e83b3 976
80bcd4dd
SS
977 # Output display table for rule files
978 print "<table width='100%'>\n";
f7fcd1c0 979
80bcd4dd
SS
980 # Loop over each rule file
981 foreach my $rulefile (sort keys(%idsrules)) {
982 my $rulechecked = '';
3ffee04b 983
80bcd4dd
SS
984 # Check if rule file is enabled
985 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
986 $rulechecked = 'CHECKED';
987 }
988
0a1bba1a
SS
989 # Convert rulefile name into category name.
990 my $categoryname = &_rulefile_to_category($rulefile);
991
80bcd4dd
SS
992 # Table and rows for the rule files.
993 print"<tr>\n";
994 print"<td class='base' width='5%'>\n";
995 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
996 print"</td>\n";
997 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
998 print"<td class='base' width='5%' align='right'>\n";
0a1bba1a 999 print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
80bcd4dd
SS
1000 print"</td>\n";
1001 print"</tr>\n";
1002
1003 # Rows which will be hidden per default and will contain the single rules.
0a1bba1a 1004 print"<tr style='display:none' id='$categoryname'>\n";
80bcd4dd 1005 print"<td colspan='3'>\n";
17726644 1006
f7fcd1c0 1007 # Local vars
80bcd4dd
SS
1008 my $lines;
1009 my $rows;
1010 my $col;
f9c2147d 1011
80bcd4dd
SS
1012 # New table for the single rules.
1013 print "<table width='100%'>\n";
e5738079 1014
80bcd4dd
SS
1015 # Loop over rule file rules
1016 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1017 # Local vars
1018 my $ruledefchecked = '';
3ffee04b 1019
80bcd4dd
SS
1020 # Skip rulefile itself.
1021 next if ($sid eq "Rulefile");
f7fcd1c0 1022
80bcd4dd
SS
1023 # If 2 rules have been displayed, start a new row
1024 if (($lines % 2) == 0) {
1025 print "</tr><tr>\n";
1026
1027 # Increase rows by once.
1028 $rows++;
1029 }
1030
1031 # Colour lines.
1032 if ($rows % 2) {
1033 $col="bgcolor='$color{'color20'}'";
1034 } else {
1035 $col="bgcolor='$color{'color22'}'";
1036 }
f7fcd1c0 1037
80bcd4dd
SS
1038 # Set rule state
1039 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1040 $ruledefchecked = 'CHECKED';
1041 }
1042
1043 # Create rule checkbox and display rule description
1044 print "<td class='base' width='5%' align='right' $col>\n";
1045 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1046 print "</td>\n";
1047 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1048
1049 # Increment rule count
1050 $lines++;
f7fcd1c0 1051 }
3ffee04b 1052
80bcd4dd
SS
1053 # If do not have a second rule for row, create empty cell
1054 if (($lines % 2) != 0) {
1055 print "<td class='base'></td>";
1056 }
17726644 1057
80bcd4dd
SS
1058 # Close display table
1059 print "</tr></table></td></tr>";
f7fcd1c0
SS
1060 }
1061
1062 # Close display table
80bcd4dd 1063 print "</table>";
17726644 1064
f7fcd1c0 1065print <<END
2999f1d2
CS
1066<table width='100%'>
1067<tr>
298723b9 1068 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
1069 &nbsp; <!-- space for future online help link -->
1070 </td>
2999f1d2
CS
1071</tr>
1072</table>
298723b9 1073</form>
3ffee04b
CS
1074END
1075;
80bcd4dd
SS
1076 &Header::closebox();
1077}
1078
ac1cfefa
MT
1079&Header::closebigbox();
1080&Header::closepage();
1081
27760092
SS
1082#
1083## A function to display a notice, to lock the webpage and
1084## tell the user which action currently will be performed.
1085#
1086sub working_notice ($) {
1087 my ($message) = @_;
1088
1089 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1090 &Header::openbigbox('100%', 'left', '', $errormessage);
1091 &Header::openbox( 'Waiting', 1,);
1092 print <<END;
1093 <table>
1094 <tr>
1095 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1096 <td>$message</td>
1097 </tr>
1098 </table>
1099END
1100 &Header::closebox();
1101 &Header::closebigbox();
1102 &Header::closepage();
1103}
1104
3983aebd
SS
1105#
1106## A tiny function to perform a reload of the webpage after one second.
1107#
1108sub reload () {
1109 print "<meta http-equiv='refresh' content='1'>\n";
1110
1111 # Stop the script.
1112 exit;
a70d269a
SS
1113}
1114
25f5cb0d
SS
1115#
1116## Private function to read-in and parse rules of a given rulefile.
1117#
1118## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 1119## message/description and it's state in the idsrules hash.
25f5cb0d 1120#
3da6e01b
SS
1121sub readrulesfile ($) {
1122 my $rulefile = shift;
1123
1124 # Open rule file and read in contents
298ef5ba 1125 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
1126
1127 # Store file content in an array.
1128 my @lines = <RULEFILE>;
1129
1130 # Close file.
1131 close(RULEFILE);
1132
1133 # Loop over rule file contents
1134 foreach my $line (@lines) {
1135 # Remove whitespaces.
1136 chomp $line;
1137
1138 # Skip blank lines.
1139 next if ($line =~ /^\s*$/);
1140
1141 # Local vars.
1142 my $sid;
1143 my $msg;
1144
1145 # Gather rule sid and message from the ruleline.
1146 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1147 $msg = $1;
1148 $sid = $2;
1149
1150 # Check if a rule has been found.
1151 if ($sid && $msg) {
9d18656b
SS
1152 # Add rule to the idsrules hash.
1153 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
1154
1155 # Grab status of the rule. Check if ruleline starts with a "dash".
1156 if ($line =~ /^\#/) {
1157 # If yes, the rule is disabled.
9d18656b 1158 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
1159 } else {
1160 # Otherwise the rule is enabled.
9d18656b 1161 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
1162 }
1163 }
1164 }
b7e29743 1165 }
3da6e01b 1166}
87660964 1167
8d2f6b0b
SS
1168#
1169## Function to get the used memory of a given process-id.
1170#
87660964 1171sub get_memory_usage($) {
004b13b7 1172 my ($pid) = @_;
87660964 1173
004b13b7 1174 my $memory = 0;
87660964 1175
004b13b7 1176 # Try to open the status file for the given process-id on the pseudo
87660964 1177 # file system proc.
004b13b7
SS
1178 if (open(FILE, "/proc/$pid/status")) {
1179 # Loop through the entire file.
1180 while (<FILE>) {
1181 # Splitt current line content and store them into variables.
1182 my ($key, $value) = split(":", $_, 2);
1183
1184 # Check if the current key is the one which contains the memory usage.
1185 # The wanted one is VmRSS which contains the Real-memory (resident set)
1186 # of the entire process.
1187 if ($key eq "VmRSS") {
1188 # Found the memory usage add it to the memory variable.
1189 $memory += $value;
1190
1191 # Break the loop.
1192 last;
1193 }
1194 }
87660964
SS
1195
1196 # Close file handle.
004b13b7 1197 close(FILE);
87660964
SS
1198
1199 # Return memory usage.
1200 return $memory;
004b13b7 1201 }
87660964
SS
1202
1203 # If the file could not be open, return nothing.
1204 return;
1205}
1206
8d2f6b0b
SS
1207#
1208## Function to generate the file which contains the home net information.
1209#
1210sub generate_home_net_file() {
1211 my %netsettings;
1212
1213 # Read-in network settings.
1214 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1215
1216 # Get available network zones.
1217 my @network_zones = &IDS::get_available_network_zones();
1218
1219 # Temporary array to store network address and prefix of the configured
1220 # networks.
1221 my @networks;
1222
1223 # Loop through the array of available network zones.
1224 foreach my $zone (@network_zones) {
1225 # Skip the red network - It never can be part to the home_net!
1226 next if($zone eq "red");
1227
1228 # Convert current zone name into upper case.
1229 $zone = uc($zone);
1230
1231 # Generate key to access the required data from the netsettings hash.
1232 my $zone_netaddress = $zone . "_NETADDRESS";
1233 my $zone_netmask = $zone . "_NETMASK";
1234
1235 # Obtain the settings from the netsettings hash.
1236 my $netaddress = $netsettings{$zone_netaddress};
1237 my $netmask = $netsettings{$zone_netmask};
1238
1239 # Convert the subnetmask into prefix notation.
1240 my $prefix = &Network::convert_netmask2prefix($netmask);
1241
1242 # Generate full network string.
1243 my $network = join("/", $netaddress,$prefix);
1244
1245 # Check if the network is valid.
1246 if(&Network::check_subnet($network)) {
1247 # Add the generated network to the array of networks.
1248 push(@networks, $network);
1249 }
1250 }
1251
1252 # Format home net declaration.
1253 my $line = "\"\[";
1254
1255 # Loop through the array of networks.
1256 foreach my $network (@networks) {
1257 # Add the network to the line.
1258 $line = "$line" . "$network";
1259
1260 # Check if the current network was the last in the array.
1261 if ($network eq $networks[-1]) {
1262 # Close the line.
1263 $line = "$line" . "\]\"";
1264 } else {
1265 # Add "," for the next network.
1266 $line = "$line" . "\,";
1267 }
1268 }
1269
1270 # Open file to store the addresses of the home net.
1271 open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
1272
1273 # Print yaml header.
1274 print FILE "%YAML 1.1\n";
1275 print FILE "---\n\n";
1276
1277 # Print notice about autogenerated file.
1278 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1279
1280 # Print the generated and required HOME_NET declaration to the file.
1281 print FILE "HOME_NET:\t$line\n";
1282
1283 # Close file handle.
1284 close(FILE);
1285
1286}
a5d61752 1287
b7e29743
SS
1288#
1289## Function to generate the rules file with whitelisted addresses.
1290#
1291sub GenerateIgnoreFile() {
1292 my %ignored = ();
1293
1294 # SID range 1000000-1999999 Reserved for Local Use
1295 # Put your custom rules in this range to avoid conflicts
1296 my $sid = 1500000;
1297
1298 # Read-in ignoredfile.
1299 &General::readhasharray($ignoredfile, \%ignored);
1300
1301 # Open ignorefile for writing.
1302 open(FILE, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n";
1303
1304 # Config file header.
1305 print FILE "# Autogenerated file.\n";
1306 print FILE "# All user modifications will be overwritten.\n\n";
1307
1308 # Add all user defined addresses to the whitelist.
1309 #
1310 # Check if the hash contains any elements.
1311 if (keys (%ignored)) {
1312 # Loop through the entire hash and write the host/network
1313 # and remark to the ignore file.
1314 while ( (my $key) = each %ignored) {
1315 my $address = $ignored{$key}[0];
1316 my $remark = $ignored{$key}[1];
1317 my $status = $ignored{$key}[2];
1318
1319 # Check if the status of the entry is "enabled".
1320 if ($status eq "enabled") {
1321 # Check if the address/network is valid.
1322 if ((&General::validip($address)) || (&General::validipandmask($address))) {
1323 # Write rule line to the file to pass any traffic from this IP
1324 print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
1325
1326 # Increment sid.
1327 $sid++;
1328 }
1329 }
1330 }
1331 }
1332
1333 close(FILE);
1334}
1335
a5d61752
SS
1336#
1337## Function to read-in the given enabled or disables sids file.
1338#
1339sub read_enabled_disabled_sids_file($) {
1340 my ($file) = @_;
1341
1342 # Temporary hash to store the sids and their state. It will be
1343 # returned at the end of this function.
1344 my %temphash;
1345
1346 # Open the given filename.
1347 open(FILE, "$file") or die "Could not open $file. $!\n";
1348
1349 # Loop through the file.
1350 while(<FILE>) {
1351 # Remove newlines.
1352 chomp $_;
1353
1354 # Skip blank lines.
1355 next if ($_ =~ /^\s*$/);
1356
1357 # Skip coments.
1358 next if ($_ =~ /^\#/);
1359
1360 # Splitt line into sid and state part.
1361 my ($state, $sid) = split(" ", $_);
1362
1363 # Skip line if the sid is not numeric.
1364 next unless ($sid =~ /\d+/ );
1365
1366 # Check if the sid was enabled.
1367 if ($state eq "enablesid") {
1368 # Add the sid and its state as enabled to the temporary hash.
1369 $temphash{$sid} = "enabled";
1370 # Check if the sid was disabled.
1371 } elsif ($state eq "disablesid") {
1372 # Add the sid and its state as disabled to the temporary hash.
1373 $temphash{$sid} = "disabled";
1374 # Invalid state - skip the current sid and state.
1375 } else {
1376 next;
1377 }
1378 }
1379
1380 # Close filehandle.
1381 close(FILE);
1382
1383 # Return the hash.
1384 return %temphash;
1385}
0a1bba1a
SS
1386
1387#
1388## Private function to convert a given rulefile to a category name.
1389## ( No file extension anymore and if the name contained a dot, it
1390## would be replaced by a underline sign.)
1391#
1392sub _rulefile_to_category($) {
1393 my ($filename) = @_;
1394
1395 # Splitt the filename into single chunks and store them in a
1396 # temorary array.
1397 my @parts = split(/\./, $filename);
1398
1399 # Return / Remove last element of the temporary array.
1400 # This removes the file extension.
1401 pop @parts;
1402
1403 # Join together the single elements of the temporary array.
1404 # If these are more than one, use a "underline" for joining.
1405 my $category = join '_', @parts;
1406
1407 # Return the converted filename.
1408 return $category;
1409}