iptables介绍和使用

iptables/netfilter:
    Packets Filter Firewall:
          包过滤型防火墙:
  
Firewall:隔离工具,工作于主机或网络的边缘处,对经由的报文根据预先定义的规则
(识别标准)进行检测,对于能够被规则匹配到的报文实行某种预定义的处理机制的一套组件
    硬件防火墙:在硬件级别实现部分功能的;
    软件防火墙:应用软件逻辑在通用硬件基础上实现;
    主机防火墙:
    网络防火墙:

iptables/netfilter:
    iptables:规则管理工具;
    netfilter:防火墙框架,承载并生效规则;
    hook functions(netfilter):
      prerouting
      input
      forward
      output
      postrouting
      
    iptables:
      PREROUTING
      INPUT
      FORWARD
      OUTPUT
      POSTROUTING
      
    允许用户自定义规则链;它们需要手动关联至指定的”钩子“;
      
netfilter:功能
      filter(“防火”):包过滤
      NAT:Network Address Translation    转发
      mangle:拆解报文,做出修改,而后重新封装
      raw:关闭nat表上启用的连接追踪机制            
      
功能(表)<–>钩子
      filter:input, forward, output
      nat:prerouting, input, output, postrouting
      mangle:prerouting, input, forward, output, postrouting
      raw:prerouting, output
      
      优先级(由高而低):raw –> mangle –> nat –> filter
    
iptables规则的组成部分:
      匹配条件:
        网络层首部:SourceIP, DestinationIP, …
        传输层首部:SourtPort, DestinationPort, TCP Flags
                                         (SYN,ACK,FIN,URG,RST,PSH), …
        扩展模块引入的辅助检查机制:
      跳转目标:-j target 
        内建的处理机制:ACCEPT, DROP, REJECT, SNAT, DNAT, MASQUERADE, 
                        MARK, LOG, …
        用户自定义链:
        
添加规则时需要考量的因素:
        (1) 实现的功能:用于判定将规则添加至哪个表;
        (2) 报文的流经位置:用于判断将规则添加至哪个链;
        (3) 报文的流向:判定规则中何为”源“,何为”目标“;
        (4) 匹配条件:用于编写正确的匹配规则;
          (a) 专用于某种应用的同类规则,匹配范围小的放前面;
          (b) 专用于某些应用的不同类规则,匹配到的可能性较多的放前面
              同一类别的规则可使用自定义链单独存放;
          (c) 用于通用目的的规则放前面;       
    
filter表:过滤,”防火墙“意义的核心所在; INPUT,FORWARD,OUTPUT
  
  安装:
    netfilter:位于内核中的tcp/ip协议栈报文处理框架;
    iptables:
      CentOS 5/6:iptables命令生成规则,可保存于文件中以反复装载生效;
        # iptables -t filter -F
        # service iptables save
           man iptables
      CentOS 7:firewalld, firewall-cmd, firewall-config
        # systemctl disable firewalld.service
                    man iptables
              man iptables-extensions    #扩展模块手册
        
iptables命令:
    iptables [-t table] {-A|-C|-D} chain rule-specification
    iptables [-t table] -I chain [rulenum] rule-specification
    iptables [-t table] -R chain rulenum rule-specification
    iptables [-t table] -D chain rulenum
    iptables [-t table] -S [chain [rulenum]]
    iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options…]
    iptables [-t table] -N chain
    iptables [-t table] -X [chain]
    iptables [-t table] -P chain target
    iptables [-t table] -E old-chain-name new-chain-name

      rule-specification = [matches…] [target]
            match = -m matchname [per-match-options]
            target = -j targetname [per-target-options]     
    
