Salut tout le monde,
Voila je voulais partager avec vous ma config actuelle d'iptables.
Si vous avez des suggestions pour l'améliorer, ou si ça aide quelqu'un 🙂 (Je suis dans la bonne catégorie pour poster ? )
Voici donc le petit schéma pour le moment. Le serveur 1 est remonté et pour le moment il n'y a rien d'exceptionnelle.
Ce que je voulais faire :
- Bloquer tout par défaut : INPUT, OUTPUT, FORWARD
- Autoriser en INPUT les connexions établies
- Autoriser lo
- Autoriser le ping depuis le réseau 192.168.1.0/24
- Autoriser le SSH depuis 192.168.1.0/24 à destination du 192.168.1.253
- Autoriser NTP, DNS, Mail, HTTP, HTTPS en sortie
- Autoriser Glances et Monit en entrée
Voici le résultat pour le moment :
07:29 [ root @ SRV1 ][ ~/scripts ] > iptables -L -nv
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 /* Autorisation lo */
0 0 ACCEPT icmp -- eth1 * 192.168.1.0/24 192.168.1.253 /* Autorisation PING */
12 812 ACCEPT all -- eth1 * 0.0.0.0/0 192.168.1.253 state RELATED,ESTABLISHED /* Autorisation connexion etablie */
0 0 ACCEPT tcp -- eth1 * 192.168.1.0/24 192.168.1.253 tcp dpt:2812 /* Monit IN */
0 0 ACCEPT tcp -- eth1 * 192.168.1.0/24 192.168.1.253 tcp dpt:22000 /* Autorisation SSH IN */
0 0 ACCEPT tcp -- eth1 * 192.168.1.0/24 192.168.1.253 tcp dpt:61208 /* Glances IN */
0 0 LOGDROP all -- * * 0.0.0.0/0 0.0.0.0/0 /* BLOQUE TOUT LE RESTE */
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 LOGDROP all -- * * 0.0.0.0/0 0.0.0.0/0 /* BLOQUE TOUT LE RESTE */
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0 /* Autorisation lo */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 192.168.1.254 tcp dpt:23 /* Autorisation Telnet server box */
0 0 ACCEPT udp -- * eth1 192.168.1.253 0.0.0.0/0 udp dpt:53 /* Autorisation DNS */
0 0 ACCEPT udp -- * eth1 192.168.1.253 0.0.0.0/0 udp dpt:123 /* Autorisation NTP */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 0.0.0.0/0 tcp spt:2812 /* Monit OUT */
7 9040 ACCEPT tcp -- * eth1 192.168.1.253 192.168.1.0/24 tcp spt:22000 /* Autorisation SSH OUT */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 192.168.1.0/24 tcp spt:61208 /* Glances OUT */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 0.0.0.0/0 tcp dpt:80 /* Autorisation HTTP OUT */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 0.0.0.0/0 tcp dpt:443 /* Autorisation HTTPS OUT */
0 0 ACCEPT tcp -- * eth1 192.168.1.253 0.0.0.0/0 tcp dpt:25 /* Autorisation mail */
0 0 LOGDROP all -- * * 0.0.0.0/0 0.0.0.0/0 /* BLOQUE TOUT LE RESTE */
Chain LOGACCEPT (0 references)
pkts bytes target prot opt in out source destination
0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix "[IPTABLES ACCEPT] : "
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain LOGDROP (3 references)
pkts bytes target prot opt in out source destination
0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix "[IPTABLES DROP] : "
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Et voici le script actuel :
#!/bin/sh
#
# Simple Firewall configuration.
#
# Author: ToToF
#
# chkconfig: 2345 9 91
# description: Activates/Deactivates the firewall at boot time
#
### BEGIN INIT INFO
# Provides: firewall.sh
# Required-Start: $syslog $network
# Required-Stop: $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start firewall daemon at boot time
# Description: Custom Firewall scrip.
### END INIT INFO
##########################
# 04/11/2016 V1
##########################
set -x
##########################
# R E G L E S I N I T
##########################
fw_start () {
# Création des chaînes de règles
iptables -N LOGACCEPT
iptables -A LOGACCEPT -j LOG --log-prefix '[IPTABLES ACCEPT] : '
iptables -A LOGACCEPT -j ACCEPT
iptables -N LOGDROP
iptables -A LOGDROP -j LOG --log-prefix '[IPTABLES DROP] : '
iptables -A LOGDROP -j DROP
# Autorisation loopback
/sbin/iptables -A INPUT -i lo -j ACCEPT -m comment --comment "Autorisation lo"
/sbin/iptables -A OUTPUT -o lo -j ACCEPT -m comment --comment "Autorisation lo"
# Autorisation PING
/sbin/iptables -A INPUT -i eth1 -s 192.168.1.0/24 -d 192.168.1.253 -p icmp -j ACCEPT -m comment --comment "Autorisation PING"
# Input traffic établies
/sbin/iptables -A INPUT -i eth1 -d 192.168.1.253 -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment "Autorisation connexion etablie"
# Autorisation Telnet
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -d 192.168.1.254 -p tcp --dport 23 -j ACCEPT -m comment --comment "Autorisation Telnet server box"
# Autorisation DNS
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p udp --dport 53 -j ACCEPT -m comment --comment "Autorisation DNS"
# Autorisation NTP
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p udp --dport 123 -j ACCEPT -m comment --comment "Autorisation NTP"
# Monit
/sbin/iptables -A INPUT -i eth1 -d 192.168.1.253 -s 192.168.1.0/24 -p tcp --dport 2812 -j ACCEPT -m comment --comment "Monit IN"
/sbin/iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p tcp --sport 2812 -j ACCEPT -m comment --comment "Monit OUT"
# SSH
/sbin/iptables -A INPUT -i eth1 -s 192.168.1.0/24 -d 192.168.1.253 -p tcp --dport 22000 -j ACCEPT -m comment --comment "Autorisation SSH IN"
/sbin/iptables -A OUTPUT -o eth1 -s 192.168.1.253 -d 192.168.1.0/24 -p tcp --sport 22000 -j ACCEPT -m comment --comment "Autorisation SSH OUT"
# Glances
/sbin/iptables -A INPUT -i eth1 -d 192.168.1.253 -s 192.168.1.0/24 -p tcp --dport 61208 -j ACCEPT -m comment --comment "Glances IN"
/sbin/iptables -A OUTPUT -o eth1 -s 192.168.1.253 -d 192.168.1.0/24 -p tcp --sport 61208 -j ACCEPT -m comment --comment "Glances OUT"
# WWW
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p tcp --dport 80 -j ACCEPT -m comment --comment "Autorisation HTTP OUT"
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p tcp --dport 443 -j ACCEPT -m comment --comment "Autorisation HTTPS OUT"
# Mail
iptables -A OUTPUT -o eth1 -s 192.168.1.253 -p tcp --dport 25 -j ACCEPT -m comment --comment "Autorisation mail"
# On LOG tous dans syslogLOG_DROP
/sbin/iptables -A OUTPUT -j LOGDROP -m comment --comment "BLOQUE TOUT LE RESTE"
/sbin/iptables -A INPUT -j LOGDROP -m comment --comment "BLOQUE TOUT LE RESTE"
/sbin/iptables -A FORWARD -j LOGDROP -m comment --comment "BLOQUE TOUT LE RESTE"
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
}
#############################
# S T O P F I R E W A L L
#############################
fw_stop () {
/sbin/iptables -F
/sbin/iptables -t nat -F
/sbin/iptables -t mangle -F
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD DROP
/sbin/iptables -P OUTPUT ACCEPT
}
#############################
# R E S E T F I R E W A L L
#############################
fw_clear () {
/sbin/iptables -F
/sbin/iptables -t nat -F
/sbin/iptables -t mangle -F
/sbin/iptables -P INPUT ACCEPT
/sbin/iptables -P FORWARD ACCEPT
/sbin/iptables -P OUTPUT ACCEPT
iptables -X
iptables -t nat -X
}
###########################
# T E S T F I R E W A L L
###########################
fw_save () {
/sbin/iptables-save > /etc/iptables.backup
}
fw_restore () {
if [ -e /etc/iptables.backup ]; then
/sbin/iptables-restore < /etc/iptables.backup
fi
}
fw_test () {
fw_save
sleep 30 && echo "Restauration des regles en cours..." && fw_restore &
fw_stop
fw_start
}
case "$1" in
start|restart)
echo -n "Demarrage du prefeu.."
fw_stop
fw_start
echo "Terminee"
;;
stop)
echo -n "Arret du parefeu.."
fw_stop
echo "Terminee"
;;
clear)
echo -n "Reinitialisation des reglges du parefeu .."
fw_clear
echo "Terminee"
;;
test)
echo -n "Test des regles du parefeu..."
fw_test
echo -n "La configuration sera restaurer dans 30secondes"
;;
*)
echo "Utilisation: $0 {start|stop|restart|clear|test}"
echo "!!"
exit 1
;;
esac
exit 0
Voila voila, qu'en pensez-vous ?