用SHELL脚本来防SSH和vsftpd暴力破解

林继 VPS 知识 1,923 共写了1054个字 (2012-12-29 22:23:47) 没有评论 打印 扫描二维码 百度已收录

新近刚上的FTP备份服务器,例行检查/var/log/secure日志时,发现不少sshd和vsftpd失败认证信息,很明显有人想用暴力破解工具窃取密码,所以需要编写一个安全脚本防止。
脚本需求如下:此SHELL脚本放在crontab计划任务里,每隔6小时(此时间根据实际情况来定义)就去读取/var/log/secure脚本,取出里面恶意猜测IP,如果单位时间内(一星期)的连接数是高于一个阀值,例如100(此阀值也可以根据实际情况来定义),则将其加进/etc/hosts.deny黑名单里,如果低于此阀值,则无视此IP。

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

  1. Nov 28 10:18:08 centos2 sshd[7556]: Connection closed by 222.216.30.109
  2. 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
  3. 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
  4. Nov 28 10:18:10 centos2 sshd[7551]: Failed password for root from 222.216.30.109 port 2391 ssh2
  5. Nov 28 10:18:10 centos2 sshd[7552]: Connection closed by 222.216.30.109
  6. Nov 28 10:18:10 centos2 sshd[7553]: Failed password for root from 222.216.30.109 port 2397 ssh2
  7. Nov 28 10:18:10 centos2 sshd[7554]: Connection closed by 222.216.30.109
  8. Nov 28 10:18:11 centos2 sshd[7557]: Failed password for root from 222.216.30.109 port 2401 ssh2
  9. Nov 28 10:18:11 centos2 sshd[7558]: Connection closed by 222.216.30.109
  10. Nov 28 10:18:11 centos2 sshd[7559]: Failed password for root from 222.216.30.109 port 2403 ssh2
  11. Nov 28 10:18:11 centos2 sshd[7560]: Connection closed by 222.216.30.109
  12. Nov 28 10:37:01 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
  13. 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
  14. Nov 28 10:37:01 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user hello
  15. Nov 28 10:37:19 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
  16. 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
  17. Nov 28 10:37:19 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yhc
  18. Nov 28 10:37:36 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
  19. 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
  20. Nov 28 10:37:36 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
  21. Nov 28 10:42:44 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
  22. 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
  23. Nov 28 10:42:44 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user yuhongchun
  24. Nov 28 10:42:56 centos2 vsftpd: pam_unix(vsftpd:auth): check pass; user unknown
  25. 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
  26. Nov 28 10:42:56 centos2 vsftpd: pam_succeed_if(vsftpd:auth): error retrieving information about user andrewyu

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

  1. [root@centos2 log]# ls -lsart secure.*
  2. 512 -rw------- 1 root root 516379 11-04 01:31 secure.4
  3. 660 -rw------- 1 root root 668192 11-11 00:05 secure.3
  4. 304 -rw------- 1 root root 306589 11-17 10:33 secure.2
  5. 484 -rw------- 1 root root 488620 11-25 02:33 secure.1

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

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

很明显,这样是取不到vsftpd失败的IP值的,sshd日志失败信息跟vsftpd日志失败信息不一样,我写了几种awk混合sed的方法,测试了效率,感觉用awk脚本速度是最快的,大家也可以写几种,用time命令测试下;最后精简了下代码,完成了整个脚本,脚本内容如下所示:

  1. #!/bin/bash
  2. awk '{for(i=1;i<=NF;i++){if($i ~ /rhost/)print substr($i,7)}}' /var/log/secure | sort | uniq    -c    >/root/black.txt
  3. DEFINE="100"
  4. cat     /root/black.txt |    while read LINE
  5. do
  6.                 NUM=`echo $LINE |awk '{print $1}'`
  7.                 host=`echo $LINE    |awk '{print $2}'`
  8.                 if [ $NUM -gt $DEFINE ];
  9.                 then
  10.                  grep $host    /etc/hosts.deny > /dev/null
  11.                     if [ $? -gt 0 ];
  12.                     then
  13.                     echo "sshd:$host"     >> /etc/hosts.deny
  14.                     echo "vsftpd:$host" >> /etc/hosts.deny
  15.                     fi
  16.                 fi
  17. done

脚本运行一段时间后,我们可以观察此脚本涉及到的一些文件,如/root/black.txt,结果如下所示:

  1. [root@centos2 ~]# cat /root/black.txt
  2.                 2 113.17.144.156
  3.                 4 114.112.51.208
  4.                 4 114.112.69.170
  5.             169 118-163-227-50.hinet-ip.hinet.net
  6.                 8 119.188.7.200
  7.                 8 122.70.130.11
  8.              61 124.248.32.246
  9.              12 183.203.14.121
  10.                 3 189.26.255.11
  11.              56 199.204.237.60
  12.                 3 199.30.53.220
  13.                 5 201.236.80.4
  14.                 6 220.172.191.31
  15.              30 222.216.30.109
  16.              60 222.253.159.111
  17.              58 223.4.180.23
  18.             166 58.221.42.178
  19.                 1 61.132.4.85
  20.             152 61.142.106.34
  21.              22 61.167.33.222
  22.                 7 85.126.166.83
  23.             166 www.b-nets.com

/etc/hosts.deny脚本内容如下:

  1. sshd:124.248.32.246
  2. vsftpd:124.248.32.246
  3. sshd:199.204.237.60
  4. vsftpd:199.204.237.60
  5. sshd:222.253.159.111
  6. vsftpd:222.253.159.111
  7. sshd:223.4.180.23
  8. vsftpd:223.4.180.23
  9. sshd:58.221.42.178
  10. vsftpd:58.221.42.178
  11. sshd:61.142.106.34
  12. vsftpd:61.142.106.34
  13. sshd:118-163-227-50.hinet-ip.hinet.net
  14. vsftpd:118-163-227-50.hinet-ip.hinet.net
  15. sshd:www.b-nets.com
  16. vsftpd:www.b-nets.com

最后,我们将此shell脚本放进crontab 里,每间隔六小时就运行一次,命令如下:

  1. * */6 * * * root /bin/bash /root/hostsdeny.sh >> /dev/null 2>&1

由于/var/log/secure日志是以星期为轮询的,此脚本执行频率可自行设定,如果感觉服务器被频繁侦测,执行频率间隔可设置短些,反之,可设置长些。

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >