93 lines
		
	
	
	
		
			3.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
	
		
			3.6 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| +++
 | |
| images = ['images/blog/encrypt-compress-emails-on-server.jpg']
 | |
| tags = ['Security','Server','Privacy', 'Dovecot']
 | |
| categories= ['Soluzioni']
 | |
| date = '2022-07-08'
 | |
| lastmod = '2022-07-08'
 | |
| title = 'Cifrare e comprimere le email lato server'
 | |
| slug = 'cifrare-comprimere-email-server'
 | |
| +++
 | |
| 
 | |
| Per migliorare la sicurezza e la privacy degli utenti e risparmiare spazio su disco, il mail server
 | |
| [Dovecot](https://www.dovecot.org/) permette di cifrare e comprimere i files che contengono la posta elettronica.
 | |
| 
 | |
| <!--more-->
 | |
| La compressione avviene tramite il plugin ```zlib``` mentre la cifratura tramite il plugin ```mail_crypt```.
 | |
| 
 | |
| ```bash
 | |
| mail_plugins = $mail_plugins zlib mail_crypt
 | |
| ```
 | |
| 
 | |
| I due plugin possono essere poi configurati con diversi opzioni
 | |
| 
 | |
| ```bash
 | |
| plugin {
 | |
|   mail_crypt_global_private_key = </etc/dovecot/crypt/master.key
 | |
|   mail_crypt_global_public_key = </etc/dovecot/crypt/master.pub
 | |
|   mail_crypt_curve = prime256v1
 | |
|   mail_crypt_save_version = 2
 | |
|   zlib_save_level = 6
 | |
|   zlib_save = lz4
 | |
| }
 | |
| ```
 | |
| Per la cifratura è inoltre necessario generare la coppia di chiavi : privata (```master.key```) per cifrare e pubblica (```master.pub```) per decifrare.
 | |
| 
 | |
| In questo modo nel caso vi fosse un'intrusione sul server ed i files delle email venissero trafugati, sarebbero illeggibili senza la chiave privata di decodifica.
 | |
| 
 | |
| Dal momento in cui la codifica e la compressione vengono attivate, tutti i nuovi messaggi verranno automaticamente cifrati e compressi in modo 
 | |
| completamente trasparente per l'utente finale.
 | |
| 
 | |
| Per cifrare e comprimere la posta preesistente è sufficiente spostare i messaggi da una cartella all'altra tramite un client IMAP.
 | |
| In alternativa, con il seguente script bash è possibile avviare la cifratura di tutti i files di posta nella directory di esempio ```/var/vmail/domain/user/Maildir``` (la compressione non è possibile).
 | |
| 
 | |
| ```bash
 | |
| find /var/vmail/domain/user/Maildir -type f -regextype egrep -regex '.*S=.*W=.*' | while read -r file; do
 | |
| if [[ $(head -c7 "$file") != "CRYPTED" ]]; then
 | |
| echo $file
 | |
| doveadm fs put crypt private_key_path=/etc/dovecot/crypt/master.key:public_key_path=/etc/dovecot/crypt/master.pub:posix:prefix=/ \
 | |
|   "$file" "$file"
 | |
|   chmod 600 "$file"
 | |
|   chown vmail:vmail "$file"
 | |
| fi
 | |
| done
 | |
| ```
 | |
| 
 | |
| Nel caso invece fosse necessario accedere ad uno o più files di email in chiaro, si possono utilizzare i seguenti scripts :
 | |
| 
 | |
| Per decifrare solamente (nel caso in cui i files non siano stati compressi)
 | |
| 
 | |
| ```bash
 | |
| find /var/vmail/domain/user/Maildir -type f -regextype egrep -regex '.*S=.*W=.*' | while read -r file; do
 | |
| if [[ $(head -c7 "$file") == "CRYPTED" ]]; then
 | |
|   echo $file
 | |
|   doveadm fs get crypt private_key_path=/etc/dovecot/crypt/master.key:public_key_path=/etc/dovecot/crypt/master.pub:posix:prefix=/ \
 | |
|   "$file" > "/tmp/$(basename "$file")"
 | |
|   if [[ -s "/tmp/$(basename "$file")" ]]; then
 | |
|     chmod 600 "/tmp/$(basename "$file")"
 | |
|     chown vmail:vmail "/tmp/$(basename "$file")"
 | |
|     mv "/tmp/$(basename "$file")" "$file"
 | |
|   else
 | |
|     rm "/tmp/$(basename "$file")"
 | |
|   fi
 | |
| fi
 | |
| done
 | |
| ```
 | |
| 
 | |
| Per decifrare e decomprimere :
 | |
| 
 | |
| ```bash
 | |
| find /var/vmail/domain/user/Maildir -type f -regextype egrep -regex '.*S=.*W=.*' | while read -r file; do
 | |
| if [[ $(head -c7 "$file") == "CRYPTED" ]]; then
 | |
|   echo $file
 | |
|   doveadm fs get compress lz4:0:crypt:private_key_path=/etc/dovecot/crypt/master.key:public_key_path=/etc/dovecot/crypt/master.pub:posix:prefix=/ \
 | |
|   "$file" > "/tmp/$(basename "$file")"
 | |
|   if [[ -s "/tmp/$(basename "$file")" ]]; then
 | |
|     chmod 600 "/tmp/$(basename "$file")"
 | |
|     chown vmail:vmail "/tmp/$(basename "$file")"
 | |
|     mv "/tmp/$(basename "$file")" "$file"
 | |
|   else
 | |
|     rm "/tmp/$(basename "$file")"
 | |
|   fi
 | |
| fi
 | |
| done
 | |
| ```
 | 
