Les forte augmentations de scan à la recherche de serveurs mails mal configurés ("open relays") sont devenu un classique.
Ces serveurs acceptent de transférer des courriels en provenance de n'importe qui (et non uniquement des utilisateurs enregistrés) : une aubaine pour les spammeurs qui peuvent les utiliser pour relayer leurs cochonneries.

Comme vous le savez peut-être, il existe pour chaque bloc d'aresses IP (ou presque) une adresse de contact à qui l'on peut écrire pour se plaindre des manquements à la nétiquette. J'avais commencé par envoyer des mails dits "d'abuse" manuels, mais j'ai vite réalisé que ça me prendrait un temps fou à chaque fois.

Automatisation afin que les fournisseurs d'accès puissent prendre des mesures contre leurs clients belliqueux (s'ils le désirent).
Le script suivant traverse les logs postfix, identifie les lignes suspectes et envoie un joli rapport au contact "abuse" de l'IP identifiée.

postfix.openrelay.sh


#!/bin/bash
# postfix.openrelay.sh ( à placer ou vous voulez, chmod +x )

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
MAIL_HEAD="Hello,\n
\n\
I have received suspicious connections on port 25 from a machine located in your network.\n\
Here are the relevant postfix logs (all timestamps are relative to the `date +"%Z"` timezone):\n"

MAIL_FOOT="\nOne of your computers may have been infected, or it is possible that one of your clients is up to no good.\n\
Could you please look into it?\n\
\n\
Regards,\n\
[Echosystem.fr]"

function check_exit_node {
        dnsel_answer=`host \`echo $1 | perl -ne '/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ && print $4 . "." . $3 . "." . $2 . "." . $1 . ".25.13.200.179.88.ip-port.exitlist.torproject.org"'\` | pe
rl -ne '/has address ([0-9\.]+)/ && print $1'`
        if [[ -z "$dnsel_answer" || "$dnsel_answer" == "127.0.0.1" ]]; then
                return 0
        else
                return 1
        fi
}

for IP in `grep -i "relay access" /var/log/mail.log.1 |perl -ne '/from [A-Za-z0-9.-]+\[([0-9.]+)\]/ && print $1 . "\n"' |sort -u`
do
        if check_exit_node $IP
        then
                ABUSE_MAIL=`whois $IP |egrep -o "[A-Za-z0-9-]*abuse[A-Za-z0-9-]*@[A-Za-z0-9.-]+" |grep -iv "abuse@ripe.net"|sort -u`
                if [[ -n "$ABUSE_MAIL" ]]
                then
                        echo -e $MAIL_HEAD > mail.tmp
                        grep $IP /var/log/mail.log.1 >> mail.tmp
                        echo -e $MAIL_FOOT >> mail.tmp
                        # Add "-b abuse@echosystem.fr" to mutt's arguments if you want to recieve a blind carbon copy of the sent e-mails.
                        mutt -e 'set from=abuse@echosystem.fr realname="[echosystem.fr]" envelope_from="yes" user_agent="no"' -s "SMTP abuse from $IP" $ABUSE_MAIL < mail.tmp
                fi
        fi
done
rm -f mail.tmp



Pour que le script soit appelé tous les jours, il suffit d'éditer le script de rotation des logs de postfix.
Créé un fichier dans /etc/logrotate.d/postfix.


/var/log/mail.log
/var/log/mail.err
/var/log/mail.info
/var/log/mail.warn
{
daily
missingok
rotate 21
compress
delaycompress
notifempty
create 640 root
sharedscripts
postrotate
if [ -f /var/run/postfix.pid ]; then
/etc/init.d/postfix reload > /dev/null
####  appel du script ici 
/home/tools/SH/postfix.openrelay.sh || true
fi
endscript
}



Si vous utilisez un autre front-end, le script à modifier sera différent mais l'esprit reste le même.


Exemple de mail généré :



Hello,

I have received suspicious connections on port 25 from a machine located in your network.
Here are the relevant postfix logs (all timestamps are relative to the CEST timezone):

