linux服务之iptables

关于防火墙的相关设置在我们的学习中也扮演了非常重要的角色,下面就让我们一起来认识一下防火墙的具体工作方式。

(1) 对于iptables命令而言有四表五链之言:
tables:
            filter:过滤,防火墙;默认表
            nat:network address translation;用于修改报文的源地址或目标地址,甚至是端口;
            mangle:拆解报文,做出修改,并重新封装起来;
            raw:关闭nat表上启用的连接追踪机制;

优先级次序(由高而低):
                raw --> mangle --> nat –> filter

chain:
                 PREROUTING
                 INPUT
                 FORWARD
                 OUTPUT
                 POSTROUTING
功能<-->钩子之间对应关系如下:
             raw:PREROUTING,OUTPUT
             mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
             nat:PREROUTING,INPUT,OUTPUT,POSTRUTING
             filter:INPUT,FORWARD,OUTPUT

(2) 规则的编写格式

iptables [-t table] COMMAND chain [-m matchname [per-match-options]] [-j targetname [per-target-options]]
                 -t table:
                默认为filter;其它可用的有raw, mangle, nat;
               COMMAND:
                 链:
                    -P:policy,策略,定义默认策略; 一般有两种选择,ACCEPT和DROP;
                    -N:new,新建一条自定义的规则链;被内建链上的规则调用才能生效;[-j  chain_name];
                    -X:drop,删除自定义的引用计数为0的空链;
                    -F:flush,清空指定的链;
                    -E:重命名自定义的引用计数和为0的链;
                规则:
                    -A:append,追加,在指定链的尾部追加一条规则;
                    -I:insert,插入,在指定的位置(省略位置时表示链首)插入一条规则;
                    -D:delelte,删除,删除指定的规则;
                    -R:replace,替换,将指定的规则替换为新规则;不能仅修改规则中的部分,而是整条规则完全替换;
                查看:
                    -L:list,列出表中的链上的规则;
                        -n:numeric,以数值格式显示;
                        -v:verbose,显示详细格式信息;
                            -vv, -vvv
                         -x:exactly,计数器的精确结果;
                        --line-numbers:显示链中的规则编号;

(3) iptables命令:
规则:根据指定的匹配条件来尝试匹配每个流经此处的报文,一旦匹配成功,就由规则后面指明的处理动作进行处理;
            匹配条件:
                 基本匹配条件:简单检查IP、TCP、UDP等报文的某属性进行匹配的机制;                   
                 扩展匹配条件:需要借助于扩展模块进行的匹配条件指定即为扩展匹配;
            处理动作:
                 基本动作:ACCEPT,DROP, ...
                 扩展动作:需要借助扩展模块进行的动作;
添加规则之时需要考量的问题:
             (1) 报文的流经路径,判断添加规则至哪个链上;
             (2) 确定要实现的功能,判断添加规则至哪个表上;
             (3) 要指定的匹配条件,以用于匹配目标报文;
下面我们再来认识一下iptables命令的具体使用操作吧!        

iptables命令的使用格式:
         iptables [-t table] -I chain [rulenum] rule-specification   列出所有的防火墙策略
         常用的命令是

  • 列出有的防火墙设置
[root@firewall ~]# iptables -L
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   
  • 设置默认的防火墙策略
[root@firewall ~]#iptables -P INPUT DROP   #也可以设置为ACCEPT,不能为REJECT

[root@firewall ~]# iptables -L
Chain INPUT (policy DROP)设置的默认策略为drop
target     prot opt source               destination        

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       
  • 清空防火墙策略除默认

 

[root@firewall ~]# iptables -F 清空所有策略,但不会影响链上的默认策略
  • 追加防火墙策略
