zoukankan      html  css  js  c++  java
  • 软件防火墙之iptables扩展模块

    扩展模块的使用帮助:

    CentOS 7,8: man iptables-extensions
    CentOS 6: man iptables
    

    iptables 在使用-p选项指明了特定的协议时,无需再用-m选项指明扩展模块的扩展机制,不需要手动加
    载扩展模块

    tcp 协议的扩展选项

    [!] --source-port, --sport port[:port]:匹配报文源端口,可为端口连续范围
    [!] --destination-port,--dport port[:port]:匹配报文目标端口,可为连续范围
    [!] --tcp-flags mask comp
       mask 需检查的标志位列表,用,分隔 , 例如 SYN,ACK,FIN,RST
       comp 在mask列表中必须为1的标志位列表,无指定则必须为0,用,分隔tcp协议的扩展选项
    

    范例:

    --tcp-flags SYN,ACK,FIN,RST SYN 表示要检查的标志位为SYN,ACK,FIN,RST四个,其中SYN必
    须为1,余下的必须为0,第一次握手
    --tcp-flags SYN,ACK,FIN,RST SYN,ACK 第二次握手
    #错误包
    --tcp-flags ALL ALL
    --tcp_flags ALL NONE
    

    [!] --syn:用于匹配第一次握手, 相当于:--tcp-flags SYN,ACK,FIN,RST SYN

    udp 协议的扩展选项

    [!] --source-port, --sport port[:port]:匹配报文的源端口或端口范围
    [!] --destination-port,--dport port[:port]:匹配报文的目标端口或端口范围
    

    icmp 协议的扩展选项

    [!] --icmp-type {type[/code]|typename}
        type/code
          0/0 echo-reply icmp应答
          8/0 echo-request icmp请求
    

    范例:限制某个固定IP不能ssh端口远程连接,21:23是区间

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.7 -p tcp --dport 21:23 -j REJECT
    

    范例:限制某个固定IP,icmp请求就拒绝

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.7 -p icmp --icmp-type 8 -j REJECT
    

    隐式扩展

    # 在序号3插入一条允许固定ip访问80端口
    [root@localhost ~]# iptables -I INPUT 3 -s 172.31.0.7 -p tcp --dport 80 -j ACCEPT
    

    范例:实现了本机能ping通某个ip,但是对方不能ping通本机

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.18 -p icmp --icmp-type 8 -j REJECT
    

    multiport扩展

    以离散方式定义多端口匹配,最多指定15个端口

    #指定多个源端口
    [!] --source-ports,--sports port[,port|,port:port]...
    # 指定多个目标端口
    [!] --destination-ports,--dports port[,port|,port:port]...
    #多个源或目标端
    [!] --ports port[,port|,port:port]...
    

    范例:使用multiport模块拒绝访问多个不连续的端口号

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.18 -p tcp -m multiport --dports 80,22,21 -j REJECT
    

    iprange扩展

    指明连续的(但一般不是整个网络)ip地址范围

    [!] --src-range from[-to] 源IP地址范围
    [!] --dst-range from[-to] 目标IP地址范围
    

    范例:拒绝某个网段的IP地址范围

    [root@localhost ~]# iptables -A INPUT -m iprange --src-range 172.31.0.1-172.31.0.7 -j REJECT
    

    mac扩展

    mac 模块可以指明源MAC地址,,适用于:PREROUTING, FORWARD,INPUT chains

    [!] --mac-source XX:XX:XX:XX:XX:XX
    

    范例:通过某台机器的mac拒绝

    [root@localhost ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:43:04:9b -j REJECT
    

    string扩展

    对报文中的应用层数据做字符串模式匹配检测

    --algo {bm|kmp} 字符串匹配检测算法
       bm:Boyer-Moore
       kmp:Knuth-Pratt-Morris
    --from offset 开始偏移
    --to offset 结束偏移
    [!] --string pattern 要检测的字符串模式
    [!] --hex-string pattern要检测字符串模式,16进制格式
    

    范例:规则遇到相对应的字符串就拒绝

    [root@localhost ~]# iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --from 62 --string "google" -j REJECT
    

    time扩展

    注意:CentOS 8 此模块有问题
    根据将报文到达的时间与指定的时间范围进行匹配

    --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 日期
    --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
    --timestart hh:mm[:ss] 时间
    --timestop hh:mm[:ss]
    [!] --monthdays day[,day...] 每个月的几号
    [!] --weekdays day[,day...] 星期几,1 – 7 分别表示星期一到星期日
    --kerneltz:内核时区(当地时间),不建议使用,CentOS 7版本以上系统默认为 UTC
    注意: centos6 不支持kerneltz ,--localtz指定本地时区(默认)
    

    范例:某个网段IP不能在某个时间拒绝访问主机80端口

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.0/16 -d 172.31.0.17 -p tcp --dport 80 -m time --timestart 16:00 --timestop 16:10  -j REJECT
    

    connlimit扩展

    根据每客户端IP做并发连接数数量匹配

    可防止Dos(Denial of Service,拒绝服务)攻击

    --connlimit-upto N #连接的数量小于等于N时匹配
    --connlimit-above N #连接的数量大于N时匹配
    

    范例:连接数超过2个就拒绝

    [root@localhost ~]# iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 2 -j REJECT
    

    limit扩展

    基于收发报文的速率做匹配 , 令牌桶过滤器

    --limit-burst number #前多少个包不限制
    --limit #[/second|/minute|/hour|/day]
    

    范例:前5个请求包不限制,后续每1分钟10个,其他不符合规则执行下面的

    [root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 10/minute --limit-burst 5 -j ACCEPT
    
    [root@localhost ~]# iptables -A INPUT -p icmp -j REJECT
    

    state扩展

    state 扩展模块,可以根据”连接追踪机制“去检查连接的状态,较耗资源

    conntrack机制:追踪本机上的请求和响应之间的关系

    状态类型:

    NEW:新发出请求;连接追踪信息库中不存在此连接的相关信息条目,因此,将其识别为第一次发
    出的请求
    ESTABLISHED:NEW状态之后,连接追踪信息库中为其建立的条目失效之前期间内所进行的通信
    状态
    RELATED:新发起的但与已有连接相关联的连接,如:ftp协议中的数据连接与命令连接之间的关
    系
    INVALID:无效的连接,如flag标记不正确
    UNTRACKED:未进行追踪的连接,如:raw表中关闭追踪
    

    记录旧信息路径

    [root@localhost ~]# cat /proc/net/nf_conntrack
    

    范例:老用户允许连接,新用户拒绝连接

    [root@localhost ~]# iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
    [root@localhost ~]# iptables -A INPUT -m state --state NEW -j REJECT
    

    范例:本机可以访问172.31.0.18,但是172.31.0.18不能访问本机

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.18 -m state --state NEW -j REJECT
    

    连接数测试案例:

    # 连接数给一个最少的数值
    [root@localhost ~]# echo 1 > /proc/sys/net/netfilter/nf_conntrack_max
    
    # 查看日志
    [root@localhost ~]# tail -f /var/log/messages
    May  8 16:34:27 localhost kernel: nf_conntrack: table full, dropping packet
    May  8 16:34:28 localhost kernel: nf_conntrack: table full, dropping packet
    ...
    
    # 建议修改适当加大连接数
    [root@localhost ~]# echo 655350 > /proc/sys/net/netfilter/nf_conntrack_max
    

    Target

    target 包括以下类型

    自定义链, ACCEPT, DROP, REJECT,RETURN,LOG,SNAT,DNAT,REDIRECT,MASQUERADE
    LOG:非中断target,本身不拒绝和允许,放在拒绝和允许规则前,并将日志记录在/var/log/messages系
    统日志中
    --log-level level 级别: debug,info,notice, warning, error, crit, alert,emerg
    --log-prefix prefix 日志前缀,用于区别不同的日志,最多29个字符
    

    范例:某个网段访问80,21:23端口的在日志重新生成日志并打印出来到系统日志,提示语句开头new connections:

    [root@localhost ~]# iptables -A INPUT -s 172.31.0.0/16 -p tcp -m multiport --dports 80,21:23 -m state --state NEW -j LOG --log-prefix "new connections: "
    
    查看日志
    [root@localhost ~]# tail -f /var/log/messages 
    May  8 15:34:30 localhost systemd: Started Vsftpd ftp daemon.
    May  8 16:01:01 localhost systemd: Started Session 470 of user root.
     full, dropping packet
    
    May  8 16:55:20 localhost kernel: new connections: IN=eth0 OUT= MAC=00:0c:29:51:72:d9:00:0c:29:43:04:9b:08:00 SRC=172.31.0.18 DST=172.31.0.17 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=60599 DF PROTO=TCP SPT=35722 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0
    

    规则优化最佳实践

    1. 安全放行所有入站和出站的状态为ESTABLISHED状态连接,建议放在第一条,效率更高
    2. 谨慎放行入站的新请求
    3. 有特殊目的限制访问功能,要在放行规则之前加以拒绝
    4. 同类规则(访问同一应用,比如:http ),匹配范围小的放在前面,用于特殊处理
    5. 不同类的规则(访问不同应用,一个是http,另一个是mysql ),匹配范围大的放在前面,效率更
    高
    6. 应该将那些可由一条规则能够描述的多个规则合并为一条,减少规则数量,提高检查效率
    7. 设置默认策略,建议白名单(只放行特定连接)
         iptables -P,不建议,容易出现“自杀现象”
         规则的最后定义规则做为默认策略,推荐使用,放在最后一条
    

    iptables保存规则

    以上命令都是临时生效,持久保存规则需要如下操作:

    方法一:

    # 保存规则
    [root@localhost ~]# iptables-save > /home/iptables.ruls
    
    # 执行导入保存的规则
    [root@localhost ~]# iptables-restore < /home/iptables.ruls
    
    写入开机启动配置
    [root@localhost ~]# vim /etc/rc.d/rc.local
    
    iptables-restore < /home/iptables.ruls
    
    # 加执行权限
    [root@localhost ~]# chmod +x /etc/rc.d/rc.local
    

    方法二:(CentOS7,8)

    # 安装
    [root@localhost ~]# yum install iptables-services
    
    添加到配置文件里面
    [root@localhost ~]# iptables-save > /etc/sysconfig/iptables
    
    启动
    [root@localhost ~]# systemctl start iptables
    设置开机启动
    [root@localhost ~]# systemctl enable --now iptables
    
  • 相关阅读:
    [LeetCode]Subsets II
    [LeetCode]Subsets
    [LeetCode]Combinations
    [LeetCode]Minimum Window Substring
    [LeetCode]Search a 2D Matrix
    [LeetCode]Edit Distance
    [LeetCode]Simplify Path
    Adaboost算法
    [LeetCode]Text Justification
    31、剑指offer--从1到n整数中1出现次数
  • 原文地址:https://www.cnblogs.com/xuanlv-0413/p/14748280.html
Copyright © 2011-2022 走看看