]>
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 | |
53817b89 SS |
792 | # Set zone name. |
793 | my $zone_name = $zone; | |
794 | ||
795 | # Dirty hack to get the correct language string for the red zone. | |
796 | if ($zone eq "red") { | |
797 | $zone_name = "red1"; | |
798 | } | |
799 | ||
1286e0d4 | 800 | # Grab checkbox status from settings hash. |
a4ccfcbb | 801 | if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") { |
1286e0d4 SS |
802 | $checked_input = "checked = 'checked'"; |
803 | } | |
1504a375 | 804 | |
1286e0d4 | 805 | print "<td class='base' width='25%'>\n"; |
99b372b5 | 806 | print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n"; |
53817b89 | 807 | print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n"; |
1286e0d4 | 808 | print "</td>\n"; |
ac1cfefa | 809 | } |
1b73b07e | 810 | |
ac1cfefa | 811 | print <<END |
1504a375 | 812 | </tr> |
ea5c8eeb | 813 | </table> |
1504a375 | 814 | |
ea5c8eeb SS |
815 | <br><br> |
816 | ||
817 | <table width='100%'> | |
818 | <tr> | |
819 | <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td> | |
820 | </tr> | |
821 | </table> | |
822 | </form> | |
823 | END | |
824 | ; | |
825 | ||
826 | &Header::closebox(); | |
827 | ||
828 | # Draw elements for ruleset configuration. | |
829 | &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'}); | |
830 | ||
ea5c8eeb SS |
831 | print <<END |
832 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
833 | <table width='100%' border='0'> | |
1504a375 | 834 | <tr> |
ea5c8eeb SS |
835 | <td><b>$Lang::tr{'ids rules update'}</b></td> |
836 | <td><b>$Lang::tr{'ids automatic rules update'}</b></td> | |
1504a375 SS |
837 | </tr> |
838 | ||
839 | <tr> | |
ea5c8eeb | 840 | <td><select name='RULES'> |
a0fa489f | 841 | <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option> |
5a3e0dca MT |
842 | <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option> |
843 | <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option> | |
844 | <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option> | |
845 | </select> | |
1504a375 | 846 | </td> |
eadad5fd | 847 | |
ea5c8eeb | 848 | <td> |
eadad5fd SS |
849 | <select name='AUTOUPDATE_INTERVAL'> |
850 | <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option> | |
851 | <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option> | |
852 | <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option> | |
853 | </select> | |
854 | </td> | |
1504a375 SS |
855 | </tr> |
856 | ||
857 | <tr> | |
ea5c8eeb | 858 | <td colspan='2'> |
1504a375 SS |
859 | <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> |
860 | <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> | |
861 | </td> | |
862 | </tr> | |
863 | ||
864 | <tr> | |
ea5c8eeb | 865 | <td colspan='2' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td> |
1504a375 SS |
866 | </tr> |
867 | ||
868 | <tr> | |
ea5c8eeb | 869 | <td> </td> |
1504a375 | 870 | |
ea5c8eeb SS |
871 | <td align='right'> |
872 | END | |
873 | ; | |
874 | # Check if a ruleset source has been configured yet. | |
875 | unless($rulessettings{'RULES'}) { | |
876 | # If no ruleset settings have been saved yet, disable the button to download / update the ruleset. | |
877 | print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n"; | |
878 | } else { | |
879 | # Ruleset setting have been saved. - Check if a ruleset already is downloaded. | |
880 | if (%idsrules) { | |
881 | # Allow to press the button and show it as "update ruleset". | |
882 | print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n"; | |
883 | } else { | |
884 | # Also allow to press the button, but show it as "download new ruleset". | |
885 | print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n"; | |
886 | } | |
887 | } | |
888 | print <<END; | |
889 | <input type='submit' name='RULESET' value='$Lang::tr{'save'}'> | |
890 | </td> | |
1504a375 | 891 | |
1504a375 SS |
892 | </tr> |
893 | </table> | |
ac1cfefa MT |
894 | </form> |
895 | END | |
896 | ; | |
897 | ||
ac1cfefa | 898 | &Header::closebox(); |
fbfdb241 | 899 | |
b7e29743 SS |
900 | # |
901 | # Whitelist / Ignorelist | |
902 | # | |
903 | &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'}); | |
904 | ||
905 | print <<END; | |
906 | <table width='100%'> | |
907 | <tr> | |
908 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td> | |
909 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td> | |
910 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
911 | </tr> | |
912 | END | |
913 | # Check if some hosts have been added to be ignored. | |
914 | if (keys (%ignored)) { | |
915 | my $col = ""; | |
916 | ||
917 | # Loop through all entries of the hash. | |
918 | while( (my $key) = each %ignored) { | |
919 | # Assign data array positions to some nice variable names. | |
920 | my $address = $ignored{$key}[0]; | |
921 | my $remark = $ignored{$key}[1]; | |
922 | my $status = $ignored{$key}[2]; | |
923 | ||
924 | # Check if the key (id) number is even or not. | |
925 | if ($cgiparams{'ID'} eq $key) { | |
926 | $col="bgcolor='${Header::colouryellow}'"; | |
927 | } elsif ($key % 2) { | |
928 | $col="bgcolor='$color{'color22'}'"; | |
929 | } else { | |
930 | $col="bgcolor='$color{'color20'}'"; | |
931 | } | |
932 | ||
933 | # Choose icon for the checkbox. | |
934 | my $gif; | |
935 | my $gdesc; | |
936 | ||
937 | # Check if the status is enabled and select the correct image and description. | |
938 | if ($status eq 'enabled' ) { | |
939 | $gif = 'on.gif'; | |
940 | $gdesc = $Lang::tr{'click to disable'}; | |
941 | } else { | |
942 | $gif = 'off.gif'; | |
943 | $gdesc = $Lang::tr{'click to enable'}; | |
944 | } | |
945 | ||
946 | print <<END; | |
947 | <tr> | |
948 | <td width='20%' class='base' $col>$address</td> | |
949 | <td width='65%' class='base' $col>$remark</td> | |
950 | ||
951 | <td align='center' $col> | |
952 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
953 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' /> | |
954 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' /> | |
955 | <input type='hidden' name='ID' value='$key' /> | |
956 | </form> | |
957 | </td> | |
958 | ||
959 | <td align='center' $col> | |
960 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
961 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' /> | |
962 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' /> | |
963 | <input type='hidden' name='ID' value='$key' /> | |
964 | </form> | |
965 | </td> | |
966 | ||
967 | <td align='center' $col> | |
968 | <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'> | |
969 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
970 | <input type='hidden' name='ID' value='$key'> | |
971 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'> | |
972 | </form> | |
973 | </td> | |
974 | </tr> | |
975 | END | |
976 | } | |
977 | } else { | |
978 | # Print notice that currently no hosts are ignored. | |
979 | print "<tr>\n"; | |
980 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
981 | print "</tr>\n"; | |
982 | } | |
983 | ||
984 | print "</table>\n"; | |
985 | ||
986 | # Section to add new elements or edit existing ones. | |
987 | print <<END; | |
988 | <br> | |
989 | <hr> | |
990 | <br> | |
991 | ||
992 | <div align='center'> | |
993 | <table width='100%'> | |
994 | END | |
995 | ||
996 | # Assign correct headline and button text. | |
997 | my $buttontext; | |
998 | my $entry_address; | |
999 | my $entry_remark; | |
1000 | ||
1001 | # Check if an ID (key) has been given, in this case an existing entry should be edited. | |
1002 | if ($cgiparams{'ID'} ne '') { | |
1003 | $buttontext = $Lang::tr{'update'}; | |
1004 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n"; | |
1005 | ||
1006 | # Grab address and remark for the given key. | |
1007 | $entry_address = $ignored{$cgiparams{'ID'}}[0]; | |
1008 | $entry_remark = $ignored{$cgiparams{'ID'}}[1]; | |
1009 | } else { | |
1010 | $buttontext = $Lang::tr{'add'}; | |
1011 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n"; | |
1012 | } | |
1013 | ||
1014 | print <<END; | |
1015 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1016 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
1017 | <tr> | |
1018 | <td width='30%'>$Lang::tr{'ip address'}: </td> | |
1019 | <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td> | |
1020 | ||
1021 | <td width='30%'>$Lang::tr{'remark'}: </td> | |
1022 | <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td> | |
1023 | <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td> | |
1024 | </tr> | |
1025 | </form> | |
1026 | </table> | |
1027 | </div> | |
1028 | END | |
1029 | ||
1030 | &Header::closebox(); | |
1031 | ||
80bcd4dd SS |
1032 | # Only show the section for configuring the ruleset if one is present. |
1033 | if (%idsrules) { | |
1034 | &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'}); | |
298723b9 | 1035 | |
80bcd4dd | 1036 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
ce0e83b3 | 1037 | |
80bcd4dd SS |
1038 | # Output display table for rule files |
1039 | print "<table width='100%'>\n"; | |
f7fcd1c0 | 1040 | |
80bcd4dd SS |
1041 | # Loop over each rule file |
1042 | foreach my $rulefile (sort keys(%idsrules)) { | |
1043 | my $rulechecked = ''; | |
3ffee04b | 1044 | |
80bcd4dd SS |
1045 | # Check if rule file is enabled |
1046 | if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') { | |
1047 | $rulechecked = 'CHECKED'; | |
1048 | } | |
1049 | ||
0a1bba1a SS |
1050 | # Convert rulefile name into category name. |
1051 | my $categoryname = &_rulefile_to_category($rulefile); | |
1052 | ||
80bcd4dd SS |
1053 | # Table and rows for the rule files. |
1054 | print"<tr>\n"; | |
1055 | print"<td class='base' width='5%'>\n"; | |
1056 | print"<input type='checkbox' name='$rulefile' $rulechecked>\n"; | |
1057 | print"</td>\n"; | |
1058 | print"<td class='base' width='90%'><b>$rulefile</b></td>\n"; | |
1059 | print"<td class='base' width='5%' align='right'>\n"; | |
0a1bba1a | 1060 | print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n"; |
80bcd4dd SS |
1061 | print"</td>\n"; |
1062 | print"</tr>\n"; | |
1063 | ||
1064 | # Rows which will be hidden per default and will contain the single rules. | |
0a1bba1a | 1065 | print"<tr style='display:none' id='$categoryname'>\n"; |
80bcd4dd | 1066 | print"<td colspan='3'>\n"; |
17726644 | 1067 | |
f7fcd1c0 | 1068 | # Local vars |
80bcd4dd SS |
1069 | my $lines; |
1070 | my $rows; | |
1071 | my $col; | |
f9c2147d | 1072 | |
80bcd4dd SS |
1073 | # New table for the single rules. |
1074 | print "<table width='100%'>\n"; | |
e5738079 | 1075 | |
80bcd4dd SS |
1076 | # Loop over rule file rules |
1077 | foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) { | |
1078 | # Local vars | |
1079 | my $ruledefchecked = ''; | |
3ffee04b | 1080 | |
80bcd4dd SS |
1081 | # Skip rulefile itself. |
1082 | next if ($sid eq "Rulefile"); | |
f7fcd1c0 | 1083 | |
80bcd4dd SS |
1084 | # If 2 rules have been displayed, start a new row |
1085 | if (($lines % 2) == 0) { | |
1086 | print "</tr><tr>\n"; | |
1087 | ||
1088 | # Increase rows by once. | |
1089 | $rows++; | |
1090 | } | |
1091 | ||
1092 | # Colour lines. | |
1093 | if ($rows % 2) { | |
1094 | $col="bgcolor='$color{'color20'}'"; | |
1095 | } else { | |
1096 | $col="bgcolor='$color{'color22'}'"; | |
1097 | } | |
f7fcd1c0 | 1098 | |
80bcd4dd SS |
1099 | # Set rule state |
1100 | if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') { | |
1101 | $ruledefchecked = 'CHECKED'; | |
1102 | } | |
1103 | ||
1104 | # Create rule checkbox and display rule description | |
1105 | print "<td class='base' width='5%' align='right' $col>\n"; | |
1106 | print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n"; | |
1107 | print "</td>\n"; | |
1108 | print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>"; | |
1109 | ||
1110 | # Increment rule count | |
1111 | $lines++; | |
f7fcd1c0 | 1112 | } |
3ffee04b | 1113 | |
80bcd4dd SS |
1114 | # If do not have a second rule for row, create empty cell |
1115 | if (($lines % 2) != 0) { | |
1116 | print "<td class='base'></td>"; | |
1117 | } | |
17726644 | 1118 | |
80bcd4dd SS |
1119 | # Close display table |
1120 | print "</tr></table></td></tr>"; | |
f7fcd1c0 SS |
1121 | } |
1122 | ||
1123 | # Close display table | |
80bcd4dd | 1124 | print "</table>"; |
17726644 | 1125 | |
f7fcd1c0 | 1126 | print <<END |
2999f1d2 CS |
1127 | <table width='100%'> |
1128 | <tr> | |
298723b9 | 1129 | <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'> |
3ffee04b CS |
1130 | <!-- space for future online help link --> |
1131 | </td> | |
2999f1d2 CS |
1132 | </tr> |
1133 | </table> | |
298723b9 | 1134 | </form> |
3ffee04b CS |
1135 | END |
1136 | ; | |
80bcd4dd SS |
1137 | &Header::closebox(); |
1138 | } | |
1139 | ||
ac1cfefa MT |
1140 | &Header::closebigbox(); |
1141 | &Header::closepage(); | |
1142 | ||
27760092 SS |
1143 | # |
1144 | ## A function to display a notice, to lock the webpage and | |
1145 | ## tell the user which action currently will be performed. | |
1146 | # | |
1147 | sub working_notice ($) { | |
1148 | my ($message) = @_; | |
1149 | ||
1150 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
1151 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
1152 | &Header::openbox( 'Waiting', 1,); | |
1153 | print <<END; | |
1154 | <table> | |
1155 | <tr> | |
1156 | <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td> | |
1157 | <td>$message</td> | |
1158 | </tr> | |
1159 | </table> | |
1160 | END | |
1161 | &Header::closebox(); | |
1162 | &Header::closebigbox(); | |
1163 | &Header::closepage(); | |
1164 | } | |
1165 | ||
3983aebd SS |
1166 | # |
1167 | ## A tiny function to perform a reload of the webpage after one second. | |
1168 | # | |
1169 | sub reload () { | |
1170 | print "<meta http-equiv='refresh' content='1'>\n"; | |
1171 | ||
1172 | # Stop the script. | |
1173 | exit; | |
a70d269a SS |
1174 | } |
1175 | ||
25f5cb0d SS |
1176 | # |
1177 | ## Private function to read-in and parse rules of a given rulefile. | |
1178 | # | |
1179 | ## The given file will be read, parsed and all valid rules will be stored by ID, | |
9d18656b | 1180 | ## message/description and it's state in the idsrules hash. |
25f5cb0d | 1181 | # |
3da6e01b SS |
1182 | sub readrulesfile ($) { |
1183 | my $rulefile = shift; | |
1184 | ||
1185 | # Open rule file and read in contents | |
298ef5ba | 1186 | open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!"; |
3da6e01b SS |
1187 | |
1188 | # Store file content in an array. | |
1189 | my @lines = <RULEFILE>; | |
1190 | ||
1191 | # Close file. | |
1192 | close(RULEFILE); | |
1193 | ||
1194 | # Loop over rule file contents | |
1195 | foreach my $line (@lines) { | |
1196 | # Remove whitespaces. | |
1197 | chomp $line; | |
1198 | ||
1199 | # Skip blank lines. | |
1200 | next if ($line =~ /^\s*$/); | |
1201 | ||
1202 | # Local vars. | |
1203 | my $sid; | |
1204 | my $msg; | |
1205 | ||
1206 | # Gather rule sid and message from the ruleline. | |
1207 | if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) { | |
1208 | $msg = $1; | |
1209 | $sid = $2; | |
1210 | ||
1211 | # Check if a rule has been found. | |
1212 | if ($sid && $msg) { | |
9d18656b SS |
1213 | # Add rule to the idsrules hash. |
1214 | $idsrules{$rulefile}{$sid}{'Description'} = $msg; | |
3da6e01b SS |
1215 | |
1216 | # Grab status of the rule. Check if ruleline starts with a "dash". | |
1217 | if ($line =~ /^\#/) { | |
1218 | # If yes, the rule is disabled. | |
9d18656b | 1219 | $idsrules{$rulefile}{$sid}{'State'} = "off"; |
3da6e01b SS |
1220 | } else { |
1221 | # Otherwise the rule is enabled. | |
9d18656b | 1222 | $idsrules{$rulefile}{$sid}{'State'} = "on"; |
3da6e01b SS |
1223 | } |
1224 | } | |
1225 | } | |
b7e29743 | 1226 | } |
3da6e01b | 1227 | } |
87660964 | 1228 | |
8d2f6b0b SS |
1229 | # |
1230 | ## Function to get the used memory of a given process-id. | |
1231 | # | |
87660964 | 1232 | sub get_memory_usage($) { |
004b13b7 | 1233 | my ($pid) = @_; |
87660964 | 1234 | |
004b13b7 | 1235 | my $memory = 0; |
87660964 | 1236 | |
004b13b7 | 1237 | # Try to open the status file for the given process-id on the pseudo |
87660964 | 1238 | # file system proc. |
004b13b7 SS |
1239 | if (open(FILE, "/proc/$pid/status")) { |
1240 | # Loop through the entire file. | |
1241 | while (<FILE>) { | |
1242 | # Splitt current line content and store them into variables. | |
1243 | my ($key, $value) = split(":", $_, 2); | |
1244 | ||
1245 | # Check if the current key is the one which contains the memory usage. | |
1246 | # The wanted one is VmRSS which contains the Real-memory (resident set) | |
1247 | # of the entire process. | |
1248 | if ($key eq "VmRSS") { | |
1249 | # Found the memory usage add it to the memory variable. | |
1250 | $memory += $value; | |
1251 | ||
1252 | # Break the loop. | |
1253 | last; | |
1254 | } | |
1255 | } | |
87660964 SS |
1256 | |
1257 | # Close file handle. | |
004b13b7 | 1258 | close(FILE); |
87660964 SS |
1259 | |
1260 | # Return memory usage. | |
1261 | return $memory; | |
004b13b7 | 1262 | } |
87660964 SS |
1263 | |
1264 | # If the file could not be open, return nothing. | |
1265 | return; | |
1266 | } | |
1267 | ||
8d2f6b0b SS |
1268 | # |
1269 | ## Function to generate the file which contains the home net information. | |
1270 | # | |
1271 | sub generate_home_net_file() { | |
1272 | my %netsettings; | |
1273 | ||
1274 | # Read-in network settings. | |
1275 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
1276 | ||
1277 | # Get available network zones. | |
1278 | my @network_zones = &IDS::get_available_network_zones(); | |
1279 | ||
1280 | # Temporary array to store network address and prefix of the configured | |
1281 | # networks. | |
1282 | my @networks; | |
1283 | ||
1284 | # Loop through the array of available network zones. | |
1285 | foreach my $zone (@network_zones) { | |
1286 | # Skip the red network - It never can be part to the home_net! | |
1287 | next if($zone eq "red"); | |
1288 | ||
1289 | # Convert current zone name into upper case. | |
1290 | $zone = uc($zone); | |
1291 | ||
1292 | # Generate key to access the required data from the netsettings hash. | |
1293 | my $zone_netaddress = $zone . "_NETADDRESS"; | |
1294 | my $zone_netmask = $zone . "_NETMASK"; | |
1295 | ||
1296 | # Obtain the settings from the netsettings hash. | |
1297 | my $netaddress = $netsettings{$zone_netaddress}; | |
1298 | my $netmask = $netsettings{$zone_netmask}; | |
1299 | ||
1300 | # Convert the subnetmask into prefix notation. | |
1301 | my $prefix = &Network::convert_netmask2prefix($netmask); | |
1302 | ||
1303 | # Generate full network string. | |
1304 | my $network = join("/", $netaddress,$prefix); | |
1305 | ||
1306 | # Check if the network is valid. | |
1307 | if(&Network::check_subnet($network)) { | |
1308 | # Add the generated network to the array of networks. | |
1309 | push(@networks, $network); | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | # Format home net declaration. | |
1314 | my $line = "\"\["; | |
1315 | ||
1316 | # Loop through the array of networks. | |
1317 | foreach my $network (@networks) { | |
1318 | # Add the network to the line. | |
1319 | $line = "$line" . "$network"; | |
1320 | ||
1321 | # Check if the current network was the last in the array. | |
1322 | if ($network eq $networks[-1]) { | |
1323 | # Close the line. | |
1324 | $line = "$line" . "\]\""; | |
1325 | } else { | |
1326 | # Add "," for the next network. | |
1327 | $line = "$line" . "\,"; | |
1328 | } | |
1329 | } | |
1330 | ||
1331 | # Open file to store the addresses of the home net. | |
1332 | open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n"; | |
1333 | ||
1334 | # Print yaml header. | |
1335 | print FILE "%YAML 1.1\n"; | |
1336 | print FILE "---\n\n"; | |
1337 | ||
1338 | # Print notice about autogenerated file. | |
1339 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
1340 | ||
1341 | # Print the generated and required HOME_NET declaration to the file. | |
1342 | print FILE "HOME_NET:\t$line\n"; | |
1343 | ||
1344 | # Close file handle. | |
1345 | close(FILE); | |
1346 | ||
1347 | } | |
a5d61752 | 1348 | |
b7e29743 SS |
1349 | # |
1350 | ## Function to generate the rules file with whitelisted addresses. | |
1351 | # | |
1352 | sub GenerateIgnoreFile() { | |
1353 | my %ignored = (); | |
1354 | ||
1355 | # SID range 1000000-1999999 Reserved for Local Use | |
1356 | # Put your custom rules in this range to avoid conflicts | |
1357 | my $sid = 1500000; | |
1358 | ||
1359 | # Read-in ignoredfile. | |
1360 | &General::readhasharray($ignoredfile, \%ignored); | |
1361 | ||
1362 | # Open ignorefile for writing. | |
1363 | open(FILE, ">$whitelistfile") or die "Could not write to $whitelistfile. $!\n"; | |
1364 | ||
1365 | # Config file header. | |
1366 | print FILE "# Autogenerated file.\n"; | |
1367 | print FILE "# All user modifications will be overwritten.\n\n"; | |
1368 | ||
1369 | # Add all user defined addresses to the whitelist. | |
1370 | # | |
1371 | # Check if the hash contains any elements. | |
1372 | if (keys (%ignored)) { | |
1373 | # Loop through the entire hash and write the host/network | |
1374 | # and remark to the ignore file. | |
1375 | while ( (my $key) = each %ignored) { | |
1376 | my $address = $ignored{$key}[0]; | |
1377 | my $remark = $ignored{$key}[1]; | |
1378 | my $status = $ignored{$key}[2]; | |
1379 | ||
1380 | # Check if the status of the entry is "enabled". | |
1381 | if ($status eq "enabled") { | |
1382 | # Check if the address/network is valid. | |
1383 | if ((&General::validip($address)) || (&General::validipandmask($address))) { | |
1384 | # Write rule line to the file to pass any traffic from this IP | |
1385 | print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n"; | |
1386 | ||
1387 | # Increment sid. | |
1388 | $sid++; | |
1389 | } | |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | close(FILE); | |
1395 | } | |
1396 | ||
a5d61752 SS |
1397 | # |
1398 | ## Function to read-in the given enabled or disables sids file. | |
1399 | # | |
1400 | sub read_enabled_disabled_sids_file($) { | |
1401 | my ($file) = @_; | |
1402 | ||
1403 | # Temporary hash to store the sids and their state. It will be | |
1404 | # returned at the end of this function. | |
1405 | my %temphash; | |
1406 | ||
1407 | # Open the given filename. | |
1408 | open(FILE, "$file") or die "Could not open $file. $!\n"; | |
1409 | ||
1410 | # Loop through the file. | |
1411 | while(<FILE>) { | |
1412 | # Remove newlines. | |
1413 | chomp $_; | |
1414 | ||
1415 | # Skip blank lines. | |
1416 | next if ($_ =~ /^\s*$/); | |
1417 | ||
1418 | # Skip coments. | |
1419 | next if ($_ =~ /^\#/); | |
1420 | ||
1421 | # Splitt line into sid and state part. | |
1422 | my ($state, $sid) = split(" ", $_); | |
1423 | ||
1424 | # Skip line if the sid is not numeric. | |
1425 | next unless ($sid =~ /\d+/ ); | |
1426 | ||
1427 | # Check if the sid was enabled. | |
1428 | if ($state eq "enablesid") { | |
1429 | # Add the sid and its state as enabled to the temporary hash. | |
1430 | $temphash{$sid} = "enabled"; | |
1431 | # Check if the sid was disabled. | |
1432 | } elsif ($state eq "disablesid") { | |
1433 | # Add the sid and its state as disabled to the temporary hash. | |
1434 | $temphash{$sid} = "disabled"; | |
1435 | # Invalid state - skip the current sid and state. | |
1436 | } else { | |
1437 | next; | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | # Close filehandle. | |
1442 | close(FILE); | |
1443 | ||
1444 | # Return the hash. | |
1445 | return %temphash; | |
1446 | } | |
0a1bba1a SS |
1447 | |
1448 | # | |
1449 | ## Private function to convert a given rulefile to a category name. | |
1450 | ## ( No file extension anymore and if the name contained a dot, it | |
1451 | ## would be replaced by a underline sign.) | |
1452 | # | |
1453 | sub _rulefile_to_category($) { | |
1454 | my ($filename) = @_; | |
1455 | ||
1456 | # Splitt the filename into single chunks and store them in a | |
1457 | # temorary array. | |
1458 | my @parts = split(/\./, $filename); | |
1459 | ||
1460 | # Return / Remove last element of the temporary array. | |
1461 | # This removes the file extension. | |
1462 | pop @parts; | |
1463 | ||
1464 | # Join together the single elements of the temporary array. | |
1465 | # If these are more than one, use a "underline" for joining. | |
1466 | my $category = join '_', @parts; | |
1467 | ||
1468 | # Return the converted filename. | |
1469 | return $category; | |
1470 | } |