[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
  • 追加防火墙策略
[root@firewall ~]#  iptables -I INPUT 1 -p tcp -j ACCEPT
  • 删除防火墙策略
[root@firewall ~]# iptables -D INPUT 2 指定删除那条策略
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere           

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  
  • 替换防火墙策略

 

[root@firewall ~]# iptables -R INPUT 1 -p tcp --dport 80 -j ACCEPT             代表80端口是拒绝的

(4)匹配条件:
      多重条件:逻辑关系为“与”;
      基本匹配条件:
                     [!] -s, --source address[/mask][,...]:检查报文中的源IP地址是否符合此处指定的地址或范围;
                     [!] -d, --destination address[/mask][,...]:检查报文中的目标IP地址是否符合此处指定的地址或范围;
                     [!] -p, --protocol protocol:
                         protocol:{tcp|udp|icmp}
                     [!] -i, --in-interface name:数据报文的流入接口;INPUT, FORWARD  and  PREROUTING
                     [!] -o, --out-interface name:数据报文的流出接口; FORWARD, OUTPUT and POSTROUTING
        扩展匹配条件:
         隐式扩展:不用-m选项指出matchname即可使用此match的专用选项进行匹配;
                         -p tcp:隐含了-m tcp;
                             [!] --source-port,--sport port[:port]:匹配报文中传输层的源端口;
                             [!] --destination-port,--dport port[:port]:匹配报文中传输层的目标端口;
                             [!] --tcp-flags mask comp
                                 SYN,ACK,FIN,RST,URG,PSH;   
                                 mask:要检查的标志位列表,以逗号分隔;
                                 comp:必须为1的标志位列表,余下的出现在mask列表中的标志位则必须为0;
                                 --tcp-flags  SYN,ACK,FIN,RST  SYN
                             [!] --syn:
                                 相当于--tcp-flags  SYN,ACK,FIN,RST  SYN
                         -p udp:隐含了-m udp:
                             [!] --source-port,--sport port[:port]:匹配报文中传输层的源端口;
                             [!] --destination-port,--dport port[:port]:匹配报文中传输层的目标端口;
                         -p icmp:隐含了-m icmp:
                              [!] --icmp-type {type[/code]|typename}
                                 8:echo-request
                                 0:echo-reply

        显式扩展:必须使用-m选项指出matchname,有的match可能存在专用的选项   
         获取帮助:
                             CentOS 7:man iptables-extensions
                             CentOS 6:man iptables
          下面详细的介绍一下扩展模块                  
                         1、multiport扩展          以离散或连续的方式定义多端口匹配

 

[root@firewall ~]# iptables -R INPUT 1 -p tcp -m multiport --dport 21:23,80,53 -j ACCEPT       dport指定目的端口,sport指定源端口

                         2、iprange扩展              以连续的ip地址范围指明连续的多地址匹配条件;
                             [!] --src-range from[-to]:源IP地址;
                             [!] --dst-range from[-to]:目标IP地址;

[root@firewall ~]# iptables -I INPUT 1 -p tcp --dport 22 -m iprange --src-range 192.168.111.101-192.168.111.103 -j ACCEPT  101到103之间的允许连接xshell.
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh source IP range 192.168.111.101-192.168.111.103
ACCEPT     tcp  --  anywhere             anywhere             multiport dports ftp:telnet,http,domain

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  

                        3、set扩展                   依赖于ipset命令行工具;对不连续的ip进行访问设置

[root@firewall ~]# iptables -I INPUT 1 -p tcp --dport 80 -m set --match-set httplist src -j ACCEPT
[root@firewall ~]# iptables -I OUTPUT 1 -p tcp --sport 80 -m set --match-set httplist dst -j ACCEPT

[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http match-set httplist src
ACCEPT     tcp  --  anywhere             anywhere             multiport dports ftp:telnet,http,domain

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp spt:http match-set httplist dst
做测试的话,httplist中的主机名是允许访问的,在策略默认是关着的时候。                        

                         4、string扩展
                         对报文中的应用层数据做字符串匹配检测;

[root@outside html]#  iptables -I INPUT 1 -m string --algo bm --string "google" -j REJECT

[root@inside ~]# curl 172.18.254.72/baidu.com
baidu
[root@inside ~]# curl 172.18.254.72/google.com
^C

                        5、time扩展
                             根据报文到达的时间与指定的时间范围进行匹配度检测;    --kerneltz:使用内核中配置的时区
                            --timestart hh:mm[:ss]
                            --timestop  hh:mm[:ss]

[root@outside html]# iptables -R INPUT 1 -m time --timestart 11:00 --timestop 19:00 --weekdays Mon,Thu -m string --algo bm --string "baidu" -j REJECT
[root@inside ~]# curl 172.18.254.72/baidu.com
^C
[root@inside ~]# curl 172.18.254.72/google.com
goo000le   

                         6、connlimit扩展
                             根据每客户端IP做并发连接数匹配;
                             --connlimit-upto n:连接数数量小于等于n,此时应该允许;
                             --connlimit-above n:连接数数量大于n,此时应该拒绝

[root@outside html]#iptables -A INPUT -d 172.18.254.72 -p tcp --dport 22 -m connlimit --connlimit-upto 2 -j ACCEPT
[root@outside html]# iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 1 -j REJECT

                        7、limit扩展
                             基于收发报文的速率进行匹配;
                          --limit rate[/second|/minute|/hour|/day]:平均速率
                          --limit-burst number:峰值速率

[root@outside html]# iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 10/minute --limit-burst 6 -j ACCEPT
[root@outside html]# iptables -A INPUT -p icmp -j REJECT

[root@inside ~]# ping 172.18.254.72
PING 172.18.254.72 (172.18.254.72) 56(84) bytes of data.
64 bytes from 172.18.254.72: icmp_seq=1 ttl=64 time=0.621 ms
64 bytes from 172.18.254.72: icmp_seq=2 ttl=64 time=2.09 ms
64 bytes from 172.18.254.72: icmp_seq=3 ttl=64 time=0.859 ms
64 bytes from 172.18.254.72: icmp_seq=4 ttl=64 time=0.707 ms
64 bytes from 172.18.254.72: icmp_seq=5 ttl=64 time=0.842 ms
64 bytes from 172.18.254.72: icmp_seq=6 ttl=64 time=0.799 ms
64 bytes from 172.18.254.72: icmp_seq=7 ttl=64 time=0.801 ms
From 172.18.254.72 icmp_seq=8 Destination Port Unreachable
From 172.18.254.72 icmp_seq=9 Destination Port Unreachable
From 172.18.254.72 icmp_seq=10 Destination Port Unreachable
From 172.18.254.72 icmp_seq=11 Destination Port Unreachable
From 172.18.254.72 icmp_seq=12 Destination Port Unreachable
64 bytes from 172.18.254.72: icmp_seq=13 ttl=64 time=0.813 ms

                         8、state扩展
                             状态检测;连接追踪机制(conntrack);
                              INVALID:无法识别的状态;
                             ESTABLISHED:已建立的连接;
                             NEW:新连接;
                             RELATED:相关联的连接;
                             UNTRACKED:未追踪的连接;
如何开放被模式的ftp服务:

(1) 装载追踪ftp协议的模块;
  [root@outside html]#    modprobe nf_conntrack_ftp
(2) 放行命令连接
[root@outside ~]# iptables -I INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT
(3) 放行数据连接
[root@outside ~]# iptables -I INPUT 2 -m state --state ESTABLISHED,RELATED -j ACCEPT 

                    9、保存和重载规则:

         centos7:
         [root@outside ~]# iptables-save > /root/source
         [root@outside ~]# iptables-restore < /root/source
         CentOS 6:
             保存规则:
                 service iptables save
                 自动保存规则至/etc/sysconfig/iptables文件中;
             重载规则:
                 server iptables restore
                 从/etc/sysconfig/iptables文件中重载规则;

 iptables的基础命令已经介绍完了,相关的命令大致就像上边所写的一样,后续内容以后再做更新。


          

 

 

 

posted @ 2018-10-29 21:01  www岩  阅读(564)  评论(0编辑  收藏  举报