Linux防火墙安全设定专题详解篇:iptables和firewalld安全规则设定

一,防火墙iptables(centos6版本)

1,防火墙分类

逻辑分类:个人主机防火墙和集体网络防火墙
物理分类:硬件防火墙(性能高成本高)和软件防火墙(性能低成本低)

iptables本身不是防火墙,但是它可以通过该命令进行安全设定,然后由对应的
防火墙安全框架来执行,这个框架是"netfilter"
netfilter  内核态  位于内核空间
iptables   用户态  位于用户空间

2,四表五链

四表:
     raw            追踪数据包
     mangle      对数据包打标记
     nat             地址转换
     filter           数据包过滤

     五链:
     PREROUTING           在路由之前     prerouting
     INPUT               数据包进入时      input
     FORWARD             数据包经过时     forward
     OUTPUT              数据包出去时     output
     POSTROUTING          在路由之后     postrouting

注意:匹配规则
    从上往下以此匹配,匹配过的数据包不会在去匹配

数据包流向:
     PREROUTING [route]    FORWARD          POSTROUTING
    
     OUTPUT                                        INPUT                                                       
        
     注意:  iptables是centos6之前的  firewalld是centos7的
 删除规则:
 iptables -F   [-t 表名] 默认时删除filter表  删除的是表里的所有
[root@ansible-1 ~]# rpm -qi iptables  #查看安装包工具包和命令
[root@ansible-1 ~]# yum -y install  iptables  iptables-services
[root@ansible-1 ~]#systemctl start  iptables

3,查看规则和删除规则

查看规则:
    iptables -L
    iptables -L -n
    iptables -L --line   (man手册上没有这个参数)
    iptables -L -t  filter/nat/mangle/raw 查看表中的链

4,添加规则和删除规则

语法:
iptables -t 表 动作 链 匹配条件 -j 目标动作

1,添加规则:-A
 iptables -F   [-t 表名] 默认时删除filter表  删除的是表里的所有
 拒绝所有的主机ping自己
[root@ansible-1 ~]# iptables -t filter -A INPUT -p icmp -j REJECT
[root@ansible-2 ~]# ping ansible-1 -c2
PING ansible-1 (192.168.222.131) 56(84) bytes of data.
From ansible-1 (192.168.222.131) icmp_seq=1 Destination Port Unreachable
From ansible-1 (192.168.222.131) icmp_seq=2 Destination Port Unreachable

--- ansible-1 ping statistics ---
 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1004ms
 
 拒绝192.168.222.129ssh连接自己,但是不影响自己连接它
[root@ansible-1 ~]#iptables -t filter -A INPUT -p tcp --dport 22 -s 192.168.222.129  -j REJECT
[root@ansible-2 ~]# ssh ansible-1
ssh: connect to host ansible-1 port 22: Connection refused
注意:
-p:协议(icmp tcp udp)
--dport 目标端口  --sport 源端口
-s 源地址          -d 目标地址


2,删除规则:-D
[root@ansible-1 ~]#iptables -t filter -D INPUT -p tcp --dport 22 -s 192.168.122.115  -j REJECT
iptables -L --line (序号)
[root@ansible-1 ~]#iptables -t filter -D INPUT(链)  1

5,插入规则和替换修改规则

1,插入规则:-I  
如果不指定插入到第几条,默认插入到第一条,插到那默认就是第几条
 (谁发送数据的谁就是源地址  谁接受数据的谁是目标地址)
 
插入到第二条规则上面
[root192 ~]#iptables -t filter -I INPUT  2 -p tcp --dport 22 -s 192.168.122.114  -j REJECT

2,替换(修改)规则:-R可以修改条件和目标动作
把ip:114-->115
[root192 ~]#iptables -t filter -R INPUT  2 -p tcp --dport 22 -s 192.168.122.115  -j  REJECT 

6,修改默认策略和添加自定义链

1,一般不做修改,了解修改默认策略:大-P  只能用DROP和ACCEPT  
[root192 ~]#iptables -t filter -P INPUT DROP
[root192 ~]#iptables -t filter -P INPUT ACCEPT

2,添加自定义链
自己创建的链
iptables -N chen   

关联自定义链到已有的链,不然添加规则不生效
iptables -A INPUT -j chen 

往自定义链上添加规则
iptables -t filter -A chen  -p tcp --dport 22 -s 192.168.122.116  -j REJECT 

修改自定义链名chen
iptables -E chen  zhang

注意删除自定义链,要先删除规则,取消关联,然后才能删除。

往自定义链zhang添加规则
iptables -t filter -A zhang -p tcp --dport 22 -s 192.168.222.200 -j REJECT

