当前位置: 首页 > news >正文

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/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文件轮询特征,如下所示:

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


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

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


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

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


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

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

由于/var/log/secure日志是以星期为轮询的,此脚本执行频率可自行设定,如果感觉服务器被频繁侦测,执行频率间隔可设置短些,反之,可设置长些。附注:如果仅仅只是要防止SSH暴力破解,这个脚本就没必要更新了,可以采纳我原先的SHELL脚本(即原始版本),此更新脚本适合部署在有FTP的公网机器上面,目前测试比较稳定,但感觉还是有不完美的地方,欢迎大家来信交流,抚琴煮酒(yuhognchun027@163.com)。










本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/1074650,如需转载请自行联系原作者

相关文章:

  • 8.10 shell特殊符号cut命令
  • ckeditor body与P标签去除
  • es6中新增的常用数值扩展
  • 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 1、两层神经网络的单样本向量化表示与多样本向量化表示...
  • 华为产品升级及打补丁具体步骤
  • 职场中的那点事--享受“无间道”
  • java基础-基本数据类型
  • 介绍UbuntuServer
  • 信息化建设者的愿景,诚意与信念
  • 书籍
  • 微软私有云分享(R2)18Windows Azure Pack 命令行安装
  • ss
  • 优化javaScript代码,提高执行效率
  • 小型企业Exchange server 2010高可用性方案要注意咯!
  • CentOS6.8 搭建SVN并用钩子自动实现同步到web目录
  • [译]Python中的类属性与实例属性的区别
  • create-react-app做的留言板
  • HomeBrew常规使用教程
  • Iterator 和 for...of 循环
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • PV统计优化设计
  • 闭包--闭包作用之保存(一)
  • 人脸识别最新开发经验demo
  • 使用 QuickBI 搭建酷炫可视化分析
  • 世界上最简单的无等待算法(getAndIncrement)
  • 为什么要用IPython/Jupyter?
  • 新手搭建网站的主要流程
  • Nginx实现动静分离
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (9)STL算法之逆转旋转
  • (java)关于Thread的挂起和恢复
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .form文件_SSM框架文件上传篇
  • .NET DataGridView数据绑定说明
  • .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上
  • .net 程序发生了一个不可捕获的异常
  • .NET/C# 使用反射注册事件
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • [2]十道算法题【Java实现】
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [28期] lamp兄弟连28期学员手册,请大家务必看一下
  • [BUAA软工]第一次博客作业---阅读《构建之法》
  • [BUG]Datax写入数据到psql报不能序列化特殊字符
  • [bzoj 3124][sdoi 2013 省选] 直径
  • [C#]使用DlibDotNet人脸检测人脸68特征点识别人脸5特征点识别人脸对齐人脸比对FaceMesh
  • [c++] 自写 MyString 类
  • [C++]:for循环for(int num : nums)
  • [hive] sql中distinct的用法和注意事项
  • [I2C]I2C通信协议详解(二) --- I2C时序及规格指引