Chinaunix首页 | 论坛 | 博客
  • 博客访问: 37959
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-31 12:31
文章分类

全部博文(17)

分类: 系统运维

2014-04-04 18:36:47

一: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服务,其余服务都拒绝

三:脚本编写

点击(此处)折叠或打开

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


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