Linux中的火墙策略优化(iptables和firewall)

Linux中的火墙策略优化

1.火墙介绍:

1.netfilter:记录火墙策略的表的名字
2.iptables:书写netfilter的方式
3.iptables|firewalld: 使用的管理工具有iptables和firewalld

2.火墙管理工具切换:

在rhel8中默认使用的是firewalld

firewalld----->iptables

dnf install iptables-services -y
systemctl stop firewalld
systemctl disable firewalld 
systemctl mask firewalld            锁定firewalld不被其他服务篡改状态;使用unmask解锁;
systemctl enable --now iptables

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

3. iptables 的使用:

#火墙策略的永久保存#
/etc/sysconfig/iptables		##iptables 策略记录文件

永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save


4.iptables火墙默认策略:

注意:iptables火墙策略是由上自下匹配的,在读到符合服务的策略1时,便不会继续读之后的与该服务相关的策略
如图:ssh服务在input的第一条策略中已经被拒绝访问,因此第二条策略无效,如果想要让ssh服务通过火墙而其他的服务不能,需要将1,2策略位置交换即可

在这里插入图片描述

iptables的基本使用:

默认策略中的5条链
input			输入
output			输出
forward			转发
postrouting		路由之后
prerouting		路由之前


默认的3张表

filter		经过本机内核的数据(input output forward)
nat		    不经过内核的数据(postrouting,prerouting,input,output)
mangle		当filter和nat表不够用时使用(input output forward postrouting,prerouting,)

iptables命令
iptables
	-t		##指定表名称
	-n		##不做地址解析
	-L		##查看
	-A		##添加策略
	-p		##协议
	--dport		##目的地端口
	-s		##来源
	-m state --state  ##指定数据包状态	
	-j		##动作
	ACCEPT	##允许
	DROP	##丢弃
	REJECT	##拒绝
	SNAT	##源地址转换
	DNAT	##目的地地址转换
	-N		##新建链
	-E		##更改链名称
	-X		##删除链
	-D		##删除规则
	-I		##插入规则
	-R		##更改规则
	-P		##更改默认规则
	-F      ##刷新表的信息(清空,重启服务后会恢复)

数据包状态
RELATED		##建立过连接的
ESTABLISHED	##正在连接的
NEW		    ##新的

(1)设定建立过连接和正在建立连接的数据包的火墙策略为允许:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
(2)设定本机的回环接口允许新的数据包:
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT    ##lo 回环接口 表示只有自身主机
(3)对于新的指定服务的数据包添加火墙策略设定为允许:
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
(4)添加火墙策略,设定除指定IP的所有主机都可以进行的服务:(!表示非)
iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPT
(5)对于剩余其他新的数据包添加火墙策略设定为拒绝:
ptables -A INPUT -m state --state NEW -j REJECT

iptables的策略保存:
service iptables  save   保存iptables中现存的策略(更改/etc/sysconfig/iptables 文件)
iptales-save > /etc/sysconfig/iptables  读取火墙策略信息并导入iptables策略记录文件


nat表中的dnat snat

snat (源地址转换-->postrouting)
iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 172.25.254.54 
将传入数据的ip地址(任意,如 1.1.1.1)伪装成地址 172.25.254.54  输出

dnat (目的地址转换-->prerouting)
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 1.1.1.200
将原本的目的地地址(任意,如 172.25.254.54)伪装成1.1.1.200 

内核路由功能:

内核路由功能:sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1  表示内核路由功能开启;
功能未开启时编辑:/etc/sysctl.conf 在最后添上net.ipv4.ip_forward = 1 ;

5.firewalld:
firewalld管理方式:xml 数据封装 : /lib/firewalld/services

5-1:firewalld的开启:

systemctl stop iptables 
systemctl disable iptables
systemctl mask iptables 

systemctl unmask firewalld
systemctl enable --now firewalld 

5-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

5-3:关于firewalld的设定原理及数据存储 :

如果要添加火墙中没有的服务时,需要在/lib/firewalld/services中编写新的服务封装数据包(可以复制其他服务):

在这里插入图片描述

/etc/firewalld	        	火墙配置目录
/lib/firewalld/services	    火墙模块目录
--permanent     只更改/etc/firewalld/zones文件,表示永久设定,需要重启服务后生效

5-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=172.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-5:firewalld的高级规则(direct):

firewall-cmd --direct --get-all-rules	##查看高级规则 
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.250 -p tcp --dport 22  -j REJECT 

5-6:firewalld中的NAT:

SNAT
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload 

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

猜你喜欢

转载自blog.csdn.net/lb1331/article/details/110948985