Chinaunix首页 | 论坛 | 博客
  • 博客访问: 500261
  • 博文数量: 110
  • 博客积分: 3971
  • 博客等级: 中校
  • 技术积分: 1175
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-20 23:27
文章分类

全部博文(110)

文章存档

2015年(2)

2014年(1)

2013年(1)

2011年(9)

2010年(28)

2009年(12)

2008年(13)

2007年(23)

2006年(21)

我的朋友

分类: LINUX

2006-07-16 22:45:38

设置iptables
[root@linux ~]# vi iptables.sh ← 编辑设定文件
#!/bin/bash
#---------------------------------------#
# 定义
LAN=eth0
#---------------------------------------#
# 取得内部Mask
LOCALNET_MASK=`ifconfig $LAN|sed -e 's/^.*Mask:\([^ ]*\)$/\1/p' -e d`

# 取得内部IP
LOCALNET_ADDR=`netstat -rn|grep $LAN|grep $LOCALNET_MASK|cut -f1 -d' '`
LOCALNET=$LOCALNET_ADDR/$LOCALNET_MASK

# 追加
sed -i '/IPTABLES_MODULES/d' /etc/sysconfig/iptables-config
echo "IPTABLES_MODULES=\"ip_conntrack_ftp\"" >> /etc/sysconfig/iptables-config

# 停止所有规则
/etc/rc.d/init.d/iptables stop

# 设定
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

# 设置SYN Cookies有效
# ※防止TCP SYN Flood攻击
sysctl -w net.ipv4.tcp_syncookies=1 > /dev/null
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf

# ping应答设定
# ※防止Smurf攻击
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 > /dev/null
sed -i '/net.ipv4.icmp_echo_ignore_broadcasts/d' /etc/sysctl.conf
echo "net.ipv4.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf

# 拒绝ICMP Redirect包
sed -i '/net.ipv4.conf.*.accept_redirects/d' /etc/sysctl.conf
for dev in `ls /proc/sys/net/ipv4/conf/`
do
sysctl -w net.ipv4.conf.$dev.accept_redirects=0 > /dev/null
echo "net.ipv4.conf.$dev.accept_redirects=0" >> /etc/sysctl.conf
done

# 拒绝Source Routed包
sed -i '/net.ipv4.conf.*.accept_source_route/d' /etc/sysctl.conf
for dev in `ls /proc/sys/net/ipv4/conf/`
do
sysctl -w net.ipv4.conf.$dev.accept_source_route=0 > /dev/null
echo "net.ipv4.conf.$dev.accept_source_route=0" >> /etc/sysctl.conf
done

# 丢弃分段储存化的包并且记录
iptables -N LOG_FRAGMENT
iptables -A LOG_FRAGMENT -j LOG --log-tcp-options --log-ip-options --log-prefix '[IPTABLES FRAGMENT] : '
iptables -A LOG_FRAGMENT -j DROP
iptables -A INPUT -f -j LOG_FRAGMENT

# NetBIOS相关的访问不记录直接丢弃
iptables -A INPUT -s ! $LOCALNET -p tcp -m multiport --dports 135,137,138,139,445 -j DROP
iptables -A INPUT -s ! $LOCALNET -p udp -m multiport --dports 135,137,138,139,445 -j DROP
iptables -A OUTPUT -d ! $LOCALNET -p tcp -m multiport --sports 135,137,138,139,445 -j DROP
iptables -A OUTPUT -d ! $LOCALNET -p udp -m multiport --sports 135,137,138,139,445 -j DROP

# 1秒4回以上的ping不记录直接丢弃
# ※防止Ping of Death攻击
iptables -N LOG_PINGDEATH
iptables -A LOG_PINGDEATH -m limit --limit 1/s --limit-burst 4 -j ACCEPT
iptables -A LOG_PINGDEATH -j LOG --log-tcp-options --log-ip-options --log-prefix '[IPTABLES PINGDEATH] : '
iptables -A LOG_PINGDEATH -j DROP
iptables -A INPUT -p icmp --icmp-type echo-request -j LOG_PINGDEATH

# 本机访问许可
iptables -A INPUT -i lo -j ACCEPT

# 内部访问许可
iptables -A INPUT -s $LOCALNET -j ACCEPT

# 相对内部的外部的应答许可
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# ※丢弃没用的记录
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP

