要放假了,今天實在是閑得蛋疼,本來只想寫個配置iptables的指令碼的,沒想到被自己搞得這麼複雜了,還是分享出來給大家,有興趣的可以自己再改改.
ps:本指令碼只支援centos5和centos6,不支援centos 7,並且在指令碼裡也限制了.
指令碼內容:
#!/bin/sh
# By rocdk890
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
#Begin
start(){
sshport=`netstat -ntlp|grep sshd |awk -F: '{if($4!="")print $4}'`
# Require root to run this script.
echo " check user......"
if [[ $(whoami) != root ]];then
echo "Please run this script as root !"
exit 1
fi
echo " check lsb_release....."
chlsb=`rpm -qa|grep redhat-lsb|wc -l`
if [ $chlsb -ge 1 ];then
echo " has installed redhat-lsb."
else
echo " no found. install redhat-lsb......"
yum -y install redhat-lsb
fi
#===============================================================================
#this script is only for CentOS 5 and CentOS 6
#check the OS
#===============================================================================
version=`lsb_release -r |awk '{print substr($2,1,1)}'`
if [ $version != 5 ] || [ $version != 6 ];then
echo "the platform is ok !"
else
echo "this script is only for CentOS 5 or CentOS 6 !"
exit 1
fi
#iptables setting
echo "Iptables Setting..."
/sbin/iptables -P INPUT ACCEPT
#清空所有規則
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat
/sbin/iptables -X -t mangle
#允許來自於lo介面的資料包,通過127.0.0.1訪問本地服務
/sbin/iptables -A INPUT -i lo -j ACCEPT
#允許dns和ntp的資料通過
/sbin/iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
/sbin/iptables -A INPUT -p udp -m udp --sport 123 -j ACCEPT
#限制對內部封包的發送速度
/sbin/iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
#/sbin/iptables -A INPUT -p icmp -m limit --limit 12/min --limit-burst 5 -j DROP
#限制建立聯機的轉寄速度
/sbin/iptables -A FORWARD -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
/sbin/iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
#開放ssh連接埠
/sbin/iptables -A INPUT -p tcp --dport $sshport -j ACCEPT
#開放web服務預設連接埠80
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#開放https服務預設連接埠443
#/sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#開放指定ip,指定連接埠
#/sbin/iptables -A INPUT -p tcp -s 100.240.143.33 --dport 8080 -j ACCEPT
#允許ping
/sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
#允許TCP建立串連,接受成功狀態握手
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A INPUT -m state --state INVALID,NEW -j DROP
#屏蔽規則外請求
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD DROP
BADIPS="`curl -s http://feeds.dshield.org/block.txt | awk '/^[1-9]/ {print $1 "/" $3}'`"
if [ "$BADIPS" ];then
for ip in $BADIPS
do
iptables -I INPUT -s $ip -j DROP
done
fi
}
stop(){
echo "Cleaning your Iptables:..."
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
/etc/init.d/iptables stop
if [ "$?" == "0" ];then
echo "Done!"
fi
}
save(){
echo "Save your Iptables:..."
/etc/init.d/iptables save
/etc/init.d/iptables restart
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop && start
;;
save)
start && save
;;
*)
echo $"Usage: $0 {start|stop|restart|save}"
exit 1
esac
exit 0
#End