10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表应用

10.15 iptables filter 表小案列

mark-需要把80端口、22端口、21端口放行,22端口指定一个ip段,只有这个ip段的ip访问的时候才可以访问到,其他拒绝,这个需求怎么实现,我们用一个脚本来实现,关于shell脚本后面会 讲到 -现在暂时认为脚本就是批量执行的一些命令而已 -首先用vim 编辑脚本文件 vim /usr/local/sbin/iptables.sh 在里面加入如下脚本

[root@aminglinux-001 ~]# vim /usr/local/sbin/iptables.sh#! /bin/bashipt="/usr/sbin/iptables"$ipt -F$ipt -P INPUT DROP$ipt -P OUTPUT ACCEPT$ipt -P FORWARD ACCEPT$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT$ipt -A INPUT -s 192.168.202.0/24 -p tcp --dport 22 -j ACCEPT$ipt -A INPUT -p tcp --dport 80 -j ACCEPT$ipt -A INPUT -p tcp --dport 21 -j ACCEPT
~                                                                                           
~                                                                                           
~                                                                                           
                                                                                    
~                                                                                           
~                                                                                           
~                                                                                           
~                                                                                           
"/usr/local/sbin/iptables.sh" 10L, 317C                                   10,1
  • $ipt 定义了一个变量 路径要写绝对路径 ,以后写shell脚本尽量写全局路径

  • $ipt -F 把之前的规则清空掉,不要了 首先没有指定-t 那默认操作的是是filter表

  • $ipt -INPUT DROP 把默认的策略定义一下,INPUT 是 DROP 的

  • $ipt -OUTPUT 和 FORWARD 都 ACCEPT

  • $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 放行这俩个状态的数据包,tcp/ip ESTABLISHED表示保持连接,RELATED建立完连接之后还有一些额外的连接,这点也需要保持连接 -$ipt -A INPUT -s 192.168.202.0/24 -p tcp --dport 22 -j ACCEPT 把这个网段192.168.202.0/24 的访问 22端口的 数据包放行, -$ipt -A INPUT -p tcp --dport 80 -j ACCEPT ,$ipt -A INPUT -p tcp --dport 21 -j ACCEPT 把80端口和21端口的数据包放行

[root@aminglinux-001 ~]# vim /usr/local/sbin/iptables.sh
[root@aminglinux-001 ~]# w 22:07:42 up 21 min,  2 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21:46   20:54   0.04s  0.04s -bash
root     pts/0    192.168.202.1    21:47    6.00s  0.04s  0.00s w
[root@aminglinux-001 ~]# sh /usr/local/sbin/iptables.sh

