Security Groups vs NACL
Introduction
AWS propose deux niveaux de pare-feu pour protéger vos ressources :
- Security Groups : au niveau de l'instance
- NACL (Network ACL) : au niveau du subnet
┌─────────────────────────────────────────────────────────────┐
│ DEUX NIVEAUX DE SÉCURITÉ │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ SUBNET │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ NACL (1er niveau) │ │ │
│ │ │ Filtre TOUT le trafic entrant/sortant │ │ │
│ │ │ du subnet │ │ │
│ │ └───────────────────┬────────────────────────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ INSTANCE EC2 │ │ │
│ │ │ ┌──────────────────────────────────────────┐ │ │ │
│ │ │ │ SECURITY GROUP (2e niveau) │ │ │ │
│ │ │ │ Filtre le trafic vers/depuis instance │ │ │ │
│ │ │ └──────────────────────────────────────────┘ │ │ │
│ │ │ │ │ │ │
│ │ │ ┌─────▼─────┐ │ │ │
│ │ │ │ Application│ │ │ │
│ │ │ └───────────┘ │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Security Groups
Caractéristiques
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUPS │
├─────────────────────────────────────────────────────────────┤
│ │
│ TYPE : Pare-feu STATEFUL (avec état) │
│ │
│ Stateful signifie : │
│ • Si le trafic entrant est autorisé │
│ • Alors la réponse sortante est automatiquement permise │
│ • Pas besoin de règle explicite pour la réponse │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Client ──── Requête ────► Instance │ │
│ │ (autorisé par règle inbound) │ │
│ │ │ │
│ │ Client ◄─── Réponse ──── Instance │ │
│ │ (automatiquement autorisé - stateful) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ FONCTIONNEMENT │
│ • Par défaut : TOUT bloqué en entrée │
│ • Par défaut : TOUT autorisé en sortie │
│ • Règles ALLOW uniquement (pas de DENY) │
│ • Évaluées toutes ensemble (pas d'ordre) │
│ │
└─────────────────────────────────────────────────────────────┘
Anatomie d'une Règle SG
┌─────────────────────────────────────────────────────────────┐
│ RÈGLE SECURITY GROUP │
├─────────────────────────────────────────────────────────────┤
│ │
│ Type : SSH │
│ Protocol : TCP │
│ Port Range : 22 │
│ Source : 203.0.113.0/24 │
│ Description: Accès admin depuis bureau │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ SIGNIFICATION : │ │
│ │ │ │
│ │ "Autoriser le trafic TCP sur le port 22 (SSH) │ │
│ │ depuis les IPs 203.0.113.0 à 203.0.113.255" │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ SOURCES POSSIBLES : │
│ • IP unique : 203.0.113.45/32 │
│ • Plage CIDR : 203.0.113.0/24 │
│ • Autre SG : sg-0123456789 │
│ • Prefix List : pl-xxx │
│ • Partout : 0.0.0.0/0 (⚠️ attention !) │
│ │
└─────────────────────────────────────────────────────────────┘
Exemples de Security Groups
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUP - SERVEUR WEB │
├─────────────────────────────────────────────────────────────┤
│ │
│ Nom : sg-web-server │
│ │
│ RÈGLES ENTRANTES (Inbound) │
│ ┌───────┬──────────┬───────┬─────────────────────────┐ │
│ │ Type │ Protocol │ Port │ Source │ │
│ ├───────┼──────────┼───────┼─────────────────────────┤ │
│ │ HTTP │ TCP │ 80 │ 0.0.0.0/0 │ │
│ │ HTTPS │ TCP │ 443 │ 0.0.0.0/0 │ │
│ │ SSH │ TCP │ 22 │ 10.0.0.0/16 (VPC only) │ │
│ └───────┴──────────┴───────┴─────────────────────────┘ │
│ │
│ RÈGLES SORTANTES (Outbound) │
│ ┌───────┬──────────┬───────┬─────────────────────────┐ │
│ │ Type │ Protocol │ Port │ Destination │ │
│ ├───────┼──────────┼───────┼─────────────────────────┤ │
│ │ All │ All │ All │ 0.0.0.0/0 │ │
│ └───────┴──────────┴───────┴─────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUP - BASE DE DONNÉES │
├─────────────────────────────────────────────────────────────┤
│ │
│ Nom : sg-database │
│ │
│ RÈGLES ENTRANTES (Inbound) │
│ ┌────────┬──────────┬───────┬────────────────────────┐ │
│ │ Type │ Protocol │ Port │ Source │ │
│ ├────────┼──────────┼───────┼────────────────────────┤ │
│ │ MySQL │ TCP │ 3306 │ sg-web-server ✓ │ │
│ │ MySQL │ TCP │ 3306 │ sg-app-server ✓ │ │
│ └────────┴──────────┴───────┴────────────────────────┘ │
│ │
│ ✓ Référencer un autre SG = bonne pratique │
│ Seules les instances dans ce SG peuvent accéder │
│ │
│ RÈGLES SORTANTES (Outbound) │
│ ┌───────┬──────────┬───────┬─────────────────────────┐ │
│ │ Type │ Protocol │ Port │ Destination │ │
│ ├───────┼──────────┼───────┼─────────────────────────┤ │
│ │ All │ All │ All │ 0.0.0.0/0 │ │
│ └───────┴──────────┴───────┴─────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Network ACL (NACL)
Caractéristiques
┌─────────────────────────────────────────────────────────────┐
│ NETWORK ACL (NACL) │
├─────────────────────────────────────────────────────────────┤
│ │
│ TYPE : Pare-feu STATELESS (sans état) │
│ │
│ Stateless signifie : │
│ • Chaque direction doit être explicitement autorisée │
│ • Si entrée autorisée, sortie doit aussi être configurée │
│ • Règles séparées pour inbound ET outbound │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Client ──── Requête ────► Subnet │ │
│ │ (besoin règle inbound) │ │
│ │ │ │
│ │ Client ◄─── Réponse ──── Subnet │ │
│ │ (besoin règle outbound séparée !) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ FONCTIONNEMENT │
│ • Par défaut : TOUT autorisé (NACL par défaut) │
│ • Règles ALLOW et DENY │
│ • Évaluées dans l'ORDRE des numéros (priorité) │
│ • Première règle qui match = décision finale │
│ • S'applique à TOUT le subnet │
│ │
└─────────────────────────────────────────────────────────────┘
Anatomie d'une Règle NACL
┌─────────────────────────────────────────────────────────────┐
│ RÈGLE NACL │
├─────────────────────────────────────────────────────────────┤
│ │
│ Rule Number : 100 │
│ Type : HTTP (80) │
│ Protocol : TCP (6) │
│ Port Range : 80 │
│ Source : 0.0.0.0/0 │
│ Allow/Deny : ALLOW │
│ │
│ ORDRE D'ÉVALUATION : │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Rule 100: ALLOW HTTP from 0.0.0.0/0 │ │
│ │ Rule 200: DENY HTTP from 192.168.1.0/24 │ │
│ │ Rule * : DENY ALL │ │
│ │ │ │
│ │ Paquet HTTP de 192.168.1.50 ? │ │
│ │ → Évalué par Rule 100 d'abord │ │
│ │ → 192.168.1.50 match 0.0.0.0/0 │ │
│ │ → ALLOW ! (Rule 200 jamais atteinte) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ⚠️ L'ordre des règles est CRITIQUE dans NACL │
│ │
└─────────────────────────────────────────────────────────────┘
Ports Éphémères
┌─────────────────────────────────────────────────────────────┐
│ PORTS ÉPHÉMÈRES (Important pour NACL) │
├─────────────────────────────────────────────────────────────┤
│ │
│ Problème NACL : Comment autoriser les réponses ? │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Client (port 54321) ──HTTP:80──► Serveur │ │
│ │ Client (port 54321) ◄──TCP:54321── Serveur │ │
│ │ │ │
│ │ Le serveur répond sur le PORT SOURCE du client │ │
│ │ Ce port est "éphémère" (aléatoire, temporaire) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ PLAGES DE PORTS ÉPHÉMÈRES : │
│ • Linux : 32768 - 60999 │
│ • Windows : 49152 - 65535 │
│ • NAT Gateway : 1024 - 65535 │
│ │
│ SOLUTION : Autoriser les ports éphémères en outbound │
│ │
│ NACL Outbound Rule : │
│ Rule 100: ALLOW TCP 1024-65535 to 0.0.0.0/0 │
│ │
└─────────────────────────────────────────────────────────────┘
Comparaison SG vs NACL
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUP vs NACL │
├─────────────────────────────────────────────────────────────┤
│ │
│ Caractéristique │ Security Group │ NACL │
│ ─────────────────────┼─────────────────┼─────────────────│
│ Niveau │ Instance │ Subnet │
│ Type │ Stateful │ Stateless │
│ Règles │ ALLOW only │ ALLOW & DENY │
│ Évaluation │ Toutes ensemble │ Par ordre │
│ Ports éphémères │ Automatique │ Manuel │
│ Par défaut │ Deny in/Allow out│ Allow all │
│ Modifiable à chaud │ Oui │ Oui │
│ │
│ QUAND UTILISER QUOI ? │
│ ───────────────────── │
│ │
│ Security Group : │
│ ✅ Cas d'usage principal │
│ ✅ Contrôle par application/rôle │
│ ✅ Plus facile à gérer │
│ │
│ NACL : │
│ ✅ Bloquer IP malveillantes (DENY rule) │
│ ✅ Ligne de défense supplémentaire │
│ ✅ Conformité (defense in depth) │
│ │
└─────────────────────────────────────────────────────────────┘
Exemples Pratiques
Créer un Security Group (CLI)
# Créer le Security Group
aws ec2 create-security-group \
--group-name sg-web-server \
--description "Security group for web servers" \
--vpc-id vpc-xxx
# Ajouter règle HTTP
aws ec2 authorize-security-group-ingress \
--group-id sg-xxx \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0
# Ajouter règle SSH (restrictive)
aws ec2 authorize-security-group-ingress \
--group-id sg-xxx \
--protocol tcp \
--port 22 \
--cidr 10.0.0.0/16
# Autoriser depuis un autre SG
aws ec2 authorize-security-group-ingress \
--group-id sg-database \
--protocol tcp \
--port 3306 \
--source-group sg-web-server
Créer une NACL (CLI)
# Créer la NACL
aws ec2 create-network-acl \
--vpc-id vpc-xxx \
--tag-specifications 'ResourceType=network-acl,Tags=[{Key=Name,Value=nacl-public}]'
# Ajouter règle inbound HTTP
aws ec2 create-network-acl-entry \
--network-acl-id acl-xxx \
--rule-number 100 \
--protocol tcp \
--port-range From=80,To=80 \
--cidr-block 0.0.0.0/0 \
--rule-action allow \
--ingress
# Ajouter règle outbound pour réponses (ports éphémères)
aws ec2 create-network-acl-entry \
--network-acl-id acl-xxx \
--rule-number 100 \
--protocol tcp \
--port-range From=1024,To=65535 \
--cidr-block 0.0.0.0/0 \
--rule-action allow \
--egress
Bonnes Pratiques
┌─────────────────────────────────────────────────────────────┐
│ BONNES PRATIQUES SÉCURITÉ │
├─────────────────────────────────────────────────────────────┤
│ │
│ SECURITY GROUPS │
│ ✅ Principe du moindre privilège │
│ ✅ Un SG par rôle/fonction │
│ ✅ Référencer les SG entre eux (pas IPs) │
│ ✅ Éviter 0.0.0.0/0 pour SSH/RDP │
│ ✅ Documenter chaque règle (Description) │
│ │
│ NACL │
│ ✅ Garder la NACL par défaut (Allow all) │
│ ✅ Utiliser NACL pour bloquer IPs malveillantes │
│ ✅ Espacer les numéros de règles (100, 200, 300...) │
│ ✅ Ne pas oublier les ports éphémères │
│ │
│ ⚠️ ERREURS COURANTES │
│ ❌ SSH ouvert à 0.0.0.0/0 │
│ ❌ Tous les ports ouverts "pour tester" │
│ ❌ Oublier les règles outbound NACL │
│ ❌ Règles trop permissives en production │
│ │
└─────────────────────────────────────────────────────────────┘
Quiz de Validation
- Quelle est la différence entre stateful et stateless ?
- Pourquoi doit-on configurer les ports éphémères dans NACL ?
- Comment autoriser un serveur app à accéder à une DB via SG ?
- Dans quel ordre sont évaluées les règles NACL ?
- Quel pare-feu utiliser pour bloquer une IP malveillante ?
Prochaine section : Dépannage Réseau