centos6环境下iptables防火墙控制规则设置

一:SNAT和DNAT原理

1.SNAT 和 DNAT

#SNAT 配合POSTROUTING链使用,修改来源的私有地址为公网地址
#DNAT 配合PREROUTING链使用,修改数据报头目的地址为私有地址

2.vim /etc/sysctl.conf #开启iptables防火墙filter表里的FORWARD链

net.ipv4.ip_forward = 1
运行:sysctl -p命令使设置生效

二:实现要求

(1).内部服务器与:ftp(20,21)/http(80)/snmp(25)/ssh(22)

(2).内部服务器能够解析域名和ping通域名或者IP地址

(3).能够访问NAT服务器上的ssh服务,其余服务都拒绝

三:脚本编写

#!/bin/bash
# firewall.sh
# <说明:本脚本没有启用内核的SYN Cookie模块,对于ping回应请求是通过防火墙的相应设置来阻挡>#
# NAT服务器(eth0 && eth1)
        # 1.外部网络使用eth0网卡,ip:10.10.54.151/24
        # 2.内部网络使用eth1网卡,ip:172.31.31.254/24
# 内部服务器(eth0)
        # 网卡eth0 ip:172.31.31.51/24 gateway:172.31.31.254
###########################第一部分:系统环境设置
# 相关变量设置
 eth_pub="eth0" # public ip网卡
 eth_pri="eth1" # private ip 网卡
ip_out="10.10.54.151"  # NAT服务器对外共有ip
 innet="172.31.31.0/24" # 内部网络
 ip_in="172.31.31.51"   # 内部服务器私有ip
export eth_pub eth_pri innet ip_out ip_in 
# 设置/etc/sysctl.conf 开启filter表的FORWARD链
 sed -i "s/\(net.ipv4.ip_forward\) = 0/\1 = 1/g" /etc/sysctl.conf
##########################第二部分:针对NAT服务器本身的防火墙设置
# 1.清除规则,设置默认策略,开放lo接口
 iptables -F
 iptables -X
 iptables -Z
 iptables -P INPUT DROP         # NAT服务器INPUT链设置原则:关闭所有连接,开放特定服务
 iptables -P OUTPUT ACCEPT
 iptables -P FORWARD ACCEPT
 iptables -A INPUT -i lo -j ACCEPT      # loopback接口
# 2.启动额外的防火墙模块
 if [ -f /usr/local/virus/iptables/iptables.deny ]; then
        sh /usr/local/virus/iptables/iptables.deny
 fi
 if [ -f /usr/local/virus/iptables/iptables.allow ]; then
        sh /usr/local/virus/iptables/iptables.allow
 fi
 if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
        sh /usr/local/virus/httpd-err/iptables.http
 fi
# 3.让NAT服务器通过主动向外发出请求而相应的数据包可以进入本机
iptables -t filter -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 4.允许外部主机访问NAT服务器上的ssh服务,其余全部拒绝
iptables -t filter -A INPUT -p tcp --sport 1024:65534 --dport 22 -j ACCEPT
# 5.仅允许内部服务器ping NAT服务器
iptables -A INPUT -i $eth_pri -p icmp -s $innet -j ACCEPT
##########################第三部分,与内部服务器有关的防火墙设置有关
# 1.清除nat表的规则,设置默认策略
 iptables -F -t nat
 iptables -X -t nat
 iptables -Z -t nat
 iptables -t nat -P POSTROUTING ACCEPT
 iptables -t nat -P PREROUTING ACCEPT
 iptables -t nat -P OUTPUT ACCEPT
# 2.内部服务器开放特定服务:ftp/http/snmp/ssh
 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 20 -j DNAT --to-destination ${ip_in}:20    # ftp/20
 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 21 -j DNAT --to-destination ${ip_in}:21    # ftp/21
 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 80 -j DNAT --to-destination ${ip_in}:80    # http/80
 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 25 -j DNAT --to-destination ${ip_in}:25    # snmp/25
 iptables -t nat -A PREROUTING -i $eth_pub -p tcp --sport 1024:65534 --dport 4000 -j DNAT --to-destination ${ip_in}:22  # ssh/22
# 3.内部服务器可以使用域名解析和ping
 iptables -t nat -A POSTROUTING -o $eth_pub -p udp --sport 1024:65534 --dport 53 -j SNAT --to-source ${ip_out}  # 域名解析
 iptables -t nat -A POSTROUTING -o $eth_pub -p icmp -j SNAT --to-source ${ip_out}       # ping服
务
# 4.存储规则
 /etc/init.d/iptables save


转载于:https://my.oschina.net/zijian1315/blog/194067

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值