COMMANDs:
      链管理:
        -N, –new-chain chain:新建一个自定义的规则链;
        -X, –delete-chain [chain]:删除用户自定义的引用计数为0的空链;
        -F, –flush [chain]:清空指定的规则链上的规则;
        -E, –rename-chain old-chain new-chain:重命名链;
        -Z, –zero [chain [rulenum]]:置零计数器;
          注意:每个规则都有两个计数器
            packets:被本规则所匹配到的所有报文的个数;
            bytes:被本规则所匹配到的所有报文的大小之和;
        -P, –policy chain target
      规则管理:
        -A, –append chain 
                     rule-specification:追加新规则于指定链的尾部; 
        -I, –insert chain [rulenum] 
            rule-specification:插入新规则于指定链的指定位置,默认为首部
        -R, –replace chain rulenum 
                     rule-specification:替换指定的规则为新的规则;
        -D, –delete chain rulenum:根据规则编号删除规则;
        -D, –delete chain rule-specification:根据规则本身删除规则

      规则显示:
        -L, –list [chain]:列出规则;
          -v, –verbose:详细信息; 
          -n, –numeric:数字格式显示主机地址和端口号;
          -x, –exact:显示计数器的精确值,而非圆整后的数据;
          –line-numbers:列出规则时,显示其在链上的相应的编号;
        -S, –list-rules [chain]:显示指定链的所有规则;  
      
rule-specification = [matches…] [target]
        matches:匹配条件 
      target:跳转目标
      
      匹配条件: 
        通用匹配(PARAMETERS):
          [!] -s, –source address[/mask][,…]:检查报文的源IP地址是否符合此处指定的范围,或是否等于此处给定的地址;
          [!] -d, –destination address[/mask][,…]:检查报文的目标IP
          地址是否符合此处指定的范围,或是否等于此处给定的地址;
          [!] -p, –protocol protocol:匹配报文中的协议,可用值tcp,
           udp,  udplite, icmp,  icmpv6,esp,  ah, sctp, mh 或者  “all”
          亦可以数字格式指明协议;
           -m, –match 
           match:调用指定的扩展匹配模块来扩展匹配条件检查机制;
           [!] -i, –in-interface 
           name:限定报文仅能够从指定的接口流入;only for packets 
                 entering the INPUT, FORWARD  and  PREROUTING  chains.
           [!] -o, –out-interface 
           name:限定报文仅能够从指定的接口流出;for packets entering 
                  the FORWARD, OUTPUT and POSTROUTING chains.
        扩展匹配(MATCH EXTENSIONS)
          -m tcp 
            –sport, –dport
        
      跳转目标:
        -j targetname [per-target-options]
          ACCEPT:接受
          DROP:丢弃
          REJECT:拒绝
          
练习:172.18.0.67
  1、开放本机的所有tcp服务给所有主机;    
              # iptables -I INPUT  -d  172.18.0.67 -p tcp -j ACCEPT
        # iptables -I OUTPUT  -s 172.18.0.67 -p tcp -j ACCEPT 
  2、开放本机的所有udp服务给172.16.0.0/16网络中的主机,但不包含172.16.0.200;
    # iptables -I INPUT 2 -d 172.18.0.67 -s 172.18.0.200 -p udp -j REJECT
    # iptables -I INPUT 3 -d 172.18.0.67 -s 172.18.0.0/16 -p udp -j ACCEPT
    # iptables -I OUTPUT 2 -s 172.18.0.67 -d 172.18.0.0/16 -p udp -j ACCEPT
  3、默认策略为REJECT;
      扩展:
      1、仅开放本机的ssh服务给172.16.0.0/16中的主机,而且不包含172.16.0.200
        
      
回顾:
  iptables/netfilter:
    netfilter:框架
    iptables:管理规则的工具
    
    四表:filter, nat, mangle, raw
    五链:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
      流经位置:
        PREROUTING –> INPUT
        PREROUTING –> FORWARD –> POSTROUTING
        OUTPUT –> POSTROUTING
    
