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