美文网首页
iptables详解

iptables详解

作者: ckhzw | 来源:发表于2017-05-02 13:03 被阅读0次

    netfilter 内核中的防火墙框架,承载并生效规则;4表5链;

    • netfilter功能:
      。 filter 包过滤
      。 NAT 网络地址转换
      。mangle 拆解报文,作出修改,而后重新封装;
      。 raw 关闭nat表上启用的连接追踪机制;

    iptables 规则管理工具;

    内核中的TCP/IP协议栈上,本生并没有防护的功能,netfilter在TCP/IP协议栈上设置几道关卡,勾住流经的报文;根据在其上使用iptables命令设置的规则,作出相应的操作;

    • 表(功能):
      。filter:INPUT,FORWARD,OUTPUT
      。 nat:PREROUTING,INPUT,OUTPUT,POSTROUTING
      。mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
      。raw:PREROUTING,OUTPUT

    • 链:
      。PREROUTING
      。 INPUT
      。 FORWARD
      。 OUTPUT
      。 POSTING

    • 报文的流向:
      流入:PREROUTING-->INPUT
      转发:PREROUTING-->FORWARD-->POSTROUTING
      流出:OUTPUT-->POSTROUTING

    • 添加规则时需要考量的因素:

      1.实现的功能:用于判定将规则添加至哪个表;
      2.报文的流经位置:用户判断将规则添加至哪个链;
      3.报文的流向:判定规则中何为源,何为目标;
      4.匹配条件:用户编写正确的匹配规则;
      5. 专用于某种应用的同类规则,匹配范围小的放在前面;
      6.专用于某些应用的不同规则,匹配到的可能性较多的放在前面;同类别的规则可以使用自定义链单独存放;
      7.用于通用目录的规则放在前面;
    

    iptables命令语法格式:

    iptables [-t TABLE] COMMAND CHAIN 基本匹配条件 [-m 扩展匹配条件] -j 处理动作
    -t TABLE 默认filter表

      COMMAND :
        链管理:
          -N CHAIN:用户自定义链,被调用后才能使用;
          -X CHAIN 删除用户自定义的且引用计数为0的空链
          -E old-chain new-chain 重命名自定义链,且引用计数为0;
          -P chain target 设置默认策略;
          -F [chain] 清空链上的规则,默认为清空表上的所有链上的规则;
          -S chain 列出指定链的规则;
          -Z chain [rulenum] 将指定链上的计数器至零; 被匹配到的包个数和所有被本规则匹配到的总字节数
        规则显示:-vnL 
              -vnL --line-numbers 列出规则是,显示其在链上的相应的编号;
                -S chain 显示指定链上的所有规则
        规则管理:增删改查
          -A CHAIN 追加一条规则;
          -I CHAIN NUM 在指定的位置插入一条规则;
          -R CHAIN NUM 替换指定的规则
          -D chain num 根据编号删除规则
          -D chain rule 根据规则删除
    
    

    通用匹配条件:

      -d 目标地址;
      -s 源地址;
      -p tcp|udp|icmp 指定协议
      -i IFACE 报文流入接口;PREROUTING,INPUT,FORWARD
      -o IFACE 报文流出接口:OUTPUT,FORWARD,POSTROUTING 
    

    扩展匹配条件:

    • 隐式扩展:-p 指定协议后,无需使用-m指定扩展模块;
      (1)tcp隐式扩展:-p tcp
      --dport 目标端口,可以是连续的端口范围;例 --dport 22:23 表示22到23号3个端口;
      --sport 源端口
      --tcp-flags FLAGS comp;
      FLAGS:SYN,ACK,FIN,RST
      comp:表示前面的标志位列表中,哪些标志位必须为1;其他的在标志位列表中出现的标志位都为0;
      例:
      匹配三次握手的第一次: -p tcp --tcp-flags SYN,ACK,FIN,RST SYN
      可简写为:-p tcp --syn
      非法报文:
      -p tcp --tcp-flags ALL ALL
      -p tcp --tcp-flags NONE NONE

        (2)udp隐式扩展:-p udp 
           --dport 目标端口;
           --sport 源端口;
           
        (3)icmp隐式扩展: -p icmp 
           --icmp-type 0|8
             0:响应
             8:请求
      
    • 显示扩展:-p 指定协议后,必须使用-m指定扩展模块;
      1.多端口:multiport,最多15个;
      -m multiport --dports|--sports port[:port],port,.... //可以是连续或者离散的端口;
      示例:iptables -A INPUT -d 172.18.20.7 -p tcp -m multiport --dports 22,80,443 -j ACCEPT
      2.时间:time
      -m time --timestart HH:MM:SS --timestop HH:MM:SS
      -m time --datestart|--datestop YYYY-MM-DD HH:MM:SS
      -m time --weekdays MON,TUS,WES,FRI,STA,SUN
      --kerneltz 使用内核配置的时区,而非默认的UTC
      示例:iptables -A INPUT -d 172.18.20.7 -p tcp --dport 80 -m time --timestart 14:00:00 --timestop 15:00:00 --kerneltz -j REJECT

        3.匹配字符串:string 
          -m string --algo bm --string "STRING"  //algo指定算法(必须指定);用来过滤网页中的字符串;
          示例:iptables -A INPUT -s 172.18.20.7 -p tcp --sport 80 -m string --algo bm --string gay -j REJECT 
          
        4.匹配状态:state 
          -m state --state STATE 
            STATE:ESTABLISHED,NEW,RELATED(相关联的连接),INVAILED 
            注意:开放ftp服务:开放21号端口,并且状态为ESTABLISHED,RELATED开放后,需要手动加载ftp的追踪模块: modprobe nf_conntrack_ftp 
        5.iprange 以连续地址块的方式来指明多ip地址匹配条件;
          --src-range start-stop
          --dst-range start-stop
          示例:iptables -A INPUT -d 172.18.20.7 -p tcp --dport 23 -m iprange --src-range 172.18.20.1-172.18.20.7 
        
        6.connlimit 限制每个客户端最大连接数
          --connlimit-above 如果默认策略为允许,此操作表示拒绝;
          --connlimit-upto  如果默认策略为拒绝,此操作表示允许;
          示例:
            172.18.20.7默认策略为开放ssh
            iptables -I INPUT 2 -d 172.18.20.7 -p tcp --dport 22 -m connlimit --connnlimit-above 2 -j REJECT //每个客户端仅能连接ssh服务两次
         
         7.limit 限制ping包流量
          --limit 20/minute --limit-burst 4 
          示例:
            iptables -A INPUT -d 172.18.20.7 -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 4 -j ACCEPT //客户端一次最多发起3个ping请求,--limit-burst 4 空闲时,客户端一个可以发送4个ping请求;
      

    规则的保存和还原:

        保存规则:
          iptables-save >iptables
        还原规则:
          iptables-restore <iptables 
    

    示例:

    开放ssh给所有主机,开放samba给172.18.20.6 
    [root@CentOS7 ~]# iptables -F  
    [root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -p tcp --dport 22 -j ACCEPT  
    [root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -j REJECT
    [root@CentOS7 ~]# iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 59 packets, 6120 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      286 19032 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:22
    2       33  2712 REJECT     all  --  *      *       0.0.0.0/0            172.18.20.7          reject-with icmp-port-unreachable
    [root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p udp --dport 137:138 -j ACCEPT 
    [root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p tcp --dport 139 -j ACCEPT 
    [root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p tcp --dport 445 -j ACCEPT 
    [root@CentOS7 ~]#iptables -A OUTPUT -d 172.18.20.7 -j REJECT
    [root@CentOS7 ~]# iptables -vnL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
     1276 95960 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:22
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:445
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:139
        0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            172.18.20.7          udp dpts:137:138
       39  3052 REJECT     all  --  *      *       0.0.0.0/0            172.18.20.7          reject-with icmp-port-unreachable
    
    #自定义smb_input_rules 链开放ssh给所有客户端,拒绝所有连接,允许172.18.20.7访问samba服务
    [root@CentOS7 ~]# iptables -F 
    [root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -p tcp --dport 22 -j ACCEPT 
    [root@CentOS7 ~]# iptables -N smb_input_rules 
    [root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p udp --dport 137:138 -j ACCEPT 
    [root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p tcp --dport 139 -j ACCEPT 
    [root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p tcp --dport 445 -j ACCEPT 
    [root@CentOS7 ~]# iptables -vnL smb_input_rules 
    Chain smb_input_rules (0 references)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            172.18.20.7          udp dpts:137:138
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:139
        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:445
    [root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -j smb_input_rules 
     [root@CentOS7 ~]iptables -A INPUT -d 172.18.20.7 -j REJECT 
    [root@CentOS7 ~]# iptables -vnL OUTPUT 
    Chain OUTPUT (policy ACCEPT 44 packets, 5080 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    #删除自定义链(smb_input_rules)
    [root@CentOS7 ~]# iptables -D INPUT 2   #删除引用计数
    [root@CentOS7 ~]# iptables -F smb_input_rules  #清空规则
    [root@CentOS7 ~]# iptables -X smb_input_rules  #删除自定且引用计数为0的空链
    

    相关文章

      网友评论

          本文标题:iptables详解

          本文链接:https://www.haomeiwen.com/subject/llmazttx.html