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