Greylisting con postfix come tecnica antispam: Difference between revisions

From RVM Wiki
Jump to navigation Jump to search
mNo edit summary
Line 26: Line 26:
  sudo sed -i 's@^POSTGREY_OPTS="--inet=10023"@POSTGREY_OPTS="--inet=10023 --delay=60"@' /etc/default/postgrey
  sudo sed -i 's@^POSTGREY_OPTS="--inet=10023"@POSTGREY_OPTS="--inet=10023 --delay=60"@' /etc/default/postgrey
  echo "352c8a6936542f0933eb14e9cc5142bf  /etc/default/postgrey" | md5sum --check
  echo "352c8a6936542f0933eb14e9cc5142bf  /etc/default/postgrey" | md5sum --check
* Applicare il seguente [https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=670681;att=1;filename=postgrey_1.34-1.2_1.34-1.3.debdiff;msg=75 patch], perchè con lo script di init standard, lo stop ed il restart non funzionano ([https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=670681 #670681 - postgrey: stop action of init script does not work - Debian Bug report logs])
vi /tmp/postgrey.net
<pre>
--- postgrey-1.34/debian/postgrey.init
+++ postgrey-1.34/debian/postgrey.init
@@ -51,7 +51,7 @@
  start)
log_daemon_msg "Starting $DESC" "$NAME"
if start-stop-daemon --start --oknodo --quiet \
- --pidfile $PIDFILE --name $NAME \
+ --pidfile $PIDFILE \
--startas $DAEMON -- $POSTGREY_OPTS "$POSTGREY_TEXT_OPT"
then
    log_end_msg 0
@@ -63,7 +63,7 @@
  stop)
log_daemon_msg "Stopping $DESC" "$NAME"
if start-stop-daemon --stop --oknodo --quiet \
- --pidfile $PIDFILE --name $NAME
+ --pidfile $PIDFILE
then
    log_end_msg 0
else
@@ -75,7 +75,7 @@
  reload|force-reload)
log_action_begin_msg "Reloading $DESC configuration..."
if start-stop-daemon --stop --signal 1 --quiet \
- --pidfile $PIDFILE --name $NAME
+ --pidfile $PIDFILE
then
    log_action_end_msg 0
else
</pre>
sudo patch -p0 /etc/init.d/postgrey < /tmp/postgrey.new


== Configurazione di Postfix ==
== Configurazione di Postfix ==

Revision as of 13:54, 8 June 2016

Il Greylisting dice ad un mail server di rimandare la mail dopo un tot di tempo.

Se la mail è mandata da un vero mailserver, allora la rimanderà, se invece è mandata da uno spammer, difficilmente la rimanderà.

Installazione

Debian Squeeze

Assicurarsi che ci sia il repository ex volatile:

sudoedit /etc/apt/sources.list
...
deb http://ftp.it.debian.org/debian	squeeze-updates main contrib non-free
deb http://security.debian.org/		squeeze/updates main contrib non-free
...

Debian >= Wheezy

  • Il pacchetto è standard:
sudo apt-get update

Installare postgrey:

sudo apt-get install postgrey
  • Impostare il ritardo per accettare mail a 60 secondi:
sudo sed -i 's@^POSTGREY_OPTS="--inet=10023"@POSTGREY_OPTS="--inet=10023 --delay=60"@' /etc/default/postgrey
echo "352c8a6936542f0933eb14e9cc5142bf  /etc/default/postgrey" | md5sum --check
vi /tmp/postgrey.net
--- postgrey-1.34/debian/postgrey.init
+++ postgrey-1.34/debian/postgrey.init
@@ -51,7 +51,7 @@
   start)
 	log_daemon_msg "Starting $DESC" "$NAME"
 	if start-stop-daemon --start --oknodo --quiet \
-		--pidfile $PIDFILE --name $NAME \
+		--pidfile $PIDFILE \
 		--startas $DAEMON -- $POSTGREY_OPTS "$POSTGREY_TEXT_OPT"
 	then
 	    log_end_msg 0
@@ -63,7 +63,7 @@
   stop)
 	log_daemon_msg "Stopping $DESC" "$NAME"
 	if start-stop-daemon --stop --oknodo --quiet \
-		--pidfile $PIDFILE --name $NAME
+		--pidfile $PIDFILE
 	then
 	    log_end_msg 0
 	else
@@ -75,7 +75,7 @@
   reload|force-reload)
 	log_action_begin_msg "Reloading $DESC configuration..."
 	if start-stop-daemon --stop --signal 1 --quiet \
