1. 程式人生 > >10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表應用

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

restart user recent 不可 routing hat col back NPU

10.15 iptables filter 表小案列

技術分享圖片-需要把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 ~]#

技術分享圖片

-刪掉剛剛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可以通信互聯

-先做準備工作,先準備倆臺虛擬機, 技術分享圖片 1.首先給aminglinux01 添加一個塊網卡 技術分享圖片給這個網卡添加一個LAN區段 技術分享圖片技術分享圖片 技術分享圖片 2.給aminglinux02 機器也要添加一塊網卡,先把之前的 一個外網卡 斷開 技術分享圖片再添加一個塊新網卡,也同樣添加一個LAN區段 技術分享圖片技術分享圖片 -準備工作做好了 下面啟動倆太機器

-這個是第一臺機器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 沒有辦法遠程,只能虛擬機登錄

技術分享圖片-

第二臺機器ens33雖然在,但是我們已經關掉了, 所以可以先ifdown ens33 我們只需要第二塊網ens37

技術分享圖片

  • 同樣給第二臺機器 ens37 配置ip 為 192.168.100.100

  • 下面來ping下 看看能不能ping通192.168.100.1 就是aminglinux01機器

  • 看來是可以ping通的 技術分享圖片

-同樣用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,實際上是不行的

技術分享圖片 -

再看下B機器 (aminglinux02)可與不可以連通外網,明顯不可以

技術分享圖片

  •  需求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 技術分享圖片

  2. 接下來看下能不能ping通192.168.202.1 ,事實證明可以ping通 也可以ping通192.168.202.130

  3. 技術分享圖片

  4. 技術分享圖片

  • 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攻擊,也可以有效防止機器人發垃圾帖。

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