]>
Commit | Line | Data |
---|---|---|
ac1cfefa | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2007 Michael Tremer & Christian Schmidt # | |
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 |
22 | |
23 | use LWP::UserAgent; | |
24 | use File::Copy; | |
25 | use File::Temp qw/ tempfile tempdir /; | |
26 | use strict; | |
27 | ||
28 | # enable only the following on debugging purpose | |
cb5e9c6c CS |
29 | #use warnings; |
30 | #use CGI::Carp 'fatalsToBrowser'; | |
ac1cfefa | 31 | |
986e08d9 | 32 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
33 | require "${General::swroot}/lang.pl"; |
34 | require "${General::swroot}/header.pl"; | |
35 | ||
f2fdd0c1 CS |
36 | my %color = (); |
37 | my %mainsettings = (); | |
38 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
39 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
40 | ||
ac1cfefa MT |
41 | my %snortsettings=(); |
42 | my %checked=(); | |
5a3e0dca | 43 | my %selected=(); |
ac1cfefa MT |
44 | my %netsettings=(); |
45 | our $errormessage = ''; | |
46 | our $md5 = '0';# not '' to avoid displaying the wrong message when INSTALLMD5 not set | |
47 | our $realmd5 = ''; | |
48 | our $results = ''; | |
49 | our $tempdir = ''; | |
50 | our $url=''; | |
51 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); | |
52 | ||
53 | &Header::showhttpheaders(); | |
54 | ||
55 | $snortsettings{'ENABLE_SNORT'} = 'off'; | |
56 | $snortsettings{'ENABLE_SNORT_GREEN'} = 'off'; | |
57 | $snortsettings{'ENABLE_SNORT_BLUE'} = 'off'; | |
58 | $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off'; | |
1b73b07e | 59 | $snortsettings{'ENABLE_GUARDIAN'} = 'off'; |
fbfdb241 CS |
60 | $snortsettings{'GUARDIAN_INTERFACE'} = `cat /var/ipfire/red/iface`; |
61 | $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'} = '1'; | |
62 | $snortsettings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log'; | |
63 | $snortsettings{'GUARDIAN_ALERTFILE'} = '/var/log/snort/alert'; | |
64 | $snortsettings{'GUARDIAN_IGNOREFILE'} = '/var/ipfire/guardian/guardian.ignore'; | |
65 | $snortsettings{'GUARDIAN_TARGETFILE'} = '/var/ipfire/guardian/guardian.target'; | |
66 | $snortsettings{'GUARDIAN_TIMELIMIT'} = '86400'; | |
ac1cfefa | 67 | $snortsettings{'ACTION'} = ''; |
fbfdb241 | 68 | $snortsettings{'ACTION2'} = ''; |
5a3e0dca | 69 | $snortsettings{'RULES'} = ''; |
ac1cfefa MT |
70 | $snortsettings{'OINKCODE'} = ''; |
71 | $snortsettings{'INSTALLDATE'} = ''; | |
72 | $snortsettings{'INSTALLMD5'} = ''; | |
73 | ||
74 | &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'}); | |
75 | ||
395e3b90 | 76 | ####################### Added for snort rules control ################################# |
cf29614f | 77 | my $snortrulepath; # change to "/etc/snort/rules" - maniac |
395e3b90 MT |
78 | my @snortconfig; |
79 | my $restartsnortrequired = 0; | |
80 | my %snortrules; | |
81 | my $rule = ''; | |
82 | my $table1colour = ''; | |
83 | my $table2colour = ''; | |
84 | my $var = ''; | |
85 | my $value = ''; | |
86 | my $tmp = ''; | |
87 | my $linkedrulefile = ''; | |
88 | my $border = ''; | |
89 | my $checkboxname = ''; | |
90 | ||
91 | if (-e "/etc/snort/snort.conf") { | |
2999f1d2 CS |
92 | |
93 | ||
395e3b90 MT |
94 | # Open snort.conf file, read it in, close it, and re-open for writing |
95 | open(FILE, "/etc/snort/snort.conf") or die 'Unable to read snort config file.'; | |
96 | @snortconfig = <FILE>; | |
97 | close(FILE); | |
d192af92 | 98 | open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.'; |
395e3b90 | 99 | |
2999f1d2 CS |
100 | my @rules = `cd /etc/snort/rules/ && ls *.rules`; # With this loop the rule might be display with correct rulepath set |
101 | foreach (@rules) { | |
102 | chomp $_; | |
103 | my $temp = join(";",@snortconfig); | |
104 | if ( $temp =~ /$_/ ){next;} | |
105 | else { push(@snortconfig,"#include \$RULE_PATH/".$_);} | |
106 | } | |
f9c2147d | 107 | |
395e3b90 MT |
108 | # Loop over each line |
109 | foreach my $line (@snortconfig) { | |
429f7008 | 110 | # Trim the line |
395e3b90 MT |
111 | chomp $line; |
112 | ||
113 | # Check for a line with .rules | |
114 | if ($line =~ /\.rules$/) { | |
115 | # Parse out rule file name | |
116 | $rule = $line; | |
117 | $rule =~ s/\$RULE_PATH\///i; | |
118 | $rule =~ s/ ?include ?//i; | |
119 | $rule =~ s/\#//i; | |
120 | my $snortrulepathrule = "$snortrulepath/$rule"; | |
121 | ||
122 | # Open rule file and read in contents | |
123 | open(RULEFILE, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule."; | |
124 | my @snortrulefile = <RULEFILE>; | |
125 | close(RULEFILE); | |
126 | open(RULEFILE, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule"; | |
127 | ||
128 | # Local vars | |
129 | my $dashlinecnt = 0; | |
130 | my $desclook = 1; | |
131 | my $snortruledesc = ''; | |
132 | my %snortruledef = (); | |
133 | my $rulecnt = 1; | |
134 | ||
135 | # Loop over rule file contents | |
136 | foreach my $ruleline (@snortrulefile) { | |
137 | chomp $ruleline; | |
138 | ||
139 | # If still looking for a description | |
140 | if ($desclook) { | |
141 | # If line does not start with a # anymore, then done looking for a description | |
142 | if ($ruleline !~ /^\#/) { | |
143 | $desclook = 0; | |
144 | } | |
145 | ||
146 | # If see more than one dashed line, (start to) create rule file description | |
147 | if ($dashlinecnt > 1) { | |
148 | # Check for a line starting with a # | |
149 | if ($ruleline =~ /^\#/) { | |
150 | # Create tempruleline | |
151 | my $tempruleline = $ruleline; | |
152 | ||
153 | # Strip off # and clean up line | |
154 | $tempruleline =~ s/\# ?//i; | |
155 | ||
156 | # Check for part of a description | |
157 | if ($snortruledesc eq '') { | |
158 | $snortruledesc = $tempruleline; | |
159 | } else { | |
160 | $snortruledesc .= " $tempruleline"; | |
161 | } | |
162 | } else { | |
163 | # Must be done | |
164 | $desclook = 0; | |
165 | } | |
166 | } | |
167 | ||
168 | # If have a dashed line, increment count | |
169 | if ($ruleline =~ /\# ?\-+/) { | |
170 | $dashlinecnt++; | |
171 | } | |
172 | } else { | |
173 | # Parse out rule file rule's message for display | |
174 | if ($ruleline =~ /(msg\:\"[^\"]+\";)/) { | |
175 | my $msg = ''; | |
176 | $msg = $1; | |
177 | $msg =~ s/msg\:\"//i; | |
178 | $msg =~ s/\";//i; | |
179 | $snortruledef{$rulecnt}{'Description'} = $msg; | |
180 | ||
181 | # Check for 'Save' and rule file displayed in query string | |
182 | if (($snortsettings{'ACTION'} eq $Lang::tr{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) { | |
183 | # Check for a disable rule which is now enabled, or an enabled rule which is now disabled | |
184 | if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) { | |
185 | $restartsnortrequired = 1; | |
186 | } | |
187 | ||
188 | # Strip out leading # from rule line | |
189 | $ruleline =~ s/\# ?//i; | |
190 | ||
191 | # Check if it does not exists (which means it is disabled), append a # | |
192 | if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) { | |
193 | $ruleline = "#"." $ruleline"; | |
194 | } | |
195 | } | |
196 | ||
197 | # Check if ruleline does not begin with a #, so it is enabled | |
198 | if ($ruleline !~ /^\#/) { | |
199 | $snortruledef{$rulecnt++}{'State'} = 'Enabled'; | |
200 | } else { | |
201 | # Otherwise it is disabled | |
202 | $snortruledef{$rulecnt++}{'State'} = 'Disabled'; | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | # Print ruleline to RULEFILE | |
208 | print RULEFILE "$ruleline\n"; | |
209 | } | |
210 | ||
211 | # Close RULEFILE | |
212 | close(RULEFILE); | |
213 | ||
214 | # Check for 'Save' | |
215 | if ($snortsettings{'ACTION'} eq $Lang::tr{'update'}) { | |
216 | # Check for a disable rule which is now enabled, or an enabled rule which is now disabled | |
217 | if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) { | |
218 | $restartsnortrequired = 1; | |
219 | } | |
220 | ||
221 | # Strip out leading # from rule line | |
222 | $line =~ s/\# ?//i; | |
223 | ||
224 | # Check if it does not exists (which means it is disabled), append a # | |
225 | if (!exists $snortsettings{"SNORT_RULE_$rule"}) { | |
226 | $line = "# $line"; | |
227 | } | |
f9c2147d | 228 | |
395e3b90 MT |
229 | } |
230 | ||
231 | # Check for rule state | |
232 | if ($line =~ /^\#/) { | |
233 | $snortrules{$rule}{"State"} = "Disabled"; | |
234 | } else { | |
235 | $snortrules{$rule}{"State"} = "Enabled"; | |
236 | } | |
237 | ||
238 | # Set rule description | |
239 | $snortrules{$rule}{"Description"} = $snortruledesc; | |
240 | ||
241 | # Loop over sorted rules | |
242 | foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) { | |
243 | $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'}; | |
244 | $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'}; | |
245 | } | |
246 | ||
247 | $snortruledesc = ''; | |
248 | print FILE "$line\n"; | |
249 | } elsif ($line =~ /var RULE_PATH/) { | |
250 | ($tmp, $tmp, $snortrulepath) = split(' ', $line); | |
251 | print FILE "$line\n"; | |
252 | } else { | |
253 | print FILE "$line\n"; | |
254 | } | |
255 | } | |
256 | close(FILE); | |
257 | ||
258 | if ($restartsnortrequired) { | |
18322edf | 259 | system('/usr/local/bin/snortctrl restart >/dev/null'); |
395e3b90 MT |
260 | } |
261 | } | |
262 | ||
263 | ####################### End added for snort rules control ################################# | |
264 | ||
5a3e0dca | 265 | if ($snortsettings{'RULES'} eq 'subscripted') { |
8c7f7ed4 | 266 | $url="http://dl.snort.org/sub-rules/snortrules-snapshot-2.8_s.tar.gz?oink_code=$snortsettings{'OINKCODE'}"; |
be9e0412 | 267 | #$url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.8_s.tar.gz"; |
5a3e0dca | 268 | } elsif ($snortsettings{'RULES'} eq 'registered') { |
be9e0412 CS |
269 | $url="http://dl.snort.org/reg-rules/snortrules-snapshot-2.8.tar.gz?oink_code=$snortsettings{'OINKCODE'}"; |
270 | #$url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-2.8.tar.gz"; | |
ac1cfefa | 271 | } else { |
5a3e0dca | 272 | $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz"; |
ac1cfefa MT |
273 | } |
274 | ||
fbfdb241 | 275 | if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) |
ac1cfefa MT |
276 | { |
277 | $errormessage = $Lang::tr{'invalid input for oink code'} unless ( | |
278 | ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) || | |
279 | ($snortsettings{'RULESTYPE'} eq 'nothing' ) ); | |
280 | ||
281 | &General::writehash("${General::swroot}/snort/settings", \%snortsettings); | |
282 | if ($snortsettings{'ENABLE_SNORT'} eq 'on') | |
283 | { | |
9833e7d8 | 284 | system ('/usr/bin/touch', "${General::swroot}/snort/enable"); |
ac1cfefa MT |
285 | } else { |
286 | unlink "${General::swroot}/snort/enable"; | |
f9c2147d | 287 | } |
ac1cfefa MT |
288 | if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on') |
289 | { | |
9833e7d8 | 290 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_green"); |
ac1cfefa MT |
291 | } else { |
292 | unlink "${General::swroot}/snort/enable_green"; | |
f9c2147d | 293 | } |
ac1cfefa MT |
294 | if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on') |
295 | { | |
9833e7d8 | 296 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue"); |
ac1cfefa MT |
297 | } else { |
298 | unlink "${General::swroot}/snort/enable_blue"; | |
f9c2147d | 299 | } |
ac1cfefa MT |
300 | if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on') |
301 | { | |
9833e7d8 | 302 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange"); |
ac1cfefa MT |
303 | } else { |
304 | unlink "${General::swroot}/snort/enable_orange"; | |
305 | } | |
429f7008 DG |
306 | if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on') |
307 | { | |
308 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect"); | |
309 | } else { | |
310 | unlink "${General::swroot}/snort/enable_preprocessor_http_inspect"; | |
f9c2147d | 311 | } |
1b73b07e CS |
312 | if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on') |
313 | { | |
314 | system ('/usr/bin/touch', "${General::swroot}/guardian/enable"); | |
315 | } else { | |
316 | unlink "${General::swroot}/guardian/enable"; | |
317 | } | |
ac1cfefa | 318 | |
429f7008 | 319 | system('/usr/local/bin/snortctrl restart >/dev/null'); |
395e3b90 | 320 | |
fbfdb241 CS |
321 | } elsif ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "guardian" ){ |
322 | open(IGNOREFILE, ">$snortsettings{'GUARDIAN_IGNOREFILE'}") or die "Unable to write guardian ignore file $snortsettings{'GUARDIAN_IGNOREFILE'}"; | |
f9c2147d | 323 | print IGNOREFILE $snortsettings{'IGNOREFILE_CONTENT'}; |
fbfdb241 CS |
324 | close(IGNOREFILE); |
325 | open(GUARDIAN, ">/var/ipfire/guardian/guardian.conf") or die "Unable to write guardian conf /var/ipfire/guardian/guardian.conf"; | |
326 | print GUARDIAN <<END | |
327 | Interface $snortsettings{'GUARDIAN_INTERFACE'} | |
328 | HostGatewayByte $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'} | |
329 | LogFile $snortsettings{'GUARDIAN_LOGFILE'} | |
330 | AlertFile $snortsettings{'GUARDIAN_ALERTFILE'} | |
331 | IgnoreFile $snortsettings{'GUARDIAN_IGNOREFILE'} | |
332 | TargetFile $snortsettings{'GUARDIAN_TARGETFILE'} | |
333 | TimeLimit $snortsettings{'GUARDIAN_TIMELIMIT'} | |
334 | END | |
335 | ; | |
336 | close(GUARDIAN); | |
337 | system('/usr/local/bin/snortctrl restart >/dev/null'); | |
75b5a616 | 338 | } |
ac1cfefa MT |
339 | # INSTALLMD5 is not in the form, so not retrieved by getcgihash |
340 | &General::readhash("${General::swroot}/snort/settings", \%snortsettings); | |
ac1cfefa MT |
341 | |
342 | if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) { | |
343 | $md5 = &getmd5; | |
344 | if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) { | |
345 | chomp($md5); | |
346 | my $filename = &downloadrulesfile(); | |
347 | if (defined $filename) { | |
348 | # Check MD5sum | |
349 | $realmd5 = `/usr/bin/md5sum $filename`; | |
350 | chomp ($realmd5); | |
351 | $realmd5 =~ s/^(\w+)\s.*$/$1/; | |
be9e0412 CS |
352 | if ( $md5 ne $realmd5 ) { |
353 | $errormessage = "$Lang::tr{'invalid md5sum'} - $md5 - $realmd5"; | |
ac1cfefa MT |
354 | } else { |
355 | $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>"; | |
395e3b90 | 356 | $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`; |
ac1cfefa MT |
357 | $results .= "</pre>"; |
358 | } | |
359 | unlink ($filename); | |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | $checked{'ENABLE_SNORT'}{'off'} = ''; | |
365 | $checked{'ENABLE_SNORT'}{'on'} = ''; | |
366 | $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'"; | |
367 | $checked{'ENABLE_SNORT_GREEN'}{'off'} = ''; | |
368 | $checked{'ENABLE_SNORT_GREEN'}{'on'} = ''; | |
369 | $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'"; | |
370 | $checked{'ENABLE_SNORT_BLUE'}{'off'} = ''; | |
371 | $checked{'ENABLE_SNORT_BLUE'}{'on'} = ''; | |
372 | $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'"; | |
373 | $checked{'ENABLE_SNORT_ORANGE'}{'off'} = ''; | |
374 | $checked{'ENABLE_SNORT_ORANGE'}{'on'} = ''; | |
375 | $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'"; | |
1b73b07e CS |
376 | $checked{'ENABLE_GUARDIAN'}{'off'} = ''; |
377 | $checked{'ENABLE_GUARDIAN'}{'on'} = ''; | |
378 | $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'"; | |
5a3e0dca | 379 | $selected{'RULES'}{'nothing'} = ''; |
5a3e0dca MT |
380 | $selected{'RULES'}{'community'} = ''; |
381 | $selected{'RULES'}{'registered'} = ''; | |
382 | $selected{'RULES'}{'subscripted'} = ''; | |
383 | $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'"; | |
ac1cfefa MT |
384 | |
385 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
386 | ||
395e3b90 MT |
387 | ####################### Added for snort rules control ################################# |
388 | print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>"; | |
389 | print <<END | |
f9c2147d | 390 | <STYLE TYPE="text/css"> |
395e3b90 MT |
391 | <!-- |
392 | .section { | |
393 | border: groove; | |
394 | } | |
395 | .row1color { | |
396 | border: ridge; | |
f2fdd0c1 | 397 | background-color: $color{'color22'}; |
395e3b90 MT |
398 | } |
399 | .row2color { | |
400 | border: ridge; | |
f2fdd0c1 | 401 | background-color: $color{'color20'}; |
395e3b90 MT |
402 | } |
403 | .rowselected { | |
404 | border: double #FF0000; | |
405 | background-color: #DCDCDC; | |
406 | } | |
407 | --> | |
408 | </STYLE> | |
409 | END | |
410 | ; | |
411 | ####################### End added for snort rules control ################################# | |
412 | ||
ac1cfefa MT |
413 | &Header::openbigbox('100%', 'left', '', $errormessage); |
414 | ||
415 | if ($errormessage) { | |
416 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
417 | print "<class name='base'>$errormessage\n"; | |
418 | print " </class>\n"; | |
419 | &Header::closebox(); | |
420 | } | |
421 | ||
422 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'}); | |
423 | print <<END | |
424 | <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'> | |
1b73b07e | 425 | <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort |
ac1cfefa MT |
426 | END |
427 | ; | |
428 | if ($netsettings{'BLUE_DEV'} ne '') { | |
1b73b07e | 429 | print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort"; |
ac1cfefa MT |
430 | } |
431 | if ($netsettings{'ORANGE_DEV'} ne '') { | |
1b73b07e | 432 | print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort"; |
ac1cfefa | 433 | } |
1b73b07e CS |
434 | print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort"; |
435 | if ( -e "/var/ipfire/guardian/guardian.conf" ) { | |
436 | print " <input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian"; | |
437 | } | |
438 | ||
ac1cfefa | 439 | print <<END |
1b73b07e | 440 | </td></tr> |
ac1cfefa MT |
441 | <tr> |
442 | <td><hr /></td> | |
443 | </tr> | |
444 | <tr> | |
445 | <td><b>$Lang::tr{'ids rules update'}</b></td> | |
446 | </tr> | |
447 | <tr> | |
5a3e0dca MT |
448 | <td><select name='RULES'> |
449 | <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option> | |
5a3e0dca MT |
450 | <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option> |
451 | <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option> | |
452 | <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option> | |
453 | </select> | |
454 | </td> | |
ac1cfefa MT |
455 | </tr> |
456 | <tr> | |
457 | <td><br /> | |
4935eb8b JPT |
458 | $Lang::tr{'ids rules license'} <a href='https://www.snort.org/signup' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br /> |
459 | $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 |
460 | </td> |
461 | </tr> | |
462 | <tr> | |
5a3e0dca | 463 | <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td> |
ac1cfefa MT |
464 | </tr> |
465 | <tr> | |
466 | <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' /> | |
467 | END | |
468 | ; | |
469 | ||
470 | if ($snortsettings{'INSTALLMD5'} eq $md5) { | |
471 | print " $Lang::tr{'rules already up to date'}</td>"; | |
472 | } else { | |
473 | if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} && $md5 eq $realmd5 ) { | |
474 | $snortsettings{'INSTALLMD5'} = $realmd5; | |
475 | $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`; | |
476 | &General::writehash("${General::swroot}/snort/settings", \%snortsettings); | |
477 | } | |
478 | print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>"; | |
479 | } | |
480 | print <<END | |
481 | </tr> | |
482 | </table> | |
483 | <hr /> | |
484 | <table width='100%'> | |
485 | <tr> | |
fbfdb241 | 486 | <td align='center'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td> |
ac1cfefa MT |
487 | </tr> |
488 | </table> | |
489 | </form> | |
490 | END | |
491 | ; | |
492 | ||
493 | if ($results ne '') { | |
494 | print "$results"; | |
495 | } | |
496 | ||
497 | &Header::closebox(); | |
fbfdb241 CS |
498 | |
499 | ####################### Added for guardian control #################################### | |
500 | if ( -e "/var/ipfire/guardian/guardian.conf" ) { | |
501 | &Header::openbox('100%', 'LEFT', $Lang::tr{'guardian configuration'}); | |
502 | print <<END | |
503 | <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'> | |
504 | <tr><td align='left' width='40%'>$Lang::tr{'guardian interface'}</td><td align='left'><input type='text' name='INTERFACE' value='$snortsettings{'GUARDIAN_INTERFACE'}' size="30" /></td></tr> | |
505 | <tr><td align='left' width='40%'>$Lang::tr{'guardian timelimit'}</td><td align='left'><input type='text' name='TIMELIMIT' value='$snortsettings{'GUARDIAN_TIMELIMIT'}' size="30" /></td></tr> | |
506 | <tr><td align='left' width='40%'>$Lang::tr{'guardian logfile'}</td><td align='left'><input type='text' name='LOGFILE' value='$snortsettings{'GUARDIAN_LOGFILE'}' size="30" /></td></tr> | |
507 | <tr><td align='left' width='40%'>$Lang::tr{'guardian alertfile'}</td><td align='left'><input type='text' name='ALERTFILE' value='$snortsettings{'GUARDIAN_ALERTFILE'}' size="30" /></td></tr> | |
e55a2b81 CS |
508 | <tr><td align='left' width='40%'>$Lang::tr{'guardian ignorefile'}</td><td align='left'><textarea name='IGNOREFILE_CONTENT' cols='32' rows='6' wrap='off'> |
509 | END | |
510 | ; | |
511 | print `cat /var/ipfire/guardian/guardian.ignore`; | |
512 | print <<END | |
513 | </textarea></td></tr> | |
fbfdb241 CS |
514 | <tr><td align='center' colspan='2'><input type='hidden' name='ACTION2' value='guardian' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr> |
515 | </table> | |
516 | </form> | |
517 | END | |
518 | ; | |
519 | &Header::closebox(); | |
520 | } | |
521 | ||
522 | ||
523 | ||
524 | ||
395e3b90 | 525 | ####################### Added for snort rules control ################################# |
3ffee04b CS |
526 | if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) { |
527 | &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'}); | |
528 | # Output display table for rule files | |
529 | print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>"; | |
f9c2147d | 530 | |
3ffee04b CS |
531 | print "<form method='post'>"; |
532 | ||
533 | # Local vars | |
534 | my $ruledisplaycnt = 1; | |
535 | my $rulecnt = keys %snortrules; | |
536 | $rulecnt++; | |
537 | $rulecnt = $rulecnt / 2; | |
538 | ||
539 | # Loop over each rule file | |
540 | foreach my $rulefile (sort keys(%snortrules)) { | |
541 | my $rulechecked = ''; | |
542 | ||
543 | # Check if reached half-way through rule file rules to start new column | |
544 | if ($ruledisplaycnt > $rulecnt) { | |
545 | print "</TABLE></TD><TD VALIGN='TOP'><TABLE>"; | |
546 | $ruledisplaycnt = 0; | |
2999f1d2 | 547 | } |
3ffee04b CS |
548 | |
549 | # Check if rule file is enabled | |
550 | if ($snortrules{$rulefile}{"State"} eq 'Enabled') { | |
551 | $rulechecked = 'CHECKED'; | |
552 | } | |
553 | ||
554 | # Create rule file link, vars array, and display flag | |
2999f1d2 | 555 | my $rulefilelink = "?RULEFILE=$rulefile"; |
3ffee04b CS |
556 | my $rulefiletoclose = ''; |
557 | my @queryvars = (); | |
558 | my $displayrulefilerules = 0; | |
559 | ||
560 | # Check for passed in query string | |
561 | if ($ENV{'QUERY_STRING'}) { | |
562 | # Split out vars | |
2999f1d2 | 563 | @queryvars = split(/\&/, $ENV{'QUERY_STRING'}); |
3ffee04b CS |
564 | |
565 | # Loop over values | |
566 | foreach $value (@queryvars) { | |
567 | # Split out var pairs | |
2999f1d2 | 568 | ($var, $linkedrulefile) = split(/=/, $value); |
3ffee04b CS |
569 | |
570 | # Check if var is 'RULEFILE' | |
571 | if ($var eq 'RULEFILE') { | |
572 | # Check if rulefile equals linkedrulefile | |
573 | if ($rulefile eq $linkedrulefile) { | |
574 | # Set display flag | |
575 | $displayrulefilerules = 1; | |
576 | ||
577 | # Strip out rulefile from rulefilelink | |
578 | $rulefilelink =~ s/RULEFILE=$linkedrulefile//g; | |
579 | } else { | |
580 | # Add linked rule file to rulefilelink | |
581 | $rulefilelink .= "&RULEFILE=$linkedrulefile"; | |
582 | } | |
583 | } | |
584 | } | |
585 | } | |
2999f1d2 | 586 | |
3ffee04b CS |
587 | # Strip out extra & & ? from rulefilelink |
588 | $rulefilelink =~ s/^\?\&/\?/i; | |
589 | ||
590 | # Check for a single '?' and replace with page for proper link display | |
591 | if ($rulefilelink eq '?') { | |
592 | $rulefilelink = "ids.cgi"; | |
593 | } | |
594 | ||
595 | # Output rule file name and checkbox | |
596 | print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>"; | |
597 | print "<TR><TD CLASS='base' VALIGN='TOP'>"; | |
598 | ||
599 | # Check for empty 'Description' | |
600 | if ($snortrules{$rulefile}{'Description'} eq '') { | |
601 | print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>"; | |
602 | } else { | |
603 | # Output rule file 'Description' | |
604 | print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>"; | |
605 | } | |
606 | ||
607 | # Check for display flag | |
608 | if ($displayrulefilerules) { | |
609 | # Rule file definition rule display | |
610 | print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>"; | |
611 | ||
612 | # Local vars | |
613 | my $ruledefdisplaycnt = 0; | |
614 | my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}}; | |
615 | $ruledefcnt++; | |
616 | $ruledefcnt = $ruledefcnt / 2; | |
617 | ||
618 | # Loop over rule file rules | |
619 | foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) { | |
620 | # Local vars | |
621 | my $ruledefchecked = ''; | |
622 | ||
623 | # If have display 2 rules, start new row | |
624 | if (($ruledefdisplaycnt % 2) == 0) { | |
625 | print "</TR><TR>"; | |
626 | $ruledefdisplaycnt = 0; | |
627 | } | |
628 | ||
629 | # Check for rules state | |
630 | if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') { | |
631 | $ruledefchecked = 'CHECKED'; | |
632 | } | |
633 | ||
634 | # Create rule file rule's checkbox | |
635 | $checkboxname = "SNORT_RULE_$rulefile"; | |
636 | $checkboxname .= "_$ruledef"; | |
637 | print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>"; | |
638 | ||
639 | # Increment count | |
640 | $ruledefdisplaycnt++; | |
641 | } | |
f9c2147d | 642 | |
3ffee04b CS |
643 | # If do not have second rule for row, create empty cell |
644 | if (($ruledefdisplaycnt % 2) != 0) { | |
645 | print "<TD CLASS='base'></TD>"; | |
646 | } | |
647 | ||
648 | # Close display table | |
649 | print "</TR></TABLE></TD></TR>"; | |
2999f1d2 | 650 | } |
3ffee04b CS |
651 | |
652 | # Close display table | |
653 | print "</TABLE>"; | |
654 | ||
655 | # Increment ruledisplaycnt | |
2999f1d2 | 656 | $ruledisplaycnt++; |
3ffee04b | 657 | } |
3ffee04b CS |
658 | print "</TD></TR></TABLE></TD></TR></TABLE>"; |
659 | print <<END | |
2999f1d2 CS |
660 | <table width='100%'> |
661 | <tr> | |
3ffee04b CS |
662 | <td width='33%'> </td> |
663 | <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td> | |
664 | <td width='33%'> | |
665 | <!-- space for future online help link --> | |
666 | </td> | |
2999f1d2 CS |
667 | </tr> |
668 | </table> | |
669 | </form> | |
3ffee04b CS |
670 | END |
671 | ; | |
672 | &Header::closebox(); | |
673 | } | |
395e3b90 | 674 | |
395e3b90 | 675 | ####################### End added for snort rules control ################################# |
ac1cfefa MT |
676 | &Header::closebigbox(); |
677 | &Header::closepage(); | |
678 | ||
679 | sub getmd5 { | |
680 | # Retrieve MD5 sum from $url.md5 file | |
be9e0412 CS |
681 | |
682 | my $md5buf; | |
683 | if ($snortsettings{'RULES'} eq 'subscripted') { | |
684 | $md5buf = &geturl("http://dl.snort.org/reg-rules/snortrules-snapshot-2.8_s.tar.gz.md5?oink_code=$snortsettings{'OINKCODE'}"); | |
685 | } elsif ($snortsettings{'RULES'} eq 'registered') { | |
686 | $md5buf = &geturl("http://dl.snort.org/reg-rules/snortrules-snapshot-2.8.tar.gz.md5?oink_code=$snortsettings{'OINKCODE'}"); | |
687 | } else { | |
688 | $md5buf = &geturl("http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz.md5"); | |
689 | } | |
690 | ||
ac1cfefa MT |
691 | return undef unless $md5buf; |
692 | ||
693 | if (0) { # 1 to debug | |
694 | my $filename=''; | |
695 | my $fh=''; | |
be9e0412 | 696 | ($fh, $filename) = tempfile('/var/tmp/XXXXXXXX',SUFFIX => '.md5' ); |
ac1cfefa MT |
697 | binmode ($fh); |
698 | syswrite ($fh, $md5buf->content); | |
699 | close($fh); | |
700 | } | |
f9c2147d | 701 | |
698e3c2a | 702 | return $md5buf->content; |
ac1cfefa MT |
703 | } |
704 | sub downloadrulesfile { | |
705 | my $return = &geturl($url); | |
706 | return undef unless $return; | |
707 | ||
708 | if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning | |
709 | $errormessage = $Lang::tr{'invalid loaded file'}; | |
710 | return undef; | |
711 | } | |
712 | ||
713 | my $filename=''; | |
714 | my $fh=''; | |
be9e0412 | 715 | ($fh, $filename) = tempfile('/var/tmp/XXXXXXXX',SUFFIX => '.tar.gz' );#oinkmaster work only with this extension |
ac1cfefa MT |
716 | binmode ($fh); |
717 | syswrite ($fh, $return->content); | |
718 | close($fh); | |
719 | return $filename; | |
720 | } | |
721 | ||
722 | sub geturl ($) { | |
723 | my $url=$_[0]; | |
724 | ||
725 | unless (-e "${General::swroot}/red/active") { | |
726 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
727 | return undef; | |
728 | } | |
729 | ||
730 | my $downloader = LWP::UserAgent->new; | |
731 | $downloader->timeout(5); | |
732 | ||
733 | my %proxysettings=(); | |
734 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); | |
735 | ||
736 | if ($_=$proxysettings{'UPSTREAM_PROXY'}) { | |
737 | my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/); | |
738 | if ($proxysettings{'UPSTREAM_USER'}) { | |
739 | $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/"); | |
740 | } else { | |
741 | $downloader->proxy("http","http://$peer:$peerport/"); | |
742 | } | |
743 | } | |
744 | ||
745 | my $return = $downloader->get($url,'Cache-Control','no-cache'); | |
746 | ||
747 | if ($return->code == 403) { | |
748 | $errormessage = $Lang::tr{'access refused with this oinkcode'}; | |
749 | return undef; | |
750 | } elsif (!$return->is_success()) { | |
751 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
752 | return undef; | |
753 | } | |
754 | ||
755 | return $return; | |
756 | ||
757 | } |