linux服务之iptables

关于防火墙的相关设置在咱们的学习中也扮演了很是重要的角色,下面就让咱们一块儿来认识一下防火墙的具体工做方式。html

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

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

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) 规则的编写格式app

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:显示链中的规则编号;
dom

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

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

  • 列出有的防火墙设置
[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
tcp

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

 

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

                         二、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 

                        三、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中的主机名是容许访问的,在策略默认是关着的时候。                     

                         四、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

                        五、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   

                         六、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

                        七、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

                         八、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 

                    九、保存和重载规则:

         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的基础命令已经介绍完了,相关的命令大体就像上边所写的同样,后续内容之后再作更新。

相关文章
相关标签/搜索