删除自定义链zhang的规则
iptables -t filter -D zhang -p tcp --dport 22 -s 192.168.222.200 -j REJECT

iptables -L --line  查看序号
iptables -t filter -D zhang 1  删除序号1的规则

从关联链上删除自定义zhang的链
[root@ansible ~]# iptables -D INPUT -j zhang

删掉自定义规则:不能被关联,必须是空链
iptables -X +链名   删除自定义链
iptables -X zhang

7,匹配条件

匹配条件:
基本匹配:
协议 -p tcp udp icmp
查端口:vim /etc/services 记录的是tcp/udp协议簇
vim /etc/protocols 记录的是icmp
-p   tcp  udp icmp
端口:使用端口前加协议(-p)
--sport源端口
iptables -A INPUT -p tcp --sport 22 -s 10.18.44.10  -j REJECT
--dport目标端口
iptables -A INPUT -p tcp --dport 22 -s 10.18.44.10  -j REJECT
IP
-s  源IP  source ip地址  网段 逗号可以分开多个地址
-d  目标IP destination 

扩展匹配: 
                -m 后面+扩展匹配
               -m multiport 多端口
iptables -A INPUT -m multiport --sports(--source-ports) 
80,20,22,1000:2000 -j DROP
        
        --dports(--destination-ports)
                -m iprange    多ip地址
iptables -A INPUT -m iprange --src-range 10.18.44.211-10.18.44.250 -j 
DROP

-m mac     
iptables -A INPUT -m mac  --mac-source 52:54:00:86:a1:86 -j REJECT               
获取MAC的方式
[root@xingdian opt]# arp -a 10.0.102.101
[root@xingdian opt]# arping -I eth0 10.0.102.101

8,目标动作

目标动作:
ACCEPT   允许数据包通过(默认策略)
DROP     直接丢弃数据包,不给任何回应
REJECT   拒绝数据包,必要时会给数据发送端一个响应
SNAT      源地址转换,可以解决內网用户用一个公网IP上网问题 POSTROUTING
DNAT      目标地址转换  PREROUTING
REDIRECT  做端口映射(扩展)

9,面试题

面试题:
iptables -t nat -A PREROUTING -p tcp -d 192.168.122.1 -dport 80  -j 
REDIRECT  --to-destination 192.168.1.1:8080

docker 端口映射 namespace 命名空间  -p 80:8080

二,firewalld(centos7版本之后)

1,简介

centos7默认将原来的防火墙iptables升级为firewalld,firewalld跟iptables
比起来至少有两大好处:
1、firewalld可以动态修改单条规则,而不需要像iptables那样,在修改了规则
后必须得全部刷新才可以生效;
2、firewalld在使用上要比iptables人性化很多,即使不明白“四张表五条链”而且
对TCP/IP协议也不理解也可以实现大部分功能。

2,firewalld目录结构

firewalld的配置文件以xml格式为主(主配置文件firewalld.conf例外),有两个
存储位置
    1、/etc/firewalld/
    2、/usr/lib/firewalld/
使用规则:
当需要一个文件时firewalld会首先到第一个目录中去查找,如果可以找到,那么就
直接使用,否则会继续到第二个目录中查找。注:两个目录下的同名配置文件第一个
目录内的生效

第二个目录中存放的是firewalld给提供的通用配置文件,如果我们想修改配置,那
么可以copy一份到第一个目录中,然后再进行修改,当然也可以直接修改第二个
目录下的文件。
    

3,查看服务状态和启动关闭加载配置

查看服务状态:
    systemctl status firewalld
    systemctl status -l  firewalld
    -l  完整信息 如果屏幕显示空间不够,默认情况下会缩略显示

启动关闭服务:
    systemctl  start  firewalld
    systemctl  stop  firewalld
    systemctl reload firewalld  重新加载配置

4,firewalld九个zone配置文件

[root@ansible-1 ~]# man firewall-cmd   man手册查看
zone:
    firewalld默认提供了九个zone配置文件:block.xml、dmz.xml、drop.xml、
    external.xml、 home.xml、internal.xml、public.xml、trusted.xml、
    work.xml,他们都保存在“/usr/lib /firewalld/zones/”目录下

    九个zone其实就是九种方案,而且起决定作用的其实是每个xml文件所包含的
    内容,而不是文件名,所以不需要对每种zone(每个文件名)的含义花费过多
    的精力,比如trusted这个zone会信任所有的数据包,也就是说所有数据包都
    会放行,但是public这个zone只会放行其中所配置的服务,其他的一律不予
    放行,其实我们如果将这两个文件中的内容互换一下他们的规则就换过来了,
    也就是public这个zone会放行所有的数据包

    注意:生效的只有默认zone里面的规则
    trusted.xml中zone的target,就是因为他设置为了ACCEPT,所以才会放行
    所有的数据包,而 public.xml中的zone没有target属性,这样就会默认
    拒绝通过,所以public这个zone只有其中配置过的服务才可以通过
    
    九区:
    drop(丢弃)
        任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络
        连接。
    block(限制)
        任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和
         IPv6 的 icmp6-adm-prohibited 信息所拒绝。
    public(公共)添加在这里面的,默认是放行允许
        在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成
        危害,只能接收经过选取的连接。
    external(外部)
        特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他
        计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的
        连接。
    dmz(非军事区)
        用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的
        内部网络,仅仅接收经过选择的连接。
    work(工作)
        用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅
        接收经过选择的连接。
    home(家庭)
        用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
    internal(内部)
        用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
    trusted(信任)
        可接受所有的网络连接。

