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