iptables命令:
    iptables [-t table] COMMAND [chain] [PARAMETERS] [-m matchname 
    [per-match-options]] [-j targetname [per-target-options]]
    
    PARAMETERS:
      -s, -d, -p {tcp|udp|icmp|sctp|udplite|…}, -i, -o, -m
      
    COMMAND:
      链管理:-P, -X, -N, -E, -F, -Z
      规则管理:-A,-I,-R,-D
      查看:
        -L,-n, -v, -x, –line-numbers
        -S
        
    -j targetname                  #处理动作
      ACCEPT, DROP, REJECT, RETURN
      
      
    匹配条件:
      基本匹配条件:PARAMETERS
      扩展匹配条件:
        隐式扩展:在使用-p选项指明了特定的协议时,无需再同时使用-m选项指
                  明扩展模块的扩展机制;
        显式扩展:必须使用-m选项指明要调用的扩展模块的扩展机制;
        
      隐式扩展:
        -p tcp:可直接使用tcp扩展模块的专用选项;
          [!] –sport       
             匹配报文源端口;可以给出多个端口,但只能是连续的端口范围 
          [!] –dport
             匹配报文目标端口;可以给出多个端口,但只能是连续的端口范围
          [!] –tcp-flags mask comp 
             匹配报文中的tcp协议的标志位;Flags are: SYN ACK FIN 
                RST URG PSH ALL NONE;
          mask:要检查的FLAGS list,以逗号分隔;
          comp:在mask给定的诸多的FLAGS中,其值必须为1的FLAGS列表,
                余下的其值必须为0;
              –tcp-flags SYN,ACK,FIN,RST  SYN
              –tcp-flags ALL ALL
              –tcp-flags ALL NONE
          [!] –syn:  –tcp-flags SYN,ACK,FIN,RST  SYN  
          
        -p udp:可直接使用udp协议扩展模块的专用选项:
          [!] –sport
          [!] –dport 
          
        [!] –icmp-type {type[/code]|typename}   #互联网控制消息协议
          0/0: echo reply        #回显应答   用0标识
          8/0: echo request      #回显请求
        iptables -A INPUT -d 172.18.12.11 -p icmp –icmp-type 0 -j ACCEPT

      显式扩展:必须使用-m选项指明要调用的扩展模块的扩展机制;
        1、multiport
          以离散或连续的 方式定义多端口匹配条件,最多15个;    
          # iptables -I INPUT  -d 172.16.0.7 -p tcp -m multiport 
                           –dports 22,80,139,445,3306 -j ACCEPT
          
        2、iprange
          以连续地址块的方式来指明多IP地址匹配条件;
          [!] –src-range from[-to]
          [!] –dst-range from[-to]
          
          # iptables -I INPUT -d 172.16.0.7 -p tcp -m multiport 
          –dports 22,80,139,445,3306 -m iprange –src-range 
          172.16.0.61-172.16.0.70 -j REJECT
          
        3、time
          This  matches  if the packet arrival time/date is within 
            a given range.
          
           –timestart hh:mm[:ss]
           –timestop hh:mm[:ss]
           
           [!] –weekdays day[,day…]
           [!] –monthdays day[,day…]
           
          –datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
          –datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
          
           –kerneltz:使用内核配置的时区而非默认的UTC;
           
        4、string
          This modules matches a given string by using some pattern
           matching strategy. 
          
          –algo {bm|kmp}
          [!] –string pattern
          [!] –hex-string pattern
            
          –from offset
          –to offset
          
          ~]# iptables -I OUTPUT -m string –algo bm –string “gay” -j
           REJECT
          
        5、connlimit     #限制客户端的连接数
          Allows  you  to  restrict  the  number  of parallel 
          connections to a server per client IP address 
          (or client address block).
          
          –connlimit-upto n        #达到
          –connlimit-above n       #超过   两者只能使用一个
          
          ~]# iptables -I INPUT -d 172.16.0.7 -p tcp –syn –dport 22
           -m connlimit –connlimit-above 2 -j REJECT
          
        6、limit 
          This  module  matches  at  a limited rate using a token 
          bucket filter. 
          
          –limit rate[/second|/minute|/hour|/day]
          –limit-burst number
          
          ~]# iptables -I OUTPUT -s 172.16.0.7 -p icmp –icmp-type 0 
             -j ACCEPT
          
        7、state
          The “state” extension is a subset of the “conntrack” module.  “state” allows access to the connection tracking state for this packet.
          
          [!] –state state
            INVALID, ESTABLISHED, NEW, RELATED or UNTRACKED.  
            NEW: 新连接请求;
            ESTABLISHED:已建立的连接;
            INVALID:无法识别的连接;
            RELATED:相关联的连接,当前连接是一个新请求,但附属于某个已存在的连接;
            UNTRACKED:未追踪的连接;
                      
            state扩展:
              内核模块装载:
                nf_conntrack
                nf_conntrack_ipv4
                
                手动装载:
                  nf_conntrack_ftp 
                  
          追踪到的连接:
            /proc/net/nf_conntrack
            
          调整可记录的连接数量最大值:
            /proc/sys/net/nf_conntrack_max
            
          超时时长:
            /proc/sys/net/netfilter/timeout
        
  处理动作(跳转目标):
    -j targetname [per-target-options]
      简单target:
        ACCEPT, DROP
        
      扩展target:
        REJECT
          This is used to send back an error packet in response to the matched packet: otherwise it is equivalent to  DROP  so it  is  a  terminating  TARGET,  ending  rule traversal.
          
          –reject-with type
            The type given can be icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreach‐ able, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited (*), which return  the  appropriate ICMP  error  message (icmp-port-unreachable is the default).

      
