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