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