Linux SysAdmin & DevOps

Spamassassin - perl rule generator

Spamassassin - Best free open source software for email server in order to fight spam. A simple yet very powerfull perl script which generates spamassasin spam filte rules. These rules can be used to block certain types of emails based on “email body content.

The script was initially created by darxus [at] chaosreigns [dot] com. I have just adapted it a little bit according to my needs! Save the script bellow as saword.pl

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use Text::Unidecode;
 
my $prepend = $ARGV[0];
 
while (my $line = <STDIN>) {
chomp $line;
my $length = length($line);
next if ($length < 4);
my $lc = lc($line);
my $uc = unidecode(uc($line));
$uc =~ s/!//g;
$lc =~ s/([^a-z0-9])/&encode($1)/eg;
print "body $prepend /\\b$lc\\b/i # $line\n";
print "score $prepend 12\n";
}
 
sub encode {
my $input = shift;
return "[" . sprintf("\\x{\%X}",ord(uc($1))) . sprintf("\\x{\%X}",ord(lc($1))) . "]";
}

The script syntax: echo “email body text” | perl saword.pl SPAMRULE

Example: root@zira# echo “i am a virus” | perl sawordrule.pl SPAM_1 

It will generate a simple case insensitive spamassassin rule that can be added right into local.cf file. Restart spamd and you’re good to go. All emails that have the text “i am a virus” in their body text will be rejected!