Iptables规则:

    Firewall:工作在主机或网络边缘,对进出的保温按事先定义的规则进行检查,并有匹配到的规则进行处理的一组硬件或者软件.

    用户请求进来,先到本地的route路由判断是否访问的为本地ip,如果不是则查看是否开启了ip-forward功能,如果开起来,则继续向后转发.


    iptables/netfilter:

    netfilter:工作内核中,让规则能够生效的网络框架.

    iptables:工作于用户空间,防火墙规则编写工具,编写规则并发送到netfilter

    PREROUTING:主要做目标地址转换

    INPUT :从本机进来的

    OUTPUT:从本机出去的

    FORWARD:从本机转发的

    POSTROUTING:路由之后转发,主要做源地址转换


    过滤:做报文筛选

    NAT:Network Adress Translation 网络地址转换.

        Dnat:目标地址转换 公网响应,将公网地址自动Dnat到内网.

        Snat:源地址转换 内网地址访问公网资源,出去时将源地址转换为公网地址.


    表和链的对应关系:

        filter:

            INPUT FORWARD OUTPUT

        nat:

            PREROUTING OUTPUT POSTROUTING

        mangle:

            PREROUTING INPUT FORWARD OUTPUT POSTROUTING

    检查条件,处理机制:

        通:默认为堵,只对能识别的进行放行

        堵:默认为通,只对能识别的进行阻截


    检查条件:

    IP:SIP  DIP

    TCP,UDP,ICMP协议首部


    处理机制:

    DROP:悄悄丢弃,不返回任何结果

    REJECT:拒绝,返回拒绝信息


    ACCEPT:允许通过

    SNAT:源地址转换

    DNAT:目标地址转换

    RETURN:未匹配到重新返回

    REDIRECT:映射

    LOG:日志


    Iptables规则参数:

1
  # iptables -nL   查看规则
1
 iptables [-t table] -N chain   #新增一条自定义的规则链(自定义的链只能在被调用或转发时生效)

   -I 插入,默认为第1条   

1
 iptables [-t table] -I chain [rulenum] rule-specification

    iptables [-t table] -X [chain]   #删除一条自定义的空链

    实例:

1
 # iptables -X Mew


    iptables [-t table] -E old-chain-name new-chain-name    #修改自定义链名


    实例:

1
2
3
4
5
6
7
8
9
10
    [root@localhost ~]# iptables -E Mew Redhat
        [root@localhost ~]# iptables -nL
        Chain INPUT (policy ACCEPT)
        target     prot opt source               destination         
        Chain FORWARD (policy ACCEPT)
        target     prot opt source               destination         
        Chain OUTPUT (policy ACCEPT)
        target     prot opt source               destination         
        Chain Redhat (0 references)
        target     prot opt source               destination

  

    iptables [-t table] -P chain target  #修改链的默认策略,无法修改自定义链的默认规则.

    实例:

1
2
# iptables -t filter -P FORWARD DROP
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

         -F :清空链中的规则

             规则有编号,在链中自上而下,从1开始;

         -L: list,列出表中的所有规则

             -n:数字格式显示IP和Port

             -v:以详细格式显示


 实例:

         

1
2
3
4
5
6
7
8
9
  [root@localhost ~]# iptables -nL -v
   Chain INPUT (policy ACCEPT 10936 packets, 1068K bytes)
   pkts bytes target     prot opt in     out     source               destination         
    Chain FORWARD (policy DROP 0 packets, 0 bytes)
    pkts bytes target     prot opt in     out     source               destination         
   Chain OUTPUT (policy ACCEPT 8217 packets, 698K bytes)
    pkts bytes target     prot opt in     out     source               destination         
   Chain Redhat (0 references)
   pkts bytes target     prot opt in     out     source               destination

             pkts:packets,被本规则所匹配到的报文个数

             bytes:被本规则所匹配到的所有报文的大小之和,会执行单位换算.

             target:目标,即处理机制

             prot:协议,一般为{tcp|udp|ICMP}

             opt:可选项

             in:数据包流入接口.

             out:数据包的流出接口

             source:源地址

             destination:目标地址:


            -v

            -vv

            -x:exactly,精确值,不执行单位换算

            --line-number:显示各规则的行号

        实例: --line-number 看到每条规则都有编号

           

