#! /bin/sh
# Firewall par HuGoBosS aidé par Hfr !
case "$1" in
start)
echo -n "On met les regles en place : "
#on purge les tables
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
#On jete les packets par defaut
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT 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
#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
# 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 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 -A CHECK_VALID
#iptables -A FORWARD -o eth0 -A CHECK_VALID
#iptables -A FORWARD -o eth1 -A 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#
#################################
#acces aux serveurs web
iptables -A INPUT -i ppp0 -p tcp --sport 80 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 80 -m state --state NEW -j ACCEPT
#acces aux serveurs https
iptables -A INPUT -i ppp0 -p tcp --sport 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 INPUT -i ppp0 -p tcp --sport 25 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 25 -m state --state NEW -j ACCEPT
#acces aux serveurs pop3
iptables -A INPUT -i ppp0 -p tcp --sport 110 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 110 -m state --state NEW -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport 110 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp --dport 110 -m state --state NEW -j ACCEPT
#acces aux serveurs ssh
iptables -A INPUT -i ppp0 -p tcp --sport 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 Aim
iptables -A INPUT -i ppp0 -p tcp --sport 5190 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 5190 -m state --state NEW -j ACCEPT
#acces aux serveurs Yahoo
iptables -A INPUT -i ppp0 -p tcp --sport 5050 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 5050 -m state --state NEW -j ACCEPT
#acces aux serveurs Icq
iptables -A INPUT -i ppp0 -p tcp --sport 5190 -m state --state NEW -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 5190 -m state --state NEW -j ACCEPT
#acces au serveur edk
iptables -A INPUT -i ppp0 -p tcp --sport 4661:4665 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 4661:4665 -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport 4661:4665 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp --dport 4661:4665 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp --sport 4242 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp --dport 4242 -j ACCEPT
iptables -A INPUT -i ppp0 -p udp --sport 4242 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp --dport 4242 -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 le dns sur la passerelle
iptables -A OUTPUT -o ppp0 -p tcp -d 212.32.27.5 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp -d 212.32.27.5 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p tcp -d 213.228.0.168 -j ACCEPT
iptables -A OUTPUT -o ppp0 -p udp -d 213.228.0.168 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp -s 212.32.27.5 -j ACCEPT
iptables -A INPUT -i ppp0 -p udp -s 212.32.27.5 -j ACCEPT
iptables -A INPUT -i ppp0 -p tcp -s 213.228.0.168 -j ACCEPT iptables -A INPUT -i ppp0 -p udp -s 213.228.0.168 -j ACCEPT
echo "Done."
;;
stop)
echo -n "Purge des tables: "
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
echo "Done."
;;
*)
echo "Usage: /etc/init.d/firewall {start|stop}"
exit 1
;;
esac
exit 0
|