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