]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Drop dirty hook for updating the ruleset
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/ids-functions.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings = ();
36 my %snortrules = ();
37 my %snortsettings=();
38 my %rulesetsources = ();
39 my %cgiparams=();
40 my %checked=();
41 my %selected=();
42
43 # Read-in main settings, for language, theme and colors.
44 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
45 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
46
47 # Get netsettings.
48 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
50 my $rulestarball = "/var/tmp/snortrules.tar.gz";
51 my $snortrulepath = "/etc/snort/rules";
52 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
53 my $errormessage;
54
55 &Header::showhttpheaders();
56
57 # Default settings for snort.
58 $snortsettings{'ENABLE_SNORT'} = 'off';
59 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
60 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
61 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
62 $snortsettings{'RULES'} = '';
63 $snortsettings{'OINKCODE'} = '';
64
65 #Get GUI values
66 &Header::getcgihash(\%cgiparams);
67
68 # Try to determine if oinkmaster is running.
69 my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
70
71 # If oinkmaster is running display output.
72 if ($oinkmaster_pid) {
73 &working("$Lang::tr{'snort working'}");
74 }
75
76 ## Grab all available snort rules and store them in the snortrules hash.
77 #
78 # Open snort rules directory and do a directory listing.
79 opendir(DIR, $snortrulepath) or die $!;
80 # Loop through the direcory.
81 while (my $file = readdir(DIR)) {
82
83 # We only want files.
84 next unless (-f "$snortrulepath/$file");
85
86 # Ignore empty files.
87 next if (-z "$snortrulepath/$file");
88
89 # Use a regular expression to find files ending in .rules
90 next unless ($file =~ m/\.rules$/);
91
92 # Ignore files which are not read-able.
93 next unless (-R "$snortrulepath/$file");
94
95 # Call subfunction to read-in rulefile and add rules to
96 # the snortrules hash.
97 &readrulesfile("$file");
98 }
99
100 closedir(DIR);
101
102 # Gather used rulefiles.
103 #
104 # Check if the file for activated rulefiles is not empty.
105 if(-f $snortusedrulefilesfile) {
106 # Open the file for used rulefile and read-in content.
107 open(FILE, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
108
109 # Read-in content.
110 my @lines = <FILE>;
111
112 # Close file.
113 close(FILE);
114
115 # Loop through the array.
116 foreach my $line (@lines) {
117 # Remove newlines.
118 chomp($line);
119
120 # Skip comments.
121 next if ($line =~ /\#/);
122
123 # Skip blank lines.
124 next if ($line =~ /^\s*$/);
125
126 # Gather rule sid and message from the ruleline.
127 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
128 my $rulefile = $1;
129
130 # Add the rulefile to the %snortrules hash.
131 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
132 }
133 }
134 }
135
136 # Save ruleset.
137 if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
138 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
139 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
140
141 # Arrays to store sid which should be added to the corresponding files.
142 my @enabled_sids;
143 my @disabled_sids;
144 my @enabled_rulefiles;
145
146 # Loop through the hash of snortrules.
147 foreach my $rulefile(keys %snortrules) {
148 # Check if the rulefile is enabled.
149 if ($cgiparams{$rulefile} eq "on") {
150 # Add rulefile to the array of enabled rulefiles.
151 push(@enabled_rulefiles, $rulefile);
152
153 # Drop item from cgiparams hash.
154 delete $cgiparams{$rulefile};
155 }
156 }
157
158 # Loop through the hash of snortrules.
159 foreach my $rulefile (keys %snortrules) {
160 # Loop through the single rules of the rulefile.
161 foreach my $sid (keys %{$snortrules{$rulefile}}) {
162 # Skip the current sid if it is not numeric.
163 next unless ($sid =~ /\d+/ );
164
165 # Check if there exists a key in the cgiparams hash for this sid.
166 if (exists($cgiparams{$sid})) {
167 # Look if the rule is disabled.
168 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
169 # Check if the state has been set to 'on'.
170 if ($cgiparams{$sid} eq "on") {
171 # Add the sid to the enabled_sids array.
172 push(@enabled_sids, $sid);
173
174 # Drop item from cgiparams hash.
175 delete $cgiparams{$rulefile}{$sid};
176 }
177 }
178 } else {
179 # Look if the rule is enabled.
180 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
181 # Check if the state is 'on' and should be disabled.
182 # In this case there is no entry
183 # for the sid in the cgiparams hash.
184 # Add it to the disabled_sids array.
185 push(@disabled_sids, $sid);
186
187 # Drop item from cgiparams hash.
188 delete $cgiparams{$rulefile}{$sid};
189 }
190 }
191 }
192 }
193
194 # Open enabled sid's file for writing.
195 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
196
197 # Write header to file.
198 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
199
200 # Check if the enabled_sids array contains any sid's.
201 if (@enabled_sids) {
202 # Loop through the array of enabled sids and write them to the file.
203 foreach my $sid (@enabled_sids) {
204 print FILE "enablesid $sid\n";
205 }
206 }
207
208 # Close file after writing.
209 close(FILE);
210
211 # Open disabled sid's file for writing.
212 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
213
214 # Write header to file.
215 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
216
217 # Check if the enabled_sids array contains any sid's.
218 if (@disabled_sids) {
219 # Loop through the array of disabled sids and write them to the file.
220 foreach my $sid (@disabled_sids) {
221 print FILE "disablesid $sid\n";
222 }
223 }
224
225 # Close file after writing.
226 close(FILE);
227
228 # Open file for used rulefiles.
229 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
230
231 # Write header to file.
232 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
233
234 # Check if the enabled_rulefiles array contains any entries.
235 if (@enabled_rulefiles) {
236 # Loop through the array of rulefiles which should be loaded and write the to the file.
237 foreach my $file (@enabled_rulefiles) {
238 print FILE "include \$RULE_PATH/$file\n";
239 }
240 }
241
242 # Close file after writing.
243 close(FILE);
244
245 # Call oinkmaster to alter the ruleset.
246 &oinkmaster();
247
248 # Download new ruleset.
249 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
250 # Check if the red device is active.
251 unless (-e "${General::swroot}/red/active") {
252 $errormessage = $Lang::tr{'could not download latest updates'};
253 }
254
255 # Check if there is enought free disk space available.
256 $errormessage = &IDS::checkdiskspace();
257
258 # Check if any errors happend.
259 unless ($errormessage) {
260 # Call subfunction to download the ruleset.
261 $errormessage = &IDS::downloadruleset();
262 }
263
264 # Sleep for 1 second
265 sleep(1);
266
267 # Check if the downloader returend any error message.
268 unless ($errormessage) {
269 # Call subfunction to launch oinkmaster.
270 &oinkmaster();
271
272 # Sleep for 1 seconds.
273 sleep(1);
274 }
275 # Save snort settings.
276 } elsif ($cgiparams{'SNORT'} eq $Lang::tr{'save'}) {
277 # Prevent form name from been stored in conf file.
278 delete $cgiparams{'SNORT'};
279
280 # Check if an oinkcode has been provided.
281 if ($cgiparams{'OINKCODE'}) {
282 # Check if the oinkcode contains unallowed chars.
283 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
284 $errormessage = $Lang::tr{'invalid input for oink code'};
285 }
286 }
287
288 # Go on if there are no error messages.
289 if (!$errormessage) {
290 # Store settings into settings file.
291 &General::writehash("${General::swroot}/snort/settings", \%cgiparams);
292
293 # Call snortctrl to restart snort
294 system('/usr/local/bin/snortctrl restart >/dev/null');
295 }
296 }
297
298 # Read-in snortsettings
299 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
300
301 $checked{'ENABLE_SNORT'}{'off'} = '';
302 $checked{'ENABLE_SNORT'}{'on'} = '';
303 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
304 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
305 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
306 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
307 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
308 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
309 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
310 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
311 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
312 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
313 $selected{'RULES'}{'nothing'} = '';
314 $selected{'RULES'}{'community'} = '';
315 $selected{'RULES'}{'emerging'} = '';
316 $selected{'RULES'}{'registered'} = '';
317 $selected{'RULES'}{'subscripted'} = '';
318 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
319
320 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
321
322 ### Java Script ###
323 print <<END
324 <script>
325 // Tiny java script function to show/hide the rules
326 // of a given category.
327 function showhide(tblname) {
328 \$("#" + tblname).toggle();
329 }
330 </script>
331 END
332 ;
333
334 &Header::openbigbox('100%', 'left', '', $errormessage);
335
336 if ($errormessage) {
337 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
338 print "<class name='base'>$errormessage\n";
339 print "&nbsp;</class>\n";
340 &Header::closebox();
341 }
342
343 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
344
345 my $rulesdate;
346
347 # Check if a ruleset allready has been downloaded.
348 if ( -f "$rulestarball"){
349 # Call stat on the filename to obtain detailed information.
350 my @Info = stat("$rulestarball");
351
352 # Grab details about the creation time.
353 $rulesdate = localtime($Info[9]);
354 }
355
356 print <<END
357 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
358 <table width='100%' border='0'>
359 <tr>
360 <td class='base' width='25%'>
361 <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'}>RED Snort
362 </td>
363
364 <td class='base' width='25%'>
365 <input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'}>GREEN Snort
366 </td>
367
368 <td class='base' width='25%'>
369 END
370 ;
371
372 # Check if a blue device is configured.
373 if ($netsettings{'BLUE_DEV'}) {
374 print "<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} />BLUE Snort\n";
375 }
376
377 print "</td>\n";
378
379 print "<td width='25%'>\n";
380
381 # Check if an orange device is configured.
382 if ($netsettings{'ORANGE_DEV'}) {
383 print "<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} />ORANGE Snort\n";
384 }
385
386 print <<END
387 </td>
388 </tr>
389
390 <tr>
391 <td colspan='4'><br><br></td>
392 </tr>
393
394 <tr>
395 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
396 </tr>
397
398 <tr>
399 <td colspan='4'><select name='RULES'>
400 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
401 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
402 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
403 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
404 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
405 </select>
406 </td>
407 </tr>
408
409 <tr>
410 <td colspan='4'>
411 <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>
412 <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>
413 </td>
414 </tr>
415
416 <tr>
417 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}'></td>
418 </tr>
419
420 <tr>
421 <td colspan='4' align='left'><br>
422 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
423 </td>
424
425 </tr>
426 </table>
427
428 <br><br>
429
430 <table width='100%'>
431 <tr>
432 <td align='right'><input type='submit' name='SNORT' value='$Lang::tr{'save'}' /></td>
433 </tr>
434 </table>
435 </form>
436 END
437 ;
438
439 &Header::closebox();
440
441 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
442 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
443
444 # Output display table for rule files
445 print "<table width='100%'>\n";
446
447 # Local variable required for java script to show/hide
448 # rules of a rulefile.
449 my $rulesetcount = 1;
450
451 # Loop over each rule file
452 foreach my $rulefile (sort keys(%snortrules)) {
453 my $rulechecked = '';
454
455 # Check if rule file is enabled
456 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
457 $rulechecked = 'CHECKED';
458 }
459
460 # Table and rows for the rule files.
461 print"<tr>\n";
462 print"<td class='base' width='5%'>\n";
463 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
464 print"</td>\n";
465 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
466 print"<td class='base' width='5%' align='right'>\n";
467 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
468 print"</td>\n";
469 print"</tr>\n";
470
471 # Rows which will be hidden per default and will contain the single rules.
472 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
473 print"<td colspan='3'>\n";
474
475 # Local vars
476 my $lines;
477 my $rows;
478 my $col;
479
480 # New table for the single rules.
481 print "<table width='100%'>\n";
482
483 # Loop over rule file rules
484 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
485 # Local vars
486 my $ruledefchecked = '';
487
488 # Skip rulefile itself.
489 next if ($sid eq "Rulefile");
490
491 # If 2 rules have been displayed, start a new row
492 if (($lines % 2) == 0) {
493 print "</tr><tr>\n";
494
495 # Increase rows by once.
496 $rows++;
497 }
498
499 # Colour lines.
500 if ($rows % 2) {
501 $col="bgcolor='$color{'color20'}'";
502 } else {
503 $col="bgcolor='$color{'color22'}'";
504 }
505
506 # Set rule state
507 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
508 $ruledefchecked = 'CHECKED';
509 }
510
511 # Create rule checkbox and display rule description
512 print "<td class='base' width='5%' align='right' $col>\n";
513 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
514 print "</td>\n";
515 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
516
517 # Increment rule count
518 $lines++;
519 }
520
521 # If do not have a second rule for row, create empty cell
522 if (($lines % 2) != 0) {
523 print "<td class='base'></td>";
524 }
525
526 # Close display table
527 print "</tr></table></td></tr>";
528
529 # Finished whith the rule file, increase count.
530 $rulesetcount++;
531 }
532
533 # Close display table
534 print "</table>";
535
536 print <<END
537 <table width='100%'>
538 <tr>
539 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
540 &nbsp; <!-- space for future online help link -->
541 </td>
542 </tr>
543 </table>
544 </form>
545 END
546 ;
547 &Header::closebox();
548 &Header::closebigbox();
549 &Header::closepage();
550
551 sub working ($) {
552 my $message = $_[0];
553
554 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
555 &Header::openbigbox('100%', 'left', '', $errormessage);
556 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1'>" );
557 print <<END;
558 <table>
559 <tr>
560 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
561 <td>$message</td>
562 </tr>
563 <tr>
564 <td colspan='2' align='center'>
565 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
566 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
567 </form>
568 </tr>
569 </table>
570 END
571 &Header::closebox();
572 &Header::closebigbox();
573 &Header::closepage();
574 exit;
575 }
576
577 #
578 ## Private function to read-in and parse rules of a given rulefile.
579 #
580 ## The given file will be read, parsed and all valid rules will be stored by ID,
581 ## message/description and it's state in the snortrules hash.
582 #
583 sub readrulesfile ($) {
584 my $rulefile = shift;
585
586 # Open rule file and read in contents
587 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
588
589 # Store file content in an array.
590 my @lines = <RULEFILE>;
591
592 # Close file.
593 close(RULEFILE);
594
595 # Loop over rule file contents
596 foreach my $line (@lines) {
597 # Remove whitespaces.
598 chomp $line;
599
600 # Skip blank lines.
601 next if ($line =~ /^\s*$/);
602
603 # Local vars.
604 my $sid;
605 my $msg;
606
607 # Gather rule sid and message from the ruleline.
608 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
609 $msg = $1;
610 $sid = $2;
611
612 # Check if a rule has been found.
613 if ($sid && $msg) {
614 # Add rule to the snortrules hash.
615 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
616
617 # Grab status of the rule. Check if ruleline starts with a "dash".
618 if ($line =~ /^\#/) {
619 # If yes, the rule is disabled.
620 $snortrules{$rulefile}{$sid}{'State'} = "off";
621 } else {
622 # Otherwise the rule is enabled.
623 $snortrules{$rulefile}{$sid}{'State'} = "on";
624 }
625 }
626 }
627 }
628 }