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