# 拒绝113端口(IDENT)访问
# ※防止邮件等主机反应衰弱
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset

#----------------------------------------------------------#
#
下面是各公开件组的设定 #
#----------------------------------------------------------#

# ※开放SSH 件组
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# ※开放DNS 件组
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT

# ※开放HTTP 件组
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# ※开放HTTPS 件组
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# ※开放FTP 件组
iptables -A INPUT -p tcp --dport 21 -j ACCEPT

# ※开放PASV 用端口
iptables -A INPUT -p tcp --dport 60000:60030 -j ACCEPT

# ※开放SMTP 件组
iptables -A INPUT -p tcp --dport 25 -j ACCEPT

# ※开放SMTPS 件组
iptables -A INPUT -p tcp --dport 465 -j ACCEPT

# ※开放POP3 件组
iptables -A INPUT -p tcp --dport 110 -j ACCEPT

# ※开放POP3S 件组
iptables -A INPUT -p tcp --dport 995 -j ACCEPT

# ※开放IMAP 件组
iptables -A INPUT -p tcp --dport 143 -j ACCEPT

# ※开放IMAPS 件组
iptables -A INPUT -p tcp --dport 993 -j ACCEPT

#----------------------------------------------------------#
#
各开放件组设定结束 #
#----------------------------------------------------------#

# 设置访问限制
# ※拒绝访问的IP地址记录在/root/deny_ip每一行一个IP,该访问不记录

if [ -s /root/deny_ip ]; then
for ip in `cat /root/deny_ip`
do
iptables -I INPUT -s $ip -j DROP
done
fi

# 拒绝指定国家的访问
# ※各国IP分配情报APNIC()
# ※国家和Country Code的对应 例:台湾=TW %20codes

COUNTRYLIST='TW'
wget -q
iptables -N OTHERFILTER
iptables -A OTHERFILTER -j DROP
for country in $COUNTRYLIST
do
for ip in `cat delegated-apnic-latest | grep "apnic|$country|ipv4|"`
do
FILTER_ADDR=`echo $ip |cut -d "|" -f 4`
TEMP_CIDR=`echo $ip |cut -d "|" -f 5`
FILTER_CIDR=32
while [ $TEMP_CIDR -ne 1 ];
do
TEMP_CIDR=$((TEMP_CIDR/2))
FILTER_CIDR=$((FILTER_CIDR-1))
done
iptables -I INPUT -s $FILTER_ADDR/$FILTER_CIDR -j OTHERFILTER
done
done
rm -f delegated-apnic-latest

# 上面以外的访问记录后丢弃
iptables -A INPUT -j LOG --log-tcp-options --log-ip-options --log-prefix '[IPTABLES INPUT] : '
iptables -A INPUT -j DROP
iptables -A FORWARD -j LOG --log-tcp-options --log-ip-options --log-prefix '[IPTABLES FORWARD] : '
iptables -A FORWARD -j DROP

# 为了再启动后上记规则有效保存
/etc/rc.d/init.d/iptables save

# 防火墙启动
/etc/rc.d/init.d/iptables start



[root@linux ~]# chmod 700 iptables.sh ← 权限变更

启指定国家的Country Code自动更新

[root@linux ~]# vi /etc/cron.daily/otherfilter_check.sh ← IP地址情报检测
#!/bin/bash

# 指定要检测的国家
COUNTRYLIST='TW'

cd /tmp

# 下载APNIC()最新情报
# ※有更新的时候自动下载
wget -q -N

# 检查IP信息
switch=0
for country in $COUNTRYLIST
do
grep "apnic|$country|ipv4|" delegated-apnic-latest > $country.new
diff -q $country $country.new > /dev/null 2>&1
if [ $? -ne 0 ]; then
switch=1
/bin/mv $country.new $country
else
rm -f $country.new
fi
done

cd

# IP情报更新后更新规则
[ $switch -eq 1 ] && /root/iptables.sh > /dev/null

[root@linux ~]# chmod +x /etc/cron.daily/otherfilter_check.sh ← 追加权限

启动防火墙

[root@linux ~]# ./iptables.sh ← 启动防火墙

[root@linux ~]# chkconfig iptables on ← iptables自动启动设定

[root@linux ~]# chkconfig --list iptables ← iptables自动启动
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 确认2~5为on


本文引用地址:

阅读(1221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~