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