Protezione antispam con DK DKIM e SPF in postfix
Verificato con Debian Bookworm
- Esportare il nome del dominio per facilitare la configurazione:
export OPENDKIM_DOMAIN=example.com.com
- Installare i pacchetti e sistemare l'utenza:
apt install opendkim opendkim-tools
gpasswd -a postfix opendkim
- Impostare i parametri non di default:
vi /etc/opendkim.conf
# Aggiungere all'inizio Logwhy yes # Decommentare Canonicalization relaxed/simple Mode sv SubDomains no UserID opendkim # Aggiungere alla fine AutoRestart yes AutoRestartRate 10/1M Background yes DNSTimeout 5 SignatureAlgorithm rsa-sha256 KeyTable refile:/etc/opendkim/key.table SigningTable refile:/etc/opendkim/signing.table ExternalIgnoreList /etc/opendkim/trusted.hosts InternalHosts /etc/opendkim/trusted.hosts
* Creare directories: <pre> mkdir /etc/opendkim && \ mkdir /etc/opendkim/keys && \ chown -R opendkim:opendkim /etc/opendkim && \ chmod go-rw /etc/opendkim/keys
- Creare la signing table:
cat > /etc/opendkim/signing.table <<EOFile *@$OPENDKIM_DOMAIN default._domainkey.$OPENDKIM_DOMAIN *@*.$OPENDKIM_DOMAIN default._domainkey.$OPENDKIM_DOMAIN EOFile
- Verificare:
cat /etc/opendkim/signing.table
*@example.com default._domainkey.example.com *@*.example.com default._domainkey.example.com
- Creare la key table:
cat > /etc/opendkim/key.table <<EOFile default._domainkey.$OPENDKIM_DOMAIN $OPENDKIM_DOMAIN:default:/etc/opendkim/keys/$OPENDKIM_DOMAIN/default.private EOFile
- Verificare:
cat /etc/opendkim/key.table
default._domainkey.example.com example.com:default:/etc/opendkim/keys/example.com/default.private
- Impostare gli host per cui non va fatto il signing:
cat > /etc/opendkim/trusted.hosts <<EOFile 127.0.0.1 localhost .$OPENDKIM_DOMAIN EOFile
- Verificare
cat /etc/opendkim/trusted.hosts
127.0.0.1 localhost .example.com
- Creare le keys:
mkdir /etc/opendkim/keys/$OPENDKIM_DOMAIN && \ opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/$OPENDKIM_DOMAIN -s default -v && \ chown opendkim:opendkim /etc/opendkim/keys/$OPENDKIM_DOMAIN/default.private && chmod 600 /etc/opendkim/keys/$OPENDKIM_DOMAIN/default.private && \ echo OK
- Creare la stringa per il record DNS:
- Verificarla
cat /etc/opendkim/keys/$OPENDKIM_DOMAIN/default.txt
The public key is the string that starts P= The public key is separated into two parts. Notice how in the console I have copied the first part between the ” ” and then the second part of the public key between the ” “. I added the two strings together without the ” ” to make one long string. Copy the entire lines as I have. Next in your DNS manager, you need to create a TXT record with the name default._domainkey and in the content the public key you copied above. p=MIIBIjANBgkqhklG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy6v89vlMhAtaFd4BFAPn1y2FDCsZAZugRCMMM1fRDRm3TKDU51aIJ9RKu4FJdR/7JF2BAglyBn5l24lk8MIGYPfjgJxxPWBiS/GcIwudkU/jdeQbTl9Ws90u4fRtIgx/1B1ywR7lTvTSYnWlzBUvHsaFaY800TIlT9DJSnP6PXxXMnbyCqGsYOAt4VbQZxgk9HS0JXN78f12oGdXQ7IupGUfJICzPFvbJVnH6l7ectJtEz2652OFovOp+gLT9Vr/b/vora+wknYSZvapw9iKSoxvT/nQQSN6yBtlTM/HglJt7tUwzCuGd5Njmr6wNTx5sOKQ3xgJymbtpmJm0Ho4/QIDAQAB
- Creare il socket per postfix:
mkdir /var/spool/postfix/opendkim && \ chown opendkim:postfix /var/spool/postfix/opendkim
- Impostarlo nell configurazione (commentare e decommentare)
vi /etc/opendkim.conf
Socket local:/var/spool/postfix/opendkim/opendkim.sock
- Impostarlo nel default (sostituire)
vi /etc/default/opendkim
SOCKET="local:/var/spool/postfix/opendkim/opendkim.sock"
Configurazione di POstfix
vi /etc/postfix/main.cf
milter_default_action = accept milter_protocol = 6 smtpd_milters = local:opendkim/opendkim.sock non_smtpd_milters = $smtpd_milters
systemctl restart opendkim.service; systemctl status opendkim.service
systemctl restart postfix ; systemctl status postfix
opendkim-testkey -d example.com -s default -vvv
opendkim-testkey: using default configfile /etc/opendkim.conf opendkim-testkey: checking key 'default._domainkey.example.com' opendkim-testkey: key not secure opendkim-testkey: key OK
the line saying Key not secure is perfectly normal.
Verificare con https://mxtoolbox.com
Riferimenti
Vedi nuovi link
- Part 4: Set Up SPF & DKIM with Postfix on Debian Server - LinuxBabe
- Leasons learned from implementing DMARC(SPF/DKIM) on multiple domains - If You Do .NET
- Getting DKIM, DMARC and SPF to work with Postfix, OpenDKIM and OpenDMARC - petermolnar.net
- Creating DMARC Record to Protect Your Domain Name From Email Spoofing
- How to eliminate spam and protect your name with DMARC | skelleton.net
- Debian: Postfix + DMARC – Cyberfront News Blog
- 7 Effective Tips to Stop Your Emails Being Marked as Spam - LinuxBabe
DKIM
Domain Keys Identified Mail si occupa di firmare le mail in uscita.
Si possono scegliere due alternative:
- opendkim: disponibile da squeeze, attualmente mantenuto
- dkim-filter: disponibile solo in squeeze, e deprecato (rifiuta alcuni messaggi di gmail
opendkim
- Installare il pacchetto
sudo apt-get install opendkim
- Creare la directory per le chiavi
sudo mkdir -p /etc/opendkim/keys sudo chown -R opendkim:opendkim /etc/opendkim sudo chmod -R go-wrx /etc/opendkim/keys
- Generare le chiavi per il dominio:
sudo -u opendkim mkdir -p /etc/opendkim/keys/example.com sudo -u opendkim opendkim-genkey -D /etc/opendkim/keys/example.com -d example.com -s mymailserver
- Le chiavi create saranno quindi:
sudo ls -al /etc/opendkim/keys/example.com
total 16 drwxr-xr-x 2 opendkim opendkim 4096 May 10 12:23 . drwx------ 4 opendkim opendkim 4096 May 10 12:22 .. -rw------- 1 opendkim opendkim 887 May 10 12:23 mymailserver.private -rw------- 1 opendkim opendkim 308 May 10 12:23 mymailserver.txt
- Modificare il file di configurazione:
sudoedit /etc/opendkim.conf
... UMask 022 #... Canonicalization relaxed/relaxed Mode sv #... ExternalIgnoreList refile:/etc/opendkim/TrustedHosts InternalHosts refile:/etc/opendkim/TrustedHosts KeyTable refile:/etc/opendkim/KeyTable LogWhy Yes PidFile /var/run/opendkim/opendkim.pid SigningTable refile:/etc/opendkim/SigningTable Socket inet:8891@localhost SyslogSuccess Yes TemporaryDirectory /var/tmp UserID opendkim:opendkim
- Definire ora l'utilizzo delle chiavi per i singoli domini:
sudoedit -u opendkim /etc/opendkim/KeyTable
mymailserver._domainkey.example.com example.com:mymailserver:/etc/opendkim/keys/example.com/mymailserver.private
- Definire ora come saranno usate le chiavi per la firma:
sudoedit -u opendkim /etc/opendkim/SigningTable
*@example.com mymailserver._domainkey.example.com
- Dichiarare gli host interni ed i server relay:
sudoedit -u opendkim /etc/opendkim/TrustedHosts
127.0.0.1 ::1 localhost server2.example.com server1.example.com
- Riavviare il demone
sudo invoke-rc.d opendkim restart
- Verificare che non ci siano errori:
sudo tail -f /var7log/mail.log
May 10 14:32:58 mymailserver opendkim[26101]: OpenDKIM Filter v2.0.1 starting (args: -x /etc/opendkim.conf -u opendkim -P /var/run/opendkim/opendkim.pid)
- Verificare che il socket sia in ascolto:
sudo netstat -anp | grep 8891
tcp 0 0 127.0.0.1:8891 0.0.0.0:* LISTEN 28301/opendkim
Configurazione Postfix per opendkim
- Dichiarare l'utilizzo del filtro per postfix:
sudoedit /etc/postfix/main.cf
smtpd_milters = inet:127.0.0.1:8891 non_smtpd_milters = $smtpd_milters milter_default_action = accept
- Riavviare postfix
sudo invoke-rc.d postfix restart
Riferimenti
- Re: [dkim-milter-discuss internal error from libdkim: ar_addquery() failed]
dkim-filter
QUESTO PACCHETTO È DEPRECATO E NON SUPPORTATO
- Installare il pacchetto
sudo apt-get install dkim-filter
- Preparare le directory per i socket
sudo mkdir -p /var/spool/postfix/milter/dkim-filter sudo chown dkim-filter.dkim-filter /var/spool/postfix/milter/dkim-filter sudo chmod 0755 /var/spool/postfix/milter/dkim-filter
- Generare le chiavi per il dominio:
sudo mkdir -p /etc/mail/dkim/keys/example.com cd /etc/mail/dkim/keys/example.com sudo dkim-genkey -r -d example.com
- Linkare la chiave al nome DNS del server di uscita delle email del dominio da proteggere, che ad esempio è mymailserver.example.com
sudo ln -s default.private mymailserver
- Dichiarare ora quel chiave usare per firmare le email in uscita per il dominio:
sudoedit /etc/dkim-keys.conf
*@example.com:example.com:/etc/mail/dkim/keys/example.com/mymailserver
- Dichiarare quali indirizzi ip possono spedire mail da questo server senza essere soggette a limitazioni DKIM (in pratica se ci sono server che fanno relay tramite questo). Se non ce ne sono, lasciare solo i localhost:
sudoedit /etc/dkim-internalhosts.conf
127.0.0.1 ::1 localhost server2.example.com server1.example.com
- Includere quindi questi dati nella configurazione:
sudoedit /etc/dkim-filter.conf
- Scommenatre
KeyList /etc/dkim-keys.conf
- Aggiungere
InternalHosts /etc/dkim-internalhosts.conf
- Modificare la configurazione del socket in caso di Postfix in chroot
sudoedit /etc/default/dkim-filter
SOCKET="local:/var/spool/postfix/milter/dkim-filter/dkim-filter.sock"
- Creare le directory con le corrette permission:
sudo mkdir -p /var/spool/postfix/milter/dkim-filter sudo chown dkim-filter.dkim-filter /var/spool/postfix/milter/dkim-filter sudo chmod 0755 /var/spool/postfix/milter/dkim-filter
- Aggiungere postfix al gruppo dkim-filter, per poter accedere ai socket:
sudo gpasswd -a postfix dkim-filter
- Riavviare il demone
sudo invoke-rc.d dkim-filter restart
Configurazione Postfix per dkim-filter
- Dichiare il milter DKIM per l'uso in postfix, aggiugendolo agli esistenti (se presenti):
sudoedit /etc/postfix/main.cf
smtpd_milters =
unix:milter/dkim-filter/dkim-filter.sock
non_smtpd_milters =
unix:milter/dkim-filter/dkim-filter.sock
milter_default_action = accept
- Riavviare Postfix
sudo invoke-rc.d postfix restart
mkdir -p /var/spool/postfix/milter/dk-filter
chown dk-filter.dk-filter /var/spool/postfix/milter/dk-filter
chmod 2755 /var/spool/postfix/milter/dk-filter
Configurazione DNS per DKIM
- Affinchè i mail server riceventi possano verificare la'utenticità della firma DKIM, occorre pubblicarla via DNS con un record TXT
- Il record di esempio per dkim-filter di trova in:
sudo cat /etc/mail/dkim/keys/example.com/default.txt
- Il record di esempio per opendkim di trova in:
sudo cat /etc/opendkim/keys/example.com/mymailserver.txt
default._domainkey IN TXT "v=DKIM1; g=*; k=rsa; p=MIGtsggGhShGhIh3hQEhsdUAddGdAdCdidKdggDgUd9GdgvYdgZgqqU+1vCAOitBjBLN55JCqdNdggdcgdigJdmgejy4xpRsfKWd8iJyf/pti5ps6MQdghg7dgrdSgdHgdXgdag0mBGG509MnxZFgYnWC4Ja8W6vQD0mD2KEFenymgd7AqZh6qGR/LRzUDLGts/CUtqXkUtgotb3rwIDAQAB" ; ----- DKIM default for example.com
- Per formattare il record da inserire nella zona bind. cambiare default con il nome del mailserver utilizzato:
mymailserver._domainkey IN TXT "v=DKIM1; g=*; k=rsa; p=MIGtsggGhShGhIh3hQEhsdUAddGdAdCdidKdggDgUd9GdgvYdgZgqqU+1vCAOitBjBLN55JCqdNdggdcgdigJdmgejy4xpRsfKWd8iJyf/pti5ps6MQdghg7dgrdSgdHgdXgdag0mBGG509MnxZFgYnWC4Ja8W6vQD0mD2KEFenymgd7AqZh6qGR/LRzUDLGts/CUtqXkUtgotb3rwIDAQAB" ; ----- DKIM default for example.com
- Aggiungere il flag t=y; per indicare che le mail firmate e non firmate vanno trattate nello stesso modo:
mymailserver._domainkey IN TXT "v=DKIM1; g=*; k=rsa; t=y; p=MIGtsggGhShGhIh3hQEhsdUAddGdAdCdidKdggDgUd9GdgvYdgZgqqU+1vCAOitBjBLN55JCqdNdggdcgdigJdmgejy4xpRsfKWd8iJyf/pti5ps6MQdghg7dgrdSgdHgdXgdag0mBGG509MnxZFgYnWC4Ja8W6vQD0mD2KEFenymgd7AqZh6qGR/LRzUDLGts/CUtqXkUtgotb3rwIDAQAB" ; ----- DKIM default for example.com
- Questa ottenuta è la stringa da aggiungere nel file di zona.
- Per testare se è configurata correttamente, fare la query:
host -t txt mymailserver._domainkey.example.com
- Deve ritornare
mymailserver._domainkey.example.com descriptive text "v=DKIM1; g=*; k=rsa; t=y; p=MIGtsggGhShGhIh3hQEhsdUAddGdAdCdidKdggDgUd9GdgvYdgZgqqU+1vCAOitBjBLN55JCqdNdggdcgdigJdmgejy4xpRsfKWd8iJyf/pti5ps6MQdghg7dgrdSgdHgdXgdag0mBGG509MnxZFgYnWC4Ja8W6vQD0mD2KEFenymgd7AqZh6qGR/LRzUDLGts/CUtqXkUtgotb3rwIDAQAB"
- Per testare, è possibile usare i servizi:
Abilitazione definitiva DKIM
- Quando si è sicuri che tutta la mail sarà firmata dal server DKIM, si può togliere dal record TXT il parametro t=y. IN QUESTO MODO TUTTA LA MAIL NON FIRMATA VERRÀ RIFIUTATA DAI SERVER CHE USANO DKIM
DK
- Domain Keys è obsoleto. Vedi syslog - Postfix + DK (DomainKeys) + DKIM + SPF on Debian 6/Ubuntu
SPF
- SPF indica quali sono i mailserver ufficiali per l'invio di mail di un determinato dominio. Una specie di MX al contrario.
Check mail in ingresso
- ATTENZIONE: tutti i server che fanno relay a quello dove si riceve la mail, COMPRESI I BACKUP MX, devono avere già un check SPF, perchè verranno whitelistati. Se i relay servr o i backup mx non vengono whitelistati, tutte le mail ricevute tramite loro, non passeranno SPF. Se vengono whitelistati, ma non hanno un controllo SPF, allora si riceveranno mail di spam
- Installare il pacchetto:
sudo apt-get install postfix-policyd-spf-python
- Whitelistare i backup MX o i server che mandano mail a questo in relay
sudoedit /etc/postfix-policyd-spf-python/policyd-spf.conf Whitelist = 1.2.3.4, 5.6.7.8, 9.10.11.12
- Dichiarare la regola di utilizzo SPF PRIMA DI QUELLE DNSBL e DOPO LA reject_unauth_destination
sudoedit /etc/postfix/main.cf
smtpd_recipient_restrictions = #... # reject_unauth_destination, #... check_policy_service unix:private/policyd-spf, #... # reject_rbl_client sbl.spamhaus.org,
- Aggiungere anche:
policyd-spf_time_limit = 3600
- Definire il daemon in postfix:
sudoedit /etc/postfix/master.cf
policyd-spf unix - n n - 0 spawn
user=nobody argv=/usr/bin/python /usr/bin/policyd-spf /etc/postfix-policyd-spf-python/policyd-spf.conf
- Riavviare postfix
sudo invoke-rc.d postfix restart
Configurazione DNS per SPF
- Non serve configurare nulla sul mailserver che invia la mail, ma è solo necessario configurare il record TXT che dice come comportarsi:
@ IN TXT "v=spf1 mx a:mymailserver.example.com ~all"
- @ means default for the domain (ie. when you lookup the base domain), but you can as easily specify the record for subdomains.
- v=spf1 identifies it as an SPF record and gives the version.
- mx says that mail could come from a machine matching the MX records for your domain. For smaller domains this is often all that is needed.
- a specifies an A or AAAA record where mail may come from. This may be an outbound-only mail relay, a security applicance, a webserver that mails customers directly or perhaps a marketing company's systems who sends out mail blasts on your behalf.
- ~all says that all other sources should soft fail (retryable failure, useful for testing). This can also be -all meaning to fail (reject/bounce) other sources, ?all meaning to ignore the policy (again usful for testing), and +all meaning to accept all others which is probably not a good idea. With the a, mx, etc. the + is implied - ie. saying mx really means +mx. You can find much more on this syntax at: http://www.openspf.org/SPF_Record_Syntax
- Verificare che il record sia presente con:
host -t txt example.com
example.com descriptive text "v=spf1 mx a:mailserver.example.com ~all"
- Il test è fattibile con *SPF Test - SPF Verify - SPF Validator - Sender ID