Jan 31 19:27:02 atria postfix/smtpd[21972]: warning: hostname 88.247.78.4.static.ttnet.com.tr does not resolve to address 88.247.78.4: No address associated with hostname
Jan 31 19:27:02 atria postfix/smtpd[21972]: connect from unknown[88.247.78.4]
Jan 31 19:27:03 atria postfix/smtpd[21972]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10026; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 19:27:03 atria postfix/smtpd[21972]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10024; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 19:27:03 atria postfix/smtpd[21972]: NOQUEUE: reject: RCPT from unknown[88.247.78.4]: 554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 19:27:03 atria postfix/smtpd[21972]: disconnect from unknown[88.247.78.4]
Jan 31 19:30:23 atria postfix/anvil[21974]: statistics: max connection rate 1/60s for (smtp:88.247.78.4) at Jan 31 19:27:02
Jan 31 19:30:23 atria postfix/anvil[21974]: statistics: max connection count 1 for (smtp:88.247.78.4) at Jan 31 19:27:02
Jan 31 22:15:21 atria postfix/smtpd[3430]: warning: hostname 88.247.78.4.static.ttnet.com.tr does not resolve to address 88.247.78.4: No address associated with hostname
Jan 31 22:15:21 atria postfix/smtpd[3430]: connect from unknown[88.247.78.4]
Jan 31 22:15:21 atria postfix/smtpd[3430]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10026; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:15:21 atria postfix/smtpd[3430]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10024; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:15:21 atria postfix/smtpd[3430]: NOQUEUE: reject: RCPT from unknown[88.247.78.4]: 554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:15:22 atria postfix/smtpd[3430]: disconnect from unknown[88.247.78.4]
Jan 31 22:18:42 atria postfix/anvil[3432]: statistics: max connection rate 1/60s for (smtp:88.247.78.4) at Jan 31 22:15:21
Jan 31 22:18:42 atria postfix/anvil[3432]: statistics: max connection count 1 for (smtp:88.247.78.4) at Jan 31 22:15:21
Jan 31 22:43:40 atria postfix/smtpd[16701]: warning: hostname 88.247.78.4.static.ttnet.com.tr does not resolve to address 88.247.78.4: No address associated with hostname
Jan 31 22:43:40 atria postfix/smtpd[16701]: connect from unknown[88.247.78.4]
Jan 31 22:43:41 atria postfix/smtpd[16701]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10026; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:43:41 atria postfix/smtpd[16701]: NOQUEUE: filter: RCPT from unknown[88.247.78.4]: : Sender address triggers FILTER smtp-amavis:[127.0.0.1]:10024; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:43:41 atria postfix/smtpd[16701]: NOQUEUE: reject: RCPT from unknown[88.247.78.4]: 554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=<[192.168.2.33]>Jan 31 22:43:41 atria postfix/smtpd[16701]: disconnect from unknown[88.247.78.4]
Jan 31 22:47:01 atria postfix/anvil[16703]: statistics: max connection rate 1/60s for (smtp:88.247.78.4) at Jan 31 22:43:40
Jan 31 22:47:01 atria postfix/anvil[16703]: statistics: max connection count 1 for (smtp:88.247.78.4) at Jan 31 22:43:40

One of your computers may have been infected, or it is possible that one of your clients is up to no good.
Could you please look into it?

Regards,
YOU




Un petit truc qui ne coûte pas grand chose et qui, avec un volume suffisant de plaintes, pourrait bien faire perdre de précieuses machines aux spammeurs.
N'hésitez pas à modifier le script pour qu'il réagisse à d'autre types de comportements indésirables !

EDIT du 16/05/2014 : Modification du corps du mail pour inclure le fuseau horaire de l'horodatage.
EDIT du 08/08/2015 : Les courriels d'abuse ne sont plus envoyés aux exit nodes Tor, car ils ne peuvent rien faire pour retrouver les expéditeurs des courriels et les plaintes ne font qu'irriter leurs hébergeurs.
site de reference du script  : https://blog.kwiatkowski.fr/?q=fr/node/19#comment-118
EDIT du 01/10/2016 : Modification pour postfix by 🅴🆁🆁🅴🆄🆁32.



by 🅴🆁🆁🅴🆄🆁32
Hosted on
🅴🅲🅷🅾🆂🆈🆂🆃🅴🅼