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