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