All pastes #522324 Raw Edit

Unnamed

public text v1 · immutable
#522324 ·published 2007-05-30 11:53 UTC
rendered paste body
#!/bin/sh
PATH=/usr/eth0cal/sbin:/usr/eth0cal/bin:/sbin:/bin:/usr/sbin:/usr/bin:/sbin/iptables
ssh="ssh"
File="/tmp/ipnetstat_auto"
PortClose="/tmp/ipnetstatclose"
ProgAuto="/etc/netstat_auto_iptables.conf"
ETH="eth1"
pause="0.5"
pause2="1"
DAEMON=/etc/init.d/iptable_auto
NAME=Firewall
ID=$$
DESC="Firwall 0.1"
BreakON="ON"
set -m

case "$1" in
  start)
if [ -e $ProgAuto ] ;then
	echo "Configuration trouvée Démarrage"
else
	echo "Fichier de configuration $ProgAuto non trouver. Ce fichier contien les programes autoriser par iptables!" 
	echo "Fin du programe"
	exit 
fi
rm "$File" 2> /dev/null
rm "$PortClose" 2>/dev/null



#on veux autoriser certin port
echo "21" >> $PortClose
echo "22" >> $PortClose
echo "25" >> $PortClose
echo "80" >> $PortClose
echo "110" >> $PortClose


echo "Configuration des règles de base"
#Mise a zero des regle (par précaution)
#
# On remet la police par défaut à ACCEPT
#
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

#
# On remet les polices par défaut pour la table NAT
#
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

#
# On vide (flush) toutes les règles existantes
#
iptables -F
iptables -t nat -F

#
# Et enfin, on efface toutes les chaînes qui ne
# sont pas à defaut dans la table filter et nat

iptables -X
iptables -t nat -X
#==============================================
#local
iptables -A INPUT -i $ETH -s 192.168.1.0/24 -j ACCEPT
#loopack
iptables -A INPUT -i lo -j ACCEPT
#iptables -t filter -A INPUT -i $ETH -m limit --limit 24/h --limit-burst 1 -p all -j ULOG --ulog-prefix="USER INPUT Final"
#==============================================
#==============================================
#le loopack
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -o $ETH -d 192.168.1.0/24 -j ACCEPT
#iptables -t filter -A OUTPUT -o $ETH -m limit --limit 24/h --limit-burst 1 -p all -j ULOG --ulog-prefix="USER OUTPUT Final"
#===============================================
while true
do
netstat -laputen > $File
while read line
do
#echo $line|cut -d: -f2 | cut -b1-6

n1=`echo $line | cut -d: -f2 | cut -b1-1`
n2=`echo $line | cut -d: -f2 | cut -b2-2`
n3=`echo $line | cut -d: -f2 | cut -b3-3`
n4=`echo $line | cut -d: -f2 | cut -b4-4`
n5=`echo $line | cut -d: -f2 | cut -b5-5`
n6=`echo $line | cut -d: -f2 | cut -b6-6`
step=""
if [ -n "$n1" ] ;then
num=$((`expr match "$n1" '[[:digit:]]'`))
	if [ $num -eq 1  ] ;then
	step=$step$n1
		num=$((`expr match "$n2" '[[:digit:]]'`))
		if [ $num -eq 1  ] ;then
		step=$step$n2
			num=$((`expr match "$n3" '[[:digit:]]'`))
			if [ $num -eq 1  ] ;then
			step=$step$n3
				num=$((`expr match "$n4" '[[:digit:]]'`))
				if [ $num -eq 1  ] ;then
				step=$step$n4
					num=$((`expr match "$n5" '[[:digit:]]'`))
					if [ $num -eq 1  ] ;then
					step=$step$n5
						num=$((`expr match "$n6" '[[:digit:]]'`))
						if [ $num -eq 1  ] ;then
						step=$step$n6
						fi
					fi
				fi
			fi
		fi

	Prog=`grep "$step" $ProgAuto`
	if [ "$Prog" = "$ProgAuto" ] ;then
		echo "le programe autoriser $Prog a le port $step"
	else
		if [ -e $PortClose ] ;then
			StepPort=`grep "$step" $PortClose`
#			echo " stepgrep  $StepPort"
			if [ ! -n "$StepPort" ] ;then
#				echo "port deja fermer"
#			else
#				echo "port en cour de fermeture"
			iptables -A OUTPUT -o $ETH -p tcp --dport $step -j DROP
			iptables -A OUTPUT -o $ETH -p tcp --sport $step -j DROP
			iptables -A INPUT -i $ETH -p tcp --dport $step -j DROP
			iptables -A INPUT -i $ETH -p tcp --sport $step -j DROP
			iptables -A OUTPUT -o $ETH -p udp --dport $step -j DROP
			iptables -A OUTPUT -o $ETH -p udp --sport $step -j DROP
			iptables -A INPUT -i $ETH -p udp --dport $step -j DROP
			iptables -A INPUT -i $ETH -p udp --sport $step -j DROP
				echo $step >> $PortClose
				
			fi
		else
#			echo "port en cour de fermeture $step"
			iptables -A OUTPUT -o $ETH -p tcp --dport $step -j DROP
			iptables -A OUTPUT -o $ETH -p tcp --sport $step -j DROP
			iptables -A INPUT -i $ETH -p tcp --dport $step -j DROP
			iptables -A INPUT -i $ETH -p tcp --sport $step -j DROP
			iptables -A OUTPUT -o $ETH -p udp --dport $step -j DROP
			iptables -A OUTPUT -o $ETH -p udp --sport $step -j DROP
			iptables -A INPUT -i $ETH -p udp --dport $step -j DROP
			iptables -A INPUT -i $ETH -p udp --sport $step -j DROP

			echo $step >> $PortClose
		fi
	fi

	fi
fi
if [ "$BreakON" = "OFF" ] ;then
break 1
break 2
fi

sleep $pause2
done < $File 
sleep $pause

if [ "$BreakON" = "OFF" ] ;then
break 0
break 1
break 2
fi
echo "Debug breakon : $BreakON"
done
echo "Firewall stoped"
	;;
  stop)
#sortie de la boucle principale
BreakON="OFF"
echo "fermeture en court $BreakON"
#Mise a zero des regle (par précaution)
#
# On remet la police par défaut à ACCEPT
#
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

#
# On remet les polices par défaut pour la table NAT
#
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT

#
# On vide (flush) toutes les règles existantes
#
iptables -F
iptables -t nat -F

#
# Et enfin, on efface toutes les chaînes qui ne
# sont pas à defaut dans la table filter et nat

iptables -X
iptables -t nat -X
kill 9 $$
#==============================================

	echo "$NAME."

	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: /etc/init.d/iptable_auto {start|stop} " $DAEMON >&2
	exit 1
	;;
esac

exit 0