5,配置方法(注意使用firewalld要先关闭防火墙)

配置方法:
三种:firewall-config(图形方式)、firewall-cmd(命令行方式)和直接编辑
xml文件


命令行模式查看防火墙状态
systemctl stop firewalld
firewall-cmd --state

列出当前有几个zones:
[root@ansible-1 ~]#firewall-cmd --get-zones
block dmz drop external home internal public trusted work

查看当前活动的zones:    
[root@ansible-1 ~]#firewall-cmd --get-active-zones 
public
interfaces: ens33

拒绝所有包:
    firewall-cmd --panic-on
取消拒绝状态: 
    firewall-cmd --panic-off
查看是否拒绝: add-sourc
    firewall-cmd --query-panic

检查下一次重载后将激活的服务:
    firewall-cmd --get-service --permanent

[root@ansible-1 ~]# firewall-cmd --zone=public --list-ports
443/tcp
[root@ansible-1 ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client
  ports: 443/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

6,给public增加/删除http服务(永久permanent)

临时增加http
root@ansible-1 ~]# firewall-cmd --zone=public --add-service=http
永久增加http需要参数permanent
[root@ansible-1 ~]# firewall-cmd --permanent --zone=public --add-service=http
success

[root@ansible-1 ~]# firewall-cmd --reload
success

