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