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