1
2
3
4
5
6
7
8
9
10
11
12
 [root@localhost ~]# iptables -nL --line-numbers
            Chain INPUT (policy ACCEPT)
            num  target     prot opt source               destination         
            1    ACCEPT     all  --  172.16.0.0/16        172.16.0.192       
            2    ACCEPT     tcp  --  172.16.0.0/16        172.16.0.192       
            Chain FORWARD (policy DROP)
            num  target     prot opt source               destination         
            Chain OUTPUT (policy ACCEPT)
            num  target     prot opt source               destination         
            1    ACCEPT     tcp  --  172.16.0.192         172.16.0.24         
            Chain Redhat (0 references)
            num  target     prot opt source               destination


            -Z:zero,规则计数器清零


    iptables [-t table] {-A|-D} chain rule-specification

    -A append,附加一条规则;rule-specification 条件匹配 -j 处理机制,默认在指定链的最后一行添加

        匹配条件(通用匹配)

            -s:源ip,可以时ip,也可以是网络地址,可以取反:!172.10.10.0/24,"!"取反

                -s 相当于--src或者--source


            -d:匹配目标地址

            -p:匹配协议,通常只使用{TCP|UDP|ICMP}三者之一

            -i:数据报文流入的接口:通常只用于INPUT/FORWARD/PREROUTING

            -o:流出的接口:通常只用于OUTPUT/FORWARD/POSTROUTING



            实例:         

1
# iptables -A INPUT -p tcp -s 172.16.0.0/16 -d 172.16.0.192 -j ACCEPT

                 所有来自172.16.0.0这个网段的tcp协议访问本地172.16.0.192资源的都允许进来       

1
   # iptables -A OUTPUT -p tcp -s 172.16.0.192 -d 172.16.0.24 -j ACCEPT


          所有从本机 172.16.0.192出去的协议为tcp的目标为172.16.0.24的店铺允许出去.


            -s:源地址不写,默认为4个零

                实例:

