Linux服务器总是被猜测密码怎么办?这个脚本帮你简单加固

简介: Linux服务器总是被猜测密码怎么办?这个脚本帮你简单加固

我们在使用ssh登录服务器的时候,经常会弹出类似于以下的提示:Last failed login: Fri May 1 23:31:22 CST 2021 from 87.251.74.56 on ssh:notty There were 187 failed login attempts since the last successful login.


image.png


后面一句意思是从上次登录成功之后,有187次失败的登录。也就是说有人在尝试登录我们的服务器,但是登录失败了,距离上次成功登录到本次登录之前产生了187的失败记录,不可质疑,有人在猜测服务器的登录用户名和密码. 我们来查看一下服务器失败登录记录,用以下命令查看:


# 查看失败登录记录
lastb
# 结果展示,跟上参数-xx,表示显示多少记录
root     ssh:notty    110.188.85.88    Tue Oct  5 10:46 - 10:46  (00:00)    
pi       ssh:notty    121.180.246.194  Mon Oct  4 10:53 - 10:53  (00:00)    
pi       ssh:notty    121.180.246.194  Mon Oct  4 10:53 - 10:53  (00:00)    
pi       ssh:notty    121.180.246.194  Mon Oct  4 10:53 - 10:53  (00:00)    
pi       ssh:notty    121.180.246.194  Mon Oct  4 10:53 - 10:53  (00:00)    
root     ssh:notty    110.188.84.142   Mon Oct  4 10:09 - 10:09  (00:00)    
root     ssh:notty    110.188.84.142   Mon Oct  4 10:09 - 10:09  (00:00)    
root     ssh:notty    125.70.165.6     Sun Oct  3 09:39 - 09:39  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 09:00 - 09:00  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 09:00 - 09:00  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 08:57 - 08:57  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 08:57 - 08:57  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 08:55 - 08:55  (00:00)    
db2inst1 ssh:notty    152.136.245.102  Sun Oct  3 08:55 - 08:55  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:53 - 08:53  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:53 - 08:53  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:50 - 08:50  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:50 - 08:50  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:48 - 08:48  (00:00)    
rx       ssh:notty    152.136.245.102  Sun Oct  3 08:48 - 08:48  (00:00)


从结果可以看出,第一列为登录时所用的用户名,第二列为登录方式(了解,具体含义可以参考以下摘要),第三列为登录客户端IP地址,最后一列为登录时间。

“ Notty”一词仅表示“ no tty”,大致翻译为“ no terminal”。 当您本地登录到任何Linux计算机时,终端将始终在进程列表中显示为“ tty”。 如果通过SFTP建立了连接,或者您正在使用SCP复制文件,那么它将显示为tty(notty)。
Who or what is root@notty?
If you’re looking through WHM’s process manager and you see root@notty mentioned as one of the processes, don’t be alarmed. It’s perfectly normal and it’s definitely not some hacker called ‘Notty’ who has suddenly got root permissions. Be honest, you’re here because you thought that 😉
You may also have seen sshd: root@notty in the output of ps aux too.
Why notty?
The term ‘notty’ just represents ‘no tty’ which roughly translates as meaning ‘no terminal’. When you login locally to any Linux machine the terminal will always appear in the process list as ‘tty’. If a connection is made via SFTP or you are copying files with SCP (as I did here on a test server prior to bringing up the screenshot above) then it will show as no tty (notty).
Where does TTY come from?
Many years ago, user terminals that were connected to computers were clunky and noisy Electro-mechanical Teleprinters also known as Teletypewriters. They took the latter phrase and chopped some characters out to get the TTY abbreviation:
TeleTYpewriter = TTY
Since then, TTY has been used as the shortened name for a text-only console.
————————————————
版权声明:本文为CSDN博主「bh6635」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013601606/article/details/105226727/


从上面看到这些IP在一段时间内使用不同的用户名和密码在尝试登录服务器,要是通过shell脚本的方式,统计出失败登录IP的次数,如果大于我们的设定值,我们就把该IP放入黑名单中。

#!/bin/bash
#Denyhosts SHELL SCRIPT
# 分析登录日志文件,筛选失败登录并统计次数存入文件备用
cat /var/log/secure | awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"=" $1;}' >/root/Denyhosts.txt
# 定义允许失败登录的次数
DEFINE="10"
# 读取文件,并把条件范围内的IP写到hosts.deny中,实现黑名单效果
for i in `cat /root/Denyhosts.txt`
do
  IP=`echo $i|awk -F= '{print $1}'`
  NUM=`echo $i|awk -F= '{print $2}'`
  if [ $NUM -gt $DEFINE ]
  then
    ipExists=`grep $IP /etc/hosts.deny |grep -v grep |wc -l`
    if [ $ipExists -lt 1 ]
    then
      echo "sshd:$IP" >> /etc/hosts.deny
    fi
  fi
done


最后把文件保存并添加执行权限和定时任务,就可以自动的分析IP并加黑名单了。让这些顽固分子知道我们管理员也是有做事的!

相关文章
|
弹性计算 安全 Linux
阿里云服务器ECS安装宝塔Linux面板、安装网站(新手图文教程)
本教程详解如何在阿里云服务器上安装宝塔Linux面板,涵盖ECS服务器手动安装步骤,包括系统准备、远程连接、安装命令执行、端口开放及LNMP环境部署,手把手引导用户快速搭建网站环境。
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
2429 21
|
监控 安全 Linux
在Linux中设定账户密码的安全性策略
这些操作应该由有经验的系统管理员进行,因为不当的配置可能导致无法预期的安全问题或者系统访问问题。此外,提升安全性的同时,也需要考虑到用户的便利性,避免设置过于严苛的政策导致用户体验不佳。通常,强密码策略配合两因素认证(2FA)将大大加强账户的安全性。
1114 13
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
1466 13
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
4835 0
|
存储 安全 Linux
Linux服务器上安装配置GitLab的步骤。
按照以上步骤,一个基础的GitLab服务应该运行并可以使用。记得定期检查GitLab官方文档,因为GitLab的安装和配置步骤可能随着新版本而变化。
1233 0
|
弹性计算 运维 监控
ECS使用与Linux环境搭建
本文主要记录ECS使用中,安全组配置,Linux下生成环境搭建等。
ECS使用与Linux环境搭建
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
1159 10
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
1114 8
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。