Greylisting con postfix come tecnica antispam: Difference between revisions

From RVM Wiki
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 4: Line 4:


== Installazione ==
== Installazione ==
 
===Debian Squeeze===
Abilitare il repository volatile:
Assicurarsi che ci sia il repository ex volatile:
sudoedit /etc/apt/sources.list
<pre>
<pre>
cat | sudo tee -a /etc/apt/sources.list <<EOFile
...
deb http://volatile.debian.net/debian-volatile etch/volatile main contrib non-free
deb http://ftp.it.debian.org/debian squeeze-updates main contrib non-free
EOFile
deb http://security.debian.org/ squeeze/updates main contrib non-free
...
</pre>
</pre>
===Debian >= Wheezy===
* Il pacchetto è standard:


  sudo apt-get update
  sudo apt-get update
Line 17: Line 22:


  sudo apt-get install 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
* 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 ==
Line 22: Line 69:
Aggiungere questa limitazione alle altre:
Aggiungere questa limitazione alle altre:


'''check_policy_service inet:127.0.0.1:60000'''
sudoedit /etc/postfix/main.cf
 
<pre>
<pre>
vi /etc/postfix/main.cf
...
...
smtpd_recipient_restrictions =  permit_mynetworks
smtpd_recipient_restrictions =  permit_mynetworks
Line 31: Line 76:
                                 reject_unauth_destination
                                 reject_unauth_destination
                                 check_sender_access hash:/etc/postfix/whitelist
                                 check_sender_access hash:/etc/postfix/whitelist
                                reject_rbl_client relays.ordb.org
                                reject_rbl_client nsbl.njabl.org
                                 reject_rbl_client sbl.spamhaus.org
                                 reject_rbl_client sbl.spamhaus.org
                                 reject_rbl_client cbl.abuseat.org
                                 reject_rbl_client cbl.abuseat.org
                                 check_policy_service inet:127.0.0.1:60000
                                 check_policy_service inet:127.0.0.1:10023
</pre>
</pre>


Line 44: Line 87:
Riavviare Postgrey
Riavviare Postgrey


  sudo /etc/init.d/postgrey restart
  sudo invoke-rc.d postgrey restart


Verificare che sia attivo:
Verificare che sia attivo:
Line 54: Line 97:


Riavviare postfix:
Riavviare postfix:
  sudo /etc/init.d/postfix restart
  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
 
* In particolare è necessaria whitelistare i server di Outlook365, perchè cambiano ogni volta che inviano nuovamente la mail. Se non si whitelistano, si possono verificare notevoli ritardi di recapito:
 
<pre>
# outlook365
#https://talk.plesk.com/threads/greylist-high-delays-from-protecction-outlook-com-and-others.334180/
#https://technet.microsoft.com/en-us/library/dn163583(v=exchg.150).aspx
 
/.*outbound.protection.outlook.com$/
23.103.132.0/22
23.103.136.0/21
23.103.144.0/20
23.103.198.0/23
23.103.200.0/21
40.92.0.0/14
40.107.0.0/16 
65.55.88.0/24
65.55.169.0/24
94.245.120.64/26
104.47.0.0/17
134.170.101.0/24
134.170.140.0/24
134.170.171.0/24
157.55.133.0/25
157.56.87.192/26
157.56.110.0/23
157.56.112.0/24
157.56.116.0/25
157.56.120.0/25
207.46.51.64/26
207.46.100.0/24
207.46.108.0/25
207.46.163.0/24
213.199.154.0/24
213.199.180.128/26
216.32.180.0/23
</pre>
 
* Aggiungere anche gli smtp di Amazon:
dig TXT amazonses.com +short| grep 'v=spf1'
 
<pre>
# https://sesblog.amazon.com/post/TxEH4YOF3YJG0L/Amazon-SES-IP-addresses
199.255.192.0/22
199.127.232.0/22
54.240.0.0/18
</pre>
 
*Riavviare postgrey
sudo invoke-rc.d postgrey restart


== Verifica Funzionamento ==
== Verifica Funzionamento ==
Line 83: Line 182:
=== Caso di backup MX protetti da greylist ===
=== 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.
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):
Questo comando lo fa (sostituire il nome del dominio):
Line 90: Line 192:
cat > /tmp/addmx <<'EOFile'
cat > /tmp/addmx <<'EOFile'
#!/bin/bash
#!/bin/bash
echo "# Whitelist Greylisted MX" >> /etc/postgrey/whitelist_clients
echo "# Whitelist Greylisted MX" >> /etc/postgrey/whitelist_clients.local
if [ ! -e /usr/bin/host ]; then
if [ ! -e /usr/bin/host ]; then
  echo host not installed.
  echo host not installed.
Line 102: Line 204:
  for NAME in $(host -t mx rvmgroup.it | cut -c 25- | sort -n | cut -c 4-)
  for NAME in $(host -t mx rvmgroup.it | cut -c 25- | sort -n | cut -c 4-)
   do
   do
   host $NAME |  cut -c 24- >> /etc/postgrey/whitelist_clients
   host $NAME |  cut -c 24- >> /etc/postgrey/whitelist_clients.local
   done
   done
else
else
Line 108: Line 210:
  #for NAME in $(host -t mx rvmgroup.it | cut --delim=" " -f 6- | sort -n | cut --delim=" " -f 2)
  #for NAME in $(host -t mx rvmgroup.it | cut --delim=" " -f 6- | sort -n | cut --delim=" " -f 2)
  # do
  # do
  #  host $NAME |  cut --delim=" " -f 7 >> /etc/postgrey/whitelist_clients
  #  host $NAME |  cut --delim=" " -f 7 >> /etc/postgrey/whitelist_clients.local
  # done
  # done
fi
fi

Latest revision as of 13:07, 7 October 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
  • In particolare è necessaria whitelistare i server di Outlook365, perchè cambiano ogni volta che inviano nuovamente la mail. Se non si whitelistano, si possono verificare notevoli ritardi di recapito:
# outlook365
#https://talk.plesk.com/threads/greylist-high-delays-from-protecction-outlook-com-and-others.334180/
#https://technet.microsoft.com/en-us/library/dn163583(v=exchg.150).aspx

/.*outbound.protection.outlook.com$/
23.103.132.0/22
23.103.136.0/21
23.103.144.0/20
23.103.198.0/23
23.103.200.0/21
40.92.0.0/14
40.107.0.0/16  
65.55.88.0/24
65.55.169.0/24
94.245.120.64/26
104.47.0.0/17
134.170.101.0/24
134.170.140.0/24
134.170.171.0/24
157.55.133.0/25
157.56.87.192/26
157.56.110.0/23
157.56.112.0/24
157.56.116.0/25
157.56.120.0/25
207.46.51.64/26
207.46.100.0/24
207.46.108.0/25
207.46.163.0/24
213.199.154.0/24
213.199.180.128/26
216.32.180.0/23
  • Aggiungere anche gli smtp di Amazon:
dig TXT amazonses.com +short| grep 'v=spf1'
# https://sesblog.amazon.com/post/TxEH4YOF3YJG0L/Amazon-SES-IP-addresses
199.255.192.0/22
199.127.232.0/22
54.240.0.0/18
  • 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