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