-		--pidfile $PIDFILE --name $NAME
+		--pidfile $PIDFILE
 	then
 	    log_action_end_msg 0
 	else
sudo patch -p0 /etc/init.d/postgrey < /tmp/postgrey.new

Configurazione di Postfix

Aggiungere questa limitazione alle altre:

sudoedit /etc/postfix/main.cf
...
smtpd_recipient_restrictions =  permit_mynetworks
                                permit_sasl_authenticated
                                reject_unauth_destination
                                check_sender_access hash:/etc/postfix/whitelist
                                reject_rbl_client sbl.spamhaus.org
                                reject_rbl_client cbl.abuseat.org
                                check_policy_service inet:127.0.0.1:10023

Se non esiste già una whitelist di postfix, crearne una vuota:

sudo touch /etc/postfix/whitelist; sudo postmap /etc/postfix/whitelist

Riavviare Postgrey

sudo invoke-rc.d postgrey restart

Verificare che sia attivo:

pstree -p | grep postgr
        ??postgrey(18956)

Riavviare postfix:

sudo invoke-rc.d postfix restart

Whitelist di client da non greylistare

Se si vuole evitare che determinati mailserver siano greylistati, inserire l'ip del mail server da whitelistare nel file

/etc/postgrey/whitelist_clients.local

Riavviare postgrey

sudo invoke-rc.d postgrey restart

Verifica Funzionamento

Guardare i Log:

sudo tail -f /var/log/mail.log
...
Jul 24 15:23:33 ergo postgrey[18546]: Process Backgrounded
Jul 24 15:23:33 ergo postgrey[18546]: 2006/07/24-15:23:33 postgrey (type Net::Server::Multiplex) starting! pid(18546)
Jul 24 15:23:33 ergo postgrey[18546]: Binding to TCP port 60000 on host 127.0.0.1
Jul 24 15:23:33 ergo postgrey[18546]: Setting gid to "65534 65534"
Jul 24 15:23:33 ergo postgrey[18546]: Setting uid to "104"
...
Jul 24 15:28:22 ergo postfix/smtpd[18945]: NOQUEUE: reject: RCPT from host65-14.pool8253.interbusiness.it[82.53.14.65]: 450 <carlos.casuscelli@marzottospa.com>: Recipient address rejected: Greylisted for 300 seconds (see http://isg.ee.ethz.ch/tools/postgrey/help/marzottospa.com.html); from=<demay@accordfinancialus.com> to=<carlos.casuscelli@marzottospa.com> proto=SMTP helo=<accordfinancialus.com>
Jul 24 15:28:22 ergo postfix/smtpd[18945]: lost connection after RCPT from host65-14.pool8253.interbusiness.it[82.53.14.65]
...

Reportistica

Per ottenere un report dei messaggi bloccati (farlo dopo un giorno) :

sudo cat /var/log/mail.log | sudo postgreyreport | less

Gestione Eccezioni

Caso di backup MX protetti da greylist

Se i backup MX sono già protetti da Greylisting, ci si può fidare delle mail provenienti da loro, e si può inserire il loro indirizzo ip (si può inserire il nome se il loro PTR è quello corrispondente al nome dell'mx) in /etc/postgrey/whitelist_clients, e riavviare postgrey.

Questo oneliner va bene per etch:

for MX in $(host -t mx galimberti.net | sort | cut -f 7 --delim=' '); do host $MX; done | grep 'has address' | cut -f 4 --delim=' '

Questo comando lo fa (sostituire il nome del dominio):


cat > /tmp/addmx <<'EOFile'
#!/bin/bash
echo "# Whitelist Greylisted MX" >> /etc/postgrey/whitelist_clients.local
if [ ! -e /usr/bin/host ]; then
 echo host not installed.
 if ! apt-get install bind9-host; then
  failed to install bind9-host
  exit 1
 fi
fi
if [ "$(host --version 2>/dev/null)" == "host version 991529" ]; then
 echo using host
 for NAME in $(host -t mx rvmgroup.it | cut -c 25- | sort -n | cut -c 4-)
  do
   host $NAME |  cut -c 24- >> /etc/postgrey/whitelist_clients.local
  done
else
 echo using bind9-host. You are on your own....
 #for NAME in $(host -t mx rvmgroup.it | cut --delim=" " -f 6- | sort -n | cut --delim=" " -f 2)
 # do
 #  host $NAME |  cut --delim=" " -f 7 >> /etc/postgrey/whitelist_clients.local
 # done
fi
EOFile
chmod 755 /tmp/addmx
/tmp/addmx
rm /tmp/addmx

Riferimenti