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