Skip to content

TP7 : Sécurisation du service mail

PasRP-Theo edited this page Jun 10, 2025 · 15 revisions

TP7 : Sécurisation du service mail

Introduction

Ce TP vise à mettre en place et sécuriser un service mail complet utilisant Docker Mailserver (DMS) avec authentification de domaine, chiffrement TLS et filtrage anti-spam.

Environnement de travail et organisation

Répartition des services

  • VPS Principal : Hébergement du service mail (mail.l2-2.ephec-ti.be)
  • IP du serveur : 54.36.181.95
  • Domaine utilisé : ephec-ti.be
  • Container : Docker Mailserver (DMS) avec Postfix/Dovecot

Le service mail est isolé dans un container Docker pour faciliter la maintenance et la sécurité.

1. Mise en place du service mail

Configuration DNS initiale

# Records DNS ajoutés
ephec-ti.be.    IN MX 10 mail.l2-2.ephec-ti.be.
mail.l2-2.ephec-ti.be.    IN A    54.36.181.95

Étapes principales de configuration

1.1 Récupération des fichiers de configuration

DMS_GITHUB_URL="https://raw.githubusercontent.com/docker-mailserver/docker-mailserver/master"
wget "${DMS_GITHUB_URL}/compose.yaml"
wget "${DMS_GITHUB_URL}/mailserver.env"

1.2 Configuration des fichiers

  • compose.yaml : Modification du hostname vers mail.l2-2.ephec-ti.be
  • mailserver.env : Configuration SSL_TYPE=letsencrypt

1.3 Génération des certificats TLS

sudo certbot certonly --standalone -d mail.l2-2.ephec-ti.be

1.4 Démarrage et création des utilisateurs

docker compose up -d

# Création des utilisateurs (dans les 2 minutes suivant le démarrage)
docker exec -ti mailserver setup email list
* user1@l2-2.ephec-ti.be ( 0 / ~ ) [0%]
    [ aliases -> postmaster@l2-2.ephec-ti.be ]

* user2@l2-2.ephec-ti.be ( 0 / ~ ) [0%]

Test mail interne

theocle@anzeyimavps:/home/anzeyima/tp7$ swaks --to user2@l2-2.ephec-ti.be \
      --from user1@l2-2.ephec-ti.be \
      --server mail.l2-2.ephec-ti.be:587 \
      --tls \
      --auth \
      --auth-user user1@l2-2.ephec-ti.be \
      --auth-password user1
=== Trying mail.l2-2.ephec-ti.be:587...
=== Connected to mail.l2-2.ephec-ti.be.
<-  220 mail.l2-2.ephec-ti.be ESMTP
 -> EHLO anzeyimavps
<-  250-mail.l2-2.ephec-ti.be
<-  250-PIPELINING
<-  250-SIZE 10240000
<-  250-ETRN
<-  250-STARTTLS
<-  250-ENHANCEDSTATUSCODES
<-  250-8BITMIME
<-  250-DSN
<-  250 CHUNKING
 -> STARTTLS
<-  220 2.0.0 Ready to start TLS
=== TLS started with cipher TLSv1.3:TLS_AES_256_GCM_SHA384:256
=== TLS no local certificate set
=== TLS peer DN="/CN=mail.l2-2.ephec-ti.be"
 ~> EHLO anzeyimavps
<~  250-mail.l2-2.ephec-ti.be
<~  250-PIPELINING
<~  250-SIZE 10240000
<~  250-ETRN
<~  250-AUTH PLAIN LOGIN
<~  250-AUTH=PLAIN LOGIN
<~  250-ENHANCEDSTATUSCODES
<~  250-8BITMIME
<~  250-DSN
<~  250 CHUNKING
 ~> AUTH LOGIN
<~  334 VXNlcm5hbWU6
 ~> dXNlcjFAbDItMi5lcGhlYy10aS5iZQ==
<~  334 UGFzc3dvcmQ6
 ~> dXNlcjE=
<~  235 2.7.0 Authentication successful
 ~> MAIL FROM:<user1@l2-2.ephec-ti.be>
