PF sur FreeBSD 13/14

Voici un exemple de configuration d'un pare-feu avec PF sur FreeBSD
Considérons que ma machine a 2 cartes réseaux (wlan0 et ue0).
Par défaut ici, je bloque tout accès entrant depuis le WAN mais permet à ma machine de communiquer sans restrictions avec internet (stateful).

Adaptez les règles en fonction de vos besoins :


Passer en root

su -

Créez le fichier de règles : /etc/pf.conf

Créez le fichier de règles : /etc/pf.conf avec le contenu suivant et adaptez-le à vos besoins.

#--- Variables pour les interfaces
wan1='ue0' # Première interface WAN
wan2='wlan0' # Seconde interface WAN

#--- Options de normalisation
set block-policy drop
set skip on lo0

#--- Définition de la table bruteforce
table <bruteforce> persist

#--- Scrub (nettoyage) tout le trafic passant par les interfaces
scrub in on {$wan1 $wan2} all fragment reassemble

#--- Règles de blocage et de passage par défaut : autoriser la machine à communiquer avec internet sans restriction (stateful)
block in all
pass out on {$wan1 $wan2} inet from any to any keep state

##############################################
# Exemples de règles pour autoriser des flux entrants spécifiques

#--- Limiter les tentatives de connexion SSH à 5 par minute par IP
#pass in on {$wan1 $wan2} proto tcp from any to any port 22 \
# flags S/SA keep state \
# (max-src-conn-rate 5/60, overload <bruteforce> flush global)

#--- Autoriser les pings ICMP entrants (IPv4) et ICMPv6 (IPv6)
#pass in on {$wan1 $wan2} inet proto icmp all icmp-type echoreq keep state
#pass in on {$wan1 $wan2} inet6 proto icmp6 all icmp-type echoreq keep state

#--- Autoriser SSH (port 22) de n'importe quelle adresse IP (IPv4 et IPv6)
#pass in on {$wan1 $wan2} all proto tcp from any to any port 22 keep state

#--- Autoriser HTTP (port 80) et HTTPS (port 443) uniquement depuis des plages IP spécifiques (IPv4 et IPv6)
#pass in on {$wan1 $wan2} all proto tcp from {192.0.2.0/24, 2001:DB8::/32} to any port {80 443} keep state

#--- Autoriser HTTP (port 80) en limitant les tentatives de connexion à 5 par minute par IPpass in on $wan1 proto tcp from any to any port 80 \ flags S/SA keep state \ (max-src-conn-rate 5/60, overload <bruteforce> flush global)

#--- Autoriser le trafic FTP (port 21) (IPv4 et IPv6)
#pass in on {$wan1 $wan2} all proto tcp from any to any port 21 keep state

#--- Autoriser le trafic vers un serveur DNS spécifique sur le port 53 (TCP et UDP) (IPv4 et IPv6)
#pass in on {$wan1 $wan2} all proto {tcp udp} from any to {198.51.100.5, 2001:DB8::1} port 53 keep state

#--- Autoriser les connexions DNS (Domain Name System)
#pass in on {$wan1 $wan2} all proto {tcp udp} from any to any port 53 keep state

#--- Autoriser NTP (Network Time Protocol)
#pass in on {$wan1 $wan2} all proto udp from any to any port 123 keep state

#--- Autoriser SMTP (Simple Mail Transfer Protocol) pour un serveur de messagerie
#pass in on {$wan1 $wan2} all proto tcp from any to any port 25 keep state

#--- Autoriser IMAP (Internet Message Access Protocol) pour un serveur de messagerie
#pass in on {$wan1 $wan2} all proto tcp from any to any port 143 keep state

#--- Autoriser POP3 (Post Office Protocol version 3) pour un serveur de messagerie
#pass in on {$wan1 $wan2} all proto tcp from any to any port 110 keep state

#--- Autoriser HTTPS (HTTP Secure)
#pass in on {$wan1 $wan2} all proto tcp from any to any port 443 keep state

#--- Autoriser les connexions entrantes pour un service de jeu en ligne (ex : port 27015 pour un serveur de jeux)
#pass in on {$wan1 $wan2} all proto tcp from any to any port 27015 keep state
#pass in on {$wan1 $wan2} all proto udp from any to any port 27015 keep state

#--- Autoriser les accès à un serveur de base de données MySQL
#pass in on {$wan1 $wan2} all proto tcp from any to any port 3306 keep state

#--- Autoriser les connexions entrantes pour un service VoIP (SIP sur le port 5060)
#pass in on {$wan1 $wan2} all proto tcp from any to any port 5060 keep state
#pass in on {$wan1 $wan2} all proto udp from any to any port 5060 keep state

#--- Autoriser le trafic pour les serveurs VPN (ex : OpenVPN sur le port 1194)
#pass in on {$wan1 $wan2} all proto udp from any to any port 1194 keep state

#--- Autoriser les connexions SSH seulement depuis une adresse IP spécifique
#pass in on {$wan1 $wan2} all proto tcp from {203.0.113.0/24, 2001:DB8:1234::/48} to any port 22 flags S/SA keep state

#--- Logger et passer le trafic autorisé spécifiquement
#pass in log on {$wan1 $wan2} all proto tcp from any to any port 80 keep state

#--- Logs spécifiques pour le trafic entrant non autorisé
#block in log on {$wan1 $wan2} all

Activez le pare-feu au démarrage du serveur

sysrc pf_enable=YES pf_rules='/etc/pf.conf'

Démarrer le pare-feu pf

service pf start

Voir la liste de règles actives

pfctl -s rules

Voir l'état et les stats

pfctl -s info

Arrêter le pare-feu (si besoin)

service pf stop

⬆️ Retour en haut de la page