]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids-functions.pl: Add function to create empty files
[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
43263ea6
SS
56my $errormessage;
57
ac1cfefa
MT
58&Header::showhttpheaders();
59
298723b9
SS
60#Get GUI values
61&Header::getcgihash(\%cgiparams);
ac1cfefa 62
3983aebd
SS
63# Check if any error has been stored.
64if (-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
9d18656b 79## Grab all available snort rules and store them in the idsrules hash.
3da6e01b 80#
422204ff 81# Open snort rules directory and do a directory listing.
298ef5ba 82opendir(DIR, $IDS::rulespath) or die $!;
422204ff
SS
83 # Loop through the direcory.
84 while (my $file = readdir(DIR)) {
85
86 # We only want files.
298ef5ba 87 next unless (-f "$IDS::rulespath/$file");
422204ff
SS
88
89 # Ignore empty files.
298ef5ba 90 next if (-z "$IDS::rulespath/$file");
422204ff 91
3da6e01b 92 # Use a regular expression to find files ending in .rules
422204ff
SS
93 next unless ($file =~ m/\.rules$/);
94
3da6e01b 95 # Ignore files which are not read-able.
298ef5ba 96 next unless (-R "$IDS::rulespath/$file");
395e3b90 97
3da6e01b 98 # Call subfunction to read-in rulefile and add rules to
9d18656b 99 # the idsrules hash.
3da6e01b 100 &readrulesfile("$file");
395e3b90 101 }
395e3b90 102
3da6e01b 103closedir(DIR);
395e3b90 104
e5738079
SS
105# Gather used rulefiles.
106#
107# Check if the file for activated rulefiles is not empty.
02844177 108if(-f $idsusedrulefilesfile) {
e5738079 109 # Open the file for used rulefile and read-in content.
02844177 110 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
e5738079
SS
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.
101c8881 130 if ($line =~ /.*- (.*)/) {
e5738079
SS
131 my $rulefile = $1;
132
9d18656b
SS
133 # Add the rulefile to the %idsrules hash.
134 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
e5738079
SS
135 }
136 }
137}
138
298723b9
SS
139# Save ruleset.
140if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
02844177
SS
141 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
142 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
298723b9 143
d2212836 144 # Arrays to store which rulefiles have been enabled and will be used.
e5738079 145 my @enabled_rulefiles;
298723b9 146
d2212836
SS
147 # Hash to store the user-enabled and disabled sids.
148 my %enabled_disabled_sids;
149
9d18656b
SS
150 # Loop through the hash of idsrules.
151 foreach my $rulefile(keys %idsrules) {
e5738079
SS
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);
b65b5ef3
SS
156
157 # Drop item from cgiparams hash.
158 delete $cgiparams{$rulefile};
e5738079 159 }
466c6779 160 }
e5738079 161
d2212836
SS
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
9d18656b
SS
169 # Loop through the hash of idsrules.
170 foreach my $rulefile (keys %idsrules) {
298723b9 171 # Loop through the single rules of the rulefile.
9d18656b 172 foreach my $sid (keys %{$idsrules{$rulefile}}) {
c51a044a
SS
173 # Skip the current sid if it is not numeric.
174 next unless ($sid =~ /\d+/ );
175
298723b9
SS
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.
9d18656b 179 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
298723b9
SS
180 # Check if the state has been set to 'on'.
181 if ($cgiparams{$sid} eq "on") {
d2212836
SS
182 # Add/Modify the sid to/in the enabled_disabled_sids hash.
183 $enabled_disabled_sids{$sid} = "enabled";
298723b9
SS
184
185 # Drop item from cgiparams hash.
60333473 186 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
187 }
188 }
189 } else {
190 # Look if the rule is enabled.
9d18656b 191 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
298723b9
SS
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.
d2212836
SS
195 # Add/Modify it to/in the enabled_disabled_sids hash.
196 $enabled_disabled_sids{$sid} = "disabled";
298723b9
SS
197
198 # Drop item from cgiparams hash.
60333473 199 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
200 }
201 }
202 }
203 }
204
37659505 205 # Open enabled sid's file for writing.
d2212836 206 open(ENABLED_FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
37659505
SS
207
208 # Open disabled sid's file for writing.
d2212836
SS
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 }
37659505
SS
231 }
232 }
298723b9 233
d2212836
SS
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);
e5738079
SS
239
240 # Open file for used rulefiles.
02844177 241 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
e5738079 242
101c8881
SS
243 # Write yaml header to the file.
244 print FILE "%YAML 1.1\n";
245 print FILE "---\n\n";
246
e5738079
SS
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) {
101c8881 254 print FILE " - $file\n";
e5738079
SS
255 }
256 }
257
258 # Close file after writing.
259 close(FILE);
52599865 260
27760092
SS
261 # Lock the webpage and print message.
262 &working_notice("$Lang::tr{'snort working'}");
263
52599865 264 # Call oinkmaster to alter the ruleset.
27760092
SS
265 &IDS::oinkmaster();
266
e2e7880d 267 # Check if the IDS is running.
5a28e721 268 if(&IDS::ids_is_running()) {
e2e7880d
SS
269 # Call suricatactrl to perform a reload.
270 &IDS::call_suricatactrl("reload");
271 }
272
27760092
SS
273 # Reload page.
274 &reload();
52599865
SS
275
276# Download new ruleset.
277} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
43263ea6
SS
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 }
52599865 282
3983aebd 283 # Check if enought free disk space is availabe.
434001d0
SS
284 if(&IDS::checkdiskspace()) {
285 $errormessage = "$Lang::tr{'not enough disk space'}";
286 }
52599865 287
43263ea6
SS
288 # Check if any errors happend.
289 unless ($errormessage) {
27760092
SS
290 # Lock the webpage and print notice about downloading
291 # a new ruleset.
292 &working_notice("$Lang::tr{'snort working'}");
3983aebd 293
43263ea6 294 # Call subfunction to download the ruleset.
434001d0
SS
295 if(&IDS::downloadruleset()) {
296 $errormessage = $Lang::tr{'could not download latest updates'};
8f22237b 297
3983aebd 298 # Call function to store the errormessage.
434001d0 299 &IDS::_store_error_message($errormessage);
52599865 300
3983aebd
SS
301 # Preform a reload of the page.
302 &reload();
303 } else {
304 # Call subfunction to launch oinkmaster.
305 &IDS::oinkmaster();
43263ea6 306
e2e7880d 307 # Check if the IDS is running.
5a28e721 308 if(&IDS::ids_is_running()) {
e2e7880d
SS
309 # Call suricatactrl to perform a reload.
310 &IDS::call_suricatactrl("reload");
311 }
312
3983aebd
SS
313 # Perform a reload of the page.
314 &reload();
315 }
52599865 316 }
a232b58c 317# Save snort settings.
e0bfd338 318} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
bbb6efae
SS
319 my %oldidssettings;
320 my $reload_page;
321
322 # Read-in current (old) IDS settings.
323 &General::readhash("$IDS::settingsdir/settings", \%oldidssettings);
324
a232b58c 325 # Prevent form name from been stored in conf file.
e0bfd338 326 delete $cgiparams{'IDS'};
a232b58c
SS
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 }
f9c2147d 334 }
ac1cfefa 335
a232b58c
SS
336 # Go on if there are no error messages.
337 if (!$errormessage) {
338 # Store settings into settings file.
02844177 339 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
a9a91e5f 340 }
8d2f6b0b
SS
341
342 # Generate file to store the home net.
343 &generate_home_net_file();
e2e7880d 344
bbb6efae
SS
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
e2e7880d
SS
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 }
bbb6efae
SS
392
393 # Check if the page should be reloaded.
394 if ($reload_page) {
395 # Perform a reload of the page.
396 &reload();
397 }
ac1cfefa
MT
398}
399
1286e0d4
SS
400# Read-in idssettings
401&General::readhash("$IDS::settingsdir/settings", \%idssettings);
402
a4ccfcbb
SS
403# If the runmode has not been configured yet, set default value.
404unless(exists($idssettings{'RUN_MODE'})) {
405 # Set default to IPS.
406 $idssettings{'RUN_MODE'} = 'IPS';
407}
408
1286e0d4
SS
409$checked{'ENABLE_IDS'}{'off'} = '';
410$checked{'ENABLE_IDS'}{'on'} = '';
411$checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
a4ccfcbb
SS
412$checked{'RUN_MODE'}{'IDS'} = '';
413$checked{'RUN_MODE'}{'IPS'} = '';
414$checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
5a3e0dca 415$selected{'RULES'}{'nothing'} = '';
5a3e0dca 416$selected{'RULES'}{'community'} = '';
a0fa489f 417$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
418$selected{'RULES'}{'registered'} = '';
419$selected{'RULES'}{'subscripted'} = '';
1286e0d4 420$selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
421
422&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
423
17726644
SS
424### Java Script ###
425print <<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>
433END
434;
435
ac1cfefa
MT
436&Header::openbigbox('100%', 'left', '', $errormessage);
437
438if ($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
87660964 445# Draw current state of the IDS
7cc8a0e5 446&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 447
87660964
SS
448# Check if the IDS is running and obtain the process-id.
449my $pid = &IDS::ids_is_running();
450
451# Display some useful information, if suricata daemon is running.
452if ($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>
479END
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>
87660964
SS
493END
494}
495&Header::closebox();
496
497# Draw elements for IDS configuration.
498&Header::openbox('100%', 'center', $Lang::tr{'settings'});
499
1504a375
SS
500my $rulesdate;
501
502# Check if a ruleset allready has been downloaded.
a69b96d2 503if ( -f "$IDS::rulestarball"){
1504a375 504 # Call stat on the filename to obtain detailed information.
a69b96d2 505 my @Info = stat("$IDS::rulestarball");
1504a375
SS
506
507 # Grab details about the creation time.
508 $rulesdate = localtime($Info[9]);
509}
510
ac1cfefa 511print <<END
1504a375
SS
512<form method='post' action='$ENV{'SCRIPT_NAME'}'>
513 <table width='100%' border='0'>
514 <tr>
a4ccfcbb 515 <td class='base' colspan='4'>
1286e0d4
SS
516 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
517 </td>
a4ccfcbb 518 </tr>
1286e0d4 519
a4ccfcbb
SS
520 <tr>
521 <td colspan='4'><br><br></td>
1286e0d4
SS
522 </tr>
523
524 <tr>
a4ccfcbb 525 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
1286e0d4 526 </tr>
1504a375 527
1286e0d4 528 <tr>
a4ccfcbb
SS
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'}
1504a375 532 </td>
a4ccfcbb 533 </tr>
1504a375 534
a4ccfcbb
SS
535 <tr>
536 <td colspan='4'><br></td>
1286e0d4 537 </tr>
a4ccfcbb
SS
538
539 <tr>
540 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
541 </tr>
542
543 <tr>
ac1cfefa
MT
544END
545;
1504a375 546
1286e0d4
SS
547# Loop through the array of available networks and print config options.
548foreach my $zone (@network_zones) {
549 my $checked_input;
550 my $checked_forward;
1504a375 551
1286e0d4
SS
552 # Convert current zone name to upper case.
553 my $zone_upper = uc($zone);
1504a375 554
1286e0d4 555 # Grab checkbox status from settings hash.
a4ccfcbb 556 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1286e0d4
SS
557 $checked_input = "checked = 'checked'";
558 }
1504a375 559
1286e0d4 560 print "<td class='base' width='25%'>\n";
a4ccfcbb 561 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
1286e0d4 562 print "</td>\n";
ac1cfefa 563}
1b73b07e 564
ac1cfefa 565print <<END
1504a375
SS
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'>
5a3e0dca 578 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 579 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
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>
1504a375
SS
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>
1286e0d4 595 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
1504a375
SS
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>
e0bfd338 610 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1504a375
SS
611 </tr>
612 </table>
ac1cfefa
MT
613</form>
614END
615;
616
ac1cfefa 617&Header::closebox();
fbfdb241 618
f7fcd1c0 619&Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9
SS
620 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
621
f7fcd1c0 622 # Output display table for rule files
17726644 623 print "<table width='100%'>\n";
ce0e83b3 624
17726644
SS
625 # Local variable required for java script to show/hide
626 # rules of a rulefile.
627 my $rulesetcount = 1;
f7fcd1c0
SS
628
629 # Loop over each rule file
9d18656b 630 foreach my $rulefile (sort keys(%idsrules)) {
f7fcd1c0
SS
631 my $rulechecked = '';
632
f7fcd1c0 633 # Check if rule file is enabled
9d18656b 634 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
f7fcd1c0
SS
635 $rulechecked = 'CHECKED';
636 }
3ffee04b 637
17726644
SS
638 # Table and rows for the rule files.
639 print"<tr>\n";
640 print"<td class='base' width='5%'>\n";
e5738079 641 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
17726644
SS
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
9d18656b 662 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
f7fcd1c0 663 # Local vars
17726644 664 my $ruledefchecked = '';
f9c2147d 665
e5738079
SS
666 # Skip rulefile itself.
667 next if ($sid eq "Rulefile");
668
17726644
SS
669 # If 2 rules have been displayed, start a new row
670 if (($lines % 2) == 0) {
671 print "</tr><tr>\n";
3ffee04b 672
17726644
SS
673 # Increase rows by once.
674 $rows++;
675 }
f7fcd1c0 676
17726644
SS
677 # Colour lines.
678 if ($rows % 2) {
679 $col="bgcolor='$color{'color20'}'";
680 } else {
681 $col="bgcolor='$color{'color22'}'";
f7fcd1c0
SS
682 }
683
17726644 684 # Set rule state
9d18656b 685 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
17726644 686 $ruledefchecked = 'CHECKED';
f7fcd1c0 687 }
3ffee04b 688
17726644
SS
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";
9d18656b 693 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
17726644
SS
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>";
f7fcd1c0
SS
702 }
703
704 # Close display table
17726644 705 print "</tr></table></td></tr>";
3ffee04b 706
17726644
SS
707 # Finished whith the rule file, increase count.
708 $rulesetcount++;
f7fcd1c0 709 }
17726644
SS
710
711 # Close display table
712 print "</table>";
713
f7fcd1c0 714print <<END
2999f1d2
CS
715<table width='100%'>
716<tr>
298723b9 717 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
718 &nbsp; <!-- space for future online help link -->
719 </td>
2999f1d2
CS
720</tr>
721</table>
298723b9 722</form>
3ffee04b
CS
723END
724;
f7fcd1c0 725&Header::closebox();
ac1cfefa
MT
726&Header::closebigbox();
727&Header::closepage();
728
27760092
SS
729#
730## A function to display a notice, to lock the webpage and
731## tell the user which action currently will be performed.
732#
733sub 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>
746END
747 &Header::closebox();
748 &Header::closebigbox();
749 &Header::closepage();
750}
751
3983aebd
SS
752#
753## A tiny function to perform a reload of the webpage after one second.
754#
755sub reload () {
756 print "<meta http-equiv='refresh' content='1'>\n";
757
758 # Stop the script.
759 exit;
a70d269a
SS
760}
761
25f5cb0d
SS
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,
9d18656b 766## message/description and it's state in the idsrules hash.
25f5cb0d 767#
3da6e01b
SS
768sub readrulesfile ($) {
769 my $rulefile = shift;
770
771 # Open rule file and read in contents
298ef5ba 772 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
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) {
9d18656b
SS
799 # Add rule to the idsrules hash.
800 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
801
802 # Grab status of the rule. Check if ruleline starts with a "dash".
803 if ($line =~ /^\#/) {
804 # If yes, the rule is disabled.
9d18656b 805 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
806 } else {
807 # Otherwise the rule is enabled.
9d18656b 808 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
809 }
810 }
811 }
812 }
813}
87660964 814
8d2f6b0b
SS
815#
816## Function to get the used memory of a given process-id.
817#
87660964
SS
818sub 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
8d2f6b0b
SS
846#
847## Function to generate the file which contains the home net information.
848#
849sub 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}
a5d61752
SS
926
927#
928## Function to read-in the given enabled or disables sids file.
929#
930sub 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}