保存和载入规则:
    保存:iptables-save > /PATH/TO/SOME_RULE_FILE
    重载:iptabls-restore < /PATH/FROM/SOME_RULE_FILE
      -n, –noflush:不清除原有规则
      -t, –test:仅分析生成规则集,但不提交
      
    CentOS 6:
      保存规则:
        service iptables save
        保存规则于/etc/sysconfig/iptables文件,覆盖保存;
      重载规则:
        service iptables restart
        默认重载/etc/sysconfig/iptables文件中的规则 
        
      配置文件:/etc/sysconfig/iptables-config
      
    CentOS 7:
      (1) 自定义Unit File,进行iptables-restore;
      (2) firewalld服务;
      (3) 自定义脚本;
      
  规则优化的思路:
    使用自定义链管理特定应用的相关规则,模块化管理规则;
    (1) 优先放行双方向状态为ESTABLISHED的报文
    (2) 服务于不同类别的功能的规则,匹配到报文可能性更大的放前面;
    (3) 服务于同一类别的功能的规则,匹配条件较严格的放在前面;
    (4) 设置默认策略:白名单机制
      (a) iptables -P,不建议;
      (b) 建议在规则的最后定义规则做为默认策略;

原创文章,作者:shewei,如若转载,请注明出处:http://www.178linux.com/74420

(0)
上一篇 2017-04-28 17:39
下一篇 2017-04-30 00:03

相关推荐

  • N21沉舟第六周作业

    N21沉舟第六周作业 1、  复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; cp /etc/rc.d/rc.sysinit /tmp vim /tmp/rc.sysinit %s/^[[:space:]]\+/#&…

    Linux干货 2016-08-08
  • RPM与YUM命令异同详解

    RPM和YUM都是用来安装程序的工具,但是它们有太多相似的用法以至于有事很难分清应该哪个那就来一起详细的了解一下他们的功能及用法,以及他们的相同以及不同之处。 RPM 软件包管理器功能:安装,卸载,升级,查询,校验,打包,数据库管理RPM在安装时不能处理程序之间复杂的依赖关系RPM可以验证软件包来源的合法性、完整性。常用选项: -i:表示安装。(常用:-iv…

    2017-06-18
  • N26-博客作业-week8

    1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;      在线的主机使用绿色显示;      不在线的主使用红色显示; #!/bin/bash # for i in {1..254}; do if -W 1 -c 1 ping 1…

    Linux干货 2017-03-26
  • 学习宣言

    书山有路勤为径,学海无涯苦作舟。学吧!!!

    Linux干货 2016-10-29