1
2
3
4
5
 [root@localhost ~]# iptables -A INPUT -d 172.16.0.192 -j ACCEPT
 [root@localhost ~]# iptables -nL -v
 Chain INPUT (policy ACCEPT 163 packets, 15385 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 23  1752 ACCEPT     all  --  *      *       0.0.0.0/0            172.16.0.192

    

   -i :指定流入网卡,不能在output上使用,否则会报错.               

1
 # iptables -A INPUT -i eth0 -d 172.16.0.192 -j ACCEPT

只要从eth0进来访问本机的就允许


             从本机eth0出去访问公网的就允许.

1
2
3
4
 # iptables -A OUTPUT -o eth0 -s  172.16.0.192 -j ACCEPT
Chain OUTPUT (policy ACCEPT 66 packets, 5489 bytes)
pkts bytes target     prot opt in     out     source               destination         
 4   592 ACCEPT     all  --  *      eth0    172.16.0.192         0.0.0.0/0



            保存恢复规则:

                service iptables save

                规则会被保存至/etc/sysconfig/iptables文件中;



                将火墙规则保存到其他文件中:iptables-save

                # iptables-save > iptables.20170707



                从某个文件中恢复规则,iptables-restore

                # iptables-restore < iptables.20170707



    隐含扩展:使用-p{TCP|UDP|ICMP}指定某特定协议后,协议自身所能够进行的自动扩展

        -p:tcp

            --dport :可以指定单个端口,可以使连续的多个端口.

            --sport: 可以指定源端口---

            --tcp-flags:标志位

                RST,SYN,ACK,FIN

        实例:

            #允许172.16.0.0/16通过tcp协议的22端口访问本地资源

           

1
 [root@localhost ~]# iptables -A INPUT -i eth0 -s 172.16.0.0/16 -d 172.16.0.192 -p tcp --dport 22 -j ACCEPT


   #允许本地通过tcp协议的22端口与172.16.0.0/16通讯.

1
[root@localhost ~]# iptables -A OUTPUT -s 172.16.0.192 -d 172.16.0.0/16 -p tcp --sport 22 -j ACCEPT

            #允许本地lo端口进出.

           

1
2
# iptables -A INPUT -i lo -j ACCEPT
# iptables -A OUTPUT -o lo -j ACCEPT


        -p:    udp

            --sport:

            --dport:

            udp协议没有标志位。


        -p:icmp

            --icmp-type 8是ping请求 0是ping响应


            例子:放行ping其他主机:

       

1
2
     # iptables -A OUTPUT -s 172.16.0.177 -p icmp --icmp-type 8 -j ACCEPT
     # iptables -A INPUT -d 172.16.0.177 -p icmp --icmp-type 0 -j ACCEPT


        规则命令:

            iptables -D chain rulenum [options] 删除规则


            实例:删除OUTPUT链上的第1条规则

1
  # iptables -D OUTPUT 1


        iptables [-t table] -I chain [rulenum] rule-specification   在指定的地方插入规则

        如果不指定默认为第一行.

            实例:在第三行

1
2
# iptables -I INPUT 3 -i lo -j ACCEPT
 # iptables -I INPUT 3 -i eth0 -d 172.16.0.192  -j ACCEPT


        iptables [-t table] -R chain rulenum rule-specification 替换指定规则


        明确指定将第几行替换为.

            实例:      

1
     # iptables -R INPUT 4 -i lo -p tcp -j ACCEPT


        显示指定链的规则:-S 后面未指定具体的链,则显示所有链的规则.

        iptables [-t table] -S [chain [rulenum]]       

        实例:

1
2
3
4
5
6
 # iptables -S INPUT
 -P INPUT ACCEPT
 -A INPUT -s 172.16.0.0/16 -d 172.16.0.192/32 -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
 -A INPUT -i lo -j ACCEPT
 -A INPUT -d 172.16.0.192/32 -i eth0 -j ACCEPT
 -A INPUT -i lo -p tcp -j ACCEPT



    显示扩展:必须明确指定扩展模块


        -m 扩展模块名称 --专用选项


        multiport:多端口匹配,一次指定多个(15个以内)零散端口

             [!] --source-port,--sport port[:port]


             [!] --destination-port,--dport port[:port]

             --ports:


             实例:


             iptables -A INPUT -d 172.16.0.192 -p tcp -m multiport --dport 22,80 -j ACCEPT

             # iptables -A OUTPUT -s 172.16.0.192 -p tcp -m multiport --sport 22,80 -j ACCEPT


        iprange:ip地址范围


        time:指定时间范围


         --datestart:

         --datestop:

         --timestart:

         --timestop:



         实例:

         # iptables -A INPUT -d 172.16.100.7 -p tcp --dport 901 -m time --weekdays Mon,Tus,Wed,Thu,Fri --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT



         string:字符串

         --algo {bm|kmp} :字符匹配时使用到的算法

         --string "string":要查找的字符串

          --from offset


          从本地80端口出去包含字符串"hello"的全丢掉.

1
2
# iptables -A OUTPUT -s 172.16.0.7 -p tcp --sport 80 -m string --algo kmp --string "hello" -j DROP
# iptables -A INPUT -d 192.168.149.128  -p tcp -m multiport --dports 22,80 -j ACCEPT 

 [!] --src-range from[-to]

 [!] --dst-range from[-to]

ip-range:指定ip段访问:

只允许这个网段访问本地的23端口telnet服务.

1
2
 # iptables -A INPUT -d 192.168.149.128 -p tcp --dport 23 -m iprange --src-range 172.16.0.9-172.16.0.110 -j ACCEPT
# iptables -A OUTPUT -s 192.168.149.128  -p tcp --sport 23  -m iprange --dst-range 172.16.0.9-172.16.0.110 -j ACCEPT

time:指定时间daunt

--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]

--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]

--timestart hh:mm[:ss]

--timestop hh:mm[:ss]

[!] --weekdays day[,day...]

1
2
# iptables -A OUTPUT -s 192.168.149.128 -p tcp --sport 91 -m time --weekdays 1,7 --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT
# iptables -A INPUT -d 192.168.149.128 -p tcp --dport 91 -m time --weekdays 1,7 --timestart 08:00:00 --timestop 18:00:00 -j ACCEPT


string:字符串访问控制

--algo {bm|kmp}:字符匹配查找时使用的算法

--string:查找的字符串

[!] --hex-string pattern:要查找的字符串,字符编码转换

--from :

--to

1
# iptables -I OUTPUT -s 192.168.149.128 -p tcp --sport 80 -m string --algo kmp --string "hello" -j DROP

connlimit :每个ip对指定服务的最大连接数

[!] --connlimit-above n

请求本地80端口,并发大于5的请求全部丢掉.

