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