A simple yet very useful perl script to generate rules for spamassasin (linux spam filter) in order to create spam rules 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
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 | #!/usr/bin/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])/sprintf("\\x{%X}",ord($1))/eg; $lc =~ s/([^a-z0-9])/&encode($1)/eg; #print "body $prepend$uc /\\b$lc\\b/i # $line ($length)\n"; 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!