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