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

本文涉及的产品
运维安全中心(堡垒机),企业双擎版 50资产 7天
日志服务 SLS,月写入数据量 50GB 1个月
运维安全中心(堡垒机),免费版 6个月
简介:

新近刚上的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天前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
149 10
|
7天前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
62 2
|
22天前
|
Unix Linux Shell
指定端口-SSH连接的目标(告别 22 端口暴力破解)
本文介绍了 SSH 命令 `ssh -p 44907 root@IP` 的含义与使用方法,包括命令结构拆解、完整示例及执行过程详解,帮助用户安全地远程登录服务器。
118 0
|
27天前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
4月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
203 60
|
7月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
3月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
213 1
|
5月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
108 17
|
5月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
128 4
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
271 3