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

VAlidation sous domaine

theocle@anzeyimavps:/home/anzeyima/tp7$ # Obtenir l'IP de votre serveur
dig A mail.l2-2.ephec-ti.be
# ou
nslookup mail.l2-2.ephec-ti.be

; <<>> DiG 9.18.33-1~deb12u2-Debian <<>> A mail.l2-2.ephec-ti.be
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44157
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;mail.l2-2.ephec-ti.be.         IN      A

;; ANSWER SECTION:
mail.l2-2.ephec-ti.be.  86400   IN      A       54.36.181.95

;; Query time: 15 msec
;; SERVER: 213.186.33.99#53(213.186.33.99) (UDP)
;; WHEN: Tue Jun 10 07:51:19 UTC 2025
;; MSG SIZE  rcvd: 66

Server:         213.186.33.99
Address:        213.186.33.99#53

Non-authoritative answer:
Name:   mail.l2-2.ephec-ti.be
Address: 54.36.181.95

theocle@anzeyimavps:/home/anzeyima/tp7$```

#### 1.4 Démarrage et création des utilisateurs
```bash
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

Validation

theocle@anzeyimavps:/home/anzeyima/tp7$ dig TXT mail._domainkey.l2-2.ephec-ti.be

; <<>> DiG 9.18.33-1~deb12u2-Debian <<>> TXT mail._domainkey.l2-2.ephec-ti.be
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14182
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;mail._domainkey.l2-2.ephec-ti.be. IN   TXT

;; ANSWER SECTION:
mail._domainkey.l2-2.ephec-ti.be. 86400 IN TXT  "v=DKIM1; h=sha256; k=rsa; " "p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA10wspjhAnqNECa/si5oG0vnmd2TumNFdCgVzDd4qqJOusZRzOnmUQG+8xHysvbwlohC9FKfG9eQNJvxS/duCQs55LV32Io6gRb3pesOorefOqgvsihwDwekbGH1pDSF4FM/wgtAaVt0FmwlnIIu8aVB1GjmDo2b3QSeE8DziGH8/7HDd6iuNwmdStc+NB9UbB5Xfs0bV2rAwSJ" "gHwTMG5NeEVv9QavZ6G8YqsYhniS+mifnM5bQngPWVrVf9e6Hb8HZztATRORKU138VlZ87byaAEK8kwe+Z/oU88o5gjATRrzzU2sHG3G1WjWgALUvI93SiiAj0TPJlqGrOyCOzmQIDAQAB"

;; Query time: 7 msec
;; SERVER: 213.186.33.99#53(213.186.33.99) (UDP)
;; WHEN: Tue Jun 10 07:49:32 UTC 2025
;; MSG SIZE  rcvd: 496

theocle@anzeyimavps:/home/anzeyima/tp7$

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

##Mail log

debian@anzeyimavps:~$ sudo grep "3500E8C6E1" /home/anzeyima/tp7/docker-data/dms/mail-logs/mail.log
2025-06-10T07:39:04.217244+00:00 mail postfix/submission/smtpd[66843]: 3500E8C6E1: client=unknown[54.36.181.95], sasl_method=LOGIN, sasl_username=user1@l2-2.ephec-ti.be
2025-06-10T07:39:04.219901+00:00 mail postfix/sender-cleanup/cleanup[66845]: 3500E8C6E1: prepend: header Message-Id: <20250610073903.599108@anzeyimavps> from unknown[54.36.181.95]; from=<user1@l2-2.ephec-ti.be> to=<user2@l2-2.ephec-ti.be> proto=ESMTP helo=<anzeyimavps>: X-MS-Reactions: disallow
2025-06-10T07:39:04.220319+00:00 mail postfix/sender-cleanup/cleanup[66845]: 3500E8C6E1: message-id=<20250610073903.599108@anzeyimavps>
2025-06-10T07:39:04.269703+00:00 mail opendkim[664]: 3500E8C6E1: DKIM-Signature field added (s=mail, d=l2-2.ephec-ti.be)
2025-06-10T07:39:04.313673+00:00 mail postfix/qmgr[771]: 3500E8C6E1: from=<user1@l2-2.ephec-ti.be>, size=329, nrcpt=1 (queue active)
2025-06-10T07:39:04.410423+00:00 mail postfix/smtp-amavis/smtp[66846]: 3500E8C6E1: prepend: header Message-Id: <20250610073903.599108@anzeyimavps>: X-MS-Reactions: disallow
2025-06-10T07:39:05.011105+00:00 mail amavis[66120]: (66120-01) Passed SPAM {RelayedTaggedInbound,Quarantined}, [54.36.181.95]:40288 [54.36.181.95] <user1@l2-2.ephec-ti.be> -> <user2@l2-2.ephec-ti.be>, quarantine: L/spam-L1VSA2bgNtye.gz, Queue-ID: 3500E8C6E1, Message-ID: <20250610073903.599108@anzeyimavps>, mail_id: L1VSA2bgNtye, Hits: 999.799, size: 832, queued_as: F17A48C7C9, 659 ms
2025-06-10T07:39:05.014082+00:00 mail postfix/smtp-amavis/smtp[66846]: 3500E8C6E1: to=<user2@l2-2.ephec-ti.be>, relay=127.0.0.1[127.0.0.1]:10024, delay=0.81, delays=0.11/0.03/0.02/0.65, dsn=2.0.0, status=sent (250 2.0.0 from MTA(smtp:[127.0.0.1]:10025): 250 2.0.0 Ok: queued as F17A48C7C9)
2025-06-10T07:39:05.014401+00:00 mail postfix/qmgr[771]: 3500E8C6E1: removed
debian@anzeyimavps:~$```

 ```sudo grep "3500E8C6E1" /home/anzeyima/tp7/docker-data/dms/mail-logs/mail.log```

**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