`
zqjshiyingxiong
  • 浏览: 433104 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

linux: nat and iptables

阅读更多


设置主机的nat服务:

在/etc/rc.d/rc.local文件里设置
#vi /etc/rc.d/rc.local

在文件的最下面,输入:

echo "1" >/proc/sys/net/ipv4/ip_forward


iptables -t nat -A   POSTROUTING -o ppp0 -j   MASQUERADE

 

-----------------------------------------------控制某个网段是否可以上网,iptables的规则和执行顺序(匹配一次)-------------------------------------------------------------------------------

网络架构一般是一个外网ip,内网的用的是192.168.0.0/16 段的ip。
在网关的机子上配置好了nat以后,要控制某些人不能上网,咋搞咧?呵呵,我把
我的经验写下来吧,搞公司的电脑,搞了几个小时的成果。

比如网关的内网ip是 192.168.0.1  ,客户机的则是0.2等等啦,现在我想让0.3ip的机子可以上网,其他的都不能上,这么来

iptables -I FORWARD 1 -s 192.168.0.2 -d 0.0.0.0/0 -j ACCEPT
iptables -I FORWARD 2 -s 192.168.0.0/24 -d 0.0.0.0/0 -j DROP


说明一下,第一条命令的意思就是:在filt表里面的FORWARD链的第一行加入允许0.2这个ip地址的机子访问任何ip地址。

第二条命令意思:拒绝局域网内所有的机子访问网络。

为什么非要这么设置呢?(执行规则)
大家注意了,iptables防火墙规则是有先后顺序的,防火墙匹配规则是从第一条开始匹配,如果符合,则不再匹配第二条,如果不符合,则接着匹配第二条,同理,如果匹配了,则结束匹配,如果仍然不匹配,则接着向下,一直到匹配为止。
所以这个顺序是相当重要的。


假如现在有个ip为0.3的机子想访问网络,iptables开始匹配第一行,发现不匹配,则继续匹配第二行,这个时候,匹配了,所以执行drop的动作。


-I -D的区别:一个插入一条规则,一个是添加一条规则


同理,如果还想继续添加可以上网的机子,则应该如下命令
iptables -I FORWARD 2 -s 192.168.0.X -d 0.0.0.0/0 -j ACCEPT
这个地方注意了,为什么序号还是2呢?
这个命令的意思是在Forward链的第二行加入一行,等这个命令执行以后,用iptables -L查看的时候,就会发现
iptables -I FORWARD 2 -s 192.168.0.0/24 -d 0.0.0.0/0 -j DROP 这个命令已经变成第三行了,明白了,-I参数的作用就是插入的意思,以后的依次往后推。

最重要的一点,就是要保证最后的一条就是拒绝所有。

还有一点要注意,控制网络访问权限一定要用forward链,因为input链是控制本机的,forward链则是控制转发的链。

 

---------------------控制某一个网段--------------------------------------------------------------------

/sbin/iptables -P FORWARD -j ACCEPT
/sbin/iptables -A FORWARD -m iprange --src-range 192.168.0.3-192.168.0.61 -j DROP

 

低于1.2.4好像无法直接匹配部分IP地址,源地址只能匹配单个IP地址或者IP地址段
只能用个循环来实现了
i=10;
while [ $i -le 50  ]
do
iptables -A FORWARD -s 192.168.0.$i -j DROP
i=`expr $i + 1`
done

 

---------------------------------添加某条规则--------------------------------------------------------

 

在某些情况下,我们可能要禁止某个IP或某个网段的计算机上网。
如果要禁止IP地址为192.168.0.3的客户机上网,可以这样设:
/sbin/iptables -A FORWARD -s 192.168.0.3 -j DROP

如果要禁止192.168.0.0这个子网里所以有客户机上网,可以这样设:
/sbin/iptables -A FORWARD -s 192.168.0.0/24 -j DROP

其中“/24”表示禁止整个C 类网段访问INTERNET.

不过我发现我们公司的用上面的语法不好用,在别的地方好用。不知是为什么呀。

我们公司的语法是这样的才好用呀。语法如下:
如果要禁止IP地址为192.168.0.3的客户机上网,可以这样设:
/sbin/iptables -t nat -A PREROUTING -s 192.168.0.3 -j DROP

如果要禁止192.168.0.0这个子网里所以有客户机上网,可以这样设:
/sbin/iptables -t nat -A PREROUTING -s 192.168.0.0/24 -j DROP

 

-------------------------------删除某条规则------------------------------------------------------------

-A 添加一条规则
-D 删除一条规则
-I 插入一条规则
-F 清空一个链


我们可以用两种办法中的任一种删除规则。 首先,因为知道这是INPUT链中唯一的规则,我们用编号删除:
# iptables -D INPUT 1
删除INPUT链中的编号为1的规则

 

使用--line-numbers参数,列出的表中开头一个是NUM。
然后根据NUM很快删除的。
iptables -D chain rulenum [options]

例如:
列出规则
[root@localhost rc.d]# iptables -L FORWARD --line-numbers
Chain FORWARD (policy DROP)
num  target     prot opt source               destination         
1    REJECT     tcp  --  anywhere             anywhere            tcp dpt:microsoft-ds reject-with icmp-port-unreachable
2    REJECT     tcp  --  anywhere             anywhere            tcp dpt:135 reject-with icmp-port-unreachable
3    REJECT     tcp  --  anywhere             anywhere            tcp dpt:netbios-ssn reject-with icmp-port-unreachable
4    REJECT     udp  --  anywhere             anywhere            udp dpt:microsoft-ds reject-with icmp-port-unreachable
5    REJECT     udp  --  anywhere             anywhere            udp dpt:135 reject-with icmp-port-unreachable
...
...
删除指定行规则:
[root@localhost rc.d]# iptables -D FORWARD 4



第二种办法是 -A 命令的映射,不过用-D替换-A。 当你的链中规则很复杂,而你不想计算它们的编号的时候这就十分有用了。这样的话,我们可以使用:
# iptables -D INPUT -s 127.0.0.1 -p icmp -j DROP
-D的语法必须和-A(或者-I或者-R)一样精确。如果链中有多个相同的规则,只会删除第一个。

 

 

-----------------------置两块网卡时,出现的问题,不听的出现一下信息----------------------------------------------------

http://21nw.com/wk/kernel%3A_martian_source_255.255.255.255

 

NET: 7 messages suppressed.
NET: martian source 192.168.10.255 from 192.168.10.20, on dev eth0
NET: ll header: ff:ff:ff:ff:ff:ff:00:02:55:07:61:fb:08:00
NET: 5 messages suppressed.
.......

 

两块网卡,只设置为同一个网段时,iptables,就会产生以上错误。停止其中一块就不报错了。可能是测试环境才有这样的问题出现。

 

------------------------------------------DNS的问题----------------------------------------------------------------

 

其中有一台机器,ACCEPT,可以上网,但是只能上qq msn,网上地址什么都打不开,后来,直接ping ip地址是可以通的,这样就基本确定是dns的问题了,修改了网关的/etc/resolv.conf,添加了相应的nameserver,但是还是不可以。

几经波折,在主机上直接设置DNS,哈哈,可以了。

 

这应该跟测试环境也有一定的关系。

 

以下为简单firewall set:

 

iptables.rule

#!/bin/bash
#
# ========================================================
#
#       dos2unix iptables.rule
#
#       chmod 755 iptables.rule
# /usr/local/virus/iptables
#       mkdir -p /usr/local/virus/iptables
#       mv / /iptables.rule /usr/local/virus/iptables
#       /usr/local/virus/iptables/iptables.rule
#       iptables -L -n
# /etc/rc.d/rc.local
#       /usr/local/virus/iptables/iptables.rule
#
#       iptables -F
#       iptables -X
#       iptables -t nat -F
#       iptables -t nat -X

#
###########################################################################################

# English: Please input your networks parameters ( including your LAN NIC )
  EXTIF="eth1"
  INIF="eth0"
  INNET="192.168.1.0/24"
  export EXTIF INIF INNET

# These settings is about yourself's paramters.
  allowname=''
  allowip=""
  if [ "$allowname" != "" ]; then
    for siteiptmp in `echo $allowname`
    do
          siteip=`/usr/bin/host $siteiptmp 168.95.1.1    | grep address|tail -n 1 | awk '{print $4}'`
          testip=`echo $siteip | grep [^0-9.]`
          if [ "$testip" == "" ]; then
               allowip="$allowip  $siteip"
          fi
    done
  fi
  export allowip

##########################################################
# First, your server's firewall settings.
# 1. the kernel's firewall settings.
  #  TCP Flooding's setting.  this setting is no good for high loading servers
  echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  # unset reply of ping.
  echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  #
  for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
        echo "1" > $i
  done
  # record some problems packets.
  for i in /proc/sys/net/ipv4/conf/*/log_martians; do
        echo "1" > $i
  done
  #
  for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
        echo "0" > $i
  done
  #
  for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
        echo "0" > $i
  done
  #
  for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
        echo "0" > $i
  done

# 2. clear rule, set the policy rule and allow lo connect.
  PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
  iptables -F
  iptables -X
  iptables -Z
  iptables -P INPUT   DROP
  iptables -P OUTPUT  ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -A INPUT -i lo -j ACCEPT
  iptables -A INPUT -m state --state RELATED -j ACCEPT

# 3. other shell scripts, written by VBird.
  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
  iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

# 4. allow some types of ICMP
#  AICMP="0 3 3/4 4 11 12 14 16 18"
#  for tyicmp in $AICMP
#  do
#       iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
#  done

# 5.
# iptables -A INPUT -p TCP -i $EXTIF --dport  22  -j ACCEPT     # SSH
# iptables -A INPUT -p TCP -i $EXTIF --dport  25  -j ACCEPT     # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --dport  53  -j ACCEPT     # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport  53  -j ACCEPT     # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport  80  -j ACCEPT     # WWW
# iptables -A INPUT -p TCP -i $EXTIF --dport 110  -j ACCEPT     # POP3
# iptables -A INPUT -p TCP -i $EXTIF --dport 443  -j ACCEPT     # HTTPS


########################################################
# Second, the NAT settings.
# 1. loading some good modules of iptables.
  modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc"
  for mod in $modules
  do
        testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
        if [ "$testmod" == "" ]; then
                modprobe $mod
        fi
  done

# 2. clean NAT table's rule
  iptables -F -t nat
  iptables -X -t nat
  iptables -Z -t nat
  iptables -t nat -P PREROUTING  ACCEPT
  iptables -t nat -P POSTROUTING ACCEPT
  iptables -t nat -P OUTPUT      ACCEPT
# 3. NAT server's settings
  if [ "$INIF" != "" ]; then
        iptables -A INPUT -i $INIF -j ACCEPT
        echo "1" > /proc/sys/net/ipv4/ip_forward
        if [ "$INNET" != "" ]; then
                for innet in $INNET
                do
                        #iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
                        iptables -t nat -A POSTROUTING -s $innet -j MASQUERADE
                done
        fi
  fi
# iptables -A FORWARD -s 192.168.1.195 -d www.sina.com.cn -j ACCEPT
# iptables -A FORWARD -s 192.168.1.195 -j DROP
    iptables -A FORWARD -s 192.168.1.195 -m mac --mac-source 00:E0:4C:77:1B:C8 -m limit --limit 25/s -j ACCEPT
# iptables -A FORWARD -m iprange --src-range 192.168.1.196-192.168.1.200 -j DROP
  i=196;
  while [ $i -le 200 ];
  do
        iptables -A FORWARD -s 192.168.1.$i -j DROP
        i=`expr $i + 1`
  done
  iptables -A FORWARD -s 192.168.1.0/24 -j DROP
  # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu

# 4.
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 -j DNAT --to 192.168.1.210:80 # WWW

iptables.allow

#!/bin/bash
#iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT

 iptables.deny

#!/bin/bash
#iptable -A INPUT -i $EXTIF -s 14.116.44.254 -j DROP
 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics