網(wǎng)站首頁 編程語言 正文
shell腳本設(shè)置訪問控制,多次失敗登錄即封掉IP,防止暴力破解ssh
當(dāng)發(fā)現(xiàn)某一臺主機(jī)向服務(wù)器進(jìn)行SSH遠(yuǎn)程管理,在10分鐘內(nèi)輸錯(cuò)密碼3次,使用firewalld進(jìn)行響應(yīng), 禁止此IP再次連接,6個(gè)小時(shí)候后再次允許連接。
一、系統(tǒng):Centos7.1 64位
二、方法:讀取/var/log/secure,查找關(guān)鍵字 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代碼,創(chuàng)建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計(jì)劃任務(wù),每60分鐘執(zhí)行一次。
# crontab -e */60 * * * * sh /root/test.sh
五、測試
1、開一個(gè)終端窗口,一個(gè)ssh連上服務(wù)器,另一個(gè)用錯(cuò)誤的密碼連接服務(wù)器幾次。
很快,服務(wù)器上黑名單baduser.txt文件里已經(jīng)有記錄了:
IP 已經(jīng)被加入到服務(wù)器的baduser.txt文件中,無法連接服務(wù)器,被拒絕:
原文鏈接:https://blog.csdn.net/m0_46369125/article/details/122305489
相關(guān)推薦
- 2022-11-10 Redis源碼設(shè)計(jì)剖析之事件處理示例詳解_Redis
- 2023-02-27 pandas?實(shí)現(xiàn)?in?和?not?in?的用法及使用心得_python
- 2022-11-27 關(guān)于HTTPS端口443的技術(shù)講解(什么是443端口)_服務(wù)器其它
- 2022-05-16 .NET中的HashSet及原理解析_實(shí)用技巧
- 2022-06-15 C#數(shù)據(jù)類型實(shí)現(xiàn)背包、隊(duì)列和棧_C#教程
- 2022-06-12 Python裝飾器的定義和使用詳情_python
- 2022-04-18 啟動(dòng)項(xiàng)目: getaddrinfo ENOTFOUND localhost
- 2022-11-18 React網(wǎng)絡(luò)請求發(fā)起方法詳細(xì)介紹_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支