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