<~  250 2.1.0 Ok
 ~> RCPT TO:<user2@l2-2.ephec-ti.be>
<~  250 2.1.5 Ok
 ~> DATA
<~  354 End data with <CR><LF>.<CR><LF>
 ~> Date: Tue, 10 Jun 2025 07:10:43 +0000
 ~> To: user2@l2-2.ephec-ti.be
 ~> From: user1@l2-2.ephec-ti.be
 ~> Subject: test Tue, 10 Jun 2025 07:10:43 +0000
 ~> Message-Id: <20250610071043.594123@anzeyimavps>
 ~> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
 ~>
 ~> This is a test mailing
 ~>
 ~>
 ~> .
<~  250 2.0.0 Ok: queued as EF8618C6DF
 ~> QUIT
<~  221 2.0.0 Bye
=== Connection closed with remote host.
theocle@anzeyimavps:/home/anzeyima/tp7$

##Test mail exterieur

theocle@anzeyimavps:/home/anzeyima/tp7$ swaks --to theo7.kill@gmail.com \
      --from user1@l2-2.ephec-ti.be \
      --server mail.l2-2.ephec-ti.be:587 \
      --tls \
      --auth \
      --auth-user user1@l2-2.ephec-ti.be \
      --auth-password user1
=== Trying mail.l2-2.ephec-ti.be:587...
=== Connected to mail.l2-2.ephec-ti.be.
<-  220 mail.l2-2.ephec-ti.be ESMTP
 -> EHLO anzeyimavps
<-  250-mail.l2-2.ephec-ti.be
<-  250-PIPELINING
<-  250-SIZE 10240000
<-  250-ETRN
<-  250-STARTTLS
<-  250-ENHANCEDSTATUSCODES
<-  250-8BITMIME
<-  250-DSN
<-  250 CHUNKING
 -> STARTTLS
<-  220 2.0.0 Ready to start TLS
=== TLS started with cipher TLSv1.3:TLS_AES_256_GCM_SHA384:256
=== TLS no local certificate set
=== TLS peer DN="/CN=mail.l2-2.ephec-ti.be"
 ~> EHLO anzeyimavps
<~  250-mail.l2-2.ephec-ti.be
<~  250-PIPELINING
<~  250-SIZE 10240000
<~  250-ETRN
<~  250-AUTH PLAIN LOGIN
<~  250-AUTH=PLAIN LOGIN
<~  250-ENHANCEDSTATUSCODES
<~  250-8BITMIME
<~  250-DSN
<~  250 CHUNKING
 ~> AUTH LOGIN
<~  334 VXNlcm5hbWU6
 ~> dXNlcjFAbDItMi5lcGhlYy10aS5iZQ==
<~  334 UGFzc3dvcmQ6
 ~> dXNlcjE=
<~  235 2.7.0 Authentication successful
 ~> MAIL FROM:<user1@l2-2.ephec-ti.be>
<~  250 2.1.0 Ok
 ~> RCPT TO:<theo7.kill@gmail.com>
<~  250 2.1.5 Ok
 ~> DATA
<~  354 End data with <CR><LF>.<CR><LF>
 ~> Date: Tue, 10 Jun 2025 07:30:59 +0000
 ~> To: theo7.kill@gmail.com
 ~> From: user1@l2-2.ephec-ti.be
 ~> Subject: test Tue, 10 Jun 2025 07:30:59 +0000
 ~> Message-Id: <20250610073059.597858@anzeyimavps>
 ~> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
 ~>
 ~> This is a test mailing
 ~>
 ~>
 ~> .
<~  250 2.0.0 Ok: queued as 14B638C72E
 ~> QUIT
<~  221 2.0.0 Bye
=== Connection closed with remote host.
theocle@anzeyimavps:/home/anzeyima/tp7$

Première évaluation MXToolbox

Résultats attendus avant sécurisation :

  • SPF Record : Absent
  • DKIM : Non configuré
  • DMARC : Absent
  • PTR Record : Non aligné
  • MX Record : Configuré
  • Ports ouverts : 25, 587, 465, 143, 993

Réponses aux questions

1. Gestion des utilisateurs

