]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Rework code for displaying the single rules
[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 use File::Copy;
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
36 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
37
38 my %snortsettings=();
39 my %checked=();
40 my %selected=();
41 my %netsettings=();
42 our $errormessage = '';
43 our $results = '';
44 our $tempdir = '';
45 our $url='';
46 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
47
48 &Header::showhttpheaders();
49
50 $snortsettings{'ENABLE_SNORT'} = 'off';
51 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
52 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
53 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
54 $snortsettings{'ACTION'} = '';
55 $snortsettings{'RULES'} = '';
56 $snortsettings{'OINKCODE'} = '';
57 $snortsettings{'INSTALLDATE'} = '';
58 $snortsettings{'FILE'} = '';
59
60 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
61
62 my $snortrulepath = "/etc/snort/rules";
63 my $restartsnortrequired = 0;
64 my %snortrules;
65 my $rule = '';
66 my $table1colour = '';
67 my $table2colour = '';
68 my $var = '';
69 my $value = '';
70 my $tmp = '';
71 my $linkedrulefile = '';
72 my $border = '';
73 my $checkboxname = '';
74
75 ## Grab all available snort rules and store them in the snortrules hash.
76 #
77 # Open snort rules directory and do a directory listing.
78 opendir(DIR, $snortrulepath) or die $!;
79 # Loop through the direcory.
80 while (my $file = readdir(DIR)) {
81
82 # We only want files.
83 next unless (-f "$snortrulepath/$file");
84
85 # Ignore empty files.
86 next if (-z "$snortrulepath/$file");
87
88 # Use a regular expression to find files ending in .rules
89 next unless ($file =~ m/\.rules$/);
90
91 # Ignore files which are not read-able.
92 next unless (-R "$snortrulepath/$file");
93
94 # Call subfunction to read-in rulefile and add rules to
95 # the snortrules hash.
96 &readrulesfile("$file");
97 }
98
99 closedir(DIR);
100
101 if ($snortsettings{'OINKCODE'} ne "") {
102 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
103 }
104
105 if (!$errormessage) {
106 if ($snortsettings{'RULES'} eq 'subscripted') {
107 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
108 } elsif ($snortsettings{'RULES'} eq 'registered') {
109 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
110 } elsif ($snortsettings{'RULES'} eq 'community') {
111 $url=" https://www.snort.org/rules/community";
112 } else {
113 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
114 }
115
116 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
117 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
118 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
119 {
120 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
121 } else {
122 unlink "${General::swroot}/snort/enable";
123 }
124 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
125 {
126 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
127 } else {
128 unlink "${General::swroot}/snort/enable_green";
129 }
130 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
131 {
132 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
133 } else {
134 unlink "${General::swroot}/snort/enable_blue";
135 }
136 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
137 {
138 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
139 } else {
140 unlink "${General::swroot}/snort/enable_orange";
141 }
142 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
143 {
144 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
145 } else {
146 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
147 }
148
149 system('/usr/local/bin/snortctrl restart >/dev/null');
150 }
151
152 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
153 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
154
155 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
156 my @df = `/bin/df -B M /var`;
157 foreach my $line (@df) {
158 next if $line =~ m/^Filesystem/;
159 my $return;
160
161 if ($line =~ m/dev/ ) {
162 $line =~ m/^.* (\d+)M.*$/;
163 my @temp = split(/ +/,$line);
164 if ($1<300) {
165 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
166 } else {
167 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
168 &downloadrulesfile();
169 sleep(3);
170 $return = `cat /var/tmp/log 2>/dev/null`;
171
172 }
173
174 if ($return =~ "ERROR") {
175 $errormessage = "<br /><pre>".$return."</pre>";
176 } else {
177 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
178 sleep(2);
179 }
180 }
181 }
182 }
183 }
184 }
185
186 $checked{'ENABLE_SNORT'}{'off'} = '';
187 $checked{'ENABLE_SNORT'}{'on'} = '';
188 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
189 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
190 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
191 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
192 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
193 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
194 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
195 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
196 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
197 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
198 $selected{'RULES'}{'nothing'} = '';
199 $selected{'RULES'}{'community'} = '';
200 $selected{'RULES'}{'emerging'} = '';
201 $selected{'RULES'}{'registered'} = '';
202 $selected{'RULES'}{'subscripted'} = '';
203 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
204
205 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
206
207 ### Java Script ###
208 print <<END
209 <script>
210 // Tiny java script function to show/hide the rules
211 // of a given category.
212 function showhide(tblname) {
213 \$("#" + tblname).toggle();
214 }
215 </script>
216 END
217 ;
218
219 print <<END
220 <style type="text/css">
221 <!--
222 .section {
223 border: groove;
224 }
225 .row1color {
226 border: ridge;
227 background-color: $color{'color22'};
228 }
229 .row2color {
230 border: ridge;
231 background-color: $color{'color20'};
232 }
233 .rowselected {
234 border: double #FF0000;
235 background-color: #DCDCDC;
236 }
237 -->
238 </style>
239 END
240 ;
241
242 &Header::openbigbox('100%', 'left', '', $errormessage);
243
244 ###############
245 # DEBUG DEBUG
246 # &Header::openbox('100%', 'left', 'DEBUG');
247 # my $debugCount = 0;
248 # foreach my $line (sort keys %snortsettings) {
249 # print "$line = $snortsettings{$line}<br />\n";
250 # $debugCount++;
251 # }
252 # print "&nbsp;Count: $debugCount\n";
253 # &Header::closebox();
254 # DEBUG DEBUG
255 ###############
256
257 if ($errormessage) {
258 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
259 print "<class name='base'>$errormessage\n";
260 print "&nbsp;</class>\n";
261 &Header::closebox();
262 }
263
264 my $return = `pidof oinkmaster.pl -x`;
265 chomp($return);
266 if ($return) {
267 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
268 print <<END;
269 <table>
270 <tr><td>
271 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
272 <td>
273 $Lang::tr{'snort working'}
274 <tr><td colspan='2' align='center'>
275 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
276 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
277 </form>
278 <tr><td colspan='2' align='left'><pre>
279 END
280 my @output = `tail -20 /var/tmp/log`;
281 foreach (@output) {
282 print "$_";
283 }
284 print <<END;
285 </pre>
286 </table>
287 END
288 &Header::closebox();
289 &Header::closebigbox();
290 &Header::closepage();
291 exit;
292 refreshpage();
293 }
294
295 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
296 print <<END
297 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
298 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
299 END
300 ;
301 if ($netsettings{'BLUE_DEV'} ne '') {
302 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
303 }
304 if ($netsettings{'ORANGE_DEV'} ne '') {
305 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
306 }
307 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
308
309 print <<END
310 </td></tr>
311 <tr>
312 <td><br><br></td>
313 </tr>
314 <tr>
315 <td><b>$Lang::tr{'ids rules update'}</b></td>
316 </tr>
317 <tr>
318 <td><select name='RULES'>
319 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
320 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
321 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
322 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
323 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
324 </select>
325 </td>
326 </tr>
327 <tr>
328 <td><br />
329 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
330 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
331 </td>
332 </tr>
333 <tr>
334 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
335 </tr>
336 <tr>
337 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
338 END
339 ;
340 if ( -e "/var/tmp/snortrules.tar.gz"){
341 my @Info = stat("/var/tmp/snortrules.tar.gz");
342 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
343 }
344 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
345
346 print <<END
347 </tr>
348 </table>
349 <br><br>
350 <table width='100%'>
351 <tr>
352 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
353 </tr>
354 </table>
355 </form>
356 END
357 ;
358
359 if ($results ne '') {
360 print "$results";
361 }
362
363 &Header::closebox();
364
365 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
366 # Output display table for rule files
367 print "<table width='100%'>\n";
368
369 # Local variable required for java script to show/hide
370 # rules of a rulefile.
371 my $rulesetcount = 1;
372
373 # Loop over each rule file
374 foreach my $rulefile (sort keys(%snortrules)) {
375 my $rulechecked = '';
376
377 # Check if rule file is enabled
378 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
379 $rulechecked = 'CHECKED';
380 }
381
382 # Table and rows for the rule files.
383 print"<tr>\n";
384 print"<td class='base' width='5%'>\n";
385 print"<input type='checkbox' name='SNORT_RULE_$rulefile' $rulechecked>\n";
386 print"</td>\n";
387 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
388 print"<td class='base' width='5%' align='right'>\n";
389 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
390 print"</td>\n";
391 print"</tr>\n";
392
393 # Rows which will be hidden per default and will contain the single rules.
394 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
395 print"<td colspan='3'>\n";
396
397 # Local vars
398 my $lines;
399 my $rows;
400 my $col;
401
402 # New table for the single rules.
403 print "<table width='100%'>\n";
404
405 # Loop over rule file rules
406 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
407 # Local vars
408 my $ruledefchecked = '';
409
410 # If 2 rules have been displayed, start a new row
411 if (($lines % 2) == 0) {
412 print "</tr><tr>\n";
413
414 # Increase rows by once.
415 $rows++;
416 }
417
418 # Colour lines.
419 if ($rows % 2) {
420 $col="bgcolor='$color{'color20'}'";
421 } else {
422 $col="bgcolor='$color{'color22'}'";
423 }
424
425 # Set rule state
426 if ($snortrules{$rulefile}{$sid}{'State'} eq 'Enabled') {
427 $ruledefchecked = 'CHECKED';
428 }
429
430 # Create rule checkbox and display rule description
431 print "<td class='base' width='5%' align='right' $col>\n";
432 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
433 print "</td>\n";
434 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
435
436 # Increment rule count
437 $lines++;
438 }
439
440 # If do not have a second rule for row, create empty cell
441 if (($lines % 2) != 0) {
442 print "<td class='base'></td>";
443 }
444
445 # Close display table
446 print "</tr></table></td></tr>";
447
448 # Finished whith the rule file, increase count.
449 $rulesetcount++;
450 }
451
452 # Close display table
453 print "</table>";
454
455 print <<END
456 <table width='100%'>
457 <tr>
458 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}'>
459 &nbsp; <!-- space for future online help link -->
460 </td>
461 </tr>
462 </table>
463 END
464 ;
465 &Header::closebox();
466
467 &Header::closebigbox();
468 &Header::closepage();
469
470 sub refreshpage {
471 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
472 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
473 &Header::closebox();
474 }
475
476 sub downloadrulesfile {
477 my $peer;
478 my $peerport;
479
480 unlink("/var/tmp/log");
481
482 unless (-e "${General::swroot}/red/active") {
483 $errormessage = $Lang::tr{'could not download latest updates'};
484 return undef;
485 }
486
487 my %proxysettings=();
488 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
489
490 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
491 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
492 }
493
494 if ($peer) {
495 system("wget -r --proxy=on --proxy-user=$proxysettings{'UPSTREAM_USER'} --proxy-passwd=$proxysettings{'UPSTREAM_PASSWORD'} -e http_proxy=http://$peer:$peerport/ -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
496 } else {
497 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
498 }
499 }
500
501 sub readrulesfile ($) {
502 my $rulefile = shift;
503
504 # Open rule file and read in contents
505 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
506
507 # Store file content in an array.
508 my @lines = <RULEFILE>;
509
510 # Close file.
511 close(RULEFILE);
512
513 # Loop over rule file contents
514 foreach my $line (@lines) {
515 # Remove whitespaces.
516 chomp $line;
517
518 # Skip blank lines.
519 next if ($line =~ /^\s*$/);
520
521 # Local vars.
522 my $sid;
523 my $msg;
524
525 # Gather rule sid and message from the ruleline.
526 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
527 $msg = $1;
528 $sid = $2;
529
530 # Check if a rule has been found.
531 if ($sid && $msg) {
532 # Add rule to the snortrules hash.
533 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
534
535 # Grab status of the rule. Check if ruleline starts with a "dash".
536 if ($line =~ /^\#/) {
537 # If yes, the rule is disabled.
538 $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
539 } else {
540 # Otherwise the rule is enabled.
541 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";
542 }
543 }
544 }
545 }
546 }