防火墙一个系统加固的例子

要求windows和linux的服务器只开必要的端口,不必要的端口全部关闭,包括主动外发。

windows里看看都是公共区域,默认出站都是关闭的,于是禁用了全部的出站规则,然后入站也全部关闭,只打开必要的服务端口。那么linux系统有点麻烦了,研究了一下,用了下面的方法:
1、先加入口放行要访问本机的IP和端口:

限制IP型的:
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=外部IP port port=本机端口 protocol=tcp accept'

通用型的:firewall-cmd --permanent --add-port=8080/tcp

加服务型的:firewall-cmd --permanent --add-service=http

加ipset型的:firewall-cmd --permanent  --add-source=ipset:xxx

2、再加出口放行IP和端口:
firewall-cmd --permanent --zone=public --direct --add-rule ipv4 filter OUTPUT 0 -p tcp -s 本机IP -d 外部IP -j ACCEPT

对本地服务的出口端口放行:
firewall-cmd --permanent --zone=public --direct --add-rule ipv4 filter OUTPUT 0 -p tcp -s 本机IP  --dport 外部端口 -j ACCEPT

加ipset型的:

firewall-cmd --permanent --zone=public --direct --add-rule ipv4 filter OUTPUT 0 -p tcp -s 本机IP  -d ipset xxx -j ACCEPT

3、再加出口拒绝所有出站:
firewall-cmd --permanent --zone=public --direct --add-rule ipv4 filter OUTPUT 0 -p tcp -s 本机IP -d 0.0.0.0/0.0.0.0 -j REJECT
 * 以上第一条命令用firewall-cmd --list-all查看,文件则是在/etc/firewalld/zones/public.xml(默认区域);第2、3条命令用firewall-cmd --direct --get-all-rules 或者用iptables -L -n查看,文件在/etc/firewalld/direct.xml,需要的也可直接编辑文档。

-----------------------------

另:如果用iptables的话,可以这样:

iptables -A INPUT -p tcp -s 来源IP -d 本机IP --dport 本机端口号 -j ACCEPT  # 放行入口ip

iptables -A INPUT -p tcp -d 本机IP -j DROP                            # 限制入站所有(需要目的ip)

iptables -A OUTPUT -p tcp -s 本机IP -d 外部IP  -j  #ACCEPT   # 放行出口ip

 iptables -A OUTPUT -p tcp -s 本机IP -j DROP 限制出站所有(需要源ip)

iptables -L -n --line-number   查看规则(有编号)

iptables -D INPUT 2  删除规则(需要规则编号)


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值