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