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