]>
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=(); |
2f252efa | 41 | my %used_providers=(); |
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 | # | |
ddaf8ae1 SS |
257 | |
258 | # Get enabled providers. | |
259 | my @enabled_providers = &IDS::get_enabled_providers(); | |
260 | ||
a468b62b SS |
261 | # Open rules directory and do a directory listing. |
262 | opendir(DIR, $IDS::rulespath) or die $!; | |
263 | # Loop through the direcory. | |
264 | while (my $file = readdir(DIR)) { | |
422204ff | 265 | |
a468b62b SS |
266 | # We only want files. |
267 | next unless (-f "$IDS::rulespath/$file"); | |
422204ff | 268 | |
a468b62b SS |
269 | # Ignore empty files. |
270 | next if (-z "$IDS::rulespath/$file"); | |
422204ff | 271 | |
a468b62b SS |
272 | # Use a regular expression to find files ending in .rules |
273 | next unless ($file =~ m/\.rules$/); | |
422204ff | 274 | |
a468b62b SS |
275 | # Ignore files which are not read-able. |
276 | next unless (-R "$IDS::rulespath/$file"); | |
395e3b90 | 277 | |
a468b62b SS |
278 | # Skip whitelist rules file. |
279 | next if( $file eq "whitelist.rules"); | |
b7e29743 | 280 | |
ddaf8ae1 SS |
281 | # Splitt vendor from filename. |
282 | my @filename_parts = split(/-/, $file); | |
283 | ||
284 | # Assign vendor name for easy processing. | |
285 | my $vendor = @filename_parts[0]; | |
286 | ||
287 | # Skip rulefile if the provider is disabled. | |
288 | next unless ($vendor ~~ @enabled_providers); | |
289 | ||
a468b62b SS |
290 | # Call subfunction to read-in rulefile and add rules to |
291 | # the idsrules hash. | |
292 | &readrulesfile("$file"); | |
293 | } | |
395e3b90 | 294 | |
a468b62b | 295 | closedir(DIR); |
395e3b90 | 296 | |
ddaf8ae1 SS |
297 | # Loop through the array of used providers. |
298 | foreach my $provider (@enabled_providers) { | |
299 | # Gather used rulefiles. | |
300 | my @used_rulesfiles = &IDS::read_used_provider_rulesfiles($provider); | |
301 | ||
302 | # Loop through the array of used rulesfiles. | |
303 | foreach my $rulefile (@used_rulesfiles) { | |
304 | # Check if the current rulefile exists in the %idsrules hash. | |
305 | # If not, the file probably does not exist anymore or contains | |
306 | # no rules. | |
307 | if($idsrules{$rulefile}) { | |
308 | # Add the rulefile state to the %idsrules hash. | |
309 | $idsrules{$rulefile}{'Rulefile'}{'State'} = "on"; | |
310 | } | |
e5738079 SS |
311 | } |
312 | } | |
313 | } | |
314 | ||
ea5c8eeb SS |
315 | # Save ruleset configuration. |
316 | if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) { | |
317 | my %oldsettings; | |
318 | ||
319 | # Read-in current (old) IDS settings. | |
b02e30fd | 320 | &General::readhash("$IDS::rules_settings_file", \%oldsettings); |
ea5c8eeb SS |
321 | |
322 | # Prevent form name from been stored in conf file. | |
323 | delete $cgiparams{'RULESET'}; | |
324 | ||
613f58fb | 325 | # Check if the choosen vendor (URL) requires an subscription/oinkcode. |
3e12c6e6 | 326 | if ($IDS::Ruleset::Providers{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") { |
613f58fb SS |
327 | # Check if an subscription/oinkcode has been provided. |
328 | if ($cgiparams{'OINKCODE'}) { | |
329 | # Check if the oinkcode contains unallowed chars. | |
330 | unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) { | |
331 | $errormessage = $Lang::tr{'invalid input for oink code'}; | |
332 | } | |
333 | } else { | |
334 | # Print an error message, that an subsription/oinkcode is required for this | |
335 | # vendor. | |
336 | $errormessage = $Lang::tr{'ids oinkcode required'}; | |
ea5c8eeb SS |
337 | } |
338 | } | |
339 | ||
340 | # Go on if there are no error messages. | |
341 | if (!$errormessage) { | |
342 | # Store settings into settings file. | |
b02e30fd | 343 | &General::writehash("$IDS::rules_settings_file", \%cgiparams); |
ea5c8eeb | 344 | |
f644a167 SS |
345 | # Check if a ruleset is present - if not or the source has been changed download it. |
346 | if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) { | |
347 | # Check if the red device is active. | |
348 | unless (-e "${General::swroot}/red/active") { | |
349 | $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}"; | |
5fd2e9d6 SS |
350 | } |
351 | ||
a5ba473c | 352 | # Check if enough free disk space is availabe. |
f644a167 SS |
353 | if(&IDS::checkdiskspace()) { |
354 | $errormessage = "$Lang::tr{'not enough disk space'}"; | |
97870bf2 SS |
355 | } |
356 | ||
f644a167 SS |
357 | # Check if any errors happend. |
358 | unless ($errormessage) { | |
359 | # Lock the webpage and print notice about downloading | |
360 | # a new ruleset. | |
5bd8940d | 361 | &working_notice("$Lang::tr{'ids working'}"); |
f644a167 | 362 | |
a5ba473c | 363 | # Write the modify sid's file and pass the taken ruleaction. |
81bae51f | 364 | &IDS::write_modify_sids_file(); |
a5ba473c | 365 | |
f644a167 SS |
366 | # Call subfunction to download the ruleset. |
367 | if(&IDS::downloadruleset()) { | |
368 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
369 | ||
370 | # Call function to store the errormessage. | |
371 | &IDS::_store_error_message($errormessage); | |
372 | } else { | |
373 | # Call subfunction to launch oinkmaster. | |
374 | &IDS::oinkmaster(); | |
375 | } | |
376 | ||
377 | # Check if the IDS is running. | |
378 | if(&IDS::ids_is_running()) { | |
379 | # Call suricatactrl to stop the IDS - because of the changed | |
380 | # ruleset - the use has to configure it before suricata can be | |
381 | # used again. | |
382 | &IDS::call_suricatactrl("stop"); | |
383 | } | |
384 | ||
385 | # Perform a reload of the page. | |
386 | &reload(); | |
387 | } | |
5fd2e9d6 SS |
388 | } |
389 | } | |
390 | ||
298723b9 | 391 | # Save ruleset. |
ee7fe87e | 392 | } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'ids apply'}) { |
d2212836 | 393 | # Arrays to store which rulefiles have been enabled and will be used. |
e5738079 | 394 | my @enabled_rulefiles; |
298723b9 | 395 | |
d2212836 SS |
396 | # Hash to store the user-enabled and disabled sids. |
397 | my %enabled_disabled_sids; | |
398 | ||
af8e5145 SS |
399 | # Store if a restart of suricata is required. |
400 | my $suricata_restart_required; | |
401 | ||
9d18656b SS |
402 | # Loop through the hash of idsrules. |
403 | foreach my $rulefile(keys %idsrules) { | |
1622e5c1 SS |
404 | # Check if the state of the rulefile has been changed. |
405 | unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) { | |
406 | # A restart of suricata is required to apply the changes of the used rulefiles. | |
407 | $suricata_restart_required = 1; | |
408 | } | |
409 | ||
e5738079 SS |
410 | # Check if the rulefile is enabled. |
411 | if ($cgiparams{$rulefile} eq "on") { | |
412 | # Add rulefile to the array of enabled rulefiles. | |
413 | push(@enabled_rulefiles, $rulefile); | |
b65b5ef3 SS |
414 | |
415 | # Drop item from cgiparams hash. | |
416 | delete $cgiparams{$rulefile}; | |
e5738079 | 417 | } |
466c6779 | 418 | } |
e5738079 | 419 | |
d2212836 SS |
420 | # Read-in the files for enabled/disabled sids. |
421 | # This will be done by calling the read_enabled_disabled_sids_file function two times | |
422 | # and merge the returned hashes together into the enabled_disabled_sids hash. | |
423 | %enabled_disabled_sids = ( | |
b02e30fd SS |
424 | &read_enabled_disabled_sids_file($IDS::disabled_sids_file), |
425 | &read_enabled_disabled_sids_file($IDS::enabled_sids_file)); | |
d2212836 | 426 | |
9d18656b SS |
427 | # Loop through the hash of idsrules. |
428 | foreach my $rulefile (keys %idsrules) { | |
298723b9 | 429 | # Loop through the single rules of the rulefile. |
9d18656b | 430 | foreach my $sid (keys %{$idsrules{$rulefile}}) { |
c51a044a SS |
431 | # Skip the current sid if it is not numeric. |
432 | next unless ($sid =~ /\d+/ ); | |
433 | ||
298723b9 SS |
434 | # Check if there exists a key in the cgiparams hash for this sid. |
435 | if (exists($cgiparams{$sid})) { | |
436 | # Look if the rule is disabled. | |
9d18656b | 437 | if ($idsrules{$rulefile}{$sid}{'State'} eq "off") { |
298723b9 SS |
438 | # Check if the state has been set to 'on'. |
439 | if ($cgiparams{$sid} eq "on") { | |
d2212836 SS |
440 | # Add/Modify the sid to/in the enabled_disabled_sids hash. |
441 | $enabled_disabled_sids{$sid} = "enabled"; | |
298723b9 SS |
442 | |
443 | # Drop item from cgiparams hash. | |
60333473 | 444 | delete $cgiparams{$rulefile}{$sid}; |
298723b9 SS |
445 | } |
446 | } | |
447 | } else { | |
448 | # Look if the rule is enabled. | |
9d18656b | 449 | if ($idsrules{$rulefile}{$sid}{'State'} eq "on") { |
298723b9 SS |
450 | # Check if the state is 'on' and should be disabled. |
451 | # In this case there is no entry | |
452 | # for the sid in the cgiparams hash. | |
d2212836 SS |
453 | # Add/Modify it to/in the enabled_disabled_sids hash. |
454 | $enabled_disabled_sids{$sid} = "disabled"; | |
298723b9 SS |
455 | |
456 | # Drop item from cgiparams hash. | |
60333473 | 457 | delete $cgiparams{$rulefile}{$sid}; |
298723b9 SS |
458 | } |
459 | } | |
460 | } | |
461 | } | |
462 | ||
37659505 | 463 | # Open enabled sid's file for writing. |
b02e30fd | 464 | open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n"; |
37659505 SS |
465 | |
466 | # Open disabled sid's file for writing. | |
b02e30fd | 467 | open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n"; |
d2212836 SS |
468 | |
469 | # Write header to the files. | |
470 | print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
471 | print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
472 | ||
473 | # Check if the hash for enabled/disabled files contains any entries. | |
474 | if (%enabled_disabled_sids) { | |
475 | # Loop through the hash. | |
476 | foreach my $sid (keys %enabled_disabled_sids) { | |
477 | # Check if the sid is enabled. | |
478 | if ($enabled_disabled_sids{$sid} eq "enabled") { | |
479 | # Print the sid to the enabled_sids file. | |
480 | print ENABLED_FILE "enablesid $sid\n"; | |
481 | # Check if the sid is disabled. | |
482 | } elsif ($enabled_disabled_sids{$sid} eq "disabled") { | |
483 | # Print the sid to the disabled_sids file. | |
484 | print DISABLED_FILE "disablesid $sid\n"; | |
485 | # Something strange happende - skip the current sid. | |
486 | } else { | |
487 | next; | |
488 | } | |
37659505 SS |
489 | } |
490 | } | |
298723b9 | 491 | |
d2212836 SS |
492 | # Close file for enabled_sids after writing. |
493 | close(ENABLED_FILE); | |
494 | ||
495 | # Close file for disabled_sids after writing. | |
496 | close(DISABLED_FILE); | |
e5738079 | 497 | |
ddaf8ae1 SS |
498 | # Handle enabled / disabled rulefiles. |
499 | # | |
500 | # Get enabled providers. | |
501 | my @enabled_providers = &IDS::get_enabled_providers(); | |
502 | ||
503 | # Loop through the array of enabled providers. | |
504 | foreach my $provider(@enabled_providers) { | |
505 | # Array to store the rulefiles which belong to the current processed provider. | |
506 | my @provider_rulefiles = (); | |
507 | ||
508 | # Loop through the array of enabled rulefiles. | |
509 | foreach my $rulesfile (@enabled_rulefiles) { | |
510 | # Split the rulefile name. | |
511 | my @filename_parts = split(/-/, "$rulesfile"); | |
512 | ||
513 | # Assign vendor name for easy processings. | |
514 | my $vendor = @filename_parts[0]; | |
515 | ||
516 | # Check if the rulesvendor is our current processed enabled provider. | |
517 | if ("$vendor" eq "$provider") { | |
518 | # Add the rulesfile to the array of provider rulesfiles. | |
519 | push(@provider_rulefiles, $rulesfile); | |
520 | } | |
521 | ||
522 | # Check if any rulesfiles have been found for this provider. | |
523 | if (@provider_rulefiles) { | |
524 | # Call function and write the providers used rulesfile file. | |
525 | &IDS::write_used_provider_rulefiles_file($provider, @provider_rulefiles); | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
b02e30fd | 530 | # Call function to generate and write the used rulefiles file. |
ddaf8ae1 | 531 | &IDS::write_main_used_rulefiles_file(@enabled_providers); |
52599865 | 532 | |
27760092 | 533 | # Lock the webpage and print message. |
5bd8940d | 534 | &working_notice("$Lang::tr{'ids apply ruleset changes'}"); |
27760092 | 535 | |
52599865 | 536 | # Call oinkmaster to alter the ruleset. |
27760092 SS |
537 | &IDS::oinkmaster(); |
538 | ||
e2e7880d | 539 | # Check if the IDS is running. |
5a28e721 | 540 | if(&IDS::ids_is_running()) { |
af8e5145 SS |
541 | # Check if a restart of suricata is required. |
542 | if ($suricata_restart_required) { | |
543 | # Call suricatactrl to perform the restart. | |
544 | &IDS::call_suricatactrl("restart"); | |
545 | } else { | |
546 | # Call suricatactrl to perform a reload. | |
547 | &IDS::call_suricatactrl("reload"); | |
548 | } | |
e2e7880d SS |
549 | } |
550 | ||
27760092 SS |
551 | # Reload page. |
552 | &reload(); | |
52599865 SS |
553 | |
554 | # Download new ruleset. | |
5fd2e9d6 | 555 | } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) { |
43263ea6 SS |
556 | # Check if the red device is active. |
557 | unless (-e "${General::swroot}/red/active") { | |
013274d7 | 558 | $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}"; |
43263ea6 | 559 | } |
52599865 | 560 | |
3983aebd | 561 | # Check if enought free disk space is availabe. |
434001d0 SS |
562 | if(&IDS::checkdiskspace()) { |
563 | $errormessage = "$Lang::tr{'not enough disk space'}"; | |
564 | } | |
52599865 | 565 | |
43263ea6 SS |
566 | # Check if any errors happend. |
567 | unless ($errormessage) { | |
27760092 SS |
568 | # Lock the webpage and print notice about downloading |
569 | # a new ruleset. | |
5bd8940d | 570 | &working_notice("$Lang::tr{'ids download new ruleset'}"); |
3983aebd | 571 | |
43263ea6 | 572 | # Call subfunction to download the ruleset. |
434001d0 SS |
573 | if(&IDS::downloadruleset()) { |
574 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
8f22237b | 575 | |
3983aebd | 576 | # Call function to store the errormessage. |
434001d0 | 577 | &IDS::_store_error_message($errormessage); |
52599865 | 578 | |
3983aebd SS |
579 | # Preform a reload of the page. |
580 | &reload(); | |
581 | } else { | |
582 | # Call subfunction to launch oinkmaster. | |
583 | &IDS::oinkmaster(); | |
43263ea6 | 584 | |
e2e7880d | 585 | # Check if the IDS is running. |
5a28e721 | 586 | if(&IDS::ids_is_running()) { |
e2e7880d SS |
587 | # Call suricatactrl to perform a reload. |
588 | &IDS::call_suricatactrl("reload"); | |
589 | } | |
590 | ||
3983aebd SS |
591 | # Perform a reload of the page. |
592 | &reload(); | |
593 | } | |
52599865 | 594 | } |
5bd8940d | 595 | # Save IDS settings. |
e0bfd338 | 596 | } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) { |
bbb6efae SS |
597 | my %oldidssettings; |
598 | my $reload_page; | |
ebdd0f9a | 599 | my $monitored_zones = 0; |
bbb6efae SS |
600 | |
601 | # Read-in current (old) IDS settings. | |
b02e30fd | 602 | &General::readhash("$IDS::ids_settings_file", \%oldidssettings); |
bbb6efae | 603 | |
a232b58c | 604 | # Prevent form name from been stored in conf file. |
e0bfd338 | 605 | delete $cgiparams{'IDS'}; |
a232b58c | 606 | |
ebdd0f9a SS |
607 | # Check if the IDS should be enabled. |
608 | if ($cgiparams{'ENABLE_IDS'} eq "on") { | |
609 | # Check if any ruleset is available. Otherwise abort and display an error. | |
2f252efa | 610 | unless(%used_providers) { |
ebdd0f9a SS |
611 | $errormessage = $Lang::tr{'ids no ruleset available'}; |
612 | } | |
613 | ||
614 | # Loop through the array of available interfaces. | |
615 | foreach my $zone (@network_zones) { | |
616 | # Convert interface name into upper case. | |
617 | my $zone_upper = uc($zone); | |
618 | ||
619 | # Check if the IDS is enabled for this interaces. | |
620 | if ($cgiparams{"ENABLE_IDS_$zone_upper"}) { | |
621 | # Increase count. | |
622 | $monitored_zones++; | |
623 | } | |
624 | } | |
625 | ||
626 | # Check if at least one zone should be monitored, or show an error. | |
627 | unless ($monitored_zones >= 1) { | |
628 | $errormessage = $Lang::tr{'ids no network zone'}; | |
629 | } | |
630 | } | |
631 | ||
a232b58c SS |
632 | # Go on if there are no error messages. |
633 | if (!$errormessage) { | |
634 | # Store settings into settings file. | |
b02e30fd | 635 | &General::writehash("$IDS::ids_settings_file", \%cgiparams); |
a9a91e5f | 636 | } |
8d2f6b0b | 637 | |
77351a6b SS |
638 | # Check if the the automatic rule update hass been touched. |
639 | if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) { | |
640 | # Call suricatactrl to set the new interval. | |
641 | &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'}); | |
642 | } | |
643 | ||
8d2f6b0b | 644 | # Generate file to store the home net. |
b02e30fd | 645 | &IDS::generate_home_net_file(); |
e2e7880d | 646 | |
a40ee6b9 SS |
647 | # Generate file to the store the DNS servers. |
648 | &IDS::generate_dns_servers_file(); | |
649 | ||
e698090e SS |
650 | # Generate file to store the HTTP ports. |
651 | &IDS::generate_http_ports_file(); | |
652 | ||
74cc8f5a | 653 | # Write the modify sid's file and pass the taken ruleaction. |
81bae51f | 654 | &IDS::write_modify_sids_file(); |
bbb6efae | 655 | |
01d02eb6 SS |
656 | # Check if "MONITOR_TRAFFIC_ONLY" has been changed. |
657 | if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) { | |
bbb6efae | 658 | # Check if a ruleset exists. |
2f252efa | 659 | if (%used_providers) { |
bbb6efae | 660 | # Lock the webpage and print message. |
5bd8940d | 661 | &working_notice("$Lang::tr{'ids working'}"); |
bbb6efae SS |
662 | |
663 | # Call oinkmaster to alter the ruleset. | |
664 | &IDS::oinkmaster(); | |
665 | ||
666 | # Set reload_page to "True". | |
667 | $reload_page="True"; | |
668 | } | |
669 | } | |
670 | ||
e2e7880d SS |
671 | # Check if the IDS currently is running. |
672 | if(&IDS::ids_is_running()) { | |
673 | # Check if ENABLE_IDS is set to on. | |
674 | if($cgiparams{'ENABLE_IDS'} eq "on") { | |
675 | # Call suricatactrl to perform a reload of suricata. | |
676 | &IDS::call_suricatactrl("reload"); | |
677 | } else { | |
678 | # Call suricatactrl to stop suricata. | |
679 | &IDS::call_suricatactrl("stop"); | |
680 | } | |
681 | } else { | |
682 | # Call suricatactrl to start suricata. | |
683 | &IDS::call_suricatactrl("start"); | |
684 | } | |
bbb6efae SS |
685 | |
686 | # Check if the page should be reloaded. | |
687 | if ($reload_page) { | |
688 | # Perform a reload of the page. | |
689 | &reload(); | |
690 | } | |
4c067847 | 691 | |
9bf260de SS |
692 | # Toggle Enable/Disable autoupdate for a provider |
693 | } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr{'toggle enable disable'}) { | |
694 | my %used_providers = (); | |
695 | ||
696 | # Only go further, if an ID has been passed. | |
697 | if ($cgiparams{'ID'}) { | |
698 | # Assign the given ID. | |
699 | my $id = $cgiparams{'ID'}; | |
700 | ||
701 | # Undef the given ID. | |
702 | undef($cgiparams{'ID'}); | |
703 | ||
704 | # Read-in providers settings file. | |
705 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
706 | ||
707 | # Grab the configured status of the corresponding entry. | |
708 | my $status_autoupdate = $used_providers{$id}[2]; | |
709 | ||
710 | # Switch the status. | |
711 | if ($status_autoupdate eq "disabled") { | |
712 | $status_autoupdate = "enabled"; | |
713 | } else { | |
714 | $status_autoupdate = "disabled"; | |
715 | } | |
716 | ||
717 | # Modify the status of the existing entry. | |
718 | $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"]; | |
719 | ||
720 | # Write the changed hash to the providers settings file. | |
721 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
722 | } | |
723 | ||
724 | # Add/Edit a provider to the list of used providers. | |
725 | # | |
4c067847 | 726 | } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) { |
aba3cbe5 SS |
727 | my %used_providers = (); |
728 | ||
729 | # Read-in providers settings file. | |
730 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); | |
731 | ||
4c067847 SS |
732 | # Assign some nice human-readable values. |
733 | my $provider = $cgiparams{'PROVIDER'}; | |
734 | my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'}; | |
bb4c30c6 SS |
735 | my $status_autoupdate; |
736 | ||
737 | # Handle autoupdate checkbox. | |
738 | if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") { | |
739 | $status_autoupdate = "enabled"; | |
740 | } else { | |
741 | $status_autoupdate = "disabled"; | |
742 | } | |
4c067847 SS |
743 | |
744 | # Check if we are going to add a new provider. | |
745 | if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") { | |
746 | # Loop through the hash of used providers. | |
747 | foreach my $id ( keys %used_providers) { | |
748 | # Check if the choosen provider is already in use. | |
749 | if ($used_providers{$id}[0] eq "$provider") { | |
750 | # XXX - add to language file. | |
751 | # Assign error message. | |
752 | $errormessage = "The choosen provider is already in use."; | |
753 | } | |
754 | } | |
755 | } | |
756 | ||
757 | # Check if the provider requires a subscription code. | |
758 | if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") { | |
759 | # Check if an subscription code has been provided. | |
760 | if ($subscription_code) { | |
761 | # Check if the code contains unallowed chars. | |
762 | unless ($subscription_code =~ /^[a-z0-9]+$/) { | |
763 | $errormessage = $Lang::tr{'invalid input for subscription code'}; | |
764 | } | |
765 | } else { | |
766 | # Print an error message, that an subsription code is required for this | |
767 | # provider. | |
768 | $errormessage = $Lang::tr{'ids subscription code required'}; | |
769 | } | |
770 | } | |
771 | ||
772 | # Go further if there was no error. | |
773 | if ($errormessage eq '') { | |
774 | my $id; | |
775 | my $status; | |
776 | ||
777 | # Check if we should edit an existing entry and got an ID. | |
778 | if (($cgiparams{'PROVIDERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) { | |
779 | # Assin the provided id. | |
780 | $id = $cgiparams{'ID'}; | |
781 | ||
782 | # Undef the given ID. | |
783 | undef($cgiparams{'ID'}); | |
784 | ||
785 | # Grab the configured status of the corresponding entry. | |
786 | $status = $used_providers{$id}[3]; | |
787 | } else { | |
788 | # Each newly added entry automatically should be enabled. | |
789 | $status = "enabled"; | |
790 | ||
791 | # Generate the ID for the new entry. | |
792 | # | |
793 | # Sort the keys by their ID and store them in an array. | |
794 | my @keys = sort { $a <=> $b } keys %used_providers; | |
795 | ||
796 | # Reverse the key array. | |
797 | my @reversed = reverse(@keys); | |
798 | ||
799 | # Obtain the last used id. | |
800 | my $last_id = @reversed[0]; | |
801 | ||
802 | # Increase the last id by one and use it as id for the new entry. | |
803 | $id = ++$last_id; | |
804 | } | |
805 | ||
806 | # Add/Modify the entry to/in the used providers hash.. | |
807 | $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"]; | |
808 | ||
809 | # Write the changed hash to the providers settings file. | |
810 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
811 | ||
b734df0e SS |
812 | # Check if a new provider will be added. |
813 | if ($cgiparams{'PROVIDERS'} eq $Lang::tr{'add'}) { | |
814 | # Lock the webpage and print notice about downloading | |
815 | # a new ruleset. | |
816 | &working_notice("$Lang::tr{'ids working'}"); | |
817 | ||
818 | # Download the ruleset. | |
819 | &IDS::downloadruleset($provider); | |
820 | ||
821 | # Extract the ruleset | |
822 | &IDS::extractruleset($provider); | |
823 | ||
824 | # Move the ruleset. | |
825 | &IDS::move_tmp_ruleset(); | |
826 | ||
827 | # Cleanup temporary directory. | |
828 | &IDS::cleanup_tmp_directory(); | |
829 | ||
ddaf8ae1 SS |
830 | # Create new empty file for used rulefiles |
831 | # for this provider. | |
832 | &IDS::write_used_provider_rulefiles_file($provider); | |
833 | ||
b734df0e SS |
834 | # Perform a reload of the page. |
835 | &reload(); | |
836 | } | |
837 | ||
4c067847 SS |
838 | # Undefine providers flag. |
839 | undef($cgiparams{'PROVIDERS'}); | |
840 | } | |
73eb03a3 SS |
841 | |
842 | ## Toggle Enabled/Disabled for an existing provider. | |
843 | # | |
844 | } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'toggle enable disable'}) { | |
845 | my %used_providers = (); | |
846 | ||
847 | # Only go further, if an ID has been passed. | |
848 | if ($cgiparams{'ID'}) { | |
849 | # Assign the given ID. | |
850 | my $id = $cgiparams{'ID'}; | |
851 | ||
852 | # Undef the given ID. | |
853 | undef($cgiparams{'ID'}); | |
854 | ||
855 | # Read-in file which contains the provider settings. | |
856 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
857 | ||
858 | # Grab the configured status of the corresponding entry. | |
859 | my $status = $used_providers{$id}[3]; | |
860 | ||
861 | # Switch the status. | |
862 | if ($status eq "enabled") { | |
863 | $status = "disabled"; | |
864 | } else { | |
865 | $status = "enabled"; | |
866 | } | |
867 | ||
868 | # Modify the status of the existing entry. | |
869 | $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"]; | |
870 | ||
871 | # Write the changed hash to the providers settings file. | |
872 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
873 | ||
a2b4488a SS |
874 | # Get all enabled providers. |
875 | my @enabled_providers = &IDS::get_enabled_providers(); | |
876 | ||
877 | # Write the main providers include file. | |
878 | &IDS::write_main_used_rulefiles_file(@enabled_providers); | |
879 | ||
73eb03a3 | 880 | # Check if the IDS is running. |
a2b4488a SS |
881 | if(&IDS::ids_is_running()) { |
882 | # Gather the amount of enabled providers (elements in the array). | |
883 | my $amount = @enabled_providers; | |
884 | ||
885 | # Check if there are still enabled ruleset providers. | |
886 | if ($amount >= 1) { | |
887 | # Call suricatactrl to perform a restart. | |
888 | &IDS::call_suricatactrl("restart"); | |
889 | ||
890 | # No active ruleset provider, suricata has to be stopped. | |
891 | } else { | |
892 | # Stop suricata. | |
893 | &IDS::call_suricatactrl("stop"); | |
894 | } | |
895 | } | |
73eb03a3 SS |
896 | |
897 | # Undefine providers flag. | |
898 | undef($cgiparams{'PROVIDERS'}); | |
899 | } | |
900 | ||
901 | ## Remove provider from the list of used providers. | |
902 | # | |
903 | } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'remove'}) { | |
904 | my %used_providers = (); | |
905 | ||
906 | # Read-in provider settings file. | |
907 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
908 | ||
2fded6d2 SS |
909 | # Grab the provider name bevore deleting it from hash. |
910 | my $provider = $used_providers{$cgiparams{'ID'}}[0]; | |
911 | ||
73eb03a3 SS |
912 | # Drop entry from the hash. |
913 | delete($used_providers{$cgiparams{'ID'}}); | |
914 | ||
915 | # Undef the given ID. | |
916 | undef($cgiparams{'ID'}); | |
917 | ||
918 | # Write the changed hash to the provide settings file. | |
919 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
920 | ||
2fded6d2 SS |
921 | # Drop the stored ruleset file. |
922 | &IDS::drop_dl_rulesfile($provider); | |
923 | ||
924 | # Get the name of the provider rulessets include file. | |
925 | my $provider_used_rulefile = &get_used_provider_rulesfile_file($provider); | |
926 | ||
927 | # Drop the file, it is not longer needed. | |
928 | unlink("$provider_used_rulefile"); | |
929 | ||
930 | # Regenerate ruleset. | |
931 | &IDS::oinkmaster(); | |
932 | ||
933 | # Gather all enabled providers. | |
934 | my @enabled_providers = &IDS::get_enabled_providers(); | |
935 | ||
936 | # Regenerate main providers include file. | |
937 | &IDS::write_main_used_rulefiles_file(@enabled_providers); | |
938 | ||
73eb03a3 | 939 | # Check if the IDS is running. |
2fded6d2 SS |
940 | if(&IDS::ids_is_running()) { |
941 | # Get amount of enabled providers. | |
942 | my $amount = @enabled_providers; | |
943 | ||
944 | # Check if at least one enabled provider remains. | |
945 | if ($amount >= 1) { | |
946 | # Call suricatactrl to perform a reload. | |
947 | &IDS::call_suricatactrl("restart"); | |
948 | ||
949 | # Stop suricata if no enabled provider remains. | |
950 | } else { | |
951 | # Call suricatactrel to perform the stop. | |
952 | &IDS::call_suricatactrl("stop"); | |
953 | } | |
954 | } | |
73eb03a3 SS |
955 | |
956 | # Undefine providers flag. | |
957 | undef($cgiparams{'PROVIDERS'}); | |
ac1cfefa MT |
958 | } |
959 | ||
ac1cfefa MT |
960 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); |
961 | ||
2bbe6ede | 962 | &Header::openbigbox('100%', 'left', '', $errormessage); |
e0cec9fe | 963 | |
2bbe6ede | 964 | &show_display_error_message(); |
0d34a479 | 965 | |
2bbe6ede SS |
966 | if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) { |
967 | &show_customize_ruleset(); | |
2f252efa SS |
968 | } elsif ($cgiparams{'PROVIDERS'} ne "") { |
969 | &show_add_provider(); | |
2bbe6ede SS |
970 | } else { |
971 | &show_mainpage(); | |
972 | } | |
029b8ed2 | 973 | |
2bbe6ede SS |
974 | &Header::closebigbox(); |
975 | &Header::closepage(); | |
e0cec9fe | 976 | |
2bbe6ede SS |
977 | # |
978 | ## Tiny function to show if a error message happened. | |
979 | # | |
980 | sub show_display_error_message() { | |
981 | if ($errormessage) { | |
982 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
983 | print "<class name='base'>$errormessage\n"; | |
984 | print " </class>\n"; | |
985 | &Header::closebox(); | |
986 | } | |
987 | } | |
e0cec9fe | 988 | |
2bbe6ede SS |
989 | # |
990 | ## Function to display the main IDS page. | |
991 | # | |
992 | sub show_mainpage() { | |
aba3cbe5 | 993 | # Read-in idssettings and provider settings. |
2bbe6ede | 994 | &General::readhash("$IDS::ids_settings_file", \%idssettings); |
aba3cbe5 | 995 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); |
2bbe6ede SS |
996 | |
997 | # If no autoupdate intervall has been configured yet, set default value. | |
77351a6b | 998 | unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) { |
2bbe6ede | 999 | # Set default to "weekly". |
77351a6b | 1000 | $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly'; |
17726644 | 1001 | } |
2bbe6ede SS |
1002 | |
1003 | # Read-in ignored hosts. | |
1004 | &General::readhasharray("$IDS::settingsdir/ignored", \%ignored); | |
1005 | ||
1006 | $checked{'ENABLE_IDS'}{'off'} = ''; | |
1007 | $checked{'ENABLE_IDS'}{'on'} = ''; | |
1008 | $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'"; | |
1009 | $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = ''; | |
1010 | $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = ''; | |
1011 | $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'"; | |
2bbe6ede SS |
1012 | $selected{'AUTOUPDATE_INTERVAL'}{'off'} = ''; |
1013 | $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = ''; | |
1014 | $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = ''; | |
2f252efa | 1015 | $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'"; |
17726644 | 1016 | |
2bbe6ede SS |
1017 | # Draw current state of the IDS |
1018 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'}); | |
1504a375 | 1019 | |
2bbe6ede SS |
1020 | # Check if the IDS is running and obtain the process-id. |
1021 | my $pid = &IDS::ids_is_running(); | |
87660964 | 1022 | |
2bbe6ede SS |
1023 | # Display some useful information, if suricata daemon is running. |
1024 | if ($pid) { | |
1025 | # Gather used memory. | |
1026 | my $memory = &get_memory_usage($pid); | |
87660964 | 1027 | |
2bbe6ede SS |
1028 | print <<END; |
1029 | <table width='95%' cellspacing='0' class='tbl'> | |
1030 | <tr> | |
1031 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
1032 | </tr> | |
87660964 | 1033 | |
2bbe6ede SS |
1034 | <tr> |
1035 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
1036 | <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td> | |
1037 | </tr> | |
87660964 | 1038 | |
2bbe6ede SS |
1039 | <tr> |
1040 | <td class='base'></td> | |
1041 | <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td> | |
1042 | <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td> | |
1043 | </tr> | |
87660964 | 1044 | |
2bbe6ede SS |
1045 | <tr> |
1046 | <td class='base'></td> | |
1047 | <td bgcolor='$color{'color22'}' align='center'>$pid</td> | |
1048 | <td bgcolor='$color{'color22'}' align='center'>$memory KB</td> | |
1049 | </tr> | |
1050 | </table> | |
87660964 | 1051 | END |
2bbe6ede SS |
1052 | } else { |
1053 | # Otherwise display a hint that the service is not launched. | |
1054 | print <<END; | |
1055 | <table width='95%' cellspacing='0' class='tbl'> | |
1056 | <tr> | |
1057 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
1058 | </tr> | |
87660964 | 1059 | |
2bbe6ede SS |
1060 | <tr> |
1061 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
1062 | <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td> | |
1063 | </tr> | |
1064 | </table> | |
87660964 | 1065 | END |
2bbe6ede | 1066 | } |
87660964 | 1067 | |
2f252efa SS |
1068 | # Only show this area, if at least one ruleset provider is configured. |
1069 | if (%used_providers) { | |
674912fc | 1070 | |
2bbe6ede | 1071 | print <<END |
674912fc | 1072 | |
2bbe6ede | 1073 | <br><br><h2>$Lang::tr{'settings'}</h2> |
1286e0d4 | 1074 | |
2bbe6ede SS |
1075 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
1076 | <table width='100%' border='0'> | |
1077 | <tr> | |
1078 | <td class='base' colspan='2'> | |
1079 | <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'} | |
1080 | </td> | |
cf02bf2f | 1081 | |
2bbe6ede SS |
1082 | <td class='base' colspan='2'> |
1083 | <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'} | |
1084 | </td> | |
1085 | </tr> | |
1504a375 | 1086 | |
2bbe6ede SS |
1087 | <tr> |
1088 | <td><br><br></td> | |
1089 | <td><br><br></td> | |
1090 | <td><br><br></td> | |
1091 | <td><br><br></td> | |
1092 | </tr> | |
a4ccfcbb | 1093 | |
2bbe6ede SS |
1094 | <tr> |
1095 | <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td> | |
1096 | </tr> | |
a4ccfcbb | 1097 | |
2bbe6ede | 1098 | <tr> |
ac1cfefa MT |
1099 | END |
1100 | ; | |
1504a375 | 1101 | |
2bbe6ede SS |
1102 | # Loop through the array of available networks and print config options. |
1103 | foreach my $zone (@network_zones) { | |
1104 | my $checked_input; | |
1105 | my $checked_forward; | |
1504a375 | 1106 | |
2bbe6ede SS |
1107 | # Convert current zone name to upper case. |
1108 | my $zone_upper = uc($zone); | |
1504a375 | 1109 | |
2bbe6ede SS |
1110 | # Set zone name. |
1111 | my $zone_name = $zone; | |
53817b89 | 1112 | |
2bbe6ede SS |
1113 | # Dirty hack to get the correct language string for the red zone. |
1114 | if ($zone eq "red") { | |
1115 | $zone_name = "red1"; | |
1116 | } | |
53817b89 | 1117 | |
2bbe6ede SS |
1118 | # Grab checkbox status from settings hash. |
1119 | if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") { | |
1120 | $checked_input = "checked = 'checked'"; | |
1121 | } | |
1504a375 | 1122 | |
2bbe6ede SS |
1123 | print "<td class='base' width='20%'>\n"; |
1124 | print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n"; | |
1125 | print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n"; | |
1126 | print "</td>\n"; | |
1127 | } | |
1b73b07e | 1128 | |
ac1cfefa | 1129 | print <<END |
2bbe6ede | 1130 | </tr> |
77351a6b SS |
1131 | |
1132 | <tr> | |
1133 | <td><br><br></td> | |
1134 | <td><br><br></td> | |
1135 | <td><br><br></td> | |
1136 | <td><br><br></td> | |
1137 | </tr> | |
1138 | ||
1139 | <tr> | |
1140 | <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td> | |
1141 | </tr> | |
1142 | ||
1143 | <tr> | |
1144 | <td> | |
1145 | <select name='AUTOUPDATE_INTERVAL'> | |
1146 | <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option> | |
1147 | <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option> | |
1148 | <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option> | |
1149 | </select> | |
1150 | </td> | |
1151 | </tr> | |
2bbe6ede | 1152 | </table> |
1504a375 | 1153 | |
2bbe6ede | 1154 | <br><br> |
ea5c8eeb | 1155 | |
2bbe6ede SS |
1156 | <table width='100%'> |
1157 | <tr> | |
1158 | <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td> | |
1159 | </tr> | |
1160 | </table> | |
1161 | </form> | |
ea5c8eeb SS |
1162 | END |
1163 | ; | |
1164 | ||
2bbe6ede | 1165 | } |
cf02bf2f | 1166 | |
2bbe6ede | 1167 | &Header::closebox(); |
ea5c8eeb | 1168 | |
2f252efa SS |
1169 | # |
1170 | # Used Ruleset Providers section. | |
1171 | # | |
2bbe6ede | 1172 | &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'}); |
ea5c8eeb | 1173 | |
2f252efa SS |
1174 | print <<END; |
1175 | <table width='100%' border='0'> | |
1176 | <tr> | |
1177 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td> | |
1178 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td> | |
1179 | <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td> | |
1180 | <td class='base' bgcolor='$color{'color20'}'></td> | |
1181 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
1182 | </tr> | |
3e12c6e6 | 1183 | END |
2f252efa SS |
1184 | # Check if some providers has been configured. |
1185 | if (keys (%used_providers)) { | |
1186 | my $col = ""; | |
3e12c6e6 | 1187 | |
2f252efa | 1188 | # Loop through all entries of the hash. |
2f252efa SS |
1189 | foreach my $id (sort keys(%used_providers)) { |
1190 | # Assign data array positions to some nice variable names. | |
1191 | my $provider = $used_providers{$id}[0]; | |
1192 | my $provider_name = &get_provider_name($provider); | |
3e12c6e6 | 1193 | |
2f252efa SS |
1194 | #XXX my $rulesdate = &IDS::get_ruleset_date($provider); |
1195 | my $rulesdate; | |
1196 | ||
1197 | my $subscription_code = $used_providers{$id}[1]; | |
1198 | my $autoupdate_status = $used_providers{$id}[2]; | |
1199 | my $status = $used_providers{$id}[3]; | |
1200 | ||
1201 | # Check if the item number is even or not. | |
1202 | if ($id % 2) { | |
1203 | $col="bgcolor='$color{'color22'}'"; | |
1204 | } else { | |
1205 | $col="bgcolor='$color{'color20'}'"; | |
2bbe6ede | 1206 | } |
1504a375 | 1207 | |
2f252efa SS |
1208 | # Choose icons for the checkboxes. |
1209 | my $status_gif; | |
1210 | my $status_gdesc; | |
1211 | my $autoupdate_status_gif; | |
1212 | my $autoupdate_status_gdesc; | |
1504a375 | 1213 | |
2f252efa SS |
1214 | # Check if the status is enabled and select the correct image and description. |
1215 | if ($status eq 'enabled' ) { | |
1216 | $status_gif = 'on.gif'; | |
1217 | $status_gdesc = $Lang::tr{'click to disable'}; | |
1218 | } else { | |
1219 | $status_gif = 'off.gif'; | |
1220 | $status_gdesc = $Lang::tr{'click to enable'}; | |
1221 | } | |
1504a375 | 1222 | |
2f252efa SS |
1223 | # Check if the autoupdate status is enabled and select the correct image and description. |
1224 | if ($autoupdate_status eq 'enabled') { | |
1225 | $autoupdate_status_gif = 'on.gif'; | |
1226 | $autoupdate_status_gdesc = $Lang::tr{'click to disable'}; | |
1227 | } else { | |
1228 | $autoupdate_status_gif = 'off.gif'; | |
1229 | $autoupdate_status_gdesc = $Lang::tr{'click to enable'}; | |
1230 | } | |
2bbe6ede | 1231 | |
2f252efa SS |
1232 | print <<END; |
1233 | <tr> | |
1234 | <td width='33%' class='base' $col>$provider_name</td> | |
1235 | <td width='30%' class='base' $col>$rulesdate</td> | |
1236 | ||
1237 | <td align='center' $col> | |
1238 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1239 | <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' /> | |
1240 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' /> | |
1241 | <input type='hidden' name='ID' value='$id' /> | |
1242 | </form> | |
1243 | </td> | |
1244 | ||
1245 | <td align='center' $col> | |
7323c72d | 1246 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
2f252efa SS |
1247 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'> |
1248 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'> | |
1249 | <input type='hidden' name='ID' value='$id'> | |
1250 | </form> | |
1251 | </td> | |
1252 | ||
1253 | <td align='center' $col> | |
1254 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1255 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'> | |
1256 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'> | |
1257 | <input type='hidden' name='ID' value='$id'> | |
1258 | </form> | |
1259 | </td> | |
1504a375 | 1260 | |
2f252efa SS |
1261 | <td align='center' $col> |
1262 | <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'> | |
1263 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
1264 | <input type='hidden' name='ID' value='$id'> | |
1265 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'> | |
1266 | </form> | |
1267 | </td> | |
1268 | </tr> | |
ea5c8eeb | 1269 | END |
2bbe6ede | 1270 | } |
2f252efa SS |
1271 | |
1272 | } else { | |
1273 | # Print notice that currently no hosts are ignored. | |
1274 | print "<tr>\n"; | |
1275 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
1276 | print "</tr>\n"; | |
1277 | } | |
1278 | ||
1279 | print "</table>\n"; | |
1280 | ||
1281 | # Section to add new elements or edit existing ones. | |
ea5c8eeb | 1282 | print <<END; |
2f252efa SS |
1283 | <br> |
1284 | <hr> | |
1285 | <br> | |
1504a375 | 1286 | |
2f252efa SS |
1287 | <div align='right'> |
1288 | <table width='100%'> | |
1289 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1290 | <tr> | |
1291 | END | |
1292 | ||
1293 | # Only show this button if a ruleset provider is configured. | |
1294 | if (%used_providers) { | |
1295 | print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n"; | |
1296 | } | |
1297 | print <<END; | |
1298 | <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'> | |
1299 | </tr> | |
1300 | </form> | |
2bbe6ede | 1301 | </table> |
2f252efa | 1302 | </div> |
ac1cfefa | 1303 | END |
ac1cfefa | 1304 | |
2bbe6ede | 1305 | &Header::closebox(); |
fbfdb241 | 1306 | |
2bbe6ede SS |
1307 | # |
1308 | # Whitelist / Ignorelist | |
1309 | # | |
1310 | &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'}); | |
b7e29743 | 1311 | |
2bbe6ede | 1312 | print <<END; |
b7e29743 SS |
1313 | <table width='100%'> |
1314 | <tr> | |
1315 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td> | |
1316 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td> | |
1317 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
1318 | </tr> | |
1319 | END | |
1320 | # Check if some hosts have been added to be ignored. | |
1321 | if (keys (%ignored)) { | |
1322 | my $col = ""; | |
1323 | ||
1324 | # Loop through all entries of the hash. | |
1325 | while( (my $key) = each %ignored) { | |
1326 | # Assign data array positions to some nice variable names. | |
1327 | my $address = $ignored{$key}[0]; | |
1328 | my $remark = $ignored{$key}[1]; | |
1329 | my $status = $ignored{$key}[2]; | |
1330 | ||
1331 | # Check if the key (id) number is even or not. | |
1332 | if ($cgiparams{'ID'} eq $key) { | |
1333 | $col="bgcolor='${Header::colouryellow}'"; | |
1334 | } elsif ($key % 2) { | |
1335 | $col="bgcolor='$color{'color22'}'"; | |
1336 | } else { | |
1337 | $col="bgcolor='$color{'color20'}'"; | |
1338 | } | |
1339 | ||
1340 | # Choose icon for the checkbox. | |
1341 | my $gif; | |
1342 | my $gdesc; | |
1343 | ||
1344 | # Check if the status is enabled and select the correct image and description. | |
1345 | if ($status eq 'enabled' ) { | |
1346 | $gif = 'on.gif'; | |
1347 | $gdesc = $Lang::tr{'click to disable'}; | |
1348 | } else { | |
1349 | $gif = 'off.gif'; | |
1350 | $gdesc = $Lang::tr{'click to enable'}; | |
1351 | } | |
1352 | ||
1353 | print <<END; | |
1354 | <tr> | |
1355 | <td width='20%' class='base' $col>$address</td> | |
1356 | <td width='65%' class='base' $col>$remark</td> | |
1357 | ||
1358 | <td align='center' $col> | |
1359 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
2f252efa SS |
1360 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'> |
1361 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'> | |
1362 | <input type='hidden' name='ID' value='$key'> | |
b7e29743 SS |
1363 | </form> |
1364 | </td> | |
1365 | ||
1366 | <td align='center' $col> | |
1367 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
2f252efa SS |
1368 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'> |
1369 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'> | |
1370 | <input type='hidden' name='ID' value='$key'> | |
b7e29743 SS |
1371 | </form> |
1372 | </td> | |
1373 | ||
1374 | <td align='center' $col> | |
1375 | <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'> | |
1376 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
1377 | <input type='hidden' name='ID' value='$key'> | |
1378 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'> | |
1379 | </form> | |
2bbe6ede SS |
1380 | </td> |
1381 | </tr> | |
b7e29743 | 1382 | END |
2bbe6ede SS |
1383 | } |
1384 | } else { | |
1385 | # Print notice that currently no hosts are ignored. | |
1386 | print "<tr>\n"; | |
1387 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
1388 | print "</tr>\n"; | |
b7e29743 | 1389 | } |
b7e29743 | 1390 | |
2bbe6ede | 1391 | print "</table>\n"; |
b7e29743 | 1392 | |
2bbe6ede | 1393 | # Section to add new elements or edit existing ones. |
b7e29743 | 1394 | print <<END; |
2bbe6ede SS |
1395 | <br> |
1396 | <hr> | |
1397 | <br> | |
1398 | ||
1399 | <div align='center'> | |
1400 | <table width='100%'> | |
b7e29743 SS |
1401 | END |
1402 | ||
2bbe6ede SS |
1403 | # Assign correct headline and button text. |
1404 | my $buttontext; | |
1405 | my $entry_address; | |
1406 | my $entry_remark; | |
b7e29743 | 1407 | |
2bbe6ede SS |
1408 | # Check if an ID (key) has been given, in this case an existing entry should be edited. |
1409 | if ($cgiparams{'ID'} ne '') { | |
1410 | $buttontext = $Lang::tr{'update'}; | |
1411 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n"; | |
b7e29743 | 1412 | |
2bbe6ede SS |
1413 | # Grab address and remark for the given key. |
1414 | $entry_address = $ignored{$cgiparams{'ID'}}[0]; | |
1415 | $entry_remark = $ignored{$cgiparams{'ID'}}[1]; | |
1416 | } else { | |
1417 | $buttontext = $Lang::tr{'add'}; | |
1418 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n"; | |
1419 | } | |
b7e29743 SS |
1420 | |
1421 | print <<END; | |
2bbe6ede SS |
1422 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
1423 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
1424 | <tr> | |
1425 | <td width='30%'>$Lang::tr{'ip address'}: </td> | |
1426 | <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td> | |
b7e29743 | 1427 | |
2bbe6ede SS |
1428 | <td width='30%'>$Lang::tr{'remark'}: </td> |
1429 | <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td> | |
1430 | <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td> | |
1431 | </tr> | |
1432 | </form> | |
1433 | </table> | |
1434 | </div> | |
b7e29743 SS |
1435 | END |
1436 | ||
2bbe6ede | 1437 | &Header::closebox(); |
fed57fe7 SS |
1438 | } |
1439 | ||
fed57fe7 SS |
1440 | # |
1441 | ## Function to show the customize ruleset section. | |
1442 | # | |
1443 | sub show_customize_ruleset() { | |
2bbe6ede SS |
1444 | ### Java Script ### |
1445 | print"<script>\n"; | |
1446 | ||
1447 | # Java script variable declaration for show and hide. | |
1448 | print"var show = \"$Lang::tr{'ids show'}\"\;\n"; | |
1449 | print"var hide = \"$Lang::tr{'ids hide'}\"\;\n"; | |
1450 | ||
1451 | print <<END | |
1452 | // Tiny java script function to show/hide the rules | |
1453 | // of a given category. | |
1454 | function showhide(tblname) { | |
1455 | \$("#" + tblname).toggle(); | |
1456 | ||
1457 | // Get current content of the span element. | |
1458 | var content = document.getElementById("span_" + tblname); | |
1459 | ||
1460 | if (content.innerHTML === show) { | |
1461 | content.innerHTML = hide; | |
1462 | } else { | |
1463 | content.innerHTML = show; | |
1464 | } | |
1465 | } | |
1466 | </script> | |
1467 | END | |
1468 | ; | |
87df37da | 1469 | &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" ); |
80bcd4dd | 1470 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
ce0e83b3 | 1471 | |
80bcd4dd SS |
1472 | # Output display table for rule files |
1473 | print "<table width='100%'>\n"; | |
f7fcd1c0 | 1474 | |
80bcd4dd SS |
1475 | # Loop over each rule file |
1476 | foreach my $rulefile (sort keys(%idsrules)) { | |
1477 | my $rulechecked = ''; | |
3ffee04b | 1478 | |
80bcd4dd SS |
1479 | # Check if rule file is enabled |
1480 | if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') { | |
1481 | $rulechecked = 'CHECKED'; | |
1482 | } | |
1483 | ||
0a1bba1a SS |
1484 | # Convert rulefile name into category name. |
1485 | my $categoryname = &_rulefile_to_category($rulefile); | |
1486 | ||
80bcd4dd SS |
1487 | # Table and rows for the rule files. |
1488 | print"<tr>\n"; | |
1489 | print"<td class='base' width='5%'>\n"; | |
1490 | print"<input type='checkbox' name='$rulefile' $rulechecked>\n"; | |
1491 | print"</td>\n"; | |
1492 | print"<td class='base' width='90%'><b>$rulefile</b></td>\n"; | |
1493 | print"<td class='base' width='5%' align='right'>\n"; | |
e0cec9fe | 1494 | print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n"; |
80bcd4dd SS |
1495 | print"</td>\n"; |
1496 | print"</tr>\n"; | |
1497 | ||
1498 | # Rows which will be hidden per default and will contain the single rules. | |
0a1bba1a | 1499 | print"<tr style='display:none' id='$categoryname'>\n"; |
80bcd4dd | 1500 | print"<td colspan='3'>\n"; |
17726644 | 1501 | |
f7fcd1c0 | 1502 | # Local vars |
80bcd4dd SS |
1503 | my $lines; |
1504 | my $rows; | |
1505 | my $col; | |
f9c2147d | 1506 | |
80bcd4dd SS |
1507 | # New table for the single rules. |
1508 | print "<table width='100%'>\n"; | |
e5738079 | 1509 | |
80bcd4dd SS |
1510 | # Loop over rule file rules |
1511 | foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) { | |
1512 | # Local vars | |
1513 | my $ruledefchecked = ''; | |
3ffee04b | 1514 | |
80bcd4dd SS |
1515 | # Skip rulefile itself. |
1516 | next if ($sid eq "Rulefile"); | |
f7fcd1c0 | 1517 | |
80bcd4dd SS |
1518 | # If 2 rules have been displayed, start a new row |
1519 | if (($lines % 2) == 0) { | |
1520 | print "</tr><tr>\n"; | |
1521 | ||
1522 | # Increase rows by once. | |
1523 | $rows++; | |
1524 | } | |
1525 | ||
1526 | # Colour lines. | |
1527 | if ($rows % 2) { | |
1528 | $col="bgcolor='$color{'color20'}'"; | |
1529 | } else { | |
1530 | $col="bgcolor='$color{'color22'}'"; | |
1531 | } | |
f7fcd1c0 | 1532 | |
80bcd4dd SS |
1533 | # Set rule state |
1534 | if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') { | |
1535 | $ruledefchecked = 'CHECKED'; | |
1536 | } | |
1537 | ||
1538 | # Create rule checkbox and display rule description | |
1539 | print "<td class='base' width='5%' align='right' $col>\n"; | |
1540 | print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n"; | |
1541 | print "</td>\n"; | |
1542 | print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>"; | |
1543 | ||
1544 | # Increment rule count | |
1545 | $lines++; | |
f7fcd1c0 | 1546 | } |
3ffee04b | 1547 | |
80bcd4dd SS |
1548 | # If do not have a second rule for row, create empty cell |
1549 | if (($lines % 2) != 0) { | |
1550 | print "<td class='base'></td>"; | |
1551 | } | |
17726644 | 1552 | |
80bcd4dd SS |
1553 | # Close display table |
1554 | print "</tr></table></td></tr>"; | |
f7fcd1c0 SS |
1555 | } |
1556 | ||
1557 | # Close display table | |
80bcd4dd | 1558 | print "</table>"; |
17726644 | 1559 | |
9268cddf | 1560 | print <<END |
2999f1d2 CS |
1561 | <table width='100%'> |
1562 | <tr> | |
4efc8ccd SS |
1563 | <td width='100%' align='right'> |
1564 | <input type='submit' value='$Lang::tr{'fwhost back'}'> | |
1565 | <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'> | |
1566 | </td> | |
2999f1d2 CS |
1567 | </tr> |
1568 | </table> | |
298723b9 | 1569 | </form> |
3ffee04b CS |
1570 | END |
1571 | ; | |
9268cddf MT |
1572 | &Header::closebox(); |
1573 | } | |
80bcd4dd SS |
1574 | } |
1575 | ||
2f252efa SS |
1576 | # |
1577 | ## Function to show section for add/edit a provider. | |
1578 | # | |
1579 | sub show_add_provider() { | |
aba3cbe5 | 1580 | my %used_providers = (); |
2f252efa SS |
1581 | my @subscription_providers; |
1582 | ||
aba3cbe5 SS |
1583 | # Read -in providers settings file. |
1584 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); | |
1585 | ||
2f252efa SS |
1586 | # Get all supported ruleset providers. |
1587 | my @ruleset_providers = &IDS::get_ruleset_providers(); | |
1588 | ||
1589 | ### Java Script ### | |
1590 | print "<script>\n"; | |
1591 | ||
1592 | # Generate Java Script Object which contains the URL of the providers. | |
1593 | print "\t// Object, which contains the webpages of the ruleset providers.\n"; | |
1594 | print "\tvar url = {\n"; | |
1595 | ||
1596 | # Loop through the array of supported providers. | |
1597 | foreach my $provider (@ruleset_providers) { | |
1598 | # Check if the provider requires a subscription. | |
1599 | if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") { | |
1600 | # Add the provider to the array of subscription_providers. | |
1601 | push(@subscription_providers, $provider); | |
1602 | } | |
1603 | ||
1604 | # Grab the URL for the provider. | |
1605 | my $url = $IDS::Ruleset::Providers{$provider}{'website'}; | |
1606 | ||
1607 | # Print the URL to the Java Script Object. | |
1608 | print "\t\t$provider: \"$url\"\,\n"; | |
1609 | } | |
1610 | ||
1611 | # Close the Java Script Object declaration. | |
1612 | print "\t}\;\n\n"; | |
1613 | ||
1614 | # Generate Java Script Array which contains the provider that requires a subscription. | |
1615 | my $line = ""; | |
1616 | $line = join("', '", @subscription_providers); | |
1617 | ||
1618 | print "\t// Array which contains the providers that requires a subscription.\n"; | |
1619 | print "\tsubscription_provider = ['$line']\;\n\n"; | |
1620 | ||
1621 | print <<END | |
1622 | // Java Script function to swap the text input field for | |
1623 | // entering a subscription code. | |
1624 | var update_provider = function() { | |
1625 | if(inArray(\$('#PROVIDER').val(), subscription_provider)) { | |
1626 | \$('.subscription_code').show(); | |
1627 | } else { | |
1628 | \$('.subscription_code').hide(); | |
1629 | } | |
1630 | ||
1631 | // Call function to change the website url. | |
1632 | change_url(\$('#PROVIDER').val()); | |
1633 | }; | |
1634 | ||
1635 | // Java Script function to check if a given value is part of | |
1636 | // an array. | |
1637 | function inArray(value,array) { | |
1638 | var count=array.length; | |
1639 | ||
1640 | for(var i=0;i<count;i++) { | |
1641 | if(array[i]===value){ | |
1642 | return true; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | return false; | |
1647 | } | |
1648 | ||
1649 | // Tiny function to change the website url based on the selected element in the "PROVIDERS" | |
1650 | // dropdown menu. | |
1651 | function change_url(provider) { | |
1652 | // Get and change the href to the corresponding url. | |
1653 | document.getElementById("website").href = url[provider]; | |
1654 | } | |
1655 | ||
1656 | // JQuery function to call corresponding function when | |
1657 | // the ruleset provider is changed or the page is loaded for showing/hiding | |
1658 | // the subscription_code area. | |
1659 | \$(document).ready(function() { | |
1660 | \$('#PROVIDER').change(update_provider); | |
1661 | update_provider(); | |
1662 | }); | |
1663 | ||
1664 | </script> | |
1665 | END | |
1666 | ; | |
1667 | ||
1668 | &Header::openbox('100%', 'center', $Lang::tr{'ids provider settings'}); | |
1669 | ||
1670 | # Check if an existing provider should be edited. | |
1671 | if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") { | |
1672 | # Check if autoupdate is enabled for this provider. | |
bb4c30c6 | 1673 | if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") { |
2f252efa SS |
1674 | # Set the checkbox to be checked. |
1675 | $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'"; | |
1676 | } | |
1677 | } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") { | |
1678 | # Set the autoupdate to true as default. | |
1679 | $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'"; | |
1680 | } | |
1681 | ||
1682 | print <<END | |
1683 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1684 | <table width='100%' border='0'> | |
1685 | <tr> | |
1686 | <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td> | |
1687 | </tr> | |
1688 | ||
1689 | <tr> | |
1690 | <td width='40%'> | |
1691 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
1692 | <select name='PROVIDER' id='PROVIDER'> | |
1693 | END | |
1694 | ; | |
1695 | # Loop through the array of ruleset providers. | |
1696 | foreach my $provider (@ruleset_providers) { | |
1697 | # Get the provider name. | |
1698 | my $provider_name = &get_provider_name($provider); | |
1699 | ||
1700 | # Pre-select the provider if one is given. | |
1701 | if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) { | |
1702 | $selected{$provider} = "selected='selected'"; | |
1703 | } | |
1704 | ||
1705 | # Add the provider to the dropdown menu. | |
1706 | print "<option value='$provider' $selected{$provider}>$provider_name</option>\n"; | |
1707 | } | |
1708 | print <<END | |
1709 | </select> | |
1710 | </td> | |
1711 | ||
1712 | <td width='60%'> | |
1713 | <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b> | |
1714 | </td> | |
1715 | </tr> | |
1716 | ||
1717 | <tr> | |
1718 | <td colspan='2'><br><br></td> | |
1719 | </tr> | |
1720 | ||
1721 | <tr class='subscription_code' style='display:none' id='subscription_code'> | |
1722 | <td colspan='2'> | |
1723 | <table border='0'> | |
1724 | <tr> | |
1725 | <td> | |
1726 | <b>$Lang::tr{'subscription code'}</b> | |
1727 | </td> | |
1728 | </tr> | |
1729 | ||
1730 | <tr> | |
1731 | <td> | |
1732 | <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'> | |
1733 | </td> | |
1734 | </tr> | |
1735 | ||
1736 | <tr> | |
1737 | <td><br><br></td> | |
1738 | </tr> | |
1739 | </table> | |
1740 | </td> | |
1741 | </tr> | |
1742 | ||
1743 | <tr> | |
1744 | <td colspan='2'> | |
1745 | <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'} | |
1746 | </td> | |
1747 | </tr> | |
1748 | ||
1749 | <tr> | |
1750 | <td colspan='2' align='right'> | |
1751 | <input type='submit' value='$Lang::tr{'back'}'> | |
1752 | END | |
1753 | ; | |
1754 | # Check if a provider should be added or edited. | |
1755 | if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") { | |
1756 | # Display button for updating the existing provider. | |
1757 | print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n"; | |
1758 | } else { | |
1759 | # Display button to add the new provider. | |
1760 | print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n"; | |
1761 | } | |
1762 | print <<END | |
1763 | </td> | |
1764 | </tr> | |
1765 | </table> | |
1766 | </form> | |
1767 | END | |
1768 | ; | |
1769 | &Header::closebox(); | |
1770 | } | |
1771 | ||
27760092 SS |
1772 | # |
1773 | ## A function to display a notice, to lock the webpage and | |
1774 | ## tell the user which action currently will be performed. | |
1775 | # | |
1776 | sub working_notice ($) { | |
1777 | my ($message) = @_; | |
1778 | ||
1779 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
1780 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
1781 | &Header::openbox( 'Waiting', 1,); | |
1782 | print <<END; | |
1783 | <table> | |
1784 | <tr> | |
1785 | <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td> | |
1786 | <td>$message</td> | |
1787 | </tr> | |
1788 | </table> | |
1789 | END | |
1790 | &Header::closebox(); | |
1791 | &Header::closebigbox(); | |
1792 | &Header::closepage(); | |
1793 | } | |
1794 | ||
3983aebd SS |
1795 | # |
1796 | ## A tiny function to perform a reload of the webpage after one second. | |
1797 | # | |
1798 | sub reload () { | |
1799 | print "<meta http-equiv='refresh' content='1'>\n"; | |
1800 | ||
1801 | # Stop the script. | |
1802 | exit; | |
a70d269a SS |
1803 | } |
1804 | ||
25f5cb0d SS |
1805 | # |
1806 | ## Private function to read-in and parse rules of a given rulefile. | |
1807 | # | |
1808 | ## The given file will be read, parsed and all valid rules will be stored by ID, | |
9d18656b | 1809 | ## message/description and it's state in the idsrules hash. |
25f5cb0d | 1810 | # |
3da6e01b SS |
1811 | sub readrulesfile ($) { |
1812 | my $rulefile = shift; | |
1813 | ||
1814 | # Open rule file and read in contents | |
298ef5ba | 1815 | open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!"; |
3da6e01b SS |
1816 | |
1817 | # Store file content in an array. | |
1818 | my @lines = <RULEFILE>; | |
1819 | ||
1820 | # Close file. | |
1821 | close(RULEFILE); | |
1822 | ||
1823 | # Loop over rule file contents | |
1824 | foreach my $line (@lines) { | |
1825 | # Remove whitespaces. | |
1826 | chomp $line; | |
1827 | ||
1828 | # Skip blank lines. | |
1829 | next if ($line =~ /^\s*$/); | |
1830 | ||
1831 | # Local vars. | |
1832 | my $sid; | |
1833 | my $msg; | |
1834 | ||
1835 | # Gather rule sid and message from the ruleline. | |
1836 | if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) { | |
1837 | $msg = $1; | |
1838 | $sid = $2; | |
1839 | ||
1840 | # Check if a rule has been found. | |
1841 | if ($sid && $msg) { | |
9d18656b SS |
1842 | # Add rule to the idsrules hash. |
1843 | $idsrules{$rulefile}{$sid}{'Description'} = $msg; | |
3da6e01b SS |
1844 | |
1845 | # Grab status of the rule. Check if ruleline starts with a "dash". | |
1846 | if ($line =~ /^\#/) { | |
1847 | # If yes, the rule is disabled. | |
9d18656b | 1848 | $idsrules{$rulefile}{$sid}{'State'} = "off"; |
3da6e01b SS |
1849 | } else { |
1850 | # Otherwise the rule is enabled. | |
9d18656b | 1851 | $idsrules{$rulefile}{$sid}{'State'} = "on"; |
3da6e01b SS |
1852 | } |
1853 | } | |
1854 | } | |
b7e29743 | 1855 | } |
3da6e01b | 1856 | } |
87660964 | 1857 | |
8d2f6b0b SS |
1858 | # |
1859 | ## Function to get the used memory of a given process-id. | |
1860 | # | |
87660964 | 1861 | sub get_memory_usage($) { |
004b13b7 | 1862 | my ($pid) = @_; |
87660964 | 1863 | |
004b13b7 | 1864 | my $memory = 0; |
87660964 | 1865 | |
004b13b7 | 1866 | # Try to open the status file for the given process-id on the pseudo |
87660964 | 1867 | # file system proc. |
004b13b7 SS |
1868 | if (open(FILE, "/proc/$pid/status")) { |
1869 | # Loop through the entire file. | |
1870 | while (<FILE>) { | |
1871 | # Splitt current line content and store them into variables. | |
1872 | my ($key, $value) = split(":", $_, 2); | |
1873 | ||
1874 | # Check if the current key is the one which contains the memory usage. | |
1875 | # The wanted one is VmRSS which contains the Real-memory (resident set) | |
1876 | # of the entire process. | |
1877 | if ($key eq "VmRSS") { | |
1878 | # Found the memory usage add it to the memory variable. | |
1879 | $memory += $value; | |
1880 | ||
1881 | # Break the loop. | |
1882 | last; | |
1883 | } | |
1884 | } | |
87660964 SS |
1885 | |
1886 | # Close file handle. | |
004b13b7 | 1887 | close(FILE); |
87660964 SS |
1888 | |
1889 | # Return memory usage. | |
1890 | return $memory; | |
004b13b7 | 1891 | } |
87660964 SS |
1892 | |
1893 | # If the file could not be open, return nothing. | |
1894 | return; | |
1895 | } | |
1896 | ||
a5d61752 SS |
1897 | # |
1898 | ## Function to read-in the given enabled or disables sids file. | |
1899 | # | |
1900 | sub read_enabled_disabled_sids_file($) { | |
1901 | my ($file) = @_; | |
1902 | ||
1903 | # Temporary hash to store the sids and their state. It will be | |
1904 | # returned at the end of this function. | |
1905 | my %temphash; | |
1906 | ||
1907 | # Open the given filename. | |
1908 | open(FILE, "$file") or die "Could not open $file. $!\n"; | |
1909 | ||
1910 | # Loop through the file. | |
1911 | while(<FILE>) { | |
1912 | # Remove newlines. | |
1913 | chomp $_; | |
1914 | ||
1915 | # Skip blank lines. | |
1916 | next if ($_ =~ /^\s*$/); | |
1917 | ||
1918 | # Skip coments. | |
1919 | next if ($_ =~ /^\#/); | |
1920 | ||
1921 | # Splitt line into sid and state part. | |
1922 | my ($state, $sid) = split(" ", $_); | |
1923 | ||
1924 | # Skip line if the sid is not numeric. | |
1925 | next unless ($sid =~ /\d+/ ); | |
1926 | ||
1927 | # Check if the sid was enabled. | |
1928 | if ($state eq "enablesid") { | |
1929 | # Add the sid and its state as enabled to the temporary hash. | |
1930 | $temphash{$sid} = "enabled"; | |
1931 | # Check if the sid was disabled. | |
1932 | } elsif ($state eq "disablesid") { | |
1933 | # Add the sid and its state as disabled to the temporary hash. | |
1934 | $temphash{$sid} = "disabled"; | |
1935 | # Invalid state - skip the current sid and state. | |
1936 | } else { | |
1937 | next; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | # Close filehandle. | |
1942 | close(FILE); | |
1943 | ||
1944 | # Return the hash. | |
1945 | return %temphash; | |
1946 | } | |
0a1bba1a | 1947 | |
019e5e9b SS |
1948 | # |
1949 | ## Function to get the provider name from the language file or providers file for a given handle. | |
1950 | # | |
1951 | sub get_provider_name($) { | |
1952 | my ($handle) = @_; | |
1953 | my $provider_name; | |
1954 | ||
1955 | # Get the required translation string for the given provider handle. | |
1956 | my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'}; | |
1957 | ||
1958 | # Check if the translation string is available in the language files. | |
1959 | if ($Lang::tr{$tr_string}) { | |
1960 | # Use the translated string from the language file. | |
1961 | $provider_name = $Lang::tr{$tr_string}; | |
1962 | } else { | |
1963 | # Fallback and use the provider summary from the providers file. | |
1964 | $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'}; | |
1965 | } | |
1966 | ||
1967 | # Return the obtained provider name. | |
1968 | return $provider_name; | |
1969 | } | |
1970 | ||
0a1bba1a SS |
1971 | # |
1972 | ## Private function to convert a given rulefile to a category name. | |
1973 | ## ( No file extension anymore and if the name contained a dot, it | |
1974 | ## would be replaced by a underline sign.) | |
1975 | # | |
1976 | sub _rulefile_to_category($) { | |
1977 | my ($filename) = @_; | |
1978 | ||
1979 | # Splitt the filename into single chunks and store them in a | |
1980 | # temorary array. | |
1981 | my @parts = split(/\./, $filename); | |
1982 | ||
1983 | # Return / Remove last element of the temporary array. | |
1984 | # This removes the file extension. | |
1985 | pop @parts; | |
1986 | ||
1987 | # Join together the single elements of the temporary array. | |
1988 | # If these are more than one, use a "underline" for joining. | |
1989 | my $category = join '_', @parts; | |
1990 | ||
1991 | # Return the converted filename. | |
1992 | return $category; | |
1993 | } |