[root@ansible-1 ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client http
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

删除zone public 中http服务
[root@ansible-1 ~]# firewall-cmd  --zone=public --remove-service=http
success
[root@ansible-1 ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

7,增加zone public开放443/tcp协议端口:

[root@ansible-1 ~]# firewall-cmd --zone=public --add-port=443/tcp
success
[root@ansible-1 ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client
  ports: 443/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

注意:
永久增加443/tcp端口需要--permanent永久生效,firewall-cmd --reload

8,查看public中services/ports

[root@ansible-1 ~]# firewall-cmd --zone=public --list-services
ssh dhcpv6-client

[root@ansible-1 ~]# firewall-cmd --zone=public --list-ports
443/tcp

9,设置黑/白名单: (hosts.allow hosts.deny)

增加172.28.129.0/24网段到zone trusted(信任)
[root@ansible-1 ~]# firewall-cmd --permanent --zone=trusted --add-source=172.28.129.0/24
success

列出zone truste的白名单:
[root@ansible-1 ~]# firewall-cmd --permanent --zone=trusted --list-sources
192.168.222.190/24 172.28.129.0/24

10,添加/删除172.28.13.0/24到zone drop:

添加172.28.13.0/24到zone drop:
    # firewall-cmd --permanent --zone=drop --add-source=172.28.13.0/24
    success

添加后需要重新加载:
    [root@localhost zones]# firewall-cmd --reload
    success

    [root@localhost zones]# firewall-cmd --zone=drop --list-all
    drop
      interfaces: 
      sources: 172.28.13.0/24
      services: 
      ports: 
      masquerade: no
      forward-ports: 
      icmp-blocks: 
      rich rules: 

    # firewall-cmd --reload
    success

从zone drop中删除172.28.13.0/24:
    # firewall-cmd --permanent --zone=drop --remove-source=172.28.13.0/24
    success

11,修改配置文件public.xml

需要开放端口或开放某IP访问权限,需要先查看我们当前默认的zone是
哪个,然后在对应的zone里面添加port和source,这样对外才会有作用。
比如我当前的默认zone是public,需要开放80端口对外访问,则执行如下
命令:
     # firewall-cmd --zone=public --permanent --add-port=80/tcp
     success
     # firewall-cmd --reload
     success

可以直接修改对应的配置文件:
      以public zone为例,对应的配置文件是/etc/firewalld/zones/public.xml,像我们刚刚添加80端口后,体现在public.xml 中的内容为:
     # cat public.xml
     <?xml version="1.0" encoding="utf-8"?>
     <zone>
       <short>Public</short>
       <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming      connections are accepted.</description>
       <service name="dhcpv6-client"/>
       <service name="ssh"/>  #增加ssh服务
       <port protocol="tcp" port="80"/>  #增加80/tcp端口
     </zone>
     注意在修改配置文件后 --reload 或重启 firewall 服务。 

三,拓展了解:firewalld防火墙高级

firewalld防火墙高级:     

1,Direct Rules概述
通过firewall-cmd工具,可以使用 --direct选项在运行时间里增加和
删除链。如果不熟悉iptables,使用直接接口非常危险,因为可能无意间
导致防火墙被入侵。直接端口模式适用于服务或者程序,以便于在运行时
间内增加特定的防火墙规则。直接端口模式添加的规则优先应用。
2,高级防火墙Direct规则的使用
Direct Options
       The direct options give a more direct access to the firewall. These options
       require user to know basic iptables concepts, i.e.  table
       (filter/mangle/nat/...), chain (INPUT/OUTPUT/FORWARD/...), commands
       (-A/-D/-I/...), parameters (-p/-s/-d/-j/...) and targets
       (ACCEPT/DROP/REJECT/...).

       Direct options should be used only as a last resort when it's not possible to
       use for example --add-service=service or --add-rich-rule='rule'.

       The first argument of each option has to be ipv4 or ipv6 or eb. With ipv4 it
       will be for IPv4 (iptables(8)), with ipv6 for IPv6 (ip6tables(8)) and with eb
       for ethernet bridges (ebtables(8)).

       [--permanent] --direct --get-all-chains
           Get all chains added to all tables. This option concerns only chains
           previously added with --direct --add-chain.

       [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table

查看防火墙上设置的规则:
[root@xingdian zones]#firewall-cmd --direct --get-all-rules
添加高级规则:

[root@xingdian zones]#firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.77 -j ACCEPT 
##只允许172.25.254.77通过80端口访问主机位为110的http服务。因为
访问的是主机位为110的http服务,需要主机位为110的内核同意开启
http服务,需要在表filter中设置INPUT。
##-p 数据包类型;--dport 服务端口;

删除防火墙上的规则:
[root@xingdian zones]#firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.217 -j REJECT
##移除:不允许172.25.254.217通过22端口的访问ssh,连接172.25.254.110这条规则


使用direct模式可以直接让规则生效:
   #firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 80 -j ACCEPT 
   #firewall-cmd --direct --get-all-rules

     
端口转发:     
        端口转发可以将指定地址访问指定的端口时,将流量转发至指定地址的指定端口。转发的目的如果不指定 ip 的话就默认为本机,如果指定了 ip 却没指定端口,则默认使用来源端口。 如果配置好端口转发之后不能用,可以检查下面两个问题:
1. 比如我将 80 端口转发至 8080 端口,首先检查本地的 80 端口和目标的 8080 端口是否开放监听了
2. 其次检查是否允许伪装 IP,没允许的话要开启伪装 IP

1. firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080   
# 将80端口的流量转发至8080
2. firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1 
# 将80端口的流量转发至192.168.0.1
3. firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080 
# 将80端口的流量转发至192.168.0.1的8080端口

开启IP伪装:
查看:
[root@xingdian zones]# firewall-cmd --query-masquerade yes no
开启:
[root@xingdian zones]# firewall-cmd --add-masquerade
关闭:
[root@xingdian zones]# firewall-cmd --remove-masquerade

作用:     
1. 当我们想把某个端口隐藏起来的时候,就可以在防火墙上阻止那个端口访问,然后再开一个不规则的端口,之后配置防火墙的端口转发,将流量转发过去。
2. 端口转发还可以做流量分发,一个防火墙拖着好多台运行着不同服务的机器,然后用防火墙将不同端口的流量转发至不同机器。
     
富规则配置举例:     
1.允许新的ipv4和ipv6连接ftp,并使用审核每分钟记录一次
[root@localhost /]# firewall-cmd --add-rich-rule='rule service name=ftp log limit value=1/m audit accept'
2.将源192.168.2.2地址加入白名单,以允许来自这个源地址的所有连接   
[root@localhost /]# firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.2.2" accept' 
3.拒绝来自public区域中IP地址192.168.0.11的所有流量:
[root@localhost /]# firewall-cmd --zone=public --add-rich-rule='rule family=ipv4 source address=192.168.0.11/32 reject'


关闭的FirewallD防火墙,使用iptables,命令如下:
    #systemctl stop firewalld
    #systemctl disable firewalld
    #yum install iptables-services
    #systemctl start iptables
    #systemctl enable iptables    
    
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshuai199533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值