網站首頁 編程語言 正文
shell腳本設置訪問控制,多次失敗登錄即封掉IP,防止暴力破解ssh
當發現某一臺主機向服務器進行SSH遠程管理,在10分鐘內輸錯密碼3次,使用firewalld進行響應, 禁止此IP再次連接,6個小時候后再次允許連接。
一、系統:Centos7.1 64位
二、方法:讀取/var/log/secure,查找關鍵字 Failed
Jan 4 16:29:01 centos7 sshd[1718]: Failed password for root from 192.168.120.1 port 2171 ssh2 Jan 4 16:29:02 centos7 sshd[1718]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root" Jan 4 16:29:04 centos7 sshd[1718]: Failed password for root from 192.168.120.1 port 2171 ssh2 Jan 4 16:29:06 centos7 sshd[1718]: error: Received disconnect from 192.168.120.1 port 2171:0: [preauth] Jan 4 16:45:53 centos7 sshd[1758]: Failed password for root from 192.168.120.134 port 40026 ssh2
三.shell代碼,創建test.sh
#!bin/bash #Intercept illegal IP addresses month=$(LANG=C date +"%b") day=$(LANG=C date +"%e") now=$(LANG=C date +"%T") ten=$(LANG=C date -d "10 minutes ago" +"%T") cat /var/log/secure |awk '$1=="'$month'" && $2=='"$day"' && $3>="'$ten'" && $3<="'$now'" { print}' |grep 'Failed'|awk -F'from' '{ print $2}' |awk '{ print $1}'|sort |uniq -c > baduser.txt #Number of calculations times=`awk '{ print $1 }' baduser.txt` #banned IP address seq=1 for i in $times do ip=`sed -n ''$seq'p' baduser.txt |awk '{ print $2}'` if [ $i -ge 3 ] then firewall-cmd --add-rich-rule='rule family=ipv4 source address='$ip' port port=22 protocol=tcp reject' --timeout=6h fi seq=`expr $seq + 1` done
四.將test.sh腳本放入cron計劃任務,每60分鐘執行一次。
# crontab -e */60 * * * * sh /root/test.sh
五、測試
1、開一個終端窗口,一個ssh連上服務器,另一個用錯誤的密碼連接服務器幾次。
很快,服務器上黑名單baduser.txt文件里已經有記錄了:
IP 已經被加入到服務器的baduser.txt文件中,無法連接服務器,被拒絕:
原文鏈接:https://blog.csdn.net/m0_46369125/article/details/122305489
相關推薦
- 2022-04-30 WPF在自定義文本框中實現輸入法跟隨光標_實用技巧
- 2022-06-19 C++深入探索類真正的形態之struct與class_C 語言
- 2022-07-23 C++超詳細講解稀疏矩陣_C 語言
- 2022-12-24 C++中STL容器的主要使用及含義說明_C 語言
- 2023-04-07 Redux?saga異步管理與生成器詳解_React
- 2022-09-18 Go實現文件分片上傳_Golang
- 2023-03-29 react?Table準備Spin?Empty?ConfigProvider組件實現_React
- 2022-09-04 C#線程委托BeginInvoke與EndInvoke的用法_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支