/usr/share/oinkmaster/addsid.pl is in oinkmaster 2.0-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | #!/usr/bin/perl -w
# $Id: addsid.pl,v 1.31 2006/10/12 08:57:26 andreas_o Exp $ #
# Copyright (c) 2004-2006 Andreas Östling <andreas_ostling@bredband.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the author nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use strict;
sub get_next_entry($ $ $ $ $ $);
sub parse_singleline_rule($ $ $);
sub get_next_available_sid(@);
# Set this to the default classtype you want to add, if missing.
# Set to 0 or "" if you don't want to add a classtype.
my $CLASSTYPE = "misc-attack";
# If ADD_REV is set to 1, "rev: 1;" will be added to rule if it has no rev.
# Set to 0 if you don't want to add it.
my $ADD_REV = 1;
# Minimum SID to add. Normally, the next available SID will be used,
# unless it's below this value. Only SIDs >= 1000000 are reserved for
# personal use.
my $MIN_SID = 1000001;
# Regexp to match the start of a multi-line rule.
# %ACTIONS% will be replaced with content of $config{actions} later.
my $MULTILINE_RULE_REGEXP = '^\s*#*\s*(?:%ACTIONS%)'.
'\s.*\\\\\s*\n$'; # ';
# Regexp to match a single-line rule.
my $SINGLELINE_RULE_REGEXP = '^\s*#*\s*(?:%ACTIONS%)'.
'\s.+;\s*\)\s*$'; # ';
my $USAGE = << "RTFM";
Parse *.rules in one or more directories and add "sid:<sid>;" to
active rules that don't have any "sid" entry, starting with the next
available SID after parsing all rules files (but $MIN_SID at minumum).
Also, "rev:1;" is added to rules without a "rev" entry, and
"classtype:misc-attack;" is added to rules without a "classtype" entry
(edit options at the top of $0 if you want to change this).
Usage: $0 <rulesdir> [rulesdir2, ...]
RTFM
# Start in verbose mode.
my $verbose = 1;
my (%all_sids, %active_sids, %config);
my @rulesdirs = @ARGV;
die($USAGE) unless ($#rulesdirs > -1);
$config{rule_actions} = "alert|drop|log|pass|reject|sdrop|activate|dynamic";
$SINGLELINE_RULE_REGEXP =~ s/%ACTIONS%/$config{rule_actions}/;
$MULTILINE_RULE_REGEXP =~ s/%ACTIONS%/$config{rule_actions}/;
# Find out the next available SID.
my $next_sid = get_next_available_sid(@rulesdirs);
# Avoid seeing possible warnings about broken rules twice.
$verbose = 0;
# Add sid/rev/classtype to active rules that don't have any.
foreach my $dir (@rulesdirs) {
opendir(RULESDIR, "$dir") or die("could not open \"$dir\": $!\n");
while (my $file = readdir(RULESDIR)) {
next unless ($file =~ /\.rules$/);
open(OLDFILE, "$dir/$file")
or die("could not open \"$dir/$file\": $!\n");
my @file = <OLDFILE>;
close(OLDFILE);
open(NEWFILE, ">", "$dir/$file")
or die("could not open \"$dir/$file\" for writing: $!\n");
my ($single, $multi, $nonrule, $msg, $sid);
while (get_next_entry(\@file, \$single, \$multi, \$nonrule, \$msg, \$sid)) {
if (defined($nonrule)) {
print NEWFILE "$nonrule";
next;
}
$multi = $single unless (defined($multi));
# Don't care about inactive rules.
if ($single =~ /^\s*#/) {
print NEWFILE "$multi";
next;
}
my $added;
# Add SID.
if ($single !~ /sid\s*:\s*\d+\s*;/) {
$added .= "SID $next_sid,";
$multi =~ s/\)\s*\n/sid:$next_sid;)\n/;
$next_sid++;
}
# Add revision.
if ($ADD_REV && $single !~ /rev\s*:\s*\d+\s*;/) {
$added .= "rev,";
$multi =~ s/\)\s*\n/rev:1;)\n/;
}
# Add classtype.
if ($CLASSTYPE && $single !~ /classtype\s*:\s*.+\s*;/) {
$added .= "classtype $CLASSTYPE,";
$multi =~ s/\)\s*\n/classtype:$CLASSTYPE;)\n/;
}
if (defined($added)) {
$added =~ s/,$//;
print "Adding $added to rule \"$msg\"\n"
if (defined($added));
}
print NEWFILE "$multi";
}
close(NEWFILE);
}
closedir(RULESDIR);
}
# Read in *.rules in given directory and return highest SID.
sub get_next_available_sid(@)
{
my @dirs = @_;
foreach my $dir (@dirs) {
opendir(RULESDIR, "$dir") or die("could not open \"$dir\": $!\n");
# Only care about *.rules.
while (my $file = readdir(RULESDIR)) {
next unless ($file =~ /\.rules$/);
open(OLDFILE, "<$dir/$file") or die("could not open \"$dir/$file\": $!\n");
my @file = <OLDFILE>;
close(OLDFILE);
my ($single, $multi, $nonrule, $msg, $sid);
while (get_next_entry(\@file, \$single, \$multi, \$nonrule, \$msg, \$sid)) {
if (defined($single) && defined($sid)) {
$all_sids{$sid}++;
# If this is an active rule add to %active_sids and
# warn if it already exists.
if ($single =~ /^\s*alert/) {
print STDERR "WARNING: duplicate SID: $sid\n"
if (exists($active_sids{$sid}));
$active_sids{$sid}++
}
}
}
}
}
# Sort sids and use highest one + 1, unless it's below MIN_SID.
@_ = sort {$a <=> $b} keys(%all_sids);
my $sid = pop(@_);
if (!defined($sid)) {
$sid = $MIN_SID
} else {
$sid++;
}
# If it's below MIN_SID, use MIN_SID instead.
$sid = $MIN_SID if ($sid < $MIN_SID);
return ($sid)
}
sub get_next_entry($ $ $ $ $ $)
{
my $arr_ref = shift;
my $single_ref = shift;
my $multi_ref = shift;
my $nonrule_ref = shift;
my $msg_ref = shift;
my $sid_ref = shift;
undef($$single_ref);
undef($$multi_ref);
undef($$nonrule_ref);
undef($$msg_ref);
undef($$sid_ref);
my $line = shift(@$arr_ref) || return(0);
my $disabled = 0;
my $broken = 0;
# Possible beginning of multi-line rule?
if ($line =~ /$MULTILINE_RULE_REGEXP/oi) {
$$single_ref = $line;
$$multi_ref = $line;
$disabled = 1 if ($line =~ /^\s*#/);
# Keep on reading as long as line ends with "\".
while (!$broken && $line =~ /\\\s*\n$/) {
# Remove trailing "\" and newline for single-line version.
$$single_ref =~ s/\\\s*\n//;
# If there are no more lines, this can not be a valid multi-line rule.
if (!($line = shift(@$arr_ref))) {
warn("\nWARNING: got EOF while parsing multi-line rule: $$multi_ref\n")
if ($config{verbose});
@_ = split(/\n/, $$multi_ref);
undef($$multi_ref);
undef($$single_ref);
# First line of broken multi-line rule will be returned as a non-rule line.
$$nonrule_ref = shift(@_) . "\n";
$$nonrule_ref =~ s/\s*\n$/\n/; # remove trailing whitespaces
# The rest is put back to the array again.
foreach $_ (reverse((@_))) {
unshift(@$arr_ref, "$_\n");
}
return (1); # return non-rule
}
# Multi-line continuation.
$$multi_ref .= $line;
# If there are non-comment lines in the middle of a disabled rule,
# mark the rule as broken to return as non-rule lines.
if ($line !~ /^\s*#/ && $disabled) {
$broken = 1;
} elsif ($line =~ /^\s*#/ && !$disabled) {
# comment line (with trailing slash) in the middle of an active rule - ignore it
} else {
$line =~ s/^\s*#*\s*//; # remove leading # in single-line version
$$single_ref .= $line;
}
} # while line ends with "\"
# Single-line version should now be a valid rule.
# If not, it wasn't a valid multi-line rule after all.
if (!$broken && parse_singleline_rule($$single_ref, $msg_ref, $sid_ref)) {
$$single_ref =~ s/^\s*//; # remove leading whitespaces
$$single_ref =~ s/^#+\s*/#/; # remove whitespaces next to leading #
$$single_ref =~ s/\s*\n$/\n/; # remove trailing whitespaces
$$multi_ref =~ s/^\s*//;
$$multi_ref =~ s/\s*\n$/\n/;
$$multi_ref =~ s/^#+\s*/#/;
return (1); # return multi
} else {
warn("\nWARNING: invalid multi-line rule: $$single_ref\n")
if ($config{verbose} && $$multi_ref !~ /^\s*#/);
@_ = split(/\n/, $$multi_ref);
undef($$multi_ref);
undef($$single_ref);
# First line of broken multi-line rule will be returned as a non-rule line.
$$nonrule_ref = shift(@_) . "\n";
$$nonrule_ref =~ s/\s*\n$/\n/; # remove trailing whitespaces
# The rest is put back to the array again.
foreach $_ (reverse((@_))) {
unshift(@$arr_ref, "$_\n");
}
return (1); # return non-rule
}
} elsif (parse_singleline_rule($line, $msg_ref, $sid_ref)) {
$$single_ref = $line;
$$single_ref =~ s/^\s*//;
$$single_ref =~ s/^#+\s*/#/;
$$single_ref =~ s/\s*\n$/\n/;
return (1); # return single
} else { # non-rule line
# Do extra check and warn if it *might* be a rule anyway,
# but that we just couldn't parse for some reason.
warn("\nWARNING: line may be a rule but it could not be parsed ".
"(missing sid or msg?): $line\n")
if ($config{verbose} && $line =~ /^\s*alert .+msg\s*:\s*".+"\s*;/);
$$nonrule_ref = $line;
$$nonrule_ref =~ s/\s*\n$/\n/;
return (1); # return non-rule
}
}
# From oinkmaster.pl except that this version
# has been modified so that the sid is *optional*.
sub parse_singleline_rule($ $ $)
{
my $line = shift;
my $msg_ref = shift;
my $sid_ref = shift;
if ($line =~ /$SINGLELINE_RULE_REGEXP/oi) {
if ($line =~ /\bmsg\s*:\s*"(.+?)"\s*;/i) {
$$msg_ref = $1;
} else {
return (0);
}
if ($line =~ /\bsid\s*:\s*(\d+)\s*;/i) {
$$sid_ref = $1;
# } else {
# return (0);
}
return (1);
}
return (0);
}
|