当前位置:网站首页>SSH访问控制,多次失败登录即封掉IP,防止暴力破解
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
2022-07-03 13:54:00 【星哥玩云】
一、系统:CentOS 6.3 64位
二、方法:读取/var/log/secure,查找关键字 Failed,例如(注:文中的IP地址特意做了删减):
Sep 17 09:08:09 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2 Sep 17 09:08:20 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2 Sep 17 09:10:02 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2 Sep 17 09:10:14 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2
从这些行中提取IP地址,如果次数达到5次则将该IP写到 /etc/hosts.deny中。
三、步骤:
1、先把始终允许的IP填入 /etc/hosts.allow ,这很重要!比如: sshd:19.16.18.1:allow sshd:19.16.18.2:allow
2、脚本 /root/secure_ssh.sh
#! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt DEFINE="5" for i in `cat /root/black.txt` do IP=`echo $i |awk -F= '{print $1}'` NUM=`echo $i|awk -F= '{print $2}'` if [ $NUM -gt $DEFINE ];then grep $IP /etc/hosts.deny > /dev/null if [ $? -gt 0 ];then echo "sshd:$IP:deny" >> /etc/hosts.deny fi fi done
3、将secure_ssh.sh脚本放入cron计划任务,每1分钟执行一次。 # crontab -e */1 * * * * sh /root/secure_ssh.sh
四、测试:
1、开两个终端窗口,一个ssh连上服务器,另一个用错误的密码连接服务器几次。
很快,服务器上黑名单文件里已经有记录了: [[email protected] ~]# $ cat /root/black.txt 13.26.21.27=3
再看看服务器上的hosts.deny [[email protected] ~]# cat /etc/hosts.deny sshd:13.7.3.6:deny sshd:92.4.0.4:deny sshd:94.10.4.2:deny sshd:94.4.1.6:deny sshd:11.64.11.5:deny
2、从另一个终端窗口继续“暴力”连接服务器。
看看服务器上的黑名单文件: [[email protected] ~]# cat black.txt 13.26.21.27=6
再看看服务器上的hosts.deny [[email protected] ~]# cat /etc/hosts.deny sshd:13.7.3.6:deny sshd:92.4.0.4:deny sshd:94.10.4.2:deny sshd:94.4.1.6:deny sshd:11.64.11.5:deny sshd:13.26.21.27:deny
IP 已经被加入到服务器的hosts.deny,再用正确的密码连接服务器,被拒绝: $ ssh [email protected] -p 2333 ssh_exchange_identification: Connection closed by remote host
注: 1.脚本为同事编写。
2.服务器sshd端口已改为2333,事实证明,改了端口后,暴力破解的ssh连接数锐减。
边栏推荐
- “又土又穷”的草根高校,凭什么被称为“东北小清华”?
- 牛客网:过河卒
- Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
- Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
- 泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
- Vite project commissioning
- JS get DPI, PX to cm, cm to PX
- Exercise 10-1 calculate the sum of 1 to n using recursive functions
- UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料
- Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry
猜你喜欢

Programmable logic device software testing

Exercise 10-1 calculate the sum of 1 to n using recursive functions

allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
[email protected] (FE) | glycyrrhetinic acid modified metal organ"/>Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ

jvm-运行时数据区

Exercise 10-6 recursively find Fabonacci sequence

Leetcode (4) -- find the median of two positively ordered arrays

Message subscription and publishing

Fabric. JS document

Generate directories from web content
随机推荐
JVM object lifecycle
Exercise 6-2 using functions to sum special A-string sequences
JS continues to explore...
Generate directories from web content
556. 下一个更大元素 III
The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log
selenium 浏览器(1)
Print. JS -- web page file printing
GRPC的四种数据流以及案例
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
Raft 协议
信创产业现状、分析与预测
Exercise 10-2 recursive factorial sum
别再问自己适不适合做软件测试了
Exercise 9-1 time conversion
jvm-运行时数据区
Message subscription and publishing
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
JS general form submission 1-onsubmit
Metal organic framework material zif-8 containing curcumin( [email protected] Nanoparticles) | nano metal organic framework carry