Les utilisateurs sont gérés via un fichier plat dans Docker Mailserver :

  • Stockage dans /var/mail/
  • Commande : docker exec -ti mailserver setup email add
  • Pas d'intégration Unix directe ni de base de données

2. Format de mailbox

Le format utilisé est Maildir :

  • Un répertoire par utilisateur
  • Emails stockés individuellement
  • Structure : /var/mail/domaine.com/utilisateur/

2. Sécurisation du service mail

2.1. Analyse du chiffrement TLS

Ports ouverts et analyse

docker exec -ti mailserver netstat -tlnp

Ports identifiés :

  • Port 25 : SMTP standard (non chiffré, STARTTLS disponible)
  • Port 587 : SMTP soumission avec STARTTLS (TLS explicite)
  • Port 465 : SMTPS (TLS implicite)
  • Port 143 : IMAP standard (STARTTLS disponible)
  • Port 993 : IMAPS (TLS implicite)

Analyse Wireshark

Configuration de capture :

# Capture sur l'interface réseau du client
# Filtres : tcp.port == 587 or tcp.port == 993

Résultats attendus :

Pour l'envoi (SMTP) :

  • Port utilisé : 587
  • Type TLS : Explicite (STARTTLS)
  • Séquence : Connexion claire → EHLO → STARTTLS → Négociation TLS → Authentication

Pour la réception (IMAP) :

  • Port utilisé : 993
  • Type TLS : Implicite
  • Séquence : Connexion TLS directe → Authentication

2.2. Authentification du domaine

2.2.1. Alignement des records MX-PTR-A

Configuration PTR :

# - IP : 54.36.181.95
# - PTR souhaité : mail.l2-2.ephec-ti.be
# - Nom actuel : vps-6fe8e7f5.vps.ovh.net

Vérification :

dig -x 54.36.181.95
nslookup 54.36.181.95

Résultat MXToolbox attendu :

  • MX Record pointe vers mail.l2-2.ephec-ti.be
  • A Record résout vers 54.36.181.95
  • PTR Record inverse vers mail.l2-2.ephec-ti.be

2.2.2. SPF

Configuration DNS :

# Record TXT ajouté
ephec-ti.be.    IN TXT    "v=spf1 mx -all"

image

2.2.3. DKIM

Configuration :

# Génération des clés
docker exec -it mailserver setup config dkim

# Vérification du fichier généré
cat config/dkim/mail.txt

Ajout du record DNS :

# Contenu du fichier mail.txt à ajouter en tant que record TXT
# Exemple :
mail._domainkey.ephec-ti.be. IN TXT "v=DKIM1; h=sha256; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."

Redémarrage :

docker compose down
docker compose up -d

Validation en-tête email :

DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ephec-ti.be;
    s=mail; h=Date:From:To:Subject; bh=xxx; b=yyy

Résultat DKIM Validator : image

2.2.4. DMARC

Configuration DNS :

# Record TXT ajouté
_dmarc.ephec-ti.be.    IN TXT    "v=DMARC1; p=quarantine; rua=mailto:postmaster@ephec-ti.be"

image

2.3. Filtrage du spam

Activation de SpamAssassin

Configuration :

# Édition mailserver.env
nano mailserver.env
# Ajout de la ligne :
ENABLE_SPAMASSASSIN=1

# Redémarrage
docker compose down
docker compose up -d

Vérification :

docker logs mailserver | grep -i spam

Validation avec email valide

Test :

# Email normal envoyé
Subject: Test normal
Content: Bonjour, ceci est un test de mail normal.

Résultat attendu :

X-Spam-Status: No, score=-0.1 required=5.0
X-Spam-Score: -0.1

Validation avec email suspect

Test GTUBE :

cat > test_spam.txt << 'EOF'
Subject: Test SPAM

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
FREE MONEY!!! CLICK HERE NOW!!!
This is a test email to trigger spam filters.
EOF

Résultat attendu :

X-Spam-Status: Yes, score=1000.0 required=5.0
X-Spam-Flag: YES
X-Spam-Level: **************************************************

Action : Email marqué comme spam et déplacé vers le dossier Junk.

Clone this wiki locally