需要python開發環境,並且版本大於2.4
[root@ localhost ~]# python -V #查看當前系統中python的版本。
Python 2.6.6
使用Yum安裝fail2ban
[root@ localhost ~]# yum -y install epel-release
[root@ localhost ~]# yum -y install fail2ban
修改實例文件/etc/fail2ban/jail.conf
[root@ localhost ~]# vim /etc/fail2ban/jail.conf
[DEFAULT] #全局設置。
ignoreip = 127.0.0.1/8 #忽略的IP列表,不受設置限制。
bantime = 600 #屏蔽時間,單位:秒。
findtime = 600 #這個時間段內超過規定次數會被ban掉。
maxretry = 3 #最大嘗試次數。
backend = auto #日誌修改檢測機制(gamin、polling和auto這三種)。
[sshd] #單個服務檢查設置,如設置bantime、findtime、maxretry和全局衝突,服務優先級大於全局設置。
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
#加入如下內容
enabled = true #是否激活此項(true/false)修改成 true。
filter = sshd #過濾規則filter的名字,對應filter.d目錄下的sshd.conf。
action = iptables[name=SSH, port=ssh, protocol=tcp] #動作的相關參數,對應action.d/iptables.conf文件。
sendmail-whois[name=SSH, [email protected], [email protected], sendername="Fail2Ban"] #觸發報警的收件人。
logpath = /var/log/secure #檢測的系統的登陸日誌文件。這裏要寫sshd服務日誌文件。 默認爲logpath = /var/log/sshd.log 。
#5分鐘內3次密碼驗證失敗,禁止用戶IP訪問主機1小時。 配置如下。
bantime = 3600 #禁止用戶IP訪問主機1小時。
findtime = 300 #在5分鐘內內出現規定次數就開始工作。
maxretry = 3 #3次密碼驗證失敗。
啓動fail2ban服務
[root@ localhost ~]# systemctl start fail2ban #啓動fail2ban服務。
[root@ localhost ~]# systemctl enable fail2ban #設置開機自動啓動。
測試
[root@ localhost ~]# > /var/log/secure #清空日誌內容。
[root@ localhost ~]# systemctl restart fail2ban #重啓fail2ban服務。
[root@ localhost ~]# iptables -L –n #在fail2ban服務啓動後,iptables會多生成一個規則鏈,
123
測試:故意輸入錯誤密碼3次,再進行登錄時,會拒絕登錄
用另一臺虛擬機進行訪問
[root@ localhost ~]# ssh 10.0.0.66
The authenticity of host '10.0.0.66 (10.0.0.66)' can't be established.
RSA key fingerprint is 9b:57:b9:86:84:90:a4:4b:44:3e:18:9f:8a:29:6f:e5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.66' (RSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Connection closed by 10.0.0.66
測試是否還能連接
[root@ localhost ~]# ssh 10.0.0.66
ssh: connect to host 10.0.0.66 port 22: Connection refused
防暴力破解完成