Postfix mail server block Malware with blacklist

Posted on in Categories Howto, Linux, Mail server, Networking, Postfix, RedHat/Fedora Linux, Security, Shell scripting, Suse Linux, Sys admin, Tips, Ubuntu Linux, UNIX last updated June 25, 2007

Malware is used for a malicious purpose. It can be in your software or hardware. Email and pirated software is the most powerful way to spread malware. Malware inserted in a system without user notification.

Wikipedia defines Malware as:

Malware is software designed to infiltrate or damage a computer system without the owner’s informed consent. It is a portmanteau of the words “malicious” and “software”. The expression is a general term used by computer professionals to mean a variety of forms of hostile, intrusive, or annoying software or program code.

It will be a nice idea to block malware spreading URLs and website. Setting up a malware blacklist in Postfix MTA is quite easy. The Malware Block List is a free, automated and user contributed system for checking URLs for the presence of Viruses, Trojans, Worms, or any other software considered Malware.

Create a blacklist

First you need to create a blacklist, type the following command:
# wget -O - '' > /etc/postfix/mbl-body-deny
Where receipt= should be followed by your personal Malwarepatrol id once registered. Do not use f1391767429. This is for demo purpose only.

Configure Postfix

Open postfix config file, run:
# vi /etc/postfix/
Setup postfix body_check directive as follows:
body_checks = regexp:/etc/postfix/mbl-body-deny
Save and close the file.

Restart Postfix

Now just restart the postfix daemon:
# postmap /etc/postfix/mbl-body-deny
# /etc/init.d/postfix restart

Automate procedure

You need to setup a cron job to automate entire procedure. Create a shell script as follows:

# Script to update malware urls
# Author: Vivek Gite <> under GPL v2.x+
# ---------------
# Set ID here (it is free)
[[ ! -z "$myid" ]] && echo "Using $myid..." || { echo "Error: Please correct set \"$myid\"."; exit 1; }
/usr/bin/wget -O - "${myid}&product=8&list=postfix" > /etc/postfix/mbl-body-deny
/usr/sbin/postmap /etc/postfix/mbl-body-deny
/etc/init.d/postfix reload

Add cronjob as follows:
40 23 * * * /etc/admin/scripts/ >/dev/null 2>&1

You may wan to combine this feature with mime attachments blocking and anti spam blacklist for the best result.

Further readings

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

16 comment

  1. I want you to know that we are very pleased with the quality of Article you have provided. I sincerely appreciate your responsiveness and the way you conduct. I have recommended your to others because of our satisfaction. I look forward to doing with you further comment or suggestions. regards.electronic projects

  2. Is it needed to ‘postmap’ the malware file? it complains about many duplicate entries, because i’m using it to block some domains (using ‘From:’ header). I think postmap is not helping in any way with this, because it’s a pcre table, that can’t be postmap’d

  3. I have the following to remove duplicates

    # Script to update malware urls
    /usr/bin/wget -O - > /etc/postfix/
    grep -ve '^#' -ve '^$' /etc/postfix/ > /etc/postfix/mbl-body-deny.uncomment
    sed -e 's/(.*[^MBL-])(MBL-.*)/1/' /etc/postfix/ > /etc/postfix/mbl-body-deny.eol
    sort /etc/postfix/mbl-body-deny.eol > /etc/postfix/mbl-body-deny.sort
    uniq /etc/postfix/mbl-body-deny.sort > /etc/postfix/mbl-body-deny
    /usr/sbin/postmap /etc/postfix/mbl-body-deny
    /etc/init.d/postfix reload
    rm -f /etc/postfix/mbl-body-deny.sort
    rm -f /etc/postfix/mbl-body-deny.eol
    rm -f /etc/postfix/mbl-body-deny.uncomment
    rm -f /etc/postfix/
  4. Here’s a simplified script. Insert your own receipt in there, of course 😉

    curl -sk '' | grep -ve '^#' -ve '^$' | sed -e 's/(.*[^MBL-])(MBL-.*)/1/' | sort | uniq -i > /etc/postfix/mbl-body-deny
    postmap /etc/postfix/mbl-body-deny
    systemctl reload postfix

  5. Why do people keep using postmap? It seems wrong.

    postmap /etc/postfix/mbl-body-deny

    Should it not be just this added to
    body_checks = regexp:/etc/postfix/mbl-body-deny

Leave a Comment