]>
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=(); |
43263ea6 | 37 | my %rulesetsources = (); |
298723b9 | 38 | my %cgiparams=(); |
ac1cfefa | 39 | my %checked=(); |
5a3e0dca | 40 | my %selected=(); |
0b89daee SS |
41 | |
42 | # Read-in main settings, for language, theme and colors. | |
43 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
44 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
45 | ||
1286e0d4 SS |
46 | # Get the available network zones, based on the config type of the system and store |
47 | # the list of zones in an array. | |
48 | my @network_zones = &IDS::get_available_network_zones(); | |
ac1cfefa | 49 | |
8d2f6b0b | 50 | # File where the used rulefiles are stored. |
101c8881 | 51 | my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml"; |
8d2f6b0b SS |
52 | |
53 | # File where the addresses of the homenet are stored. | |
54 | my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml"; | |
55 | ||
43263ea6 SS |
56 | my $errormessage; |
57 | ||
ac1cfefa MT |
58 | &Header::showhttpheaders(); |
59 | ||
298723b9 SS |
60 | #Get GUI values |
61 | &Header::getcgihash(\%cgiparams); | |
ac1cfefa | 62 | |
3983aebd SS |
63 | # Check if any error has been stored. |
64 | if (-e $IDS::storederrorfile) { | |
65 | # Open file to read in the stored error message. | |
66 | open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n"; | |
67 | ||
68 | # Read the stored error message. | |
69 | $errormessage = <FILE>; | |
70 | ||
71 | # Close file. | |
72 | close (FILE); | |
73 | ||
74 | # Delete the file, which is now not longer required. | |
75 | unlink($IDS::storederrorfile); | |
76 | } | |
77 | ||
78 | ||
9d18656b | 79 | ## Grab all available snort rules and store them in the idsrules hash. |
3da6e01b | 80 | # |
422204ff | 81 | # Open snort rules directory and do a directory listing. |
298ef5ba | 82 | opendir(DIR, $IDS::rulespath) or die $!; |
422204ff SS |
83 | # Loop through the direcory. |
84 | while (my $file = readdir(DIR)) { | |
85 | ||
86 | # We only want files. | |
298ef5ba | 87 | next unless (-f "$IDS::rulespath/$file"); |
422204ff SS |
88 | |
89 | # Ignore empty files. | |
298ef5ba | 90 | next if (-z "$IDS::rulespath/$file"); |
422204ff | 91 | |
3da6e01b | 92 | # Use a regular expression to find files ending in .rules |
422204ff SS |
93 | next unless ($file =~ m/\.rules$/); |
94 | ||
3da6e01b | 95 | # Ignore files which are not read-able. |
298ef5ba | 96 | next unless (-R "$IDS::rulespath/$file"); |
395e3b90 | 97 | |
3da6e01b | 98 | # Call subfunction to read-in rulefile and add rules to |
9d18656b | 99 | # the idsrules hash. |
3da6e01b | 100 | &readrulesfile("$file"); |
395e3b90 | 101 | } |
395e3b90 | 102 | |
3da6e01b | 103 | closedir(DIR); |
395e3b90 | 104 | |
e5738079 SS |
105 | # Gather used rulefiles. |
106 | # | |
107 | # Check if the file for activated rulefiles is not empty. | |
02844177 | 108 | if(-f $idsusedrulefilesfile) { |
e5738079 | 109 | # Open the file for used rulefile and read-in content. |
02844177 | 110 | open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n"; |
e5738079 SS |
111 | |
112 | # Read-in content. | |
113 | my @lines = <FILE>; | |
114 | ||
115 | # Close file. | |
116 | close(FILE); | |
117 | ||
118 | # Loop through the array. | |
119 | foreach my $line (@lines) { | |
120 | # Remove newlines. | |
121 | chomp($line); | |
122 | ||
123 | # Skip comments. | |
124 | next if ($line =~ /\#/); | |
125 | ||
126 | # Skip blank lines. | |
127 | next if ($line =~ /^\s*$/); | |
128 | ||
129 | # Gather rule sid and message from the ruleline. | |
101c8881 | 130 | if ($line =~ /.*- (.*)/) { |
e5738079 SS |
131 | my $rulefile = $1; |
132 | ||
9d18656b SS |
133 | # Add the rulefile to the %idsrules hash. |
134 | $idsrules{$rulefile}{'Rulefile'}{'State'} = "on"; | |
e5738079 SS |
135 | } |
136 | } | |
137 | } | |
138 | ||
298723b9 SS |
139 | # Save ruleset. |
140 | if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) { | |
02844177 SS |
141 | my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf"; |
142 | my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf"; | |
298723b9 SS |
143 | |
144 | # Arrays to store sid which should be added to the corresponding files. | |
145 | my @enabled_sids; | |
146 | my @disabled_sids; | |
e5738079 | 147 | my @enabled_rulefiles; |
298723b9 | 148 | |
9d18656b SS |
149 | # Loop through the hash of idsrules. |
150 | foreach my $rulefile(keys %idsrules) { | |
e5738079 SS |
151 | # Check if the rulefile is enabled. |
152 | if ($cgiparams{$rulefile} eq "on") { | |
153 | # Add rulefile to the array of enabled rulefiles. | |
154 | push(@enabled_rulefiles, $rulefile); | |
b65b5ef3 SS |
155 | |
156 | # Drop item from cgiparams hash. | |
157 | delete $cgiparams{$rulefile}; | |
e5738079 | 158 | } |
466c6779 | 159 | } |
e5738079 | 160 | |
9d18656b SS |
161 | # Loop through the hash of idsrules. |
162 | foreach my $rulefile (keys %idsrules) { | |
298723b9 | 163 | # Loop through the single rules of the rulefile. |
9d18656b | 164 | foreach my $sid (keys %{$idsrules{$rulefile}}) { |
c51a044a SS |
165 | # Skip the current sid if it is not numeric. |
166 | next unless ($sid =~ /\d+/ ); | |
167 | ||
298723b9 SS |
168 | # Check if there exists a key in the cgiparams hash for this sid. |
169 | if (exists($cgiparams{$sid})) { | |
170 | # Look if the rule is disabled. | |
9d18656b | 171 | if ($idsrules{$rulefile}{$sid}{'State'} eq "off") { |
298723b9 SS |
172 | # Check if the state has been set to 'on'. |
173 | if ($cgiparams{$sid} eq "on") { | |
174 | # Add the sid to the enabled_sids array. | |
175 | push(@enabled_sids, $sid); | |
176 | ||
177 | # Drop item from cgiparams hash. | |
60333473 | 178 | delete $cgiparams{$rulefile}{$sid}; |
298723b9 SS |
179 | } |
180 | } | |
181 | } else { | |
182 | # Look if the rule is enabled. | |
9d18656b | 183 | if ($idsrules{$rulefile}{$sid}{'State'} eq "on") { |
298723b9 SS |
184 | # Check if the state is 'on' and should be disabled. |
185 | # In this case there is no entry | |
186 | # for the sid in the cgiparams hash. | |
187 | # Add it to the disabled_sids array. | |
188 | push(@disabled_sids, $sid); | |
189 | ||
190 | # Drop item from cgiparams hash. | |
60333473 | 191 | delete $cgiparams{$rulefile}{$sid}; |
298723b9 SS |
192 | } |
193 | } | |
194 | } | |
195 | } | |
196 | ||
37659505 SS |
197 | # Open enabled sid's file for writing. |
198 | open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n"; | |
298723b9 | 199 | |
37659505 SS |
200 | # Write header to file. |
201 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
298723b9 | 202 | |
37659505 SS |
203 | # Check if the enabled_sids array contains any sid's. |
204 | if (@enabled_sids) { | |
298723b9 SS |
205 | # Loop through the array of enabled sids and write them to the file. |
206 | foreach my $sid (@enabled_sids) { | |
37659505 | 207 | print FILE "enablesid $sid\n"; |
298723b9 | 208 | } |
298723b9 SS |
209 | } |
210 | ||
37659505 SS |
211 | # Close file after writing. |
212 | close(FILE); | |
213 | ||
214 | # Open disabled sid's file for writing. | |
215 | open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n"; | |
298723b9 | 216 | |
37659505 SS |
217 | # Write header to file. |
218 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
298723b9 | 219 | |
37659505 SS |
220 | # Check if the enabled_sids array contains any sid's. |
221 | if (@disabled_sids) { | |
222 | # Loop through the array of disabled sids and write them to the file. | |
223 | foreach my $sid (@disabled_sids) { | |
224 | print FILE "disablesid $sid\n"; | |
225 | } | |
226 | } | |
298723b9 | 227 | |
37659505 SS |
228 | # Close file after writing. |
229 | close(FILE); | |
e5738079 SS |
230 | |
231 | # Open file for used rulefiles. | |
02844177 | 232 | open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n"; |
e5738079 | 233 | |
101c8881 SS |
234 | # Write yaml header to the file. |
235 | print FILE "%YAML 1.1\n"; | |
236 | print FILE "---\n\n"; | |
237 | ||
e5738079 SS |
238 | # Write header to file. |
239 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
240 | ||
241 | # Check if the enabled_rulefiles array contains any entries. | |
242 | if (@enabled_rulefiles) { | |
243 | # Loop through the array of rulefiles which should be loaded and write the to the file. | |
244 | foreach my $file (@enabled_rulefiles) { | |
101c8881 | 245 | print FILE " - $file\n"; |
e5738079 SS |
246 | } |
247 | } | |
248 | ||
249 | # Close file after writing. | |
250 | close(FILE); | |
52599865 | 251 | |
27760092 SS |
252 | # Lock the webpage and print message. |
253 | &working_notice("$Lang::tr{'snort working'}"); | |
254 | ||
52599865 | 255 | # Call oinkmaster to alter the ruleset. |
27760092 SS |
256 | &IDS::oinkmaster(); |
257 | ||
e2e7880d SS |
258 | # Check if the IDS is running. |
259 | if(&IDS::is_ids_running()) { | |
260 | # Call suricatactrl to perform a reload. | |
261 | &IDS::call_suricatactrl("reload"); | |
262 | } | |
263 | ||
27760092 SS |
264 | # Reload page. |
265 | &reload(); | |
52599865 SS |
266 | |
267 | # Download new ruleset. | |
268 | } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) { | |
43263ea6 SS |
269 | # Check if the red device is active. |
270 | unless (-e "${General::swroot}/red/active") { | |
271 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
272 | } | |
52599865 | 273 | |
3983aebd | 274 | # Check if enought free disk space is availabe. |
434001d0 SS |
275 | if(&IDS::checkdiskspace()) { |
276 | $errormessage = "$Lang::tr{'not enough disk space'}"; | |
277 | } | |
52599865 | 278 | |
43263ea6 SS |
279 | # Check if any errors happend. |
280 | unless ($errormessage) { | |
27760092 SS |
281 | # Lock the webpage and print notice about downloading |
282 | # a new ruleset. | |
283 | &working_notice("$Lang::tr{'snort working'}"); | |
3983aebd | 284 | |
43263ea6 | 285 | # Call subfunction to download the ruleset. |
434001d0 SS |
286 | if(&IDS::downloadruleset()) { |
287 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
8f22237b | 288 | |
3983aebd | 289 | # Call function to store the errormessage. |
434001d0 | 290 | &IDS::_store_error_message($errormessage); |
52599865 | 291 | |
3983aebd SS |
292 | # Preform a reload of the page. |
293 | &reload(); | |
294 | } else { | |
295 | # Call subfunction to launch oinkmaster. | |
296 | &IDS::oinkmaster(); | |
43263ea6 | 297 | |
e2e7880d SS |
298 | # Check if the IDS is running. |
299 | if(&IDS::is_ids_running()) { | |
300 | # Call suricatactrl to perform a reload. | |
301 | &IDS::call_suricatactrl("reload"); | |
302 | } | |
303 | ||
3983aebd SS |
304 | # Perform a reload of the page. |
305 | &reload(); | |
306 | } | |
52599865 | 307 | } |
a232b58c | 308 | # Save snort settings. |
e0bfd338 | 309 | } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) { |
a232b58c | 310 | # Prevent form name from been stored in conf file. |
e0bfd338 | 311 | delete $cgiparams{'IDS'}; |
a232b58c SS |
312 | |
313 | # Check if an oinkcode has been provided. | |
314 | if ($cgiparams{'OINKCODE'}) { | |
315 | # Check if the oinkcode contains unallowed chars. | |
316 | unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) { | |
317 | $errormessage = $Lang::tr{'invalid input for oink code'}; | |
318 | } | |
f9c2147d | 319 | } |
ac1cfefa | 320 | |
a232b58c SS |
321 | # Go on if there are no error messages. |
322 | if (!$errormessage) { | |
323 | # Store settings into settings file. | |
02844177 | 324 | &General::writehash("$IDS::settingsdir/settings", \%cgiparams); |
a9a91e5f | 325 | } |
8d2f6b0b SS |
326 | |
327 | # Generate file to store the home net. | |
328 | &generate_home_net_file(); | |
e2e7880d SS |
329 | |
330 | # Check if the IDS currently is running. | |
331 | if(&IDS::ids_is_running()) { | |
332 | # Check if ENABLE_IDS is set to on. | |
333 | if($cgiparams{'ENABLE_IDS'} eq "on") { | |
334 | # Call suricatactrl to perform a reload of suricata. | |
335 | &IDS::call_suricatactrl("reload"); | |
336 | } else { | |
337 | # Call suricatactrl to stop suricata. | |
338 | &IDS::call_suricatactrl("stop"); | |
339 | } | |
340 | } else { | |
341 | # Call suricatactrl to start suricata. | |
342 | &IDS::call_suricatactrl("start"); | |
343 | } | |
ac1cfefa MT |
344 | } |
345 | ||
1286e0d4 SS |
346 | # Read-in idssettings |
347 | &General::readhash("$IDS::settingsdir/settings", \%idssettings); | |
348 | ||
a4ccfcbb SS |
349 | # If the runmode has not been configured yet, set default value. |
350 | unless(exists($idssettings{'RUN_MODE'})) { | |
351 | # Set default to IPS. | |
352 | $idssettings{'RUN_MODE'} = 'IPS'; | |
353 | } | |
354 | ||
1286e0d4 SS |
355 | $checked{'ENABLE_IDS'}{'off'} = ''; |
356 | $checked{'ENABLE_IDS'}{'on'} = ''; | |
357 | $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'"; | |
a4ccfcbb SS |
358 | $checked{'RUN_MODE'}{'IDS'} = ''; |
359 | $checked{'RUN_MODE'}{'IPS'} = ''; | |
360 | $checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'"; | |
5a3e0dca | 361 | $selected{'RULES'}{'nothing'} = ''; |
5a3e0dca | 362 | $selected{'RULES'}{'community'} = ''; |
a0fa489f | 363 | $selected{'RULES'}{'emerging'} = ''; |
5a3e0dca MT |
364 | $selected{'RULES'}{'registered'} = ''; |
365 | $selected{'RULES'}{'subscripted'} = ''; | |
1286e0d4 | 366 | $selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'"; |
ac1cfefa MT |
367 | |
368 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
369 | ||
17726644 SS |
370 | ### Java Script ### |
371 | print <<END | |
372 | <script> | |
373 | // Tiny java script function to show/hide the rules | |
374 | // of a given category. | |
375 | function showhide(tblname) { | |
376 | \$("#" + tblname).toggle(); | |
377 | } | |
378 | </script> | |
379 | END | |
380 | ; | |
381 | ||
ac1cfefa MT |
382 | &Header::openbigbox('100%', 'left', '', $errormessage); |
383 | ||
384 | if ($errormessage) { | |
385 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
386 | print "<class name='base'>$errormessage\n"; | |
387 | print " </class>\n"; | |
388 | &Header::closebox(); | |
389 | } | |
390 | ||
87660964 | 391 | # Draw current state of the IDS |
7cc8a0e5 | 392 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'}); |
1504a375 | 393 | |
87660964 SS |
394 | # Check if the IDS is running and obtain the process-id. |
395 | my $pid = &IDS::ids_is_running(); | |
396 | ||
397 | # Display some useful information, if suricata daemon is running. | |
398 | if ($pid) { | |
399 | # Gather used memory. | |
400 | my $memory = &get_memory_usage($pid); | |
401 | ||
402 | print <<END; | |
403 | <table width='95%' cellspacing='0' class='tbl'> | |
404 | <tr> | |
405 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
406 | </tr> | |
407 | ||
408 | <tr> | |
409 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
410 | <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td> | |
411 | </tr> | |
412 | ||
413 | <tr> | |
414 | <td class='base'></td> | |
415 | <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td> | |
416 | <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td> | |
417 | </tr> | |
418 | ||
419 | <tr> | |
420 | <td class='base'></td> | |
421 | <td bgcolor='$color{'color22'}' align='center'>$pid</td> | |
422 | <td bgcolor='$color{'color22'}' align='center'>$memory KB</td> | |
423 | </tr> | |
424 | </table> | |
425 | END | |
426 | } else { | |
427 | # Otherwise display a hint that the service is not launched. | |
428 | print <<END; | |
429 | <table width='95%' cellspacing='0' class='tbl'> | |
430 | <tr> | |
431 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
432 | </tr> | |
433 | ||
434 | <tr> | |
435 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
436 | <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td> | |
437 | </tr> | |
438 | </table> | |
87660964 SS |
439 | END |
440 | } | |
441 | &Header::closebox(); | |
442 | ||
443 | # Draw elements for IDS configuration. | |
444 | &Header::openbox('100%', 'center', $Lang::tr{'settings'}); | |
445 | ||
1504a375 SS |
446 | my $rulesdate; |
447 | ||
448 | # Check if a ruleset allready has been downloaded. | |
a69b96d2 | 449 | if ( -f "$IDS::rulestarball"){ |
1504a375 | 450 | # Call stat on the filename to obtain detailed information. |
a69b96d2 | 451 | my @Info = stat("$IDS::rulestarball"); |
1504a375 SS |
452 | |
453 | # Grab details about the creation time. | |
454 | $rulesdate = localtime($Info[9]); | |
455 | } | |
456 | ||
ac1cfefa | 457 | print <<END |
1504a375 SS |
458 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
459 | <table width='100%' border='0'> | |
460 | <tr> | |
a4ccfcbb | 461 | <td class='base' colspan='4'> |
1286e0d4 SS |
462 | <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'} |
463 | </td> | |
a4ccfcbb | 464 | </tr> |
1286e0d4 | 465 | |
a4ccfcbb SS |
466 | <tr> |
467 | <td colspan='4'><br><br></td> | |
1286e0d4 SS |
468 | </tr> |
469 | ||
470 | <tr> | |
a4ccfcbb | 471 | <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td> |
1286e0d4 | 472 | </tr> |
1504a375 | 473 | |
1286e0d4 | 474 | <tr> |
a4ccfcbb SS |
475 | <td class='base' colspan='4'> |
476 | <input type='radio' name='RUN_MODE' value='IDS' $checked{'RUN_MODE'}{'IDS'}>$Lang::tr{'intrusion detection system2'}     | |
477 | <input type='radio' name='RUN_MODE' value='IPS' $checked{'RUN_MODE'}{'IPS'}>$Lang::tr{'intrusion prevention system'} | |
1504a375 | 478 | </td> |
a4ccfcbb | 479 | </tr> |
1504a375 | 480 | |
a4ccfcbb SS |
481 | <tr> |
482 | <td colspan='4'><br></td> | |
1286e0d4 | 483 | </tr> |
a4ccfcbb SS |
484 | |
485 | <tr> | |
486 | <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td> | |
487 | </tr> | |
488 | ||
489 | <tr> | |
ac1cfefa MT |
490 | END |
491 | ; | |
1504a375 | 492 | |
1286e0d4 SS |
493 | # Loop through the array of available networks and print config options. |
494 | foreach my $zone (@network_zones) { | |
495 | my $checked_input; | |
496 | my $checked_forward; | |
1504a375 | 497 | |
1286e0d4 SS |
498 | # Convert current zone name to upper case. |
499 | my $zone_upper = uc($zone); | |
1504a375 | 500 | |
1286e0d4 | 501 | # Grab checkbox status from settings hash. |
a4ccfcbb | 502 | if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") { |
1286e0d4 SS |
503 | $checked_input = "checked = 'checked'"; |
504 | } | |
1504a375 | 505 | |
1286e0d4 | 506 | print "<td class='base' width='25%'>\n"; |
a4ccfcbb | 507 | print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n"; |
1286e0d4 | 508 | print "</td>\n"; |
ac1cfefa | 509 | } |
1b73b07e | 510 | |
ac1cfefa | 511 | print <<END |
1504a375 SS |
512 | </tr> |
513 | ||
514 | <tr> | |
515 | <td colspan='4'><br><br></td> | |
516 | </tr> | |
517 | ||
518 | <tr> | |
519 | <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td> | |
520 | </tr> | |
521 | ||
522 | <tr> | |
523 | <td colspan='4'><select name='RULES'> | |
5a3e0dca | 524 | <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option> |
a0fa489f | 525 | <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option> |
5a3e0dca MT |
526 | <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option> |
527 | <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option> | |
528 | <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option> | |
529 | </select> | |
1504a375 SS |
530 | </td> |
531 | </tr> | |
532 | ||
533 | <tr> | |
534 | <td colspan='4'> | |
535 | <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> | |
536 | <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> | |
537 | </td> | |
538 | </tr> | |
539 | ||
540 | <tr> | |
1286e0d4 | 541 | <td colspan='4' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td> |
1504a375 SS |
542 | </tr> |
543 | ||
544 | <tr> | |
545 | <td colspan='4' align='left'><br> | |
546 | <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'> $Lang::tr{'updates installed'}: $rulesdate | |
547 | </td> | |
548 | ||
549 | </tr> | |
550 | </table> | |
551 | ||
552 | <br><br> | |
553 | ||
554 | <table width='100%'> | |
555 | <tr> | |
e0bfd338 | 556 | <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td> |
1504a375 SS |
557 | </tr> |
558 | </table> | |
ac1cfefa MT |
559 | </form> |
560 | END | |
561 | ; | |
562 | ||
ac1cfefa | 563 | &Header::closebox(); |
fbfdb241 | 564 | |
f7fcd1c0 | 565 | &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'}); |
298723b9 SS |
566 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
567 | ||
f7fcd1c0 | 568 | # Output display table for rule files |
17726644 | 569 | print "<table width='100%'>\n"; |
ce0e83b3 | 570 | |
17726644 SS |
571 | # Local variable required for java script to show/hide |
572 | # rules of a rulefile. | |
573 | my $rulesetcount = 1; | |
f7fcd1c0 SS |
574 | |
575 | # Loop over each rule file | |
9d18656b | 576 | foreach my $rulefile (sort keys(%idsrules)) { |
f7fcd1c0 SS |
577 | my $rulechecked = ''; |
578 | ||
f7fcd1c0 | 579 | # Check if rule file is enabled |
9d18656b | 580 | if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') { |
f7fcd1c0 SS |
581 | $rulechecked = 'CHECKED'; |
582 | } | |
3ffee04b | 583 | |
17726644 SS |
584 | # Table and rows for the rule files. |
585 | print"<tr>\n"; | |
586 | print"<td class='base' width='5%'>\n"; | |
e5738079 | 587 | print"<input type='checkbox' name='$rulefile' $rulechecked>\n"; |
17726644 SS |
588 | print"</td>\n"; |
589 | print"<td class='base' width='90%'><b>$rulefile</b></td>\n"; | |
590 | print"<td class='base' width='5%' align='right'>\n"; | |
591 | print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n"; | |
592 | print"</td>\n"; | |
593 | print"</tr>\n"; | |
594 | ||
595 | # Rows which will be hidden per default and will contain the single rules. | |
596 | print"<tr style='display:none' id='ruleset$rulesetcount'>\n"; | |
597 | print"<td colspan='3'>\n"; | |
598 | ||
599 | # Local vars | |
600 | my $lines; | |
601 | my $rows; | |
602 | my $col; | |
603 | ||
604 | # New table for the single rules. | |
605 | print "<table width='100%'>\n"; | |
606 | ||
607 | # Loop over rule file rules | |
9d18656b | 608 | foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) { |
f7fcd1c0 | 609 | # Local vars |
17726644 | 610 | my $ruledefchecked = ''; |
f9c2147d | 611 | |
e5738079 SS |
612 | # Skip rulefile itself. |
613 | next if ($sid eq "Rulefile"); | |
614 | ||
17726644 SS |
615 | # If 2 rules have been displayed, start a new row |
616 | if (($lines % 2) == 0) { | |
617 | print "</tr><tr>\n"; | |
3ffee04b | 618 | |
17726644 SS |
619 | # Increase rows by once. |
620 | $rows++; | |
621 | } | |
f7fcd1c0 | 622 | |
17726644 SS |
623 | # Colour lines. |
624 | if ($rows % 2) { | |
625 | $col="bgcolor='$color{'color20'}'"; | |
626 | } else { | |
627 | $col="bgcolor='$color{'color22'}'"; | |
f7fcd1c0 SS |
628 | } |
629 | ||
17726644 | 630 | # Set rule state |
9d18656b | 631 | if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') { |
17726644 | 632 | $ruledefchecked = 'CHECKED'; |
f7fcd1c0 | 633 | } |
3ffee04b | 634 | |
17726644 SS |
635 | # Create rule checkbox and display rule description |
636 | print "<td class='base' width='5%' align='right' $col>\n"; | |
637 | print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n"; | |
638 | print "</td>\n"; | |
9d18656b | 639 | print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>"; |
17726644 SS |
640 | |
641 | # Increment rule count | |
642 | $lines++; | |
643 | } | |
644 | ||
645 | # If do not have a second rule for row, create empty cell | |
646 | if (($lines % 2) != 0) { | |
647 | print "<td class='base'></td>"; | |
f7fcd1c0 SS |
648 | } |
649 | ||
650 | # Close display table | |
17726644 | 651 | print "</tr></table></td></tr>"; |
3ffee04b | 652 | |
17726644 SS |
653 | # Finished whith the rule file, increase count. |
654 | $rulesetcount++; | |
f7fcd1c0 | 655 | } |
17726644 SS |
656 | |
657 | # Close display table | |
658 | print "</table>"; | |
659 | ||
f7fcd1c0 | 660 | print <<END |
2999f1d2 CS |
661 | <table width='100%'> |
662 | <tr> | |
298723b9 | 663 | <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'> |
3ffee04b CS |
664 | <!-- space for future online help link --> |
665 | </td> | |
2999f1d2 CS |
666 | </tr> |
667 | </table> | |
298723b9 | 668 | </form> |
3ffee04b CS |
669 | END |
670 | ; | |
f7fcd1c0 | 671 | &Header::closebox(); |
ac1cfefa MT |
672 | &Header::closebigbox(); |
673 | &Header::closepage(); | |
674 | ||
27760092 SS |
675 | # |
676 | ## A function to display a notice, to lock the webpage and | |
677 | ## tell the user which action currently will be performed. | |
678 | # | |
679 | sub working_notice ($) { | |
680 | my ($message) = @_; | |
681 | ||
682 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
683 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
684 | &Header::openbox( 'Waiting', 1,); | |
685 | print <<END; | |
686 | <table> | |
687 | <tr> | |
688 | <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td> | |
689 | <td>$message</td> | |
690 | </tr> | |
691 | </table> | |
692 | END | |
693 | &Header::closebox(); | |
694 | &Header::closebigbox(); | |
695 | &Header::closepage(); | |
696 | } | |
697 | ||
3983aebd SS |
698 | # |
699 | ## A tiny function to perform a reload of the webpage after one second. | |
700 | # | |
701 | sub reload () { | |
702 | print "<meta http-equiv='refresh' content='1'>\n"; | |
703 | ||
704 | # Stop the script. | |
705 | exit; | |
a70d269a SS |
706 | } |
707 | ||
25f5cb0d SS |
708 | # |
709 | ## Private function to read-in and parse rules of a given rulefile. | |
710 | # | |
711 | ## The given file will be read, parsed and all valid rules will be stored by ID, | |
9d18656b | 712 | ## message/description and it's state in the idsrules hash. |
25f5cb0d | 713 | # |
3da6e01b SS |
714 | sub readrulesfile ($) { |
715 | my $rulefile = shift; | |
716 | ||
717 | # Open rule file and read in contents | |
298ef5ba | 718 | open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!"; |
3da6e01b SS |
719 | |
720 | # Store file content in an array. | |
721 | my @lines = <RULEFILE>; | |
722 | ||
723 | # Close file. | |
724 | close(RULEFILE); | |
725 | ||
726 | # Loop over rule file contents | |
727 | foreach my $line (@lines) { | |
728 | # Remove whitespaces. | |
729 | chomp $line; | |
730 | ||
731 | # Skip blank lines. | |
732 | next if ($line =~ /^\s*$/); | |
733 | ||
734 | # Local vars. | |
735 | my $sid; | |
736 | my $msg; | |
737 | ||
738 | # Gather rule sid and message from the ruleline. | |
739 | if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) { | |
740 | $msg = $1; | |
741 | $sid = $2; | |
742 | ||
743 | # Check if a rule has been found. | |
744 | if ($sid && $msg) { | |
9d18656b SS |
745 | # Add rule to the idsrules hash. |
746 | $idsrules{$rulefile}{$sid}{'Description'} = $msg; | |
3da6e01b SS |
747 | |
748 | # Grab status of the rule. Check if ruleline starts with a "dash". | |
749 | if ($line =~ /^\#/) { | |
750 | # If yes, the rule is disabled. | |
9d18656b | 751 | $idsrules{$rulefile}{$sid}{'State'} = "off"; |
3da6e01b SS |
752 | } else { |
753 | # Otherwise the rule is enabled. | |
9d18656b | 754 | $idsrules{$rulefile}{$sid}{'State'} = "on"; |
3da6e01b SS |
755 | } |
756 | } | |
757 | } | |
758 | } | |
759 | } | |
87660964 | 760 | |
8d2f6b0b SS |
761 | # |
762 | ## Function to get the used memory of a given process-id. | |
763 | # | |
87660964 SS |
764 | sub get_memory_usage($) { |
765 | my $pid = @_; | |
766 | ||
767 | my $memory=0; | |
768 | ||
769 | # Try to open statm file for the given process-id on the pseudo | |
770 | # file system proc. | |
771 | if (open(FILE, "/proc/$pid/statm")) { | |
772 | # Read file content. | |
773 | my $temp = <FILE>; | |
774 | ||
775 | # Splitt file content and store in an array. | |
776 | my @memory = split(/ /,$temp); | |
777 | ||
778 | # Close file handle. | |
779 | close(FILE); | |
780 | ||
781 | # Calculate memory usage. | |
782 | $memory+=$memory[0]; | |
783 | ||
784 | # Return memory usage. | |
785 | return $memory; | |
786 | } | |
787 | ||
788 | # If the file could not be open, return nothing. | |
789 | return; | |
790 | } | |
791 | ||
8d2f6b0b SS |
792 | # |
793 | ## Function to generate the file which contains the home net information. | |
794 | # | |
795 | sub generate_home_net_file() { | |
796 | my %netsettings; | |
797 | ||
798 | # Read-in network settings. | |
799 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
800 | ||
801 | # Get available network zones. | |
802 | my @network_zones = &IDS::get_available_network_zones(); | |
803 | ||
804 | # Temporary array to store network address and prefix of the configured | |
805 | # networks. | |
806 | my @networks; | |
807 | ||
808 | # Loop through the array of available network zones. | |
809 | foreach my $zone (@network_zones) { | |
810 | # Skip the red network - It never can be part to the home_net! | |
811 | next if($zone eq "red"); | |
812 | ||
813 | # Convert current zone name into upper case. | |
814 | $zone = uc($zone); | |
815 | ||
816 | # Generate key to access the required data from the netsettings hash. | |
817 | my $zone_netaddress = $zone . "_NETADDRESS"; | |
818 | my $zone_netmask = $zone . "_NETMASK"; | |
819 | ||
820 | # Obtain the settings from the netsettings hash. | |
821 | my $netaddress = $netsettings{$zone_netaddress}; | |
822 | my $netmask = $netsettings{$zone_netmask}; | |
823 | ||
824 | # Convert the subnetmask into prefix notation. | |
825 | my $prefix = &Network::convert_netmask2prefix($netmask); | |
826 | ||
827 | # Generate full network string. | |
828 | my $network = join("/", $netaddress,$prefix); | |
829 | ||
830 | # Check if the network is valid. | |
831 | if(&Network::check_subnet($network)) { | |
832 | # Add the generated network to the array of networks. | |
833 | push(@networks, $network); | |
834 | } | |
835 | } | |
836 | ||
837 | # Format home net declaration. | |
838 | my $line = "\"\["; | |
839 | ||
840 | # Loop through the array of networks. | |
841 | foreach my $network (@networks) { | |
842 | # Add the network to the line. | |
843 | $line = "$line" . "$network"; | |
844 | ||
845 | # Check if the current network was the last in the array. | |
846 | if ($network eq $networks[-1]) { | |
847 | # Close the line. | |
848 | $line = "$line" . "\]\""; | |
849 | } else { | |
850 | # Add "," for the next network. | |
851 | $line = "$line" . "\,"; | |
852 | } | |
853 | } | |
854 | ||
855 | # Open file to store the addresses of the home net. | |
856 | open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n"; | |
857 | ||
858 | # Print yaml header. | |
859 | print FILE "%YAML 1.1\n"; | |
860 | print FILE "---\n\n"; | |
861 | ||
862 | # Print notice about autogenerated file. | |
863 | print FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
864 | ||
865 | # Print the generated and required HOME_NET declaration to the file. | |
866 | print FILE "HOME_NET:\t$line\n"; | |
867 | ||
868 | # Close file handle. | |
869 | close(FILE); | |
870 | ||
871 | } |