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并加黑名单了。让这些顽固分子知道我们管理员也是有做事的!

相关文章
|
17天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
20天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 <<EOF binary put file.txt quit EOF ```
29 4
|
17天前
|
关系型数据库 MySQL Linux
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
21 0
|
23小时前
|
Linux
如何将一个linux服务器挂载到另外一个linux服务器上
如何将一个linux服务器挂载到另外一个linux服务器上
13 1
|
2天前
|
Linux 数据安全/隐私保护
Linux系统忘记密码的三种解决办法
这篇博客介绍了三种在Linux忘记密码时重置登录密码的方法:1) 使用恢复模式,通过控制台界面以管理员权限更改密码;2) 利用Linux Live CD/USB启动,挂载硬盘分区并使用终端更改密码;3) 进入单用户模式,自动以管理员身份登录后重置密码。每个方法都提供了详细步骤,提醒用户在操作前备份重要数据。
|
2天前
|
监控 Linux 网络安全
Linux服务器如何查询连接服务器的IP
【4月更文挑战第15天】Linux服务器如何查询连接服务器的IP
9 1
|
2天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
41 2
|
4天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
13 0
|
7天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
7天前
|
监控 安全 Linux
《Linux 简易速速上手小册》第8章: 安全性与加固(2024 最新版)
《Linux 简易速速上手小册》第8章: 安全性与加固(2024 最新版)
21 0