]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Drop unused css code
[people/pmueller/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 &Header::openbigbox('100%', 'left', '', $errormessage);
220
221 ###############
222 # DEBUG DEBUG
223 # &Header::openbox('100%', 'left', 'DEBUG');
224 # my $debugCount = 0;
225 # foreach my $line (sort keys %snortsettings) {
226 # print "$line = $snortsettings{$line}<br />\n";
227 # $debugCount++;
228 # }
229 # print "&nbsp;Count: $debugCount\n";
230 # &Header::closebox();
231 # DEBUG DEBUG
232 ###############
233
234 if ($errormessage) {
235 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
236 print "<class name='base'>$errormessage\n";
237 print "&nbsp;</class>\n";
238 &Header::closebox();
239 }
240
241 my $return = `pidof oinkmaster.pl -x`;
242 chomp($return);
243 if ($return) {
244 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
245 print <<END;
246 <table>
247 <tr><td>
248 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
249 <td>
250 $Lang::tr{'snort working'}
251 <tr><td colspan='2' align='center'>
252 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
253 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
254 </form>
255 <tr><td colspan='2' align='left'><pre>
256 END
257 my @output = `tail -20 /var/tmp/log`;
258 foreach (@output) {
259 print "$_";
260 }
261 print <<END;
262 </pre>
263 </table>
264 END
265 &Header::closebox();
266 &Header::closebigbox();
267 &Header::closepage();
268 exit;
269 refreshpage();
270 }
271
272 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
273 print <<END
274 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
275 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
276 END
277 ;
278 if ($netsettings{'BLUE_DEV'} ne '') {
279 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
280 }
281 if ($netsettings{'ORANGE_DEV'} ne '') {
282 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
283 }
284 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
285
286 print <<END
287 </td></tr>
288 <tr>
289 <td><br><br></td>
290 </tr>
291 <tr>
292 <td><b>$Lang::tr{'ids rules update'}</b></td>
293 </tr>
294 <tr>
295 <td><select name='RULES'>
296 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
297 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
298 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
299 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
300 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
301 </select>
302 </td>
303 </tr>
304 <tr>
305 <td><br />
306 $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 />
307 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
308 </td>
309 </tr>
310 <tr>
311 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
312 </tr>
313 <tr>
314 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
315 END
316 ;
317 if ( -e "/var/tmp/snortrules.tar.gz"){
318 my @Info = stat("/var/tmp/snortrules.tar.gz");
319 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
320 }
321 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
322
323 print <<END
324 </tr>
325 </table>
326 <br><br>
327 <table width='100%'>
328 <tr>
329 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
330 </tr>
331 </table>
332 </form>
333 END
334 ;
335
336 if ($results ne '') {
337 print "$results";
338 }
339
340 &Header::closebox();
341
342 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
343 # Output display table for rule files
344 print "<table width='100%'>\n";
345
346 # Local variable required for java script to show/hide
347 # rules of a rulefile.
348 my $rulesetcount = 1;
349
350 # Loop over each rule file
351 foreach my $rulefile (sort keys(%snortrules)) {
352 my $rulechecked = '';
353
354 # Check if rule file is enabled
355 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
356 $rulechecked = 'CHECKED';
357 }
358
359 # Table and rows for the rule files.
360 print"<tr>\n";
361 print"<td class='base' width='5%'>\n";
362 print"<input type='checkbox' name='SNORT_RULE_$rulefile' $rulechecked>\n";
363 print"</td>\n";
364 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
365 print"<td class='base' width='5%' align='right'>\n";
366 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
367 print"</td>\n";
368 print"</tr>\n";
369
370 # Rows which will be hidden per default and will contain the single rules.
371 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
372 print"<td colspan='3'>\n";
373
374 # Local vars
375 my $lines;
376 my $rows;
377 my $col;
378
379 # New table for the single rules.
380 print "<table width='100%'>\n";
381
382 # Loop over rule file rules
383 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
384 # Local vars
385 my $ruledefchecked = '';
386
387 # If 2 rules have been displayed, start a new row
388 if (($lines % 2) == 0) {
389 print "</tr><tr>\n";
390
391 # Increase rows by once.
392 $rows++;
393 }
394
395 # Colour lines.
396 if ($rows % 2) {
397 $col="bgcolor='$color{'color20'}'";
398 } else {
399 $col="bgcolor='$color{'color22'}'";
400 }
401
402 # Set rule state
403 if ($snortrules{$rulefile}{$sid}{'State'} eq 'Enabled') {
404 $ruledefchecked = 'CHECKED';
405 }
406
407 # Create rule checkbox and display rule description
408 print "<td class='base' width='5%' align='right' $col>\n";
409 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
410 print "</td>\n";
411 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
412
413 # Increment rule count
414 $lines++;
415 }
416
417 # If do not have a second rule for row, create empty cell
418 if (($lines % 2) != 0) {
419 print "<td class='base'></td>";
420 }
421
422 # Close display table
423 print "</tr></table></td></tr>";
424
425 # Finished whith the rule file, increase count.
426 $rulesetcount++;
427 }
428
429 # Close display table
430 print "</table>";
431
432 print <<END
433 <table width='100%'>
434 <tr>
435 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}'>
436 &nbsp; <!-- space for future online help link -->
437 </td>
438 </tr>
439 </table>
440 END
441 ;
442 &Header::closebox();
443
444 &Header::closebigbox();
445 &Header::closepage();
446
447 sub refreshpage {
448 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );
449 print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";
450 &Header::closebox();
451 }
452
453 sub downloadrulesfile {
454 my $peer;
455 my $peerport;
456
457 unlink("/var/tmp/log");
458
459 unless (-e "${General::swroot}/red/active") {
460 $errormessage = $Lang::tr{'could not download latest updates'};
461 return undef;
462 }
463
464 my %proxysettings=();
465 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
466
467 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
468 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
469 }
470
471 if ($peer) {
472 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");
473 } else {
474 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
475 }
476 }
477
478 sub readrulesfile ($) {
479 my $rulefile = shift;
480
481 # Open rule file and read in contents
482 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
483
484 # Store file content in an array.
485 my @lines = <RULEFILE>;
486
487 # Close file.
488 close(RULEFILE);
489
490 # Loop over rule file contents
491 foreach my $line (@lines) {
492 # Remove whitespaces.
493 chomp $line;
494
495 # Skip blank lines.
496 next if ($line =~ /^\s*$/);
497
498 # Local vars.
499 my $sid;
500 my $msg;
501
502 # Gather rule sid and message from the ruleline.
503 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
504 $msg = $1;
505 $sid = $2;
506
507 # Check if a rule has been found.
508 if ($sid && $msg) {
509 # Add rule to the snortrules hash.
510 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
511
512 # Grab status of the rule. Check if ruleline starts with a "dash".
513 if ($line =~ /^\#/) {
514 # If yes, the rule is disabled.
515 $snortrules{$rulefile}{$sid}{'State'} = "Disabled";
516 } else {
517 # Otherwise the rule is enabled.
518 $snortrules{$rulefile}{$sid}{'State'} = "Enabled";
519 }
520 }
521 }
522 }
523 }