routeur:~# cat /etc/init.d/firewall #! /bin/sh
# Firewall par HuGoBosS
flush(){
#on purge les tables
iptables -F
iptables -X iptables -t nat -F
iptables -t nat -X
} start(){
echo -n "On met les regles en place : "
echo 1 > /proc/sys/net/ipv4/conf/all/forwarding
#On jete les packets par defaut iptables -P INPUT DROP
iptables -P OUTPUT DROP iptables -P FORWARD DROP
iptables -t nat -P PREROUTING ACCEPT
#on autorise le traffic sur lo
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
#On accepte toutes les connexions venant du reseau local
iptables -A INPUT -i eth0 -j ACCEPT
iptables -A OUTPUT -o eth0 -j ACCEPT
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A OUTPUT -o eth1 -j ACCEPT
# Priorisation des connexions ftp et ssh
iptables -A PREROUTING -t mangle -p tcp --sport ssh -j TOS --set-tos Minimize-Delay
iptables -A PREROUTING -t mangle -p tcp --sport ftp -j TOS --set-tos Minimize-Delay
# On donne un maximum de débit aux transferts ftp, peu importe la latence
iptables -A PREROUTING -t mangle -p tcp --sport ftp-data -j TOS --set-tos Maximize-Throughput
iptables -A PREROUTING -t mangle -p tcp --sport nntp -j TOS --set-tos Maximize-Throughput
#on met le nat et le masquerading en place
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
# Toutes les connexions qui sortent du LAN vers le Net sont acceptées
iptables -A FORWARD -i eth0 -o ppp0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth1 -o ppp0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
# Seules les connexions déjà établies ou en relation avec des connexions établies sont acceptées venant du Net vers le LAN
iptables -A FORWARD -i ppp0 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i ppp0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
#on interdit le spoofing
if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]
then
for filtre in /proc/sys/net/ipv4/conf/*/rp_filter
do
echo 1 > $filtre
done
fi
####################################
#On verifie la validité des paquets#
####################################
iptables -N CHECK_VALID
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL FIN,PSH,URG -j LOG --log-prefix '[IPTABLES X-max Packet] '
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL FIN,PSH,URG -j DROP
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL NONE -j LOG --log-prefix '[IPTABLES NULL scan packet] '
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL NONE -j DROP
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL SYN,FIN -j LOG --log-prefix '[IPTABLES Syn-Fin packet] '
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID --tcp-flags ALL SYN,FIN -j DROP
#iptables -A CHECK_VALID -p tcp -m state --state NEW,RELATED ! --syn -j LOG --log-prefix '[IPTABLES ACK packet] '
iptables -A CHECK_VALID -p tcp -m state --state NEW,RELATED ! --syn -j REJECT
#iptables -A CHECK_VALID -p tcp -m state --state ESTABLISHED --syn -j LOG --log-prefix '[IPTABLES SYN packet] '
iptables -A CHECK_VALID -p tcp -m state --state ESTABLISHED --syn -j REJECT
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID,RELATED --tcp-flags ALL FIN -j LOG --log-prefix '[IPTABLES FIN packet]'
iptables -A CHECK_VALID -p tcp -m state --state NEW,INVALID,RELATED --tcp-flags ALL FIN -j REJECT
iptables -A CHECK_VALID -i ppp0 -m state --state INVALID -j LOG --log-prefix '[IPTABLES Paquet invalide] '
iptables -A CHECK_VALID -i ppp0 -m state --state INVALID -j DROP
#On se premunti contre les scans
iptables -A CHECK_VALID -i ppp0 -p tcp --syn -m limit --limit 10/m -j RETURN
#iptables -A CHECK_VALID -i ppp0 -p tcp --syn -m limit --limit 1/m -j LOG --log-prefix '[IPTABLES syn flood] '
iptables -A CHECK_VALID -i ppp0 -p tcp --syn -j DROP
#on fait passer tous les paquets par la
iptables -A INPUT -i ppp0 -j CHECK_VALID
iptables -A FORWARD -o eth0 -j CHECK_VALID
iptables -A FORWARD -o eth1 -j CHECK_VALID
#on autorise les connections etablies
iptables -A INPUT -i ppp0 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o ppp0 -m state --state ESTABLISHED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED -j ACCEPT
#################################
#Ouverture des ports specifiques#
#################################
#on autorise le ping
#iptables -A INPUT -p icmp -m state --state NEW,RELATED -j ACCEPT
iptables -A OUTPUT -p icmp -m state --state NEW,RELATED -j ACCEPT
#acces aux serveurs web
iptables -A INPUT -i ppp0 -p tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport 80 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp --dport 80 -m state --state NEW -j ACCEPT
#acces aux serveurs https
#iptables -A INPUT -i ppp0 -p tcp --dport 443 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 443 -m state --state NEW -j ACCEPT
#acces aux serveurs ftp
iptables -A INPUT -i ppp0 -p tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT #acces aux serveurs smtp
iptables -A OUTPUT -o ppp0 -p tcp --dport 25 -m state --state NEW -j ACCEPT
#acces aux serveurs pop3
iptables -A OUTPUT -o ppp0 -p tcp --dport 110 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp --dport 110 -m state --state NEW -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --dport 110 -m state --state NEW -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --dport 110 -m state --state NEW -j ACCEPT
#acces aux serveurs ssh
iptables -A INPUT -i ppp0 -p tcp --dport 22 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 22 -m state --state NEW -j ACCEPT
#acces aux serveurs Yahoo
iptables -A OUTPUT -o ppp0 -p tcp --dport 5050 -m state --state NEW -j ACCEPT
#acces aux serveurs Icq
iptables -A OUTPUT -o ppp0 -p tcp --dport 5190 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 4000:4100 -m state --state NEW -j ACCEPT
#acces au serveur edk (desactivé car plus utilisé)
#iptables -A FORWARD -i ppp0 -p tcp --dport 4662 -j ACCEPT
#iptables -A PREROUTING -t nat -i ppp0 -p tcp --dport 4662 -j DNAT --to 192.168.0.2
#iptables -A INPUT -i ppp0 -p tcp --dport 4662 -j ACCEPT
#acces au serveur XXX
#iptables -A INPUT -i ppp0 -p tcp --dport XXX -m state --state NEW -j ACCEPT
#iptables -A OUTPUT -o ppp0 -p tcp --sport XXX -m state --state NEW -j ACCEPT
#on autorise les dns sur la passerelle
iptables -A OUTPUT -o ppp0 -p tcp -d 212.32.27.5 --dport 53 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp -d 212.32.27.5 --dport 53 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp -d 127.0.0.1 --dport 53 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp -d 127.0.0.1 --dport 53 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp -d 213.228.0.168 --dport 53 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp -d 213.228.0.168 --dport 53 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp -s 212.32.27.5 --dport 53 -j ACCEPT
iptables -A INPUT -i ppp0 -p udp -s 212.32.27.5 --dport 53 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp -s 213.228.0.168 --dport 53 -j ACCEPT iptables -A INPUT -i ppp0 -p udp -s 213.228.0.168 --dport 53 -j ACCEPT
iptables -A INPUT -i eth1 -p tcp -s 192.168.1.2 --dport 53 -j ACCEPT iptables -A INPUT -i eth1 -p udp -s 192.168.1.2 --dport 53 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -s 192.168.0.2 --dport 53 -j ACCEPT iptables -A INPUT -i eth0 -p udp -s 192.168.0.2 --dport 53 -j ACCEPT
echo "Done."
}
passoire(){
#on purge les tables
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
#On accepte les packets par defaut
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
}
case "$1" in
start)
flush
start
;;
stop)
flush
;;
restart)
flush && start
;;
passoire)
passoire
;;
*)
echo "Usage: /etc/init.d/firewall {start|restart|passoire|stop}"
exit 1
;;
esac
exit 0
|