Linux中的火墙策略优化

    技术2022-07-11  80

    实验环境的配置

    该实验需要两台主机 172.25.254.10 只能连接内网 172.25.254.20 双网卡主机

    10主机: 不能连接外网: 20主机: 内网网卡: 外网网卡: 修改名称: 测试: 真机:

    一、火墙

    1.火墙介绍

    【1】netfilter 【2】iptables 【3】iptables | firewalld

    2.火墙管理工具的切换

    【1】firewalld----->iptables

    firewalld----->iptables - dnf install iptables-services -y #搭建好yum仓库后,安装iptables - systemctl stop firewalld - systemctl disable firewalld - systemctl mask firewalld - systemctl enable --now iptables

    【2】iptales -------> fiewalld

    iptales -------> fiewalld - dnf install firewalld -y - systemctl stop iptables - systemctl disable iptables - systemctl mask iptables - systemctl enable --now firewalld 在20主机: - 搜索iptables 该服务处于关闭状态: 开启iptables服务的步骤: 若要使用firewalld则: 关掉服务 firewalld----->iptables

    3. iptables 的使用

    【1】 /etc/sysconfig/iptables 是 iptables 策略记录文件 【2】永久保存策略

    iptales-save > /etc/sysconfig/iptables将策略存放在记录文件中service iptables save自动存放 将当前的火墙生成一个输出: - 将策略保存到/etc/sysconfig/iptables里 用service iptables save命令自动保存火墙

    4.火墙默认策略

    【1】火墙默认策略

    input输入output输出forward转发postrouting路由之后prerouting路由之前

    【2】默认的3张表

    filter 经过本机内核的数据 该表中有三条链:input output forward

    nat不经过内核的数据 postrouting,prerouting,input,output

    mangle 当filter和nat表不够用时使用 input output forward postrouting, prerouting 【3】iptables的常见命令

    iptables -t指定表名称iptables -n不做解析iptables -L查看iptables -A添加策略iptables -p协议iptables - -dport目的地端口iptables -s来源iptables -j动作iptables -N新建链iptables -E更改链名称iptables -X删除链iptables -D删除规则iptables -I插入规则iptables -R更改规则iptables -P更改默认规则 iptables查看(注意:如果不加-t,默认查看到filter表) - 无-n只有-L会显示数字加字符,较慢: 加n即不做解析只显示数字: -暂时删除策略(使用systemctl restart iptables.service命令重启火墙后会恢复): 查看nat表:

    查看mangle表: - 添加策略

    新建链 更改链名称: 删除 删除规则 插入规则(不写位置则默认插在最前端,可指定插入位置) 更改规则 更改默认规则(注意:默认规则只能是 DROP或者ACCEPT) -j 动作 -j ACCEPT允许-j DROP丢弃-j REJECT拒绝-j SNAT源地址转换-j DNAT目的地地址转换

    注意:火墙策略从上到下依次生效

    【4】数据包状态 上图中火墙策略从上到下依次读取的方式明显降低了效率,故采用最开始的访问状态是NEW,后续就是ESTABLISHED和RELATED了。此设定会优化我们的火墙数据包状态,使我们的数据包更易通过火墙。

    RELATED建立过连接的ESTABLISHED正在连接的NEW新的 [root@westos_firewalld Desktop]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #这两种状态的直接让过 [root@westos_firewalld Desktop]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT [root@westos_firewalld Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT [root@westos_firewalld Desktop]# iptables -A INPUT -m state --state NEW -s 172.25.254.10 -p tcp --dport 22 -j ACCEPT #不加!表示只有这个ip的可以来访问,加上!表示只有此ip不可访问 [root@westos_firewalld Desktop]# iptables -A INPUT -j REJECT [root@westos_firewalld Desktop]# service iptables save #对我们的策略进行保存

    【5】nat表中的dnat snat

    - snat iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 172.25.254.20 - dnat iptables -t nat -A PREROUTING -i enp6s0 -j DNAT --to-dest 192.168.43.20 从enp6s0进来的所有数据都转给192.168.43.20,即给enp6s0(172.25.254.20)的所有请求,都会发送到192.168.43.20。下面的练习就是真机连接双网卡主机,但是最后的连接位置却是单网卡主机

    10主机为单网卡主机,为了10主机可以上网,设定10主机网关:
    Processed: 0.012, SQL: 9