]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
Enable threshold file in suricata.yaml
[people/pmueller/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
00512a5a 67# Create files if they does not exist yet.
01ba4be4
SS
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); }
00512a5a 71unless (-f "$idsusedrulefilesfile") { &IDS::create_empty_file($idsusedrulefilesfile); }
01ba4be4 72
ac1cfefa
MT
73&Header::showhttpheaders();
74
298723b9
SS
75#Get GUI values
76&Header::getcgihash(\%cgiparams);
ac1cfefa 77
3983aebd
SS
78# Check if any error has been stored.
79if (-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
9d18656b 94## Grab all available snort rules and store them in the idsrules hash.
3da6e01b 95#
422204ff 96# Open snort rules directory and do a directory listing.
298ef5ba 97opendir(DIR, $IDS::rulespath) or die $!;
422204ff
SS
98 # Loop through the direcory.
99 while (my $file = readdir(DIR)) {
100
101 # We only want files.
298ef5ba 102 next unless (-f "$IDS::rulespath/$file");
422204ff
SS
103
104 # Ignore empty files.
298ef5ba 105 next if (-z "$IDS::rulespath/$file");
422204ff 106
3da6e01b 107 # Use a regular expression to find files ending in .rules
422204ff
SS
108 next unless ($file =~ m/\.rules$/);
109
3da6e01b 110 # Ignore files which are not read-able.
298ef5ba 111 next unless (-R "$IDS::rulespath/$file");
395e3b90 112
3da6e01b 113 # Call subfunction to read-in rulefile and add rules to
9d18656b 114 # the idsrules hash.
3da6e01b 115 &readrulesfile("$file");
395e3b90 116 }
395e3b90 117
3da6e01b 118closedir(DIR);
395e3b90 119
e5738079
SS
120# Gather used rulefiles.
121#
122# Check if the file for activated rulefiles is not empty.
02844177 123if(-f $idsusedrulefilesfile) {
e5738079 124 # Open the file for used rulefile and read-in content.
02844177 125 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
e5738079
SS
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.
101c8881 145 if ($line =~ /.*- (.*)/) {
e5738079
SS
146 my $rulefile = $1;
147
9d18656b
SS
148 # Add the rulefile to the %idsrules hash.
149 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
e5738079
SS
150 }
151 }
152}
153
298723b9
SS
154# Save ruleset.
155if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
d2212836 156 # Arrays to store which rulefiles have been enabled and will be used.
e5738079 157 my @enabled_rulefiles;
298723b9 158
d2212836
SS
159 # Hash to store the user-enabled and disabled sids.
160 my %enabled_disabled_sids;
161
9d18656b
SS
162 # Loop through the hash of idsrules.
163 foreach my $rulefile(keys %idsrules) {
e5738079
SS
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);
b65b5ef3
SS
168
169 # Drop item from cgiparams hash.
170 delete $cgiparams{$rulefile};
e5738079 171 }
466c6779 172 }
e5738079 173
d2212836
SS
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
9d18656b
SS
181 # Loop through the hash of idsrules.
182 foreach my $rulefile (keys %idsrules) {
298723b9 183 # Loop through the single rules of the rulefile.
9d18656b 184 foreach my $sid (keys %{$idsrules{$rulefile}}) {
c51a044a
SS
185 # Skip the current sid if it is not numeric.
186 next unless ($sid =~ /\d+/ );
187
298723b9
SS
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.
9d18656b 191 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
298723b9
SS
192 # Check if the state has been set to 'on'.
193 if ($cgiparams{$sid} eq "on") {
d2212836
SS
194 # Add/Modify the sid to/in the enabled_disabled_sids hash.
195 $enabled_disabled_sids{$sid} = "enabled";
298723b9
SS
196
197 # Drop item from cgiparams hash.
60333473 198 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
199 }
200 }
201 } else {
202 # Look if the rule is enabled.
9d18656b 203 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
298723b9
SS
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.
d2212836
SS
207 # Add/Modify it to/in the enabled_disabled_sids hash.
208 $enabled_disabled_sids{$sid} = "disabled";
298723b9
SS
209
210 # Drop item from cgiparams hash.
60333473 211 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
212 }
213 }
214 }
215 }
216
37659505 217 # Open enabled sid's file for writing.
d2212836 218 open(ENABLED_FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
37659505
SS
219
220 # Open disabled sid's file for writing.
d2212836
SS
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 }
37659505
SS
243 }
244 }
298723b9 245
d2212836
SS
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);
e5738079
SS
251
252 # Open file for used rulefiles.
02844177 253 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
e5738079 254
101c8881
SS
255 # Write yaml header to the file.
256 print FILE "%YAML 1.1\n";
257 print FILE "---\n\n";
258
e5738079
SS
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) {
101c8881 266 print FILE " - $file\n";
e5738079
SS
267 }
268 }
269
270 # Close file after writing.
271 close(FILE);
52599865 272
27760092
SS
273 # Lock the webpage and print message.
274 &working_notice("$Lang::tr{'snort working'}");
275
52599865 276 # Call oinkmaster to alter the ruleset.
27760092
SS
277 &IDS::oinkmaster();
278
e2e7880d 279 # Check if the IDS is running.
5a28e721 280 if(&IDS::ids_is_running()) {
e2e7880d
SS
281 # Call suricatactrl to perform a reload.
282 &IDS::call_suricatactrl("reload");
283 }
284
27760092
SS
285 # Reload page.
286 &reload();
52599865
SS
287
288# Download new ruleset.
289} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
43263ea6
SS
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 }
52599865 294
3983aebd 295 # Check if enought free disk space is availabe.
434001d0
SS
296 if(&IDS::checkdiskspace()) {
297 $errormessage = "$Lang::tr{'not enough disk space'}";
298 }
52599865 299
43263ea6
SS
300 # Check if any errors happend.
301 unless ($errormessage) {
27760092
SS
302 # Lock the webpage and print notice about downloading
303 # a new ruleset.
304 &working_notice("$Lang::tr{'snort working'}");
3983aebd 305
43263ea6 306 # Call subfunction to download the ruleset.
434001d0
SS
307 if(&IDS::downloadruleset()) {
308 $errormessage = $Lang::tr{'could not download latest updates'};
8f22237b 309
3983aebd 310 # Call function to store the errormessage.
434001d0 311 &IDS::_store_error_message($errormessage);
52599865 312
3983aebd
SS
313 # Preform a reload of the page.
314 &reload();
315 } else {
316 # Call subfunction to launch oinkmaster.
317 &IDS::oinkmaster();
43263ea6 318
e2e7880d 319 # Check if the IDS is running.
5a28e721 320 if(&IDS::ids_is_running()) {
e2e7880d
SS
321 # Call suricatactrl to perform a reload.
322 &IDS::call_suricatactrl("reload");
323 }
324
3983aebd
SS
325 # Perform a reload of the page.
326 &reload();
327 }
52599865 328 }
a232b58c 329# Save snort settings.
e0bfd338 330} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
bbb6efae
SS
331 my %oldidssettings;
332 my $reload_page;
333
334 # Read-in current (old) IDS settings.
335 &General::readhash("$IDS::settingsdir/settings", \%oldidssettings);
336
a232b58c 337 # Prevent form name from been stored in conf file.
e0bfd338 338 delete $cgiparams{'IDS'};
a232b58c
SS
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 }
f9c2147d 346 }
ac1cfefa 347
a232b58c
SS
348 # Go on if there are no error messages.
349 if (!$errormessage) {
350 # Store settings into settings file.
02844177 351 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
a9a91e5f 352 }
8d2f6b0b
SS
353
354 # Generate file to store the home net.
355 &generate_home_net_file();
e2e7880d 356
bbb6efae
SS
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
e2e7880d
SS
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 }
bbb6efae
SS
401
402 # Check if the page should be reloaded.
403 if ($reload_page) {
404 # Perform a reload of the page.
405 &reload();
406 }
ac1cfefa
MT
407}
408
1286e0d4
SS
409# Read-in idssettings
410&General::readhash("$IDS::settingsdir/settings", \%idssettings);
411
a4ccfcbb
SS
412# If the runmode has not been configured yet, set default value.
413unless(exists($idssettings{'RUN_MODE'})) {
414 # Set default to IPS.
415 $idssettings{'RUN_MODE'} = 'IPS';
416}
417
1286e0d4
SS
418$checked{'ENABLE_IDS'}{'off'} = '';
419$checked{'ENABLE_IDS'}{'on'} = '';
420$checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
a4ccfcbb
SS
421$checked{'RUN_MODE'}{'IDS'} = '';
422$checked{'RUN_MODE'}{'IPS'} = '';
423$checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
5a3e0dca 424$selected{'RULES'}{'nothing'} = '';
5a3e0dca 425$selected{'RULES'}{'community'} = '';
a0fa489f 426$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
427$selected{'RULES'}{'registered'} = '';
428$selected{'RULES'}{'subscripted'} = '';
1286e0d4 429$selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
430
431&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
432
17726644
SS
433### Java Script ###
434print <<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>
442END
443;
444
ac1cfefa
MT
445&Header::openbigbox('100%', 'left', '', $errormessage);
446
447if ($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
87660964 454# Draw current state of the IDS
7cc8a0e5 455&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 456
87660964
SS
457# Check if the IDS is running and obtain the process-id.
458my $pid = &IDS::ids_is_running();
459
460# Display some useful information, if suricata daemon is running.
461if ($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>
488END
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>
87660964
SS
502END
503}
504&Header::closebox();
505
506# Draw elements for IDS configuration.
507&Header::openbox('100%', 'center', $Lang::tr{'settings'});
508
1504a375
SS
509my $rulesdate;
510
511# Check if a ruleset allready has been downloaded.
a69b96d2 512if ( -f "$IDS::rulestarball"){
1504a375 513 # Call stat on the filename to obtain detailed information.
a69b96d2 514 my @Info = stat("$IDS::rulestarball");
1504a375
SS
515
516 # Grab details about the creation time.
517 $rulesdate = localtime($Info[9]);
518}
519
ac1cfefa 520print <<END
1504a375
SS
521<form method='post' action='$ENV{'SCRIPT_NAME'}'>
522 <table width='100%' border='0'>
523 <tr>
a4ccfcbb 524 <td class='base' colspan='4'>
1286e0d4
SS
525 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
526 </td>
a4ccfcbb 527 </tr>
1286e0d4 528
a4ccfcbb
SS
529 <tr>
530 <td colspan='4'><br><br></td>
1286e0d4
SS
531 </tr>
532
533 <tr>
a4ccfcbb 534 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
1286e0d4 535 </tr>
1504a375 536
1286e0d4 537 <tr>
a4ccfcbb
SS
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'}
1504a375 541 </td>
a4ccfcbb 542 </tr>
1504a375 543
a4ccfcbb
SS
544 <tr>
545 <td colspan='4'><br></td>
1286e0d4 546 </tr>
a4ccfcbb
SS
547
548 <tr>
549 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
550 </tr>
551
552 <tr>
ac1cfefa
MT
553END
554;
1504a375 555
1286e0d4
SS
556# Loop through the array of available networks and print config options.
557foreach my $zone (@network_zones) {
558 my $checked_input;
559 my $checked_forward;
1504a375 560
1286e0d4
SS
561 # Convert current zone name to upper case.
562 my $zone_upper = uc($zone);
1504a375 563
1286e0d4 564 # Grab checkbox status from settings hash.
a4ccfcbb 565 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1286e0d4
SS
566 $checked_input = "checked = 'checked'";
567 }
1504a375 568
1286e0d4 569 print "<td class='base' width='25%'>\n";
a4ccfcbb 570 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
1286e0d4 571 print "</td>\n";
ac1cfefa 572}
1b73b07e 573
ac1cfefa 574print <<END
1504a375
SS
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'>
5a3e0dca 587 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 588 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
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>
1504a375
SS
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>
1286e0d4 604 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
1504a375
SS
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>
e0bfd338 619 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1504a375
SS
620 </tr>
621 </table>
ac1cfefa
MT
622</form>
623END
624;
625
ac1cfefa 626&Header::closebox();
fbfdb241 627
f7fcd1c0 628&Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9
SS
629 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
630
f7fcd1c0 631 # Output display table for rule files
17726644 632 print "<table width='100%'>\n";
ce0e83b3 633
17726644
SS
634 # Local variable required for java script to show/hide
635 # rules of a rulefile.
636 my $rulesetcount = 1;
f7fcd1c0
SS
637
638 # Loop over each rule file
9d18656b 639 foreach my $rulefile (sort keys(%idsrules)) {
f7fcd1c0
SS
640 my $rulechecked = '';
641
f7fcd1c0 642 # Check if rule file is enabled
9d18656b 643 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
f7fcd1c0
SS
644 $rulechecked = 'CHECKED';
645 }
3ffee04b 646
17726644
SS
647 # Table and rows for the rule files.
648 print"<tr>\n";
649 print"<td class='base' width='5%'>\n";
e5738079 650 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
17726644
SS
651 print"</td>\n";
652 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
653 print"<td class='base' width='5%' align='right'>\n";
654 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
655 print"</td>\n";
656 print"</tr>\n";
657
658 # Rows which will be hidden per default and will contain the single rules.
659 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
660 print"<td colspan='3'>\n";
661
662 # Local vars
663 my $lines;
664 my $rows;
665 my $col;
666
667 # New table for the single rules.
668 print "<table width='100%'>\n";
669
670 # Loop over rule file rules
9d18656b 671 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
f7fcd1c0 672 # Local vars
17726644 673 my $ruledefchecked = '';
f9c2147d 674
e5738079
SS
675 # Skip rulefile itself.
676 next if ($sid eq "Rulefile");
677
17726644
SS
678 # If 2 rules have been displayed, start a new row
679 if (($lines % 2) == 0) {
680 print "</tr><tr>\n";
3ffee04b 681
17726644
SS
682 # Increase rows by once.
683 $rows++;
684 }
f7fcd1c0 685
17726644
SS
686 # Colour lines.
687 if ($rows % 2) {
688 $col="bgcolor='$color{'color20'}'";
689 } else {
690 $col="bgcolor='$color{'color22'}'";
f7fcd1c0
SS
691 }
692
17726644 693 # Set rule state
9d18656b 694 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
17726644 695 $ruledefchecked = 'CHECKED';
f7fcd1c0 696 }
3ffee04b 697
17726644
SS
698 # Create rule checkbox and display rule description
699 print "<td class='base' width='5%' align='right' $col>\n";
700 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
701 print "</td>\n";
9d18656b 702 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
17726644
SS
703
704 # Increment rule count
705 $lines++;
706 }
707
708 # If do not have a second rule for row, create empty cell
709 if (($lines % 2) != 0) {
710 print "<td class='base'></td>";
f7fcd1c0
SS
711 }
712
713 # Close display table
17726644 714 print "</tr></table></td></tr>";
3ffee04b 715
17726644
SS
716 # Finished whith the rule file, increase count.
717 $rulesetcount++;
f7fcd1c0 718 }
17726644
SS
719
720 # Close display table
721 print "</table>";
722
f7fcd1c0 723print <<END
2999f1d2
CS
724<table width='100%'>
725<tr>
298723b9 726 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
727 &nbsp; <!-- space for future online help link -->
728 </td>
2999f1d2
CS
729</tr>
730</table>
298723b9 731</form>
3ffee04b
CS
732END
733;
f7fcd1c0 734&Header::closebox();
ac1cfefa
MT
735&Header::closebigbox();
736&Header::closepage();
737
27760092
SS
738#
739## A function to display a notice, to lock the webpage and
740## tell the user which action currently will be performed.
741#
742sub working_notice ($) {
743 my ($message) = @_;
744
745 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
746 &Header::openbigbox('100%', 'left', '', $errormessage);
747 &Header::openbox( 'Waiting', 1,);
748 print <<END;
749 <table>
750 <tr>
751 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
752 <td>$message</td>
753 </tr>
754 </table>
755END
756 &Header::closebox();
757 &Header::closebigbox();
758 &Header::closepage();
759}
760
3983aebd
SS
761#
762## A tiny function to perform a reload of the webpage after one second.
763#
764sub reload () {
765 print "<meta http-equiv='refresh' content='1'>\n";
766
767 # Stop the script.
768 exit;
a70d269a
SS
769}
770
25f5cb0d
SS
771#
772## Private function to read-in and parse rules of a given rulefile.
773#
774## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 775## message/description and it's state in the idsrules hash.
25f5cb0d 776#
3da6e01b
SS
777sub readrulesfile ($) {
778 my $rulefile = shift;
779
780 # Open rule file and read in contents
298ef5ba 781 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
782
783 # Store file content in an array.
784 my @lines = <RULEFILE>;
785
786 # Close file.
787 close(RULEFILE);
788
789 # Loop over rule file contents
790 foreach my $line (@lines) {
791 # Remove whitespaces.
792 chomp $line;
793
794 # Skip blank lines.
795 next if ($line =~ /^\s*$/);
796
797 # Local vars.
798 my $sid;
799 my $msg;
800
801 # Gather rule sid and message from the ruleline.
802 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
803 $msg = $1;
804 $sid = $2;
805
806 # Check if a rule has been found.
807 if ($sid && $msg) {
9d18656b
SS
808 # Add rule to the idsrules hash.
809 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
810
811 # Grab status of the rule. Check if ruleline starts with a "dash".
812 if ($line =~ /^\#/) {
813 # If yes, the rule is disabled.
9d18656b 814 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
815 } else {
816 # Otherwise the rule is enabled.
9d18656b 817 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
818 }
819 }
820 }
821 }
822}
87660964 823
8d2f6b0b
SS
824#
825## Function to get the used memory of a given process-id.
826#
87660964 827sub get_memory_usage($) {
004b13b7 828 my ($pid) = @_;
87660964 829
004b13b7 830 my $memory = 0;
87660964 831
004b13b7 832 # Try to open the status file for the given process-id on the pseudo
87660964 833 # file system proc.
004b13b7
SS
834 if (open(FILE, "/proc/$pid/status")) {
835 # Loop through the entire file.
836 while (<FILE>) {
837 # Splitt current line content and store them into variables.
838 my ($key, $value) = split(":", $_, 2);
839
840 # Check if the current key is the one which contains the memory usage.
841 # The wanted one is VmRSS which contains the Real-memory (resident set)
842 # of the entire process.
843 if ($key eq "VmRSS") {
844 # Found the memory usage add it to the memory variable.
845 $memory += $value;
846
847 # Break the loop.
848 last;
849 }
850 }
87660964
SS
851
852 # Close file handle.
004b13b7 853 close(FILE);
87660964
SS
854
855 # Return memory usage.
856 return $memory;
004b13b7 857 }
87660964
SS
858
859 # If the file could not be open, return nothing.
860 return;
861}
862
8d2f6b0b
SS
863#
864## Function to generate the file which contains the home net information.
865#
866sub generate_home_net_file() {
867 my %netsettings;
868
869 # Read-in network settings.
870 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
871
872 # Get available network zones.
873 my @network_zones = &IDS::get_available_network_zones();
874
875 # Temporary array to store network address and prefix of the configured
876 # networks.
877 my @networks;
878
879 # Loop through the array of available network zones.
880 foreach my $zone (@network_zones) {
881 # Skip the red network - It never can be part to the home_net!
882 next if($zone eq "red");
883
884 # Convert current zone name into upper case.
885 $zone = uc($zone);
886
887 # Generate key to access the required data from the netsettings hash.
888 my $zone_netaddress = $zone . "_NETADDRESS";
889 my $zone_netmask = $zone . "_NETMASK";
890
891 # Obtain the settings from the netsettings hash.
892 my $netaddress = $netsettings{$zone_netaddress};
893 my $netmask = $netsettings{$zone_netmask};
894
895 # Convert the subnetmask into prefix notation.
896 my $prefix = &Network::convert_netmask2prefix($netmask);
897
898 # Generate full network string.
899 my $network = join("/", $netaddress,$prefix);
900
901 # Check if the network is valid.
902 if(&Network::check_subnet($network)) {
903 # Add the generated network to the array of networks.
904 push(@networks, $network);
905 }
906 }
907
908 # Format home net declaration.
909 my $line = "\"\[";
910
911 # Loop through the array of networks.
912 foreach my $network (@networks) {
913 # Add the network to the line.
914 $line = "$line" . "$network";
915
916 # Check if the current network was the last in the array.
917 if ($network eq $networks[-1]) {
918 # Close the line.
919 $line = "$line" . "\]\"";
920 } else {
921 # Add "," for the next network.
922 $line = "$line" . "\,";
923 }
924 }
925
926 # Open file to store the addresses of the home net.
927 open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
928
929 # Print yaml header.
930 print FILE "%YAML 1.1\n";
931 print FILE "---\n\n";
932
933 # Print notice about autogenerated file.
934 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
935
936 # Print the generated and required HOME_NET declaration to the file.
937 print FILE "HOME_NET:\t$line\n";
938
939 # Close file handle.
940 close(FILE);
941
942}
a5d61752
SS
943
944#
945## Function to read-in the given enabled or disables sids file.
946#
947sub read_enabled_disabled_sids_file($) {
948 my ($file) = @_;
949
950 # Temporary hash to store the sids and their state. It will be
951 # returned at the end of this function.
952 my %temphash;
953
954 # Open the given filename.
955 open(FILE, "$file") or die "Could not open $file. $!\n";
956
957 # Loop through the file.
958 while(<FILE>) {
959 # Remove newlines.
960 chomp $_;
961
962 # Skip blank lines.
963 next if ($_ =~ /^\s*$/);
964
965 # Skip coments.
966 next if ($_ =~ /^\#/);
967
968 # Splitt line into sid and state part.
969 my ($state, $sid) = split(" ", $_);
970
971 # Skip line if the sid is not numeric.
972 next unless ($sid =~ /\d+/ );
973
974 # Check if the sid was enabled.
975 if ($state eq "enablesid") {
976 # Add the sid and its state as enabled to the temporary hash.
977 $temphash{$sid} = "enabled";
978 # Check if the sid was disabled.
979 } elsif ($state eq "disablesid") {
980 # Add the sid and its state as disabled to the temporary hash.
981 $temphash{$sid} = "disabled";
982 # Invalid state - skip the current sid and state.
983 } else {
984 next;
985 }
986 }
987
988 # Close filehandle.
989 close(FILE);
990
991 # Return the hash.
992 return %temphash;
993}