用SHELL脚本来防SSH和vsftpd暴力破解(更新版)

新近刚上的FTP备份服务器,例行检查/var/log/secure日志时,发现不少sshdvsftpd失败认证信息,很明显有人想用暴力破解工具窃取密码,所以需要编写一个安全脚本防止。

脚本需求如下:此SHELL脚本放在crontab计划任务里,每隔6小时(此时间根据实际情况来定义)就去读取/var/log/secure脚本,取出里面恶意猜测IP,如果单位时间内(一星期)的连接数是高于一个阀值,例如100(此阀值也可以根据实际情况来定义),则将其加进/etc/hosts.deny黑名单里,如果低于此阀值,则无视此IP

/var/log/secure里认证失败信息如下:

Nov 28 10:18:08 CentOS2 sshd[7556]: Connection closed by 222.216.30.109
Nov 28 10:18:08 centos2 sshd[7557]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.216.30.109 user=root
Nov 28 10:18:09 centos2 sshd[7559]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=222.216.30.109 user=root
Nov 28 10:18:10 centos2 sshd[7551]: Failed password for root from 222.216.30.109 port 2391 ssh2
Nov 28 10:18:10 centos2 sshd[7552]: Connection closed by 222.216.30.109
Nov 28 10:18:10 centos2 sshd[7553]: Failed password for root from 222.216.30.109 port 2397 ssh2
Nov 28 10:18:10 centos2 sshd[7554]: Connection closed by 222.216.30.109
Nov 28 10:18:11 centos2 sshd[7557]: Failed password for root from 222.216.30.109 port 2401 ssh2
Nov 28 10:18:11 centos2 sshd[7558]: Connection closed by 222.216.30.109
Nov 28 10:18:11 centos2 sshd[7559]: Failed password for root from 222.216.30.109 port 2403 ssh2
Nov 28 10:18:11 centos2 sshd[7560]: Connection closed by 222.216.30.109
Nov 28 10:37:01 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:01 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=hello rhost=centos1.cn7788.com
Nov 28 10:37:01 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user hello
Nov 28 10:37:19 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:19 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yhc rhost=centos1.cn7788.com
Nov 28 10:37:19 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yhc
Nov 28 10:37:36 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:37:36 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yuhongchun rhost=centos1.cn7788.com
Nov 28 10:37:36 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
Nov 28 10:42:44 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:42:44 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=yuhongchun rhost=114.112.169.70
Nov 28 10:42:44 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
Nov 28 10:42:56 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
Nov 28 10:42:56 centos2 vsftpd: pam_unix(vsftpd:auth): authentication failure; logname= uid=0 euid=0 tty=ftp ruser=andrewyu rhost=114.112.169.70
Nov 28 10:42:56 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user andrewyu

我们观察下/var/log/secure文件轮询特征,如下所示:

[root@centos2 log]# ls -lsart secure.*

512 -rw------- 1 root root 516379 11-04 01:31 secure.4
660 -rw------- 1 root root 668192 11-11 00:05 secure.3
304 -rw------- 1 root root 306589 11-17 10:33 secure.2
484 -rw------- 1 root root 488620 11-25 02:33 secure.1

基本上,/var/log/secure文件是以星期为轮询周期的,如果对安全要求严格的朋友还可以本着“一个不放过”的原则来抓取上面的旧secure的恶意IP,然后扔进/etc/hosts.deny文件里。下面我们就们就要想办法高效的来抓取这些恶意IP,如果参考原始版本SHELL脚本写法,,我们这里要抓取secure日志中的侦测vsftpdsshd服务的IP地址,我们可以用如下命令,命令如下所示:

cat /var/log/secure | awk '/Failed/{print $(NF-3)}'| sort| uniq -c| awk '{print $2"="$1;}'

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyddfz.html