0
点赞
收藏
分享

微信扫一扫

shell实现将登录失败次数超过5次ip拉入黑名单,以防暴力破解

_karen 2022-04-15 阅读 37
linux

文章目录


前言

公网环境非常恶劣,经常会遇见暴力破解的攻击手段,为了预防暴力破解,我们将写一个脚本,将登录失败五次以上的ip地址拉入黑名单,禁止其访问服务器,并发邮件通知管理员。

提示:以下是本篇文章正文内容,下面案例可供参考

一、服务器环境配置准备

centos7系统
禁用firewalld防火墙
开启iptables防火墙
配置邮箱

二、环境配置

1.禁用firewalld

代码如下(示例):

systemctl stop firewalld
systemctl disable firewalld

2.关闭selinux

代码如下(示例):

[root@web_hk ~]# setenforce 0 //临时关闭selinux
[root@web_hk ~]# getenforce
Disabled
[root@web_hk ~]# cat /etc/selinux/config //检查此文件,确认selinux永久关闭

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3、安装、启用iptables服务

[root@web_hk ~]# yum install iptables-services
[root@web_hk ~]# systemctl start iptables
[root@web_hk ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

4、配置邮箱mail

[root@WEB ~]# vi /etc/mail.rc
set bsdcompat
set from=123456@qq.com  //邮箱地址,
set smtp=smtp.exmail.qq.com
set smtp-auth-user=123456@qq.com //邮箱 账户
set smtp-auth-password=123 //邮箱密码
set smtp-auth=login

邮箱测试:
echo “内容” | mail -s 主题 接收的邮箱地址

[root@web_hk ~]# echo "888888" | mail -s test 987654@qq.com

三、for循环脚本

[root@web_hk ~]# touch unlogin.sh 
[root@web_hk ~]# chmod 755 unlogin.sh //赋予执行权限
[root@web_hk ~]# ll
total 0
-rwxr-xr-x 1 root root 0 Apr 15 20:05 unlogin.sh
[root@web_hk ~]# vi unlogin.sh
lastb | awk '{print $3}' | grep ^[0-9] | uniq -c | awk '{print $1"="$2}' > /tmp/host
list=`cat /tmp/host`

for循环取值

for i in $list;do
	ip=`echo $i | awk -F= '{print $2}'`//取出登录失败的ip地址
	num=`echo $i | awk -F= '{print $1}'`//取出登录的失败次数,用于if判断
if [[ $num -gt 5 ]];then //失败次数大于5次,就执行以下的脚本
		echo "$(date +%F) $(date +%T)" >> /$HOME/error_login,log//记录时间
		echo $ip >> /$HOME/error_login,log//记录被拉黑的IP地址
		echo $ip >> /tmp/host_deny//记录将要被拉黑的临时文件
		deny_ip=`cat /tmp/host_deny`//把拉黑的ip地址赋予变量deny_ip

继续嵌套否循环输出

for i in $deny_ip;do //读取黑名单的ip地址
			/usr/sbin/iptables -I INPUT -s $i -j DROP //iptables禁止ip访问
			echo "$i 企图恶意登录,已封禁" | mail -s “疑似暴力破解” 123456@qq.com//发邮件通知管理员
			lastb >> /root/btmp //备份登录失败的ip记录
			cat /dev/null > /var/log/btmp //清空登录失败的ip地址记录,如果不清空,将会陷入死循环
			rm -rf /tmp/host_deny //删除临时文件
		done
	fi
done
#!/bin/bash
lastb | awk '{print $3}' | grep ^[0-9] | uniq -c | awk '{print $1"="$2}' > /tmp/host
list=`cat /tmp/host`
for i in $list;do
	ip=`echo $i | awk -F= '{print $2}'`
	num=`echo $i | awk -F= '{print $1}'`
	if [[ $num -gt 5 ]];then
		echo "$(date +%F) $(date +%T)" >> /$HOME/error_login,log
		echo $ip >> /$HOME/error_login,log
		echo $ip >> /tmp/host_deny
		deny_ip=`cat /tmp/host_deny`
		for i in $deny_ip;do
			/usr/sbin/iptables -I INPUT -s $i -j DROP
			echo "$i 企图恶意登录,已封禁" | mail -s “疑似暴力破解” 123456@qq.com
			lastb >> /root/btmp
			cat /dev/null > /var/log/btmp
			rm -rf /tmp/host_deny
		done
	fi
done

定时任务开启

[root@WEB ~]# crontab -e
*/1 * * * * ./etc/profile;/bin/bash /root/unlogin.sh

总结

举报

相关推荐

0 条评论