shell练习之安全脚本

题目:

将密码输入错误超过4次的IP地址通过firewalld防火墙阻止访问

1.初始配置

首先使用systemctl工具启用firewalld服务:

[root@localhost ~]# systemctl enable firewalld

如果已经启用了,我们现在可以通过执行以下命令启动firewalld:

[root@localhost ~]# systemctl start firewalld

且可以通过运行以下命令验证firewalld的状态并且以下输出确认了firewalld启动了并且在运行:

[root@localhost ~]# systemctl status firewalld

在这里插入图片描述

2.分析

1、需要知道ssh远程访问记录在哪个文件中/var/log/secure
2、模拟远程访问输错密码,查看日志文件:

cat /var/log/secure

在这里插入图片描述
3、了解firewalld添加富规则的知识:

[root@localhost ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject'
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject

4、回到192.168.210.133上进行测试:

[root@localhost ~]# ssh root@192.168.210.128
ssh: connect to host 192.168.210.128 port 22: Connection refused

5、测试后将添加的富规则删除:

[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject'
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

3.编写脚本

[root@localhost shell]# vim deny_ip.sh

#!/bin/bash
#*************************************************************
#Author:czc
#Date:  2023-01-09
#FileName: deny_ip.sh
#*************************************************************
serc_log=/avr/log/secure
ip_list=`awk '/Failed password/ {IP[$(NF-3)]++} END{for (k in IP){if (IP[k]>=4) {print k} }}' /var/log/secure`
for ip in `echo $ip_list`
do
        denyed_ip=`firewall-cmd --list-all | awk -F'[= ]' '/rule family/ && $NF="reject" && $(NF-1)~/ssh/ {print $6}' | awk -F'["/]' '{print $2}'`
        echo $denyed_ip | grep -q $ip
        [ $? -ne 0 ] && firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="$ip" service name="ssh" reject"
done

firewall-cmd --reload

[root@localhost shell]# chmod a+rx deny_ip.sh

4.测试

由于此时192.168.210.133只输错密码3次,因此执行脚本后,并未添加富规则。
在这里插入图片描述
在这里插入图片描述
此时再到192.168.210.133上对本机进行访问,累计达到4次错误及以上:[root@localhost ~]# ssh

root@192.168.210.128
root@192.168.210.128's password: 
Permission denied, please try again.
root@192.168.210.128's password: 
Permission denied, please try again.
root@192.168.210.128's password: 

可以看到现在的输入密码错误次数累计达到5次:
在这里插入图片描述

#此时在192.168.210.128上执行脚本
[root@localhost shell]# ./deny_ip.sh
success
success
[root@localhost shell]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule family="ipv4" source address="192.168.210.133" service name="ssh" reject


#再回到192.168.210.133上进行测试

[root@localhost ~]# ssh root@192.168.210.128
ssh: connect to host 192.168.210.128 port 22: Connection refused

至此,脚本的编写和测试就完成了。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值