Skip to content

Commit 5ccf5c5

Browse files
authored
Merge pull request #363 from redbaron4/snort3_compat
Modify script to handle Snort2 and Snort3 SO rule differences
2 parents a812a60 + 83de592 commit 5ccf5c5

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

pulledpork.pl

+38-11
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
my ($Process, $hmatch, $bmatch, $sid_msg_version, $skip_verify,
111111
$proxy_workaround);
112112
my $Sostubs = 1;
113+
my $Snortv3 = 0;
113114

114115
# verbose and quiet control print()
115116
# default values if not set otherwise in getopt
@@ -279,7 +280,7 @@ sub temp_cleanup {
279280
# subroutine to extract the files to a temp path so that we can do what we need to do..
280281
sub rule_extract {
281282
my ($rule_file, $temp_path, $Distro, $arch, $Snort,
282-
$Sorules, $ignore, $prefix)
283+
$Sorules, $ignore, $prefix, $Snortv3)
283284
= @_;
284285

285286
#special case to bypass file operations when -nPT are specified
@@ -324,6 +325,14 @@ sub rule_extract {
324325
$tar->remove("so_rules/precompiled/$Distro/$arch/$Snort/$_");
325326
}
326327
}
328+
my $sofile_pat_base = "^so_rules\/precompiled\/";
329+
if ($Snortv3 == 0) {
330+
$sofile_pat_base = $sofile_pat_base . "($Distro)\/($arch)\/($Snort)\/";
331+
}
332+
else {
333+
$sofile_pat_base = $sofile_pat_base . "($Distro)-($arch)\/";
334+
}
335+
my $sofile_pat = $sofile_pat_base . ".*\.so";
327336
my @files = $tar->get_files();
328337
foreach (@files) {
329338
my $filename = $_->name;
@@ -344,12 +353,12 @@ sub rule_extract {
344353
}
345354
elsif ($Sorules
346355
&& $filename
347-
=~ /^so_rules\/precompiled\/($Distro)\/($arch)\/($Snort)\/.*\.so/
356+
=~ m/$sofile_pat/
348357
&& -d $Sorules
349358
&& !$Textonly)
350359
{
351360
$singlefile
352-
=~ s/^so_rules\/precompiled\/($Distro)\/($arch)\/($Snort)\///;
361+
=~ s/$sofile_pat_base//;
353362
$tar->extract_file($filename, $Sorules . $singlefile);
354363
print "\tExtracted: $Sorules$singlefile\n"
355364
if ($Verbose && !$Quiet);
@@ -813,20 +822,29 @@ sub read_rules {
813822

814823
## sub to generate stub files using the snort --dump-dynamic-rules option
815824
sub gen_stubs {
816-
my ($Snort_path, $Snort_config, $Sostubs) = @_;
825+
my ($Snort_path, $Snort_config, $Sostubs, $Snortv3, $Sorules) = @_;
817826
print "Generating Stub Rules....\n" if !$Quiet;
818827
unless (-B $Snort_path) {
819828
Help("$Snort_path is not a valid binary file");
820829
}
821830
if (-d $Sostubs && -B $Snort_path && -f $Snort_config) {
831+
my $cmd = "";
832+
if ($Snortv3) {
833+
$cmd = "$Snort_path -c $Snort_config --plugin-path $Sorules --dump-dynamic-rules > $Sostubs/allso.rules";
834+
}
835+
else {
836+
$cmd = "$Snort_path -c $Snort_config --dump-dynamic-rules=$Sostubs"
837+
}
822838
if ($Verbose && !$Quiet) {
823839
print(
824-
"\tGenerating shared object stubs via:$Snort_path -c $Snort_config --dump-dynamic-rules=$Sostubs\n"
840+
"\tGenerating shared object stubs via: $cmd\n"
825841
);
826842
}
827-
open(FH,
828-
"$Snort_path -c $Snort_config --dump-dynamic-rules=$Sostubs 2>&1|"
829-
);
843+
if (!$Snortv3) {
844+
open(FH, "$cmd 2>&1|");
845+
} else {
846+
open(FH, "|-", "$cmd");
847+
}
830848
while (<FH>) {
831849
print "\t$_" if $_ =~ /Dumping/i && ($Verbose && !$Quiet);
832850
next unless $_ =~ /(err|warn|fail)/i;
@@ -1902,6 +1920,12 @@ sub check_file_dir {
19021920
$Textonly = 1 unless $Snort;
19031921
}
19041922

1923+
# Check if version begins with 3
1924+
if (ord($Snort) == 51) {
1925+
$Snortv3 = 1;
1926+
if ($arch) {$arch =~ s/x86-64/x64/;}
1927+
}
1928+
19051929
if (!$local_rules && ($Config_info{'local_rules'})) {
19061930
$local_rules = ($Config_info{'local_rules'});
19071931
}
@@ -1965,6 +1989,7 @@ sub check_file_dir {
19651989
}
19661990
if ($SigName) { print "\tSending signal Flag is Set: $SigName\n"; }
19671991
if ($Snort) { print "\tSnort Version is: $Snort\n"; }
1992+
if ($Snortv3) { print "\tSnort V3 detected & processing enabled\n"; }
19681993
if ($Snort_config) { print "\tSnort Config File: $Snort_config\n"; }
19691994
if ($Snort_path) { print "\tSnort Path is: $Snort_path\n"; }
19701995
if ($Sorules) { print "\tSO Output Path is: $Sorules\n"; }
@@ -2212,6 +2237,7 @@ BEGIN
22122237
'Distro' => $Distro,
22132238
'arch' => $arch,
22142239
'Snort' => $Snort,
2240+
'Snortv3' => $Snortv3,
22152241
'Sorules' => $Sorules,
22162242
'ignore_files' => $ignore_files,
22172243
'prefix' => $prefix,
@@ -2243,7 +2269,8 @@ BEGIN
22432269
$filelist->{$_}{Snort},
22442270
$filelist->{$_}{Sorules},
22452271
$filelist->{$_}{ignore_files},
2246-
$filelist->{$_}{prefix}
2272+
$filelist->{$_}{prefix},
2273+
$filelist->{$_}{Snortv3}
22472274
);
22482275
}
22492276
}
@@ -2276,7 +2303,7 @@ BEGIN
22762303
$prefix = "Snort-Community-"
22772304
if $base_url =~ /snort\.org.+community/;
22782305
rule_extract($rule_file, $temp_path, $Distro,
2279-
$arch, $Snort, $Sorules, $ignore_files, $prefix)
2306+
$arch, $Snort, $Sorules, $ignore_files, $prefix, $Snortv3)
22802307
if !$grabonly;
22812308
}
22822309
}
@@ -2306,7 +2333,7 @@ BEGIN
23062333
&& $Process)
23072334
{
23082335
gen_stubs($Snort_path, $Snort_config,
2309-
"$temp_path" . "tha_rules/so_rules/");
2336+
"$temp_path" . "tha_rules/so_rules/", $Snortv3, $Sorules);
23102337
read_rules(\%rules_hash, "$temp_path" . "tha_rules/so_rules/",
23112338
$local_rules);
23122339
}

0 commit comments

Comments
 (0)