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