Linux中的火墙策略优化

一.火墙介绍

1.netfilter
2.iptables
3.iptables|firewalld

二.火墙管理工具切换

1.iptables的安装

在rhel8中默认使用的是firewalld

dnf search iptables		//搜索iptables的安装包全称
dnf install iptables-services -y

1.firewalld----->iptables

systemctl stop firewalld
systemctl disable firewalld 
systemctl mask firewalld 				//将firewalld锁起来
systemctl enable --now iptables
iptables -nL										//nL不做解析查看配置

2.iptales -------> fiewalld

dnf install firewalld -y
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable --now firewalld

三. iptables 的使用

实验环境

(1)真机:172.25.254.41
(2)虚拟机:
westosa:双网卡
网卡1:1.1.1.141
网卡2:172.25.254.141
(3)westosb:
单网卡:1.1.1.241
网关:1.1.1.141

1.火墙策略的永久保存

/etc/sysconfig/iptables		//iptables 策略记录文件
iptales-save > /etc/sysconfig/iptables		//永久保存策略
service iptables save

1、实验(清空iptables默认配置,并使其重启不会重新加载默认配置)

(1)shell1用来输入命令

iptables -F				//删除iptables的配置文件的信息
watch -n 1 iptables -nL		//查看iptables的配置文件
systemctl restart iptables.service  //重启iptables
iptables -F				//这时候shell2中,信息会恢复原来的样子
iptables-save > /etc/sysconfig/iptables  //在将iptables的配置文件中的信息完全删除后,写入永久保存
systemctl restart iptables.service 	//这个时候,iptables的配置文件信息会是清空的

(2)shell 2用来查看iptables的配置文件的变化

watch -n 1 iptables -nL

2、实验(nat不经过内核的数据的直接转发)

(1)iptables的配置信息

iptables -t nat -nL
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.141
iptables -t nat -A PREROUTING -i ens11 -j DNAT --to-dest 1.1.1.141

(2)测试

真机的网关要设置成双网卡该网段的ip
这个时候单网卡和真机可以互相ping通

四.火墙默认策略

1.默认策略中的5条链

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			##指定表名称
	-n			##不做解析
	-L			##查看
	-A			##添加策略
	-p			##协议
	--dport		##目的地端口
	-s			##来源
	-j			##动作
		ACCEPT	##允许
		DROP	##丢弃
		REJECT	##拒绝
		SNAT	##源地址转换
		DNAT	##目的地地址转换
	-N			##新建链
	-E			##更改链名称
	-X			##删除链
	-D			##删除规则
	-I			##插入规则  //没有参数的话,默认插入到第一条
	-R			##更改规则
	-P			##更改默认规则

4.数据包状态

RELATED ##建立过连接的
ESTABLISHED ##正在连接的
NEW ##新的

5.实验

目的:
除了之前建立过连接的正在连接状态的服务,还有http(80),ssh(22),dns(53)其他的连接都会被拒绝。

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT		//建立过连接和正在连接的
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT //回环接口lo	//本机的回环接口请求
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT	//http
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT	//https
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT	//dns
iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPT //ssh
iptables -A INPUT -m state --state NEW -j REJECT	//其他的
service iptables  save	//永久保存配置,重启也不会被重置

6.nat表中的dnat snat

实验(nat不经过内核的数据的直接转发)

(1)iptables的配置信息

iptables -t nat -nL
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.141
iptables -t nat -A PREROUTING -i ens11 -j DNAT --to-dest 1.1.1.141

(2)测试

真机的网关要设置成双网卡该网段的ip
这个时候单网卡和真机可以互相ping通

五.firewalld

1 firewalld的开启

从iptables切换回来

systemctl stop iptables 
systemctl disable iptables
systemctl mask iptables 
systemctl unmask firewalld
systemctl enable --now firewalld 

2.关于firewalld的域

trusted		##接受所有的网络连接
home		##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-client
work		##工作网络 ssh ipp-client dhcp-client
public		##公共网络 ssh dhcp-client
dmz			##军级网络 ssh
block		##拒绝所有
drop		##丢弃	所有数据全部丢弃无任何回复
internal	##内部网络 ssh mdns ipp-client samba-client dhcp-client
external	##ipv4网络地址伪装转发 sshd

3.关于firewalld的设定原理及数据存储

/etc/firewalld		##火墙配置目录
/lib/firewalld		##火墙模块目录

4. firewalld的管理命令

firewall-cmd --state		##查看火墙状态 
firewall-cmd --get-active-zones ##查看当前火墙中生效的域
firewall-cmd --get-default-zone ##查看默认域
firewall-cmd --list-all		##查看默认域中的火墙策略
firewall-cmd --list-all --zone=work ##查看指定域的火墙策略
firewall-cmd --set-default-zone=trusted  ##设定默认域
firewall-cmd --get-services 	##查看所有可以设定的服务
firewall-cmd --permanent --remove-service=cockpit	##移除服务
firewall-cmd --reload 
firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定数据来源访问指定域
firewall-cmd --reload 
firewall-cmd --permanent --remove-source=17II2.25.254.0/24 --zone=block ##删除自定域中的数据来源
firewall-cmd --permanent --remove-interface=ens224 --zone=public ##删除指定域的网络接口 
firewall-cmd --permanent --add-interface=ens224 --zone=block 	##添加指定域的网络接口
firewall-cmd --permanent --change-interface=ens224 --zone=public ##更改网络接口到指定域

5.安装支持火墙图形化的插件

dnf whatprovides */firewall-config				//查找插件的全称
dnf install firewall-config-0.8.0-4.el8.noarch	//安装插件

6. firewalld的高级规则

(1)实验:

目的:让除了241其他都不能访问

firewall-cmd --direct --get-all-rules	##查看高级规则 
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.241 -p tcp --dport 80  -j REJECT 		//添加除了241主机,其他都不能访问的规则

(2)测试

测试端:非172.25.254.241ip的同网段ip
用firefox或者ping命令,连接172.25.254.141,会被拒绝

(3)移除规则

firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 ! -s 172.25.254.241 -p tcp --dport 80  -j REJECT		//移除规则

7.firewalld中的NAT

实验环境是不变的

(1)SNAT

(1)在双网卡开
firewall-cmd --permant --add-masquerade //开启地址伪装
firewall-cmd --reload
(2)在单网卡
ssh -l root 172.25.254.41
(3)在真机

w -i

显示的ip是172.25.254.141,这个是双网卡172网段网卡的ip

(2)实验

DNAT
这个实验的目的是,用真机连接刚才的172.25.254.141,看连接到后是什么效果,但是双网卡主机作为路由器,不会让其他客户端连接到自己,访问路由器的资源,所以会把来自真机的连接扔到单网卡主机。
双网卡

firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.241
firewall-cmd --reload

真机

ssh -l root 172.25.254.141
然后w -i

显示的是双网卡的1.1.1网段的ip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值