zoukankan      html  css  js  c++  java
  • linux防火墙扩展模块实战(二)

    iptables扩展模块

    扩展匹配条件:需要加载扩展模块(/usr/lib64/xtables/*.so),方可生效

    查看帮助 man iptables-extensions

    (1)隐式扩展:在使用-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协议的扩展选项

    A主机:192.168.34.101

    B主机:192.168.34.102

    (1)在B主机上先新建一个网页,并启动httpd和mariadb服务

    [root@centos777~]#yum install mariadb-server  httpd  -y
    [root@centos777~]#systemctl start httpd
    [root@centos777~]#systemctl start mariadb
    [root@centos777~]#echo welcome to beijing > /var/www/html/index.html
    

     (2)此时在B主机进行控制其他机器的访问  

    [root@centos777~]#iptables -A INPUT -s 192.168.34.1,127.0.0.1 -j ACCEPT   允许本地windows系统访问     
    [root@centos777~]#iptables -A INPUT -j REJECT   拒绝其他所有主机访问本机
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1       68  4836 ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 4 packets, 432 bytes)
    num   pkts bytes target     prot opt in     out     source               destination 
    

    (3)此时A主机无法访问B主机

    [root@centos7~]#curl 192.168.34.102
    curl: (7) Failed connect to 192.168.34.102:80; Connection refused
    

    (4)此时只允许A主机访问本机的HTTPD服务

    [root@centos777~]#iptables -I  INPUT 3 -s 192.168.34.101 -p tcp --dport 80 -j ACCEPT
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      217 15779 ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        0     0 ACCEPT     tcp  --  *      *       192.168.34.101       0.0.0.0/0            tcp dpt:80
    4        1    60 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 4 packets, 544 bytes)
    num   pkts bytes target     prot opt in     out     source               destination    
    

    (5)查看此时A主机通过tcp协议就可以访问B主机的httpd服务内容

    [root@centos7~]#curl 192.168.34.102
    welcome to beijing
    

    (6)在B主机将mysql数据库允许A主机访问

    [root@centos777~]#iptables -I  INPUT 3 -s 192.168.34.101 -p tcp --dport 3306 -j ACCEPT
    

    (7)在B主机创建一个mysql账号,验证效果

    [root@centos777~]#mysql -e "grant all on *.* to test@'192.168.34.%' identified by 'centos'"
    

    (8)此时在A主机启动自身的mysql数据库,并能连接对方的mysql数据库

    [root@centos7~]#systemctl start mariadb
    [root@centos7~]#mysql -utest -pcentos -h192.168.34.102
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 4
    Server version: 5.5.60-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    MariaDB [(none)]> 

    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

    示例:

    只允许此时有tcp规则(握手)进行拒绝,但可以允许其他方式访问

    [root@centos777~]#iptables -I INPUT 4 -s 192.168.34.100 -p tcp --syn -j REJECT  拒绝C主机进行握手访问
    [root@centos777~]#iptables -I INPUT 5 -s 192.168.34.100  -j ACCEPT  允许C主机能访问
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      634 46456 ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3       11   685 ACCEPT     tcp  --  *      *       192.168.34.101       0.0.0.0/0            tcp dpt:3306
    4        0     0 REJECT     tcp  --  *      *       192.168.34.100       0.0.0.0/0            tcp flags:0x17/0x02
    5        6   398 ACCEPT     all  --  *      *       192.168.34.100       0.0.0.0/0            
    6       34  4423 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 4 packets, 528 bytes)
    num   pkts bytes target     prot opt in     out     source               destination  

    此时在C主机(192.168.34.100)进行访问,此时通过握手协议访问被拒绝

    [root@centos7~]#curl 192.168.34.102
    curl: (7) Failed connect to 192.168.34.102:80; Connection refused
    

    此时在C主机可以ping通

    [root@centos7~]#ping 192.168.34.102
    PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data.
    64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=1.21 ms
    64 bytes from 192.168.34.102: icmp_seq=2 ttl=64 time=0.383 ms
    64 bytes from 192.168.34.102: icmp_seq=3 ttl=64 time=0.379 ms

    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请求
    

    实战演练:实现本机ping对方可以通,对方不能ping通本机,或指定对方能ping通本机

    (1)在本机进行修改防火墙策略

    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num pkts bytes target prot opt in out source destination 
    1 935 69248 ACCEPT all -- * * 192.168.34.1 0.0.0.0/0 
    2 0 0 ACCEPT all -- * * 127.0.0.1 0.0.0.0/0 
    3 39 4843 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num pkts bytes target prot opt in out source destination
    
    Chain OUTPUT (policy ACCEPT 4 packets, 416 bytes)
    num pkts bytes target prot opt in out source destination
    
    [root@centos777~]#iptables -I INPUT 3 -p icmp --icmp-type 0 -j ACCEPT      其中--icmp-type 0意思是本机ping对方是经过INPUT,此时是应答结果

    (2)验证效果,在本机进行ping192.168.34.101,可以Ping通

    [root@centos777~]#ping 192.168.34.101
    PING 192.168.34.101 (192.168.34.101) 56(84) bytes of data.
    64 bytes from 192.168.34.101: icmp_seq=1 ttl=64 time=0.745 ms
    

    (3)在对方进行ping本机IP地址,此时无法ping通

    [root@centos7~]#ping 192.168.34.102
    PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data.
    From 192.168.34.102 icmp_seq=1 Destination Port Unreachable
    From 192.168.34.102 icmp_seq=2 Destination Port Unreachable
    

    (4)将本机的icmp协议改为8,此时对方就可以ping通本机

    [root@centos777~]#iptables -I INPUT 3  -p icmp --icmp-type 8 -j ACCEPT 
    

    (5)在对方机器进行ping结果

    [root@centos7~]#ping 192.168.34.102
    PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data.
    64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=0.630 ms

    显式扩展:必须使用-m选项指明要调用的扩展模块的扩展机制,要手动加载扩展模块

    [-m matchname [per-match-options]]

    显式扩展:必须显式地指明使用的扩展模块进行的扩展

    使用帮助:

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

    1、multiport扩展

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

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

    示例:

    iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.10 -p tcp -m multiport --dports 20:22,80 -j ACCEPT
    

    演练:

    (1)安装samba服务并启动

    [root@centos777~]#yum install samba  -y   安装samba服务
    [root@centos777~]#systemctl start smb
    

    (2)创建一个系统账号并加入到samba服务中,成为samba账号

    [root@centos777~]#useradd -s /sbin/nologin smb1 ; smbpasswd -a smb1 
    New SMB password:
    Retype new SMB password:
    Added user smb1.
    

    (3)创建防火墙规则,此时可以一次性指定两个不连续的端口号,且都在一行显示,方便管理

    [root@centos777~]#iptables -I INPUT 4 -p tcp  -m multiport  --ports 139,445 -j ACCEPT 
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     2044  151K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        2   168 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8
    4       14  2394 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport ports 139,445
    5        1    84 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 0
    6       61  6969 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 26 packets, 3231 bytes)
    num   pkts bytes target     prot opt in     out     source               destination  
    

    此时在另外一台主机就可以登录samba服务

    [root@centos7~]#smbclient //192.168.34.102/smb1 -U smb1%centos 
    Try "help" to get a list of possible commands.
    smb: > 
    

    也可以在本机加入samba的UDP协议端口,由于两个端口号连续,不需要加multiport模块

    [root@centos777~]#iptables -I INPUT 4 -p udp --dport  137:138  -j ACCEPT 

    2、iprange扩展

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

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

    示例:

    iptables -A INPUT -d 172.16.1.100 -p tcp --dport 80 -m iprange --src-range 172.16.1.5-172.16.1.10 -j DROP

    3、mac扩展

    指明源MAC地址

    适用于:PREROUTING, FORWARD,INPUT chains

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

    示例:

    iptables -A INPUT -s 172.16.0.100 -m mac --mac-source 00:50:56:12:34:56 -j ACCEPT
    iptables -A INPUT -s 172.16.0.100 -j REJECT
    

     实战演示:允许B主机通过MAC地址进行ping本机

    A主机:192.168.34.102  

    B主机:192.168.34.101 

    (1)在A主机上设置B主机的MAC地址防火墙规则

    [root@centos777~]#iptables -I INPUT 3 -m mac --mac-source  00:0c:29:4e:31:b6 -j ACCEPT
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     2629  195K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        1    84 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            MAC 00:0C:29:4E:31:B6
    4       81  9753 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 5 packets, 884 bytes)
    num   pkts bytes target     prot opt in     out     source               destination  
    

    (2)在B主机开始ping主机A,此时就可以ping通

    [root@centos7~]#ping 192.168.34.102
    PING 192.168.34.102 (192.168.34.102) 56(84) bytes of data.
    64 bytes from 192.168.34.102: icmp_seq=1 ttl=64 time=0.883 ms
    ^C
    --- 192.168.34.102 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.883/0.883/0.883/0.000 ms

    4、string扩展

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

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

    示例:

    iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT
    

    实战演练:不允许对方主机访问google网页

    (1)在本机先新建几个网页

    [root@centos777~]#echo www.google.com > /var/www/html/google.html
    [root@centos777~]#echo www.google.com > /var/www/html/test.html
    [root@centos777~]#echo welcom to beijing  > /var/www/html/index.html
    [root@centos777~]#cd /var/www/html
    [root@centos777html]#ls
    google.html  index.html  test.html
    

    (2)然后对所有主机设置google关键字样拒绝访问的防火墙规则

    [root@centos777html]#iptables -A OUTPUT -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT 
    [root@centos777html]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     3010  229K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3       25  2006 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            MAC 00:0C:29:4E:31:B6
    4       99 13459 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 32 packets, 2872 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1        0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp spt:80 STRING match  "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
    

    (3)此时在对方主机进行访问本机的网页,此时就无法访问google网页

    5、time扩展

    根据将报文到达的时间与指定的时间范围进行匹配

    --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:内核时区,不建议使用,CentOS7系统默认为UTC,UTC时间与本地时间相差8小时。如:UTC时间是1点,实际时间是9点,centos6默认就是当地时间,不需要加8.

    注意: centos6 不支持kerneltz ,--localtz指定本地时区(默认)

    示例:

    iptables -A INPUT -s 172.16.0.0/16 -d 172.16.100.10 -p tcp --dport 80 -m time --timestart 14:30 --timestop 18:30 --weekdays Sat,Sun  -j DROP
    

    实战演练:

    (1)设置时间模块,指定具体时间段访问网络

    [root@centos777~]#iptables -I INPUT 3 -m time --timestart 1:00 --timestop 10:00 -j ACCEPT   # 只允许在9:00到18:00访问,centos7系统需要加8个小时,才是本地的时间。
    [root@centos777~]#cd
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     3364  255K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            TIME from 01:00:00 to 10:00:00 UTC
    4      202 48180 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 8 packets, 1024 bytes)
    num   pkts bytes target     prot opt in     out     source               destination  
    [root@centos777~]#date   此时的时间不在设置的文件范围内
    Thu Dec 5 22:49:46 CST 2019

    (2)其他主机访问此主机的网页是就会被拒绝

    [root@centos7~]#curl 192.168.34.102
    curl: (7) Failed connect to 192.168.34.102:80; Connection refused

    6、connlimit扩展

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

    缺点:就是黑客用不同的IP地址进行访问,就无法针对连接数进行阻挡。

    可防止Dos(Denial of Service,拒绝服务)攻击 --connlimit-upto #:连接的数量小于等于#时匹配

    --connlimit-above #:连接的数量大于#时匹配

    通常分别与默认的拒绝或允许策略配合使用

    示例:

    iptables -A INPUT -d 172.16.100.10 -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
    

    实战演练:防止DOS攻击,制定防火墙策略

    (1)在本机设置防火墙规则

    [root@centos777~]#iptables -A INPUT -p tcp  --dport 80 -m connlimit --connlimit-above 100 -j REJECT   制定防火墙规则,访问次数大于100的被拒绝
    [root@centos777~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     4148  325K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3        0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 #conn src/32 > 100 reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 13 packets, 1900 bytes)
    num   pkts bytes target     prot opt in     out     source               destination 
    

    (2)此时在对方主机访问本机小于100次连接的都可以访问网页

    [root@centos7~]#curl 192.168.34.102
    welcome to beijing

    7、limit扩展

    基于收发报文的速率做匹配

    令牌桶过滤器

    --limit #[/second|/minute|/hour|/day]
    --limit-burst number

    实战演练:

    [root@centos777~]#iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 10 -j ACCEPT  接收规则,并允许前10个访问网页
    [root@centos777~]#iptables -A INPUT -j REJECT  剩下的全部拒绝
    

    8、state扩展

    根据”连接追踪机制“去检查连接的状态,较耗资源

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

    状态有如下几种:

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

    示例:

    老用户通过ssh可以连接远程主机

    设置老用户连接不被拒绝,但是老用户通过ssh连接的主机退出后就无法连接,新用户连接就被拒绝防火墙

    [root@centos7~]#iptables -I INPUT 3 -p tcp --dport 22 -m state --state ESTABLISHED,RELATED -j ACCEPT
    

    由于设置了防火墙,新用户无法连接

    [!] --state state

    示例:

    iptables -A INPUT -d 172.16.1.10 -p tcp -m multiport --dports 22,80 -m state --state NEW,ESTABLISHED -j ACCEPT
    iptables -A OUTPUT -s 172.16.1.10 -p tcp -m multiport --sports 22,80 -m state --state ESTABLISHED -j ACCEPT

    已经追踪到的并记录下来的连接信息库

    /proc/net/nf_conntrack

    调整连接追踪功能所能够容纳的最大连接数量

    /proc/sys/net/nf_conntrack_max

    不同的协议的连接追踪时长

    /proc/sys/net/netfilter/

    注意:CentOS7 需要加载模块: modprobe nf_conntrack_ipv4

    /proc/sys/net/nf_conntrack_max:连接跟踪的最大连接数

    可以将此参数写在配置文件中,永久生效:

    vim /etc/sysctl.conf

    net.nf_conntrack_max=88888   临时修改到88888

    修改完配置文件之后,使配置文件生效:

    [root@centos7~]#sysctl -p
    net.nf_conntrack_max = 88888

    iptables的链接跟踪表最大容量为/proc/sys/net/nf_conntrack_max,各种状态的超时链接会从表中删除;当模板满载时,后续连接可能会超时

    解决方法两个:

    (1) 加大nf_conntrack_max 值

    vim /etc/sysctl.conf
    net.nf_conntrack_max = 393216
    net.netfilter.nf_conntrack_max = 393216

    (2) 降低 nf_conntrack timeout时间

    vim /etc/sysctl.conf
    net.netfilter.nf_conntrack_tcp_timeout_established = 300
    net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
    net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
    net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
    iptables -t nat -L -n
    

    开放被动模式的ftp服务

     (1) 装载ftp连接追踪的专用模块:
       跟踪模块路径:/lib/modules/kernelversion/kernel/net/netfilter

    vim /etc/sysconfig/iptables-config 配置文件
    IPTABLES_MODULES=“nf_conntrack_ftp"

    modproble nf_conntrack_ftp   # 加载此模块

    (2) 放行请求报文:

    命令连接:NEW, ESTABLISHED
    数据连接:RELATED, ESTABLISHED

    iptables –I INPUT -d LocalIP -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -A INPUT -d LocalIP -p tcp --dport 21 -m state --state NEW -j ACCEPT

     (3) 放行响应报文:

    iptables -I OUTPUT -s LocalIP -p tcp -m state --state ESTABLISHED -j ACCEPT

    实战演示:开放被动模式的ftp服务

    A主机:192.168.34.101

    B主机:192.168.34.102

    (1)在A主机先添加一个允许tcp协议,21端口连接的访问

    [root@centos7~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      445 34224 ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3       40  5213 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state RELATED,ESTABLISHED
    4        3   320 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 77 packets, 8175 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    [root@centos7~]#iptables -I INPUT 3 -p tcp --dport 21 -j ACCEPT 
    

    (2)在A主机安装vsftpd服务并启动服务

    [root@centos7~]#yum install vsftpd -y
    [root@centos7~]#systemctl start vsftpd
    

    (3)此时在B主机只能连接A主机的ftp服务器,被动模式的端口号是随机的,A主机不能添加指定的tcp协议端口号,因此B主机不能执行其他操作。

     

     (4)在A主机加载ftp相关模块,能识别FTP协议,能分析ftp21端口号的数据传输的信息,从而能够得知下次通讯过程中被动模式使用的端口号是多少

    [root@centos7~]#modprobe nf_conntrack_ftp
    

      

     (5)在A主机添加一个iptables防火墙规则,注意:ESTABLISHED,RELATED和tcp 21协议的合理性,将tcp 21的防火墙规则放在后面较好,当用户访问大量数据时,提高效率,优化性能方面可以考虑。

    [root@centos7~]#iptables -I INPUT 3 -m state --state  ESTABLISHED,RELATED -j ACCEPT

      

     (6)最后在B主机验证连接ftp效果,此时就可以访问文件

     

    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个字符

    收集指定的主机访问本机的log日志

    (1)在A主机配置一个防火墙规则

    [root@centos7~]#iptables -I INPUT 4 -s 192.168.34.102 -j LOG --log-prefix "from 34.102 access:"
    [root@centos7~]#iptables -vnL --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1     1467  110K ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
    2        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    3       17   939 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    4        0     0 LOG        all  --  *      *       192.168.34.102       0.0.0.0/0            LOG flags 0 level 4 prefix "from 34.102 access:"
    5       10  1226 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 32 packets, 2856 bytes)
    num   pkts bytes target     prot opt in     out     source               destination 
    

    (2)在B主机访问当前的信息,就会在系统日志中记录来自于B主机的信息 

     

     (3)在A主机进行log日志跟踪,可以看到跟踪的日志信息

     

    iptables防火墙规则总结

    任何不允许的访问,应该在请求到达时给予拒绝

    规则在链接上的次序即为其检查时的生效次序

    基于上述,规则优化

    1 安全放行所有入站和出站的状态为ESTABLISHED状态连接
    2 谨慎放行入站的新请求
    3 有特殊目的限制访问功能,要在放行规则之前加以拒绝
    4 同类规则(访问同一应用),匹配范围小的放在前面,用于特殊处理
    5 不同类的规则(访问不同应用),匹配范围大的放在前面    例如:将一个网段的IP地址放在前面,包含在此网段的IP地址放在后面
    6 应该将那些可由一条规则能够描述的多个规则合并为一条
    7 设置默认策略,建议白名单(只放行特定连接)
            1) iptables -P,不建议
            2) 建议在规则的最后定义规则做为默认策略
    

    规则有效期限:

    使用iptables命令定义的规则,手动删除之前,其生效期限为kernel存活期限

    保存规则:

    保存规则至指定的文件

    CentOS 7

    (1)将防火墙规则保存到指定的文件中

    [root@centos7~]#iptables-save > /data/iptables.rule  保存到data目录下
    [root@centos7~]#iptables -F   清空防火墙规则之后
    [root@centos7~]#iptables-restore < /data/iptables.rule  从保存的文件中导出,即可恢复之前的防火墙策略
    [root@centos7~]#iptables -vnL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
       24  1792 ACCEPT     all  --  *      *       192.168.34.1         0.0.0.0/0           
        0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
        0     0 LOG        all  --  *      *       192.168.34.102       0.0.0.0/0            LOG flags 0 level 4 prefix "from 34.102 access:"
        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes)
     pkts bytes target     prot opt in     out     source               destination   

    (2)将本地开机启动加执行权限,并将执行的文件存在此配置文件中,开机启动即可

    [root@centos7~]#chmod +x /etc/rc.d/rc.local  给开机启动的本地服务加上执行权限
    [root@centos7~]#vim /etc/rc.d/rc.local   修改本地开机配置文件信息

     

    CentOS 6防火墙保存规则

    service iptables save
    将规则覆盖保存至/etc/sysconfig/iptables文件中
    

    然后设置为开机启动

    chkconfig iptables on
    

      

      

      

     

      

     

      

      

     

      

     

     

      

     

      

     

      

     

      

     

     

      

      

     

      

     

     

     

      


  • 相关阅读:
    APP测试-流量测试
    APP测试-流畅度测试
    APP测试-耗电分析
    工具安装-Homebrew
    工具安装-go for Mac
    APP测试-耗电量测试
    APP测试-CPU测试
    APP测试-内存测试
    APP测试-monkey
    APP测试-adb命令
  • 原文地址:https://www.cnblogs.com/struggle-1216/p/11991820.html
Copyright © 2011-2022 走看看