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

相关文章
|
1天前
|
Linux 数据库 iOS开发
超级签名源码/超级签/ios分发/签名端本地linux服务器完成签名
该系统完全在linux下运行,不存在使用第三方收费工具,市面上很多系统都是使用的是第三方收费系统,例如:某心签名工具,某测侠等,不开源而且需要每年交费,这种系统只是在这些工具的基础上套了一层壳。请需要系统的放大你们的眼睛。
7 0
|
2天前
|
Linux
【Linux】一条命令,转发所有请求到另一台服务器上 -高级技巧
【Linux】一条命令,转发所有请求到另一台服务器上 -高级技巧
6 0
|
3天前
|
openCL Linux 异构计算
Linux服务器如何查询GPU型号
【6月更文挑战第13天】Linux服务器如何查询GPU型号
9 1
|
4天前
|
Linux Shell 测试技术
Linux服务器测试脚本集合
LemonBench是iLemonrain创作的Linux服务器性能测试工具,能一键检测系统信息、网络、CPU、内存和硬盘性能。
6 0
|
10天前
|
监控 安全 Linux
Linux服务器如何管理sshd的连接
【6月更文挑战第6天】Linux服务器如何管理sshd的连接
15 4
|
11天前
|
Ubuntu Linux 网络安全
ubuntu linux 搭建 webssh 网页ssh远程登录其他服务器
ubuntu linux 搭建 webssh 网页ssh远程登录其他服务器
|
21小时前
|
算法 网络协议 Linux
探索Linux命令idn:处理国际化域名
`idn`命令在Linux中用于处理国际化域名,转换成ASCII兼容的ACE格式或反之。它支持Punycode算法,提供命令行接口及多种参数,如`-a`转ASCII,`-d`转回国际化域名。示例包括将`xn--zhonggu-wu9d.com`转换。使用时注意有效输入,考虑版本兼容性,并可与其他工具结合使用。
|
21小时前
|
Linux 数据处理 数据库
深入解析Linux命令id:理解用户身份与权限
`id`命令在Linux中用于显示用户身份(UID, GID和附加组)。它查看系统用户数据库获取信息。参数如`-u`显示UID,`-g`显示GID,`-G`显示附加组,结合`-n`显示名称而非ID。用于确认命令执行者身份,确保权限正确。在脚本中使用时注意权限管理,遵循最小权限原则。
|
22小时前
|
Linux 数据处理
Linux命令iconv:字符编码转换的利器
`iconv`是Linux下的字符编码转换工具,支持多种编码如UTF-8、ISO-8859-1等。它允许用户指定源(-f)和目标(-t)编码,转换文件或输出到指定文件(-o)。使用`-l`可列出所有支持的编码。示例:将UTF-8文件转为ISO-8859-1编码:`iconv -f UTF-8 -t ISO-8859-1 input.txt -o output.txt`。在转换前确认源编码,测试小样本,备份数据,并注意特殊字符处理。
|
22小时前
|
网络协议 Linux 网络安全
Linux命令hostnamectl:掌握系统主机信息的利器
`hostnamectl`是Linux系统管理的关键工具,用于查看和设置主机名、内核信息等。它集成在`systemd`中,通过修改配置文件交互。命令特点包括综合显示多种信息、简单语法和设置功能。例如,`hostnamectl status`显示系统详情,`sudo hostnamectl set-hostname NEWHOSTNAME`用于更改主机名。使用时注意权限、备份配置、更新网络和重启相关服务,避免频繁更改。