This page looks best with JavaScript enabled

Header checks postfix

 ·  🎃 kr0m

In many occasions, we are interested in being able to treat certain emails in a certain way based on the content of certain headers/body of the email. Several operations can be performed on the emails, the list can be found here.

As I have already mentioned in the introduction, the available actions are: http://www.postfix.org/header_checks.5.html

We configure postfix to take into account the ACLs:

vi /etc/postfix/main.cf

header_checks = pcre:/etc/postfix/header_checks.pcre

The regular expression will check if the words viagra or porn appear in the subject:

vi /etc/postfix/header_checks.pcre

/^Subject: (viagra|porn)/ REJECT Illegal subject: "$1"

We send a test email:

mail -s “porn” kr0m@alfaexploit.com

Cc:
test
Ctrl+D

We can check in the logs how the email has been rejected:

tail -f /var/log/mail.log

Jun 22 09:13:09 RX4 postfix/cleanup[21476]: 8908D1204E1: to=<kr0m@alfaexploit.com>, relay=none, delay=0.01, delays=0.01/0/0/0, dsn=5.7.1, status=bounced (Illegal subject: "porn")

NOTE: It should be noted that certain actions will only work from version 3.2 of postfix, for example:

STRIP optional text…
Log a “strip:” record with the optional text… (or log a
generic text), delete the input line from the input, and inspect
the next input line. See IGNORE for a silent alternative.
This feature is available in Postfix 3.2 and later.

To have version 3.2 it would be as simple as:

emerge -av =mail-mta/postfix-3.2.0

If you liked the article, you can treat me to a RedBull here