# iptables -I INPUT -d 192.168.149.128 -p tcp --dport 80 -m connlimit --connlimit-above 5 -j DROP


limit:报文的速率控制

--limit-burst number :峰值

--limit rate[/second|/minute|/hour|/day]每秒的速率

1
# iptables -A INPUT -d 192.168.149.128 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -j ACCEPT

hping3 命令

ab -c 20 -n 2000 http://www.baidu.com/index.html  并发测试命令

网络工具排名:

sectools.org

         state:状态匹配

         ip_conntrack,nf_conntrack  Iptables根据这2个模块完成连接追踪


             --state:


                 NEW 状态;第一次连接状态叫NEW


                 ESTABLISHED 连接状态,后续的状态都是ESTABLISHED


                 RELATED 相关联的,命令请求和数据传输之间就是RELATED


                 INVALID 无效连接或者无法识别的连接


        法则:

            1、对于进入的状态为ESTABLISHED的都应该放行

            2、对于出去的状态为ESTABLISHED的都应该放行

            3、严格检查进入的状态为NEW的连接

            4、所有状态为INVIALIED的都应该拒绝



        实例:

        匹配state 状态为NEW,ESTABLISHED的放行.


        调整连接追踪功能所能容纳的最大追踪连接数:


        /proc/sys/net/nf_conntrack_max  定义了onntrack 连接追踪的最大值


        /proc/net/nf_conntrack 定义了当前追踪的所有连接


        /proc/sys/net/netfilter/ 目录定义个了超时时间

1
2
3
4
        # iptables -A INPUT -d 172.16.0.177 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
        # iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
        # iptables -A INPUT -d 172.16.0.177 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 5 -m state --state NEW,ESTABLISHED -j ACCEPT
        # iptables -A OUTPUT -s 172.16.0.177 -p icmp -m state --state NEW,ESTABLISHED -j ACCEPT


        放行本地80端口,进来时state为NEW,ESTABLISHED,出去时状态为 ESTABLISHED

1
2
  # iptables -A INPUT -d 172.16.0.177 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
        # iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT


    -j target

1
2
3
4
5
6
7
8
9
        RETURN:返回调用连
        # iptables -N clean_in
        # iptables -t filter -I clean_in -d 172.16.0.255 -p tcp --tcp-flags ALL ALL -jDROP
        # iptables -t filter -I clean_in -d 172.16.0.255 -p tcp --tcp-flags ALL NONE -jDROP
        # iptables -A clean_in -d 172.16.255.255 -p ucmp -j DROP
        # iptables -A clean_in -d 255.255.255.255 -p ucmp -j DROP
        # iptables -A clean_in -d 172.16.0.177 -j RETURN
        # iptables -I INPUT -d 172.16.0.177 -j RETURN
        # iptables -I clean_in 4 -d 172.16.0.177 -m state --state INVALID -j DROP  无法识别的都应该要删除.


        被动服务器放行例如ftp,服务器被动模式下相应端口随机相应.

        1、确保iptables加载ftp协议支持的模块:ip_nat_ftp,ip_conntrack_ftp

            编辑/rtc/sysconfig/iptables-config文件,定义如下参数:

            IPTABLES_MODULES="ip_nat_ftp,ip_conntrack_ftp"


            手动安装使用:modprobe ip_nat_ftp


        2、方形请求报文的RELATED和ESTABLISHED

1
2
3
4
5
6
7
8
9
10
        # iptables -A INPUT -d 172.16.0.177 -p tcp --dport 22 -j ACCEPT
        # iptables -A INPUT -d 172.16.0.177 -p tcp --dport 21 -j ACCEPT
        # iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 22 -j ACCEPT
        # iptables -A OUTPUT -s 172.16.0.177 -p tcp --sport 21 -j ACCEPT
        # iptables -P INPUT DROP
        # iptables -P OUTPUT DROP
        # iptables -A INPUT -d 172.16.0.177 -m state --state RELATED,ESTABLISHED -j ACCEPT
        # iptables -A INPUT -s 172.16.0.177 -m state --state ESTABLISHED -j ACCEPT
        # iptables -I INPUT 2 -d 172.16.0.177 -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT
        # iptables -I OUTPUT 1 -s 172.16.0.177 -p tcp -m state --state ESTABLISHED -j ACCEPT


本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1946477