Centos7使用DenyHosts防止ssh暴力破解

大雄45發表於2020-06-07
DenyHosts是用python2.3編寫的一個程式,會分析/var/log/secure等檔案,當發現同一個ip進行多次ssh登入失敗時會將其寫入/etc/hosts.dengy檔案,達到遮蔽該ip的目的。

Centos7使用DenyHosts防止ssh暴力破解Centos7使用DenyHosts防止ssh暴力破解

下載DenyHosts包
[root@localhost ~]# wget 
安裝DenyHosts
[root@localhost ~]# tar xf DenyHosts-2.6.tar.gz
[root@localhost ~]# cd DenyHosts-2.6
[root@localhost DenyHosts-2.6]# python setup.py install
製作配置檔案
[root@localhost DenyHosts-2.6]# cp denyhosts.cfg-dist /etc/denyhosts.cfg
[root@localhost DenyHosts-2.6]# cp daemon-control-dist daemon-control
[root@localhost DenyHosts-2.6]# chown root daemon-control
[root@localhost DenyHosts-2.6]# chmod 700 daemon-control
修改配置檔案

將daemon-control中的#DENYHOSTS_CFG = "/usr/share/denyhosts/denyhosts.cfg"改為 DENYHOSTS_CFG = "/etc/denyhosts.cfg"
Centos7使用DenyHosts防止ssh暴力破解Centos7使用DenyHosts防止ssh暴力破解

啟動
[root@localhost DenyHosts-2.6]# ./daemon-control start(注意相對路徑)
測試另外一臺伺服器遠端連線172.16.1.16

預設允許五次,測試發現登陸失敗五次之後不允許登陸

[root@localhost ~]# ssh abc@172.16.1.16
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost ~]# 
[root@localhost ~]# ssh abc@172.16.1.16
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost ~]# ssh abc@172.16.1.16
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost ~]# ssh abc@172.16.1.16
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied, please try again.
abc@172.16.1.16's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost ~]# ssh abc@172.16.1.16
ssh_exchange_identification: read: Connection reset by peer
檢視hosts.deny檔案

登陸連續失敗之後就會把ip地址寫在hosts.deny檔案
Centos7使用DenyHosts防止ssh暴力破解Centos7使用DenyHosts防止ssh暴力破解
172.16.1.112的ip已經被限制,到此功能實現。

本文原創地址:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2696700/,如需轉載,請註明出處,否則將追究法律責任。

相關文章