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