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