]>
Commit | Line | Data |
---|---|---|
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 |
22 | use strict; |
23 | ||
24 | # enable only the following on debugging purpose | |
90c2e164 CS |
25 | #use warnings; |
26 | #use CGI::Carp 'fatalsToBrowser'; | |
ac1cfefa | 27 | |
986e08d9 | 28 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
29 | require "${General::swroot}/lang.pl"; |
30 | require "${General::swroot}/header.pl"; | |
8dcebe53 | 31 | require "${General::swroot}/ids-functions.pl"; |
ac1cfefa | 32 | |
f2fdd0c1 CS |
33 | my %color = (); |
34 | my %mainsettings = (); | |
9d18656b | 35 | my %idsrules = (); |
1286e0d4 | 36 | my %idssettings=(); |
ea5c8eeb | 37 | my %rulessettings=(); |
43263ea6 | 38 | my %rulesetsources = (); |
298723b9 | 39 | my %cgiparams=(); |
ac1cfefa | 40 | my %checked=(); |
5a3e0dca | 41 | my %selected=(); |
b7e29743 | 42 | my %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. | |
50 | my @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. |
53 | if ( -e "/var/run/openvpn.pid") { | |
54 | push(@network_zones, "ovpn"); | |
55 | } | |
56 | ||
43263ea6 SS |
57 | my $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. |
63 | my %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 | # | |
78 | if (($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. |
220 | if (-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. |
236 | if (-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 | 253 | opendir(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 | 277 | closedir(DIR); |
395e3b90 | 278 | |
e5738079 SS |
279 | # Gather used rulefiles. |
280 | # | |
281 | # Check if the file for activated rulefiles is not empty. | |
b02e30fd | 282 | if(-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. |
319 | if ($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 | ||
74cc8f5a | 628 | # Write the modify sid's file and pass the taken ruleaction. |
81bae51f | 629 | &IDS::write_modify_sids_file(); |
bbb6efae | 630 | |
01d02eb6 SS |
631 | # Check if "MONITOR_TRAFFIC_ONLY" has been changed. |
632 | if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) { | |
bbb6efae SS |
633 | # Check if a ruleset exists. |
634 | if (%idsrules) { | |
635 | # Lock the webpage and print message. | |
5bd8940d | 636 | &working_notice("$Lang::tr{'ids working'}"); |
bbb6efae SS |
637 | |
638 | # Call oinkmaster to alter the ruleset. | |
639 | &IDS::oinkmaster(); | |
640 | ||
641 | # Set reload_page to "True". | |
642 | $reload_page="True"; | |
643 | } | |
644 | } | |
645 | ||
e2e7880d SS |
646 | # Check if the IDS currently is running. |
647 | if(&IDS::ids_is_running()) { | |
648 | # Check if ENABLE_IDS is set to on. | |
649 | if($cgiparams{'ENABLE_IDS'} eq "on") { | |
650 | # Call suricatactrl to perform a reload of suricata. | |
651 | &IDS::call_suricatactrl("reload"); | |
652 | } else { | |
653 | # Call suricatactrl to stop suricata. | |
654 | &IDS::call_suricatactrl("stop"); | |
655 | } | |
656 | } else { | |
657 | # Call suricatactrl to start suricata. | |
658 | &IDS::call_suricatactrl("start"); | |
659 | } | |
bbb6efae SS |
660 | |
661 | # Check if the page should be reloaded. | |
662 | if ($reload_page) { | |
663 | # Perform a reload of the page. | |
664 | &reload(); | |
665 | } | |
ac1cfefa MT |
666 | } |
667 | ||
ea5c8eeb | 668 | # Read-in idssettings and rulesetsettings |
b02e30fd SS |
669 | &General::readhash("$IDS::ids_settings_file", \%idssettings); |
670 | &General::readhash("$IDS::rules_settings_file", \%rulessettings); | |
1286e0d4 | 671 | |
ea5c8eeb SS |
672 | # If no autoupdate intervall has been configured yet, set default value. |
673 | unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) { | |
674 | # Set default to "weekly". | |
675 | $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly'; | |
676 | } | |
677 | ||
b7e29743 SS |
678 | # Read-in ignored hosts. |
679 | &General::readhasharray("$IDS::settingsdir/ignored", \%ignored); | |
680 | ||
1286e0d4 SS |
681 | $checked{'ENABLE_IDS'}{'off'} = ''; |
682 | $checked{'ENABLE_IDS'}{'on'} = ''; | |
683 | $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'"; | |
01d02eb6 SS |
684 | $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = ''; |
685 | $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = ''; | |
686 | $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'"; | |
5a3e0dca | 687 | $selected{'RULES'}{'nothing'} = ''; |
5a3e0dca | 688 | $selected{'RULES'}{'community'} = ''; |
a0fa489f | 689 | $selected{'RULES'}{'emerging'} = ''; |
5a3e0dca MT |
690 | $selected{'RULES'}{'registered'} = ''; |
691 | $selected{'RULES'}{'subscripted'} = ''; | |
ea5c8eeb | 692 | $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'"; |
eadad5fd SS |
693 | $selected{'AUTOUPDATE_INTERVAL'}{'off'} = ''; |
694 | $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = ''; | |
695 | $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = ''; | |
ea5c8eeb | 696 | $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'"; |
ac1cfefa MT |
697 | |
698 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
699 | ||
17726644 | 700 | ### Java Script ### |
e0cec9fe SS |
701 | print"<script>\n"; |
702 | ||
703 | # Java script variable declaration for show and hide. | |
704 | print"var show = \"$Lang::tr{'ids show'}\"\;\n"; | |
705 | print"var hide = \"$Lang::tr{'ids hide'}\"\;\n"; | |
706 | ||
17726644 | 707 | print <<END |
0d34a479 | 708 | // Java Script function to show/hide the text input field for |
029b8ed2 | 709 | // Oinkcode/Subscription code. |
0d34a479 SS |
710 | var update_code = function() { |
711 | if(\$('#RULES').val() == 'registered') { | |
712 | \$('#code').show(); | |
713 | } else if(\$('#RULES').val() == 'subscripted') { | |
714 | \$('#code').show(); | |
715 | } else if(\$('#RULES').val() == 'emerging_pro') { | |
716 | \$('#code').show(); | |
717 | } else { | |
718 | \$('#code').hide(); | |
719 | } | |
720 | }; | |
721 | ||
722 | // JQuery function to call corresponding function when | |
723 | // the ruleset is changed or the page is loaded for showing/hiding | |
724 | // the code area. | |
725 | \$(document).ready(function() { | |
726 | \$('#RULES').change(update_code); | |
727 | update_code(); | |
029b8ed2 SS |
728 | }); |
729 | ||
17726644 SS |
730 | // Tiny java script function to show/hide the rules |
731 | // of a given category. | |
732 | function showhide(tblname) { | |
733 | \$("#" + tblname).toggle(); | |
e0cec9fe SS |
734 | |
735 | // Get current content of the span element. | |
736 | var content = document.getElementById("span_" + tblname); | |
737 | ||
738 | if (content.innerHTML === show) { | |
739 | content.innerHTML = hide; | |
740 | } else { | |
741 | content.innerHTML = show; | |
742 | } | |
17726644 SS |
743 | } |
744 | </script> | |
745 | END | |
746 | ; | |
747 | ||
ac1cfefa MT |
748 | &Header::openbigbox('100%', 'left', '', $errormessage); |
749 | ||
750 | if ($errormessage) { | |
751 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
752 | print "<class name='base'>$errormessage\n"; | |
753 | print " </class>\n"; | |
754 | &Header::closebox(); | |
755 | } | |
756 | ||
87660964 | 757 | # Draw current state of the IDS |
7cc8a0e5 | 758 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'}); |
1504a375 | 759 | |
87660964 SS |
760 | # Check if the IDS is running and obtain the process-id. |
761 | my $pid = &IDS::ids_is_running(); | |
762 | ||
763 | # Display some useful information, if suricata daemon is running. | |
764 | if ($pid) { | |
765 | # Gather used memory. | |
766 | my $memory = &get_memory_usage($pid); | |
767 | ||
768 | print <<END; | |
769 | <table width='95%' cellspacing='0' class='tbl'> | |
770 | <tr> | |
771 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
772 | </tr> | |
773 | ||
774 | <tr> | |
775 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
776 | <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td> | |
777 | </tr> | |
778 | ||
779 | <tr> | |
780 | <td class='base'></td> | |
781 | <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td> | |
782 | <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td> | |
783 | </tr> | |
784 | ||
785 | <tr> | |
786 | <td class='base'></td> | |
787 | <td bgcolor='$color{'color22'}' align='center'>$pid</td> | |
788 | <td bgcolor='$color{'color22'}' align='center'>$memory KB</td> | |
789 | </tr> | |
790 | </table> | |
791 | END | |
792 | } else { | |
793 | # Otherwise display a hint that the service is not launched. | |
794 | print <<END; | |
795 | <table width='95%' cellspacing='0' class='tbl'> | |
796 | <tr> | |
797 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
798 | </tr> | |
799 | ||
800 | <tr> | |
801 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
802 | <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td> | |
803 | </tr> | |
804 | </table> | |
87660964 SS |
805 | END |
806 | } | |
87660964 | 807 | |
cf02bf2f SS |
808 | # Only show this area, if a ruleset is present. |
809 | if (%idsrules) { | |
674912fc | 810 | |
cf02bf2f | 811 | print <<END |
674912fc | 812 | |
cf02bf2f | 813 | <br><br><h2>$Lang::tr{'settings'}</h2> |
1286e0d4 | 814 | |
cf02bf2f SS |
815 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
816 | <table width='100%' border='0'> | |
817 | <tr> | |
818 | <td class='base' colspan='2'> | |
422dc4ca | 819 | <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'} |
cf02bf2f SS |
820 | </td> |
821 | ||
822 | <td class='base' colspan='2'> | |
422dc4ca | 823 | <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'} |
1504a375 | 824 | </td> |
cf02bf2f | 825 | </tr> |
1504a375 | 826 | |
cf02bf2f SS |
827 | <tr> |
828 | <td><br><br></td> | |
829 | <td><br><br></td> | |
830 | <td><br><br></td> | |
831 | <td><br><br></td> | |
832 | </tr> | |
a4ccfcbb | 833 | |
cf02bf2f SS |
834 | <tr> |
835 | <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td> | |
836 | </tr> | |
a4ccfcbb | 837 | |
cf02bf2f | 838 | <tr> |
ac1cfefa MT |
839 | END |
840 | ; | |
1504a375 | 841 | |
cf02bf2f SS |
842 | # Loop through the array of available networks and print config options. |
843 | foreach my $zone (@network_zones) { | |
844 | my $checked_input; | |
845 | my $checked_forward; | |
1504a375 | 846 | |
cf02bf2f SS |
847 | # Convert current zone name to upper case. |
848 | my $zone_upper = uc($zone); | |
1504a375 | 849 | |
cf02bf2f SS |
850 | # Set zone name. |
851 | my $zone_name = $zone; | |
53817b89 | 852 | |
cf02bf2f SS |
853 | # Dirty hack to get the correct language string for the red zone. |
854 | if ($zone eq "red") { | |
855 | $zone_name = "red1"; | |
856 | } | |
53817b89 | 857 | |
cf02bf2f SS |
858 | # Grab checkbox status from settings hash. |
859 | if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") { | |
860 | $checked_input = "checked = 'checked'"; | |
861 | } | |
1504a375 | 862 | |
51b63b41 | 863 | print "<td class='base' width='20%'>\n"; |
cf02bf2f | 864 | print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n"; |
422dc4ca | 865 | print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n"; |
cf02bf2f SS |
866 | print "</td>\n"; |
867 | } | |
1b73b07e | 868 | |
ac1cfefa | 869 | print <<END |
cf02bf2f SS |
870 | </tr> |
871 | </table> | |
1504a375 | 872 | |
cf02bf2f | 873 | <br><br> |
ea5c8eeb | 874 | |
cf02bf2f SS |
875 | <table width='100%'> |
876 | <tr> | |
877 | <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td> | |
878 | </tr> | |
879 | </table> | |
880 | </form> | |
ea5c8eeb SS |
881 | END |
882 | ; | |
883 | ||
cf02bf2f SS |
884 | } |
885 | ||
ea5c8eeb SS |
886 | &Header::closebox(); |
887 | ||
888 | # Draw elements for ruleset configuration. | |
889 | &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'}); | |
890 | ||
ea5c8eeb SS |
891 | print <<END |
892 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
893 | <table width='100%' border='0'> | |
1504a375 | 894 | <tr> |
ea5c8eeb SS |
895 | <td><b>$Lang::tr{'ids rules update'}</b></td> |
896 | <td><b>$Lang::tr{'ids automatic rules update'}</b></td> | |
1504a375 SS |
897 | </tr> |
898 | ||
899 | <tr> | |
029b8ed2 | 900 | <td><select name='RULES' id='RULES'> |
a0fa489f | 901 | <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option> |
34a38438 | 902 | <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option> |
5a3e0dca MT |
903 | <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option> |
904 | <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option> | |
905 | <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option> | |
906 | </select> | |
1504a375 | 907 | </td> |
eadad5fd | 908 | |
ea5c8eeb | 909 | <td> |
eadad5fd | 910 | <select name='AUTOUPDATE_INTERVAL'> |
b7a9b4ed SS |
911 | <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option> |
912 | <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option> | |
913 | <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option> | |
eadad5fd SS |
914 | </select> |
915 | </td> | |
1504a375 SS |
916 | </tr> |
917 | ||
918 | <tr> | |
bc4a2223 | 919 | <td colspan='2'><br><br></td> |
1504a375 SS |
920 | </tr> |
921 | ||
029b8ed2 SS |
922 | <tr style='display:none' id='code'> |
923 | <td colspan='2'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td> | |
1504a375 SS |
924 | </tr> |
925 | ||
926 | <tr> | |
ea5c8eeb | 927 | <td> </td> |
1504a375 | 928 | |
ea5c8eeb SS |
929 | <td align='right'> |
930 | END | |
931 | ; | |
5f2145eb SS |
932 | # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled. |
933 | if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) { | |
d618d67e | 934 | # Display button to update the ruleset. |
ea5c8eeb | 935 | print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n"; |
ea5c8eeb SS |
936 | } |
937 | print <<END; | |
938 | <input type='submit' name='RULESET' value='$Lang::tr{'save'}'> | |
939 | </td> | |
1504a375 | 940 | |
1504a375 SS |
941 | </tr> |
942 | </table> | |
ac1cfefa MT |
943 | </form> |
944 | END | |
945 | ; | |
946 | ||
ac1cfefa | 947 | &Header::closebox(); |
fbfdb241 | 948 | |
b7e29743 SS |
949 | # |
950 | # Whitelist / Ignorelist | |
951 | # | |
d0f9526b | 952 | &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'}); |
b7e29743 SS |
953 | |
954 | print <<END; | |
955 | <table width='100%'> | |
956 | <tr> | |
957 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td> | |
958 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td> | |
959 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
960 | </tr> | |
961 | END | |
962 | # Check if some hosts have been added to be ignored. | |
963 | if (keys (%ignored)) { | |
964 | my $col = ""; | |
965 | ||
966 | # Loop through all entries of the hash. | |
967 | while( (my $key) = each %ignored) { | |
968 | # Assign data array positions to some nice variable names. | |
969 | my $address = $ignored{$key}[0]; | |
970 | my $remark = $ignored{$key}[1]; | |
971 | my $status = $ignored{$key}[2]; | |
972 | ||
973 | # Check if the key (id) number is even or not. | |
974 | if ($cgiparams{'ID'} eq $key) { | |
975 | $col="bgcolor='${Header::colouryellow}'"; | |
976 | } elsif ($key % 2) { | |
977 | $col="bgcolor='$color{'color22'}'"; | |
978 | } else { | |
979 | $col="bgcolor='$color{'color20'}'"; | |
980 | } | |
981 | ||
982 | # Choose icon for the checkbox. | |
983 | my $gif; | |
984 | my $gdesc; | |
985 | ||
986 | # Check if the status is enabled and select the correct image and description. | |
987 | if ($status eq 'enabled' ) { | |
988 | $gif = 'on.gif'; | |
989 | $gdesc = $Lang::tr{'click to disable'}; | |
990 | } else { | |
991 | $gif = 'off.gif'; | |
992 | $gdesc = $Lang::tr{'click to enable'}; | |
993 | } | |
994 | ||
995 | print <<END; | |
996 | <tr> | |
997 | <td width='20%' class='base' $col>$address</td> | |
998 | <td width='65%' class='base' $col>$remark</td> | |
999 | ||
1000 | <td align='center' $col> | |
1001 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1002 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' /> | |
1003 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' /> | |
1004 | <input type='hidden' name='ID' value='$key' /> | |
1005 | </form> | |
1006 | </td> | |
1007 | ||
1008 | <td align='center' $col> | |
1009 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1010 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' /> | |
1011 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' /> | |
1012 | <input type='hidden' name='ID' value='$key' /> | |
1013 | </form> | |
1014 | </td> | |
1015 | ||
1016 | <td align='center' $col> | |
1017 | <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'> | |
1018 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
1019 | <input type='hidden' name='ID' value='$key'> | |
1020 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'> | |
1021 | </form> | |
1022 | </td> | |
1023 | </tr> | |
1024 | END | |
1025 | } | |
1026 | } else { | |
1027 | # Print notice that currently no hosts are ignored. | |
1028 | print "<tr>\n"; | |
1029 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
1030 | print "</tr>\n"; | |
1031 | } | |
1032 | ||
1033 | print "</table>\n"; | |
1034 | ||
1035 | # Section to add new elements or edit existing ones. | |
1036 | print <<END; | |
1037 | <br> | |
1038 | <hr> | |
1039 | <br> | |
1040 | ||
1041 | <div align='center'> | |
1042 | <table width='100%'> | |
1043 | END | |
1044 | ||
1045 | # Assign correct headline and button text. | |
1046 | my $buttontext; | |
1047 | my $entry_address; | |
1048 | my $entry_remark; | |
1049 | ||
1050 | # Check if an ID (key) has been given, in this case an existing entry should be edited. | |
1051 | if ($cgiparams{'ID'} ne '') { | |
1052 | $buttontext = $Lang::tr{'update'}; | |
1053 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n"; | |
1054 | ||
1055 | # Grab address and remark for the given key. | |
1056 | $entry_address = $ignored{$cgiparams{'ID'}}[0]; | |
1057 | $entry_remark = $ignored{$cgiparams{'ID'}}[1]; | |
1058 | } else { | |
1059 | $buttontext = $Lang::tr{'add'}; | |
1060 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n"; | |
1061 | } | |
1062 | ||
1063 | print <<END; | |
1064 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1065 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
1066 | <tr> | |
1067 | <td width='30%'>$Lang::tr{'ip address'}: </td> | |
1068 | <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td> | |
1069 | ||
1070 | <td width='30%'>$Lang::tr{'remark'}: </td> | |
1071 | <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td> | |
1072 | <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td> | |
1073 | </tr> | |
1074 | </form> | |
1075 | </table> | |
1076 | </div> | |
1077 | END | |
1078 | ||
1079 | &Header::closebox(); | |
1080 | ||
80bcd4dd SS |
1081 | # Only show the section for configuring the ruleset if one is present. |
1082 | if (%idsrules) { | |
5fbd7b29 SS |
1083 | # Load neccessary perl modules for file stat and to format the timestamp. |
1084 | use File::stat; | |
1085 | use POSIX qw( strftime ); | |
1086 | ||
1087 | # Call stat on the rulestarball. | |
1088 | my $stat = stat("$IDS::rulestarball"); | |
1089 | ||
1090 | # Get timestamp the file creation. | |
1091 | my $mtime = $stat->mtime; | |
1092 | ||
1093 | # Convert into human read-able format. | |
1094 | my $rulesdate = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime)); | |
1095 | ||
1096 | &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" ); | |
298723b9 | 1097 | |
80bcd4dd | 1098 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
ce0e83b3 | 1099 | |
80bcd4dd SS |
1100 | # Output display table for rule files |
1101 | print "<table width='100%'>\n"; | |
f7fcd1c0 | 1102 | |
80bcd4dd SS |
1103 | # Loop over each rule file |
1104 | foreach my $rulefile (sort keys(%idsrules)) { | |
1105 | my $rulechecked = ''; | |
3ffee04b | 1106 | |
80bcd4dd SS |
1107 | # Check if rule file is enabled |
1108 | if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') { | |
1109 | $rulechecked = 'CHECKED'; | |
1110 | } | |
1111 | ||
0a1bba1a SS |
1112 | # Convert rulefile name into category name. |
1113 | my $categoryname = &_rulefile_to_category($rulefile); | |
1114 | ||
80bcd4dd SS |
1115 | # Table and rows for the rule files. |
1116 | print"<tr>\n"; | |
1117 | print"<td class='base' width='5%'>\n"; | |
1118 | print"<input type='checkbox' name='$rulefile' $rulechecked>\n"; | |
1119 | print"</td>\n"; | |
1120 | print"<td class='base' width='90%'><b>$rulefile</b></td>\n"; | |
1121 | print"<td class='base' width='5%' align='right'>\n"; | |
e0cec9fe | 1122 | print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n"; |
80bcd4dd SS |
1123 | print"</td>\n"; |
1124 | print"</tr>\n"; | |
1125 | ||
1126 | # Rows which will be hidden per default and will contain the single rules. | |
0a1bba1a | 1127 | print"<tr style='display:none' id='$categoryname'>\n"; |
80bcd4dd | 1128 | print"<td colspan='3'>\n"; |
17726644 | 1129 | |
f7fcd1c0 | 1130 | # Local vars |
80bcd4dd SS |
1131 | my $lines; |
1132 | my $rows; | |
1133 | my $col; | |
f9c2147d | 1134 | |
80bcd4dd SS |
1135 | # New table for the single rules. |
1136 | print "<table width='100%'>\n"; | |
e5738079 | 1137 | |
80bcd4dd SS |
1138 | # Loop over rule file rules |
1139 | foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) { | |
1140 | # Local vars | |
1141 | my $ruledefchecked = ''; | |
3ffee04b | 1142 | |
80bcd4dd SS |
1143 | # Skip rulefile itself. |
1144 | next if ($sid eq "Rulefile"); | |
f7fcd1c0 | 1145 | |
80bcd4dd SS |
1146 | # If 2 rules have been displayed, start a new row |
1147 | if (($lines % 2) == 0) { | |
1148 | print "</tr><tr>\n"; | |
1149 | ||
1150 | # Increase rows by once. | |
1151 | $rows++; | |
1152 | } | |
1153 | ||
1154 | # Colour lines. | |
1155 | if ($rows % 2) { | |
1156 | $col="bgcolor='$color{'color20'}'"; | |
1157 | } else { | |
1158 | $col="bgcolor='$color{'color22'}'"; | |
1159 | } | |
f7fcd1c0 | 1160 | |
80bcd4dd SS |
1161 | # Set rule state |
1162 | if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') { | |
1163 | $ruledefchecked = 'CHECKED'; | |
1164 | } | |
1165 | ||
1166 | # Create rule checkbox and display rule description | |
1167 | print "<td class='base' width='5%' align='right' $col>\n"; | |
1168 | print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n"; | |
1169 | print "</td>\n"; | |
1170 | print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>"; | |
1171 | ||
1172 | # Increment rule count | |
1173 | $lines++; | |
f7fcd1c0 | 1174 | } |
3ffee04b | 1175 | |
80bcd4dd SS |
1176 | # If do not have a second rule for row, create empty cell |
1177 | if (($lines % 2) != 0) { | |
1178 | print "<td class='base'></td>"; | |
1179 | } | |
17726644 | 1180 | |
80bcd4dd SS |
1181 | # Close display table |
1182 | print "</tr></table></td></tr>"; | |
f7fcd1c0 SS |
1183 | } |
1184 | ||
1185 | # Close display table | |
80bcd4dd | 1186 | print "</table>"; |
17726644 | 1187 | |
f7fcd1c0 | 1188 | print <<END |
2999f1d2 CS |
1189 | <table width='100%'> |
1190 | <tr> | |
ee7fe87e | 1191 | <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td> |
2999f1d2 CS |
1192 | </tr> |
1193 | </table> | |
298723b9 | 1194 | </form> |
3ffee04b CS |
1195 | END |
1196 | ; | |
80bcd4dd SS |
1197 | &Header::closebox(); |
1198 | } | |
1199 | ||
ac1cfefa MT |
1200 | &Header::closebigbox(); |
1201 | &Header::closepage(); | |
1202 | ||
27760092 SS |
1203 | # |
1204 | ## A function to display a notice, to lock the webpage and | |
1205 | ## tell the user which action currently will be performed. | |
1206 | # | |
1207 | sub working_notice ($) { | |
1208 | my ($message) = @_; | |
1209 | ||
1210 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
1211 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
1212 | &Header::openbox( 'Waiting', 1,); | |
1213 | print <<END; | |
1214 | <table> | |
1215 | <tr> | |
1216 | <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td> | |
1217 | <td>$message</td> | |
1218 | </tr> | |
1219 | </table> | |
1220 | END | |
1221 | &Header::closebox(); | |
1222 | &Header::closebigbox(); | |
1223 | &Header::closepage(); | |
1224 | } | |
1225 | ||
3983aebd SS |
1226 | # |
1227 | ## A tiny function to perform a reload of the webpage after one second. | |
1228 | # | |
1229 | sub reload () { | |
1230 | print "<meta http-equiv='refresh' content='1'>\n"; | |
1231 | ||
1232 | # Stop the script. | |
1233 | exit; | |
a70d269a SS |
1234 | } |
1235 | ||
25f5cb0d SS |
1236 | # |
1237 | ## Private function to read-in and parse rules of a given rulefile. | |
1238 | # | |
1239 | ## The given file will be read, parsed and all valid rules will be stored by ID, | |
9d18656b | 1240 | ## message/description and it's state in the idsrules hash. |
25f5cb0d | 1241 | # |
3da6e01b SS |
1242 | sub readrulesfile ($) { |
1243 | my $rulefile = shift; | |
1244 | ||
1245 | # Open rule file and read in contents | |
298ef5ba | 1246 | open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!"; |
3da6e01b SS |
1247 | |
1248 | # Store file content in an array. | |
1249 | my @lines = <RULEFILE>; | |
1250 | ||
1251 | # Close file. | |
1252 | close(RULEFILE); | |
1253 | ||
1254 | # Loop over rule file contents | |
1255 | foreach my $line (@lines) { | |
1256 | # Remove whitespaces. | |
1257 | chomp $line; | |
1258 | ||
1259 | # Skip blank lines. | |
1260 | next if ($line =~ /^\s*$/); | |
1261 | ||
1262 | # Local vars. | |
1263 | my $sid; | |
1264 | my $msg; | |
1265 | ||
1266 | # Gather rule sid and message from the ruleline. | |
1267 | if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) { | |
1268 | $msg = $1; | |
1269 | $sid = $2; | |
1270 | ||
1271 | # Check if a rule has been found. | |
1272 | if ($sid && $msg) { | |
9d18656b SS |
1273 | # Add rule to the idsrules hash. |
1274 | $idsrules{$rulefile}{$sid}{'Description'} = $msg; | |
3da6e01b SS |
1275 | |
1276 | # Grab status of the rule. Check if ruleline starts with a "dash". | |
1277 | if ($line =~ /^\#/) { | |
1278 | # If yes, the rule is disabled. | |
9d18656b | 1279 | $idsrules{$rulefile}{$sid}{'State'} = "off"; |
3da6e01b SS |
1280 | } else { |
1281 | # Otherwise the rule is enabled. | |
9d18656b | 1282 | $idsrules{$rulefile}{$sid}{'State'} = "on"; |
3da6e01b SS |
1283 | } |
1284 | } | |
1285 | } | |
b7e29743 | 1286 | } |
3da6e01b | 1287 | } |
87660964 | 1288 | |
8d2f6b0b SS |
1289 | # |
1290 | ## Function to get the used memory of a given process-id. | |
1291 | # | |
87660964 | 1292 | sub get_memory_usage($) { |
004b13b7 | 1293 | my ($pid) = @_; |
87660964 | 1294 | |
004b13b7 | 1295 | my $memory = 0; |
87660964 | 1296 | |
004b13b7 | 1297 | # Try to open the status file for the given process-id on the pseudo |
87660964 | 1298 | # file system proc. |
004b13b7 SS |
1299 | if (open(FILE, "/proc/$pid/status")) { |
1300 | # Loop through the entire file. | |
1301 | while (<FILE>) { | |
1302 | # Splitt current line content and store them into variables. | |
1303 | my ($key, $value) = split(":", $_, 2); | |
1304 | ||
1305 | # Check if the current key is the one which contains the memory usage. | |
1306 | # The wanted one is VmRSS which contains the Real-memory (resident set) | |
1307 | # of the entire process. | |
1308 | if ($key eq "VmRSS") { | |
1309 | # Found the memory usage add it to the memory variable. | |
1310 | $memory += $value; | |
1311 | ||
1312 | # Break the loop. | |
1313 | last; | |
1314 | } | |
1315 | } | |
87660964 SS |
1316 | |
1317 | # Close file handle. | |
004b13b7 | 1318 | close(FILE); |
87660964 SS |
1319 | |
1320 | # Return memory usage. | |
1321 | return $memory; | |
004b13b7 | 1322 | } |
87660964 SS |
1323 | |
1324 | # If the file could not be open, return nothing. | |
1325 | return; | |
1326 | } | |
1327 | ||
a5d61752 SS |
1328 | # |
1329 | ## Function to read-in the given enabled or disables sids file. | |
1330 | # | |
1331 | sub read_enabled_disabled_sids_file($) { | |
1332 | my ($file) = @_; | |
1333 | ||
1334 | # Temporary hash to store the sids and their state. It will be | |
1335 | # returned at the end of this function. | |
1336 | my %temphash; | |
1337 | ||
1338 | # Open the given filename. | |
1339 | open(FILE, "$file") or die "Could not open $file. $!\n"; | |
1340 | ||
1341 | # Loop through the file. | |
1342 | while(<FILE>) { | |
1343 | # Remove newlines. | |
1344 | chomp $_; | |
1345 | ||
1346 | # Skip blank lines. | |
1347 | next if ($_ =~ /^\s*$/); | |
1348 | ||
1349 | # Skip coments. | |
1350 | next if ($_ =~ /^\#/); | |
1351 | ||
1352 | # Splitt line into sid and state part. | |
1353 | my ($state, $sid) = split(" ", $_); | |
1354 | ||
1355 | # Skip line if the sid is not numeric. | |
1356 | next unless ($sid =~ /\d+/ ); | |
1357 | ||
1358 | # Check if the sid was enabled. | |
1359 | if ($state eq "enablesid") { | |
1360 | # Add the sid and its state as enabled to the temporary hash. | |
1361 | $temphash{$sid} = "enabled"; | |
1362 | # Check if the sid was disabled. | |
1363 | } elsif ($state eq "disablesid") { | |
1364 | # Add the sid and its state as disabled to the temporary hash. | |
1365 | $temphash{$sid} = "disabled"; | |
1366 | # Invalid state - skip the current sid and state. | |
1367 | } else { | |
1368 | next; | |
1369 | } | |
1370 | } | |
1371 | ||
1372 | # Close filehandle. | |
1373 | close(FILE); | |
1374 | ||
1375 | # Return the hash. | |
1376 | return %temphash; | |
1377 | } | |
0a1bba1a SS |
1378 | |
1379 | # | |
1380 | ## Private function to convert a given rulefile to a category name. | |
1381 | ## ( No file extension anymore and if the name contained a dot, it | |
1382 | ## would be replaced by a underline sign.) | |
1383 | # | |
1384 | sub _rulefile_to_category($) { | |
1385 | my ($filename) = @_; | |
1386 | ||
1387 | # Splitt the filename into single chunks and store them in a | |
1388 | # temorary array. | |
1389 | my @parts = split(/\./, $filename); | |
1390 | ||
1391 | # Return / Remove last element of the temporary array. | |
1392 | # This removes the file extension. | |
1393 | pop @parts; | |
1394 | ||
1395 | # Join together the single elements of the temporary array. | |
1396 | # If these are more than one, use a "underline" for joining. | |
1397 | my $category = join '_', @parts; | |
1398 | ||
1399 | # Return the converted filename. | |
1400 | return $category; | |
1401 | } |