|
iptable
[IMG]https://qkzabg.bay.livefilestore.com/y1msMktwAEYV5TNukdplarHCFqlwBHoNZ_xFij_Oxsa3I6G7SXrMTmdg2R2faR1AQZpmEH4aSB6e-BU6c0lEoZwycHFqaFaJ7XS8-I3ggltI2regJeeKBm_Cg5FGNmrnqgwq6M9IZu_YvY6T0FbvT7uUQ/image_thumb[1].png[/IMG]
配置好各服务器的功能。
1、实现nat,让内网客户机可以访问外网服务器
启用路由转发:临时的:echo 1 > /proc/sys/net/ipv4/ip_forward
或者编辑/etc/sysctl.conf把net.ipv4.ip_forward = 1
配置NAT表:iptables -t nat -A POSTROUTING -s 192.168.222.0/24 -o eth0 -j SNAT --to-source 222.222.101.100
配置后 service iptables save进行保存(现将firewall和selinux关闭成功概率更高,配置保存与否影响不大)。
通过客户端测试外网服务器可以正常使用。
将内网服务器发布到外网:iptables -t nat -A PREROUTING -p tcp -d 222.222.101.100 --dport 80 -i eth0 -j DNAT --to-destination 192.168.222.103
若外网接口为拨号,则为iptables设定动态地址:iptables -t nat -A POSTROUTING -s 192.168.222.0/24 -o ppp0 -j MASQUERADE
删除规则:iptables -t nat -D POSTROUTING 2
2、限制用户访问:
首先修改FILTER表,让所有的数据都不能通过:
iptables -t filter -P INPUT DROP
iptables -t filter -P FORWARD DROP
添加需要允许的服务:
DNS:
iptables -t filter -A FORWARD -s 192.168.222.0/24 -o eth0 -p tcp --dport 53 -j ACCEPT
iptables -t filter -A FORWARD -s 192.168.222.0/24 -o eth0 -p udp --dport 53 -j ACCEPT
iptables -t filter -A FORWARD -i eth0 -p tcp --sport 53 -j ACCEPT
iptables -t filter -A FORWARD -i eth0 -p udp --sport 53 -j ACCEPT
WEB:
iptables -t filter -A FORWARD -s 192.168.222.0/24 -o eth0 -p tcp --dport 80 -j ACCEPT
iptables -t filter -A FORWARD -i eth0 -p tcp --sport 80 -j ACCEPT
需要考虑进和出。专表专用,各考虑各的。
:1024表示以下的端口,1024:表示以上的,1024:2000表示二者之间的。
iptables——脚本
一个普通的防火墙配置脚本如下:
#!/bin/bash
#定义变量
IPT="/sbin/iptables"
WAN="222.222.101.100"
LAN="192.168.222.0/24"
ACCEPT_OA="192.168.222.15 192.168.222.16 192.168.222.20 192.168.222.30"
ACCEPT_MAIL="192.168.222.150 192.168.222.160 192.168.222.200 192.168.222.35"
echo 1 > /proc/sys/net/ipv4/ip_forward
$IPT -t nat -A POSTROUTING -s $LAN -o eth0 -j SNAT --to-source $WAN
$IPT -t nat -A PREROUTING -d $WAN -i eth0 -j DNAT --to-destination 192.168.222.103
$IPT -t filter -P FORWARD DROP
$IPT -t filter -A FORWARD -s $LAN -o eth0 -p tcp --dport 53 -j ACCEPT
$IPT -t filter -A FORWARD -s $LAN -o eth0 -p udp --dport 53 -j ACCEPT
$IPT -t filter -A FORWARD -i eth0 -p tcp --sport 53 -j ACCEPT
$IPT -t filter -A FORWARD -i eth0 -p udp --sport 53 -j ACCEPT
编写防火墙脚本
#!/bin/bash
#define all veriance or parameter
WAN_INT="eth0"
WAN_INT_IP="222.222.101.1"
LAN_INT="eth1"
LAN_INT_IP="192.168.222.101"
ALLOW_ACCESS_CLIENT="192.168.222.1 192.168.222.2 192.168.222.3 192.168.222.4 192.168.222.5 192.168.222.6 192.168.222.7"
WAN_WIN2003_SRV="222.222.101.2"
PORT="20,21,25,53,80,110,143,554,1755,7070"
IPT="/sbin/iptables"
##################################################
start(){
echo ""
echo -e "\033[1;032m Flush all chains...... [OK] \033[m"
#Flush all rules at first
$IPT -t filter -F
$IPT -t nat -F
$IPT -t mangle -F
#default policy is drop
$IPT -t filter -P INPUT DROP
$IPT -t filter -P OUTPUT DROP
$IPT -t filter -P FORWARD DROP
#open ssh service
$IPT -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
$IPT -t filter -A OUTPUT -p tcp --sport 22 -j ACCEPT
#SNAT
echo 1 > /proc/sys/nat/ipv4/ip_forward
$IPT -t nat -A POSTROUTING -s 192.168.222.0/24 -o $WAN_INT -j SNAT --to-source $WAN_INT_IP
################# accept erp access #########################
if [ "$ALLOW_ACCESS_CLIENT" != "" ] ; then
for LAN in ${ALLOW_ACCESS_CLIENT} ; do
$IPT -t filter -A FORWARD -p tcp -m multiport -s ${LAN} -o $WAN_INT --dport $PORT -j ACCEPT
$IPT -t filter -A FORWARD -p udp -m multiport -s ${LAN} -o $WAN_INT --dport $PORT -j ACCEPT
$IPT -t filter -A FORWARD -p tcp -m multiport -i $WAN_INT --sport $PORT -j ACCEPT
$IPT -t filter -A FORWARD -p udp -m multiport -i $WAN_INT --sport $PORT -j ACCEPT
echo ""
echo ${LAN} Access to Externel......ACCEPT access Win2003 Server [OK]
done
fi
}
#########################################################
stop(){
###################### Flush everything
$IPT -F
$IPT -X
$IPT -Z
$IPT -F -t nat
$IPT -X -t nat
$IPT -Z -t nat
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
echo "####################################################"
echo "######### ##########"
echo "######### stop firewall server Access rule Successfull! ##########"
echo "######### ##########"
echo "####################################################"
}
###########################################################
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage0 {start|stop|restart}"
exit 1
esac
exit $?
iptables——状态防火墙
-m –state –state 状态
NEW :已经或者即将启动新连接
ESTABLISHED:已连接状态
RELATED:正在两届或已连接
INVALID:不能被识别属于哪个连接或没有任何状态
比如:
放行已经建立的连接:iptables –t filter –I FORWARD 1 –p tcp –m state –state ESTABLISHED –j ACCEPT
拒绝无效链接:iptables –t fitler –I FORWARD 2 –p tcp –m state –state INVALID –j DROP
将所有新建立的连接放入到一个叫NEWCONN的链中:
iptables –t filter –N NEWCONN //创建一个叫NEWCONN的新链
iptables –t filter –I FORWARD 3 –p tcp –m state –state NEW –j NEWCONN
同样将所有的HTTP和FTP连接放入到各自的链中:
iptables –t filter –N FTP
iptables –t filter –N WWW
iptables –t filter –A FORWARD –p tcp –dport 80 –j WWW
iptables –t filter –A FORWARD –p tcp –dport 20:21 –j FTP
扩展应用:
-m multiport //多端口匹配
-m limit --limit (4/s或4/m,每秒每分钟4个包) --limit-burst(缓冲多少包,限制收发)
-m mac –mac-source //捆绑mac
-m tos –tos(0x02、0x04、0x08、0x10) –j TOS --set-tos //服务质量
--tcp-flags All syn //多虑报文字段
-j LOG //进行iptables防火墙日志记录
增加其他模块:
modprobe ip_tables > /dev/null 2>&1
modprobe ip_conntrack > /dev/null 2>&1 |
|