[root@aminglinux-001 ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   21  1568 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED    0     0 ACCEPT     tcp  --  *      *       192.168.202.0/24     0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 15 packets, 1444 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-001 ~]#
-还有一个实例,让你ping 外面的机器可以ping通 , -iptables -I -P INPUT -p icmp --icmp-type 8 -j DROP
[root@aminglinux-001 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@aminglinux-001 ~]# ping www.qq.comPING www.qq.com (182.254.74.167) 56(84) bytes of data.
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=1 ttl=128 time=21.1 ms
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=2 ttl=128 time=18.9 ms
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=3 ttl=128 time=11.1 ms
^C
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 11.132/17.074/21.151/4.299 ms
[root@aminglinux-001 ~]#
自己可以ping通 外网 www.qq.com,而外面的机器ping不通 本机 -用windows客户端ping自己ip 自己ip 192.168.202.130
[root@aminglinux-001 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::a152:bbdf:8b2b:db9b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)
        RX packets 1169  bytes 105391 (102.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 842  bytes 120199 (117.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5524 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5524 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@aminglinux-001 ~]#

mark

-删掉刚刚icmp的规则 再 重启iptables 服务(service iptables restart) 再ping 就好了

[root@aminglinux-001 ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
[root@aminglinux-001 ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service
[root@aminglinux-001 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   428 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 512 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-001 ~]#
-如果这个时候再加上刚刚的icmp 规则,这台机器还是可以访问外网的,只是外面的机器不能ping这台机器的ip,因为刚刚添加的规则给禁ping了
[root@aminglinux-001 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@aminglinux-001 ~]# ping www.qq.comPING www.qq.com (182.254.74.167) 56(84) bytes of data.
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=1 ttl=128 time=9.32 ms
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=2 ttl=128 time=10.5 ms
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=3 ttl=128 time=21.3 ms
64 bytes from 182.254.74.167 (182.254.74.167): icmp_seq=4 ttl=128 time=8.00 ms
^C
--- www.qq.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 8.009/12.325/21.375/5.305 ms
[root@aminglinux-001 ~]#

10.16 iptables nat表应用 上

  • nat表应用

  • A机器俩块网卡ens33(192。169.202.130)、ens37(192.168.100.1)、ens33 可以上外网络、ens37 仅仅是内部网络,B机器只有ens37(192.168.100.100)和A机器的ens33可以通信互联

-先做准备工作,先准备俩台虚拟机, mark 1.首先给aminglinux01 添加一个块网卡 mark给这个网卡添加一个LAN区段 markmark mark 2.给aminglinux02 机器也要添加一块网卡,先把之前的 一个外网卡 断开 mark再添加一个块新网卡,也同样添加一个LAN区段 markmark -准备工作做好了 下面启动俩太机器

-这个是第一台机器aminglinux01 ,下面又增加了一个网卡 ens37

[root@aminglinux-001 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::a152:bbdf:8b2b:db9b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)
        RX packets 71  bytes 9446 (9.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 94  bytes 12143 (11.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::1009:3585:635a:3f83  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:55:37:82  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 2052 (2.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 3984 (3.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5524 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5524 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@aminglinux-001 ~]#
下面给aminglinux01 ens37 配置ip 可以直接使用命令 ifconfig ens37 192.168.100.1/24,ip地址为:192.168.100.1
这个ip一重启就没有了,想要永久保存必须修改配置文件
[root@aminglinux-001 ~]# ifconfig ens37 192.168.100.1/24[root@aminglinux-001 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::a152:bbdf:8b2b:db9b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)
        RX packets 169  bytes 18107 (17.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 162  bytes 22201 (21.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:55:37:78  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::20c:29ff:fe55:3782  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:55:37:82  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 6156 (6.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59  bytes 9622 (9.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5524 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5524 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@aminglinux-001 ~]#

-另外一台机器 aminglinux02 没有办法远程,只能虚拟机登录

 mark-

第二台机器ens33虽然在,但是我们已经关掉了, 所以可以先ifdown ens33 我们只需要第二块网ens37

 mark

  • 同样给第二台机器 ens37 配置ip 为 192.168.100.100

  • 下面来ping下 看看能不能ping通192.168.100.1 就是aminglinux01机器

  • 看来是可以ping通的 mark

-同样用aminglinux01 ping 第二台机器aminglinux02 ping 192.168.100.100 也可以ping通

[root@aminglinux-001 ~]# ping 192.168.100.100PING 192.168.100.100 (192.168.100.100) 56(84) bytes of data.
64 bytes from 192.168.100.100: icmp_seq=1 ttl=64 time=0.701 ms
64 bytes from 192.168.100.100: icmp_seq=2 ttl=64 time=0.487 ms
64 bytes from 192.168.100.100: icmp_seq=3 ttl=64 time=0.556 ms
64 bytes from 192.168.100.100: icmp_seq=4 ttl=64 time=0.462 ms
^C
--- 192.168.100.100 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.462/0.551/0.701/0.095 ms
[root@aminglinux-001 ~]#
  •  俩太机器之间可以互通 ,我们的准备工作做好了

10.17 iptables nat表应用 中

-先检测下window机器c 可不可以ping通 192.168.100.1 和 192.168.100.100,实际上是不行的 

mark -

再看下B机器 (aminglinux02)可与不可以连通外网,明显不可以

 mark

  •  需求1:可以让B机器(aminglinux02)连接外网
    
    A机器上(aminglinux01)打开路由转发 echo "1" > /proc/sys/net/ipv4/ip_forward
    
    A上执行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
    
    B机器上 (aminglinux02)设置网关为192.169.100.1
    1.首先A机器打开路由转发,想使用nat表,要想使用这种网络的转发,必须要修改下内核参数,默认这个文件/proc/sys/net/ipv4/ip_forward 内容是0,是0表示它没有开启内核转发, -下面要改成1 echo "1" > /proc/sys/net/ipv4/ip_forward
    [root@aminglinux-001 ~]# cat /proc/sys/net/ipv4/ip_forward0[root@aminglinux-001 ~]# echo "1" > !$echo "1" > /proc/sys/net/ipv4/ip_forward
    [root@aminglinux-001 ~]# cat /proc/sys/net/ipv4/ip_forward1[root@aminglinux-001 ~]#
    2.增加一条规则,有了这条规则就可以实现上网了,可以看到规则在里面了,这个规则就是想让192.168.100.0 可以上网
    [root@aminglinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
    [root@aminglinux-001 ~]# 
    
    [root@aminglinux-001 ~]# iptables -t nat -nvL
    Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 MASQUERADE  all  --  *      ens33   192.168.100.0/24     0.0.0.0/0           [root@aminglinux-001 ~]#
  1. B机器设置网卡,数据包要从A-B 肯定要设置一个网关 route -n 查看网关,没有就添加一下route add default gw 192.168.100.1 mark

  2. 接下来看下能不能ping通192.168.202.1 ,事实证明可以ping通 也可以ping通192.168.202.130

  3.  mark

  4. mark

  • ip段是192.168.100.100,为什么可以ping通192.168.202.1 呢,就意味着可以和外网通信了,这就实现了nat

10.18 iptables nat表应用下

  •  需求2:c机器(windows)只能和A机器(aminglinux01)通信,让C机器可以直接连接B机器的22端口
    
    A机器上(aminglinux01)打开路由转发 echo "1" > /proc/sys/net/ipv4/ip_forward
    [root@aminglinux-001 ~]# echo "1" > /proc/sys/net/ipv4/ip_forward[root@aminglinux-001 ~]# cat /proc/sys/net/ipv4/ip_forward1[root@aminglinux-001 ~]#
    -在这之前,把之前的规则清除掉,没有任何的规则 iptables -t nat -nvL
    [root@aminglinux-001 ~]# iptables -t nat -D POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
    [root@aminglinux-001 ~]# iptables -t nat -nvL
    Chain PREROUTING (policy ACCEPT 14 packets, 4592 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 6 packets, 456 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain POSTROUTING (policy ACCEPT 6 packets, 456 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    [root@aminglinux-001 ~]#
    
    进去的包,A上执行iptables -t nat -A PREROUTING -d 192.168.202.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
    命令理解:将192.168.202.130 端口为1122 进来的数据转发到 192.168.100.100 端口为22
    [root@aminglinux-001 ~]# iptables -t nat -A PREROUTING -d 192.168.202.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
    [root@aminglinux-001 ~]#
    
    出去的包,A上执行 (aminglinux01)iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.202.130
    命令理解:将192.168.100.100 源ip 转发到 192.168.202.130
    [root@aminglinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.202.130
    [root@aminglinux-001 ~]#
  1. B机器上 (aminglinux02)设置网关为192.169.100.1

命令理解:在B机器上增加网关 route add default gw 192.168.100.1route -n 查看网关 

-5.用xshell 远程连接 B机器

Connecting to 192.168.202.130:1122...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Wed Sep  6 21:16:09 2017[root@aminglinux-02 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:88:9f:89  txqueuelen 1000  (Ethernet)
        RX packets 83  bytes 8695 (8.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 96  bytes 8118 (7.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.100  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::20c:29ff:fe88:9f93  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:88:9f:93  txqueuelen 1000  (Ethernet)
        RX packets 139  bytes 14493 (14.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 202  bytes 26685 (26.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>        loop  txqueuelen 1  (Local Loopback)
        RX packets 94  bytes 7728 (7.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 94  bytes 7728 (7.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@aminglinux-02 ~]# ^C
[root@aminglinux-02 ~]# 









扩展1. iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html
iptables 针对一个网段

iptables
iptables -I INPUT -m iprange --src-range 61.4.176.0-61.4.191.255 -j DROP2. sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
(转)iptables中DNAT、SNAT和MASQUERADE的理解


DNAT(Destination Network Address Translation,目的地址转换) 通常被叫做目的映谢。而SNAT(Source Network Address Translation,源地址转换)通常被叫做源映谢。

这是我们在设置Linux网关或者防火墙时经常要用来的两种方式。以前对这两个都解释得不太清楚,现在我在这里解释一下。首先,我们要了解一下IP包的结构,如下图所示:

223011dm15llcit5lplydm.gif

在任何一个IP数据包中,都会有Source IP Address与Destination IP Address这两个字段,数据包所经过的路由器也是根据这两个字段是判定数据包是由什么地方发过来的,它要将数据包发到什么地方去。而iptables的DNAT与SNAT就是根据这个原理,对Source IP Address与Destination IP Address进行修改。然后,我们再看看数据包在iptables中要经过的链(chain):                                               

223042xv22na2tdhnc4acd.png

图中正菱形的区域是对数据包进行判定转发的地方。在这里,系统会根据IP数据包中的destination ip address中的IP地址对数据包进行分发。如果destination ip adress是本机地址,数据将会被转交给INPUT链。如果不是本机地址,则交给FORWARD链检测。

这也就是说,我们要做的DNAT要在进入这个菱形转发区域之前,也就是在PREROUTING链中做,比如我们要把访问202.103.96.112的访问转发到192.168.0.112上:

iptables -t nat -A PREROUTING -d 202.103.96.112 -j DNAT --to-destination 192.168.0.112这个转换过程当中,其实就是将已经达到这台Linux网关(防火墙)上的数据包上的destination ip address从202.103.96.112修改为192.168.0.112然后交给系统路由进行转发。

而SNAT自然是要在数据包流出这台机器之前的最后一个链也就是POSTROUTING链来进行操作

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 58.20.51.66这个语句就是告诉系统把即将要流出本机的数据的source ip address修改成为58.20.51.66。这样,数据包在达到目的机器以后,目的机器会将包返回到58.20.51.66也就是本机。如果不做这个操作,那么你的数据包在传递的过程中,reply的包肯定会丢失。

假如当前系统用的是ADSL/3G/4G动态拨号方式,那么每次拨号,出口IP都会改变,SNAT就会有局限性。

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE

重点在那个『 MASQUERADE 』!这个设定值就是『IP伪装成为封包出去(-o)的那块装置上的IP』!不管现在eth0的出口获得了怎样的动态ip,MASQUERADE会自动读取eth0现在的ip地址然后做SNAT出去,这样就实现了很好的动态SNAT地址转换。3. iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html
iptables限制syn速度

 
syn攻击 iptables
原理,每5s内tcp三次握手大于20次的属于不正常访问。
iptables -A INPUT -s ! 192.168.0.0/255.255.255.0 -d 192.168.0.101 -p tcp -m tcp --dport 80 -m state --state NEW -m recent --set --name httpuser --rsource
iptables -A INPUT -m recent --update --seconds 5 --hitcount 20 --name httpuser --rsource -j DROP

其中192.168.0.0/255.255.255.0 为不受限制的网段, 192.168.0.101  为本机IP。
该iptables策略,可有效预防syn攻击,也可以有效防止机器人发垃圾帖。

猜你喜欢

转载自www.cnblogs.com/pta188/p/9019596.html