歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux配置 >> denyhost防止SSH暴力破解配置詳解

denyhost防止SSH暴力破解配置詳解

日期:2017/2/27 14:58:40   编辑:Linux配置
DenyHosts是Python語言寫的一個程序,它會分析sshd的日志文件,當發現重復的攻擊時就會記錄IP到/etc/hosts.deny文件,從而達到自動屏IP的功能。
DenyHosts官方網站為:http://denyhosts.sourceforge.net

安裝
# cd /usr/local/src
# wget http://mesh.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.5.tar.gz
# tar -zxvf DenyHosts-2.5.tar.gz
# cd DenyHosts-2.5
# python setup.py install

默認是安裝到/usr/share/denyhosts目錄的。
# cd /usr/share/denyhosts/
# cp denyhosts.cfg-dist denyhosts.cfg
# vi denyhosts.cfg

根據自己需要進行相應的配置(解釋見下文件的配置文件)

設置啟動腳本
# cp daemon-control-dist daemon-control
# chown root daemon-control
# chmod 700 daemon-control

完了之後執行daemon-contron start就可以了。
# ./daemon-control start

如果要使DenyHosts每次重起後自動啟動還需做如下設置:
# cd /etc/init.d
# ln -s /usr/share/denyhosts/daemon-control denyhosts
# chkconfig --add denyhosts
# chkconfig --level 2345 denyhosts on

或者修改/etc/rc.local文件:
# vi /etc/rc.local

加入下面這條命令:
/usr/share/denyhosts/daemon-control start

DenyHosts配置文件:
vi /usr/share/denyhosts/denyhosts.cfg
SECURE_LOG = /var/log/secure
#ssh 日志文件,它是根據這個文件來判斷的。

HOSTS_DENY = /etc/hosts.deny
#控制用戶登陸的文件

PURGE_DENY = 5m
#過多久後清除已經禁止的

BLOCK_SERVICE = sshd
#禁止的服務名

DENY_THRESHOLD_INVALID = 1
#允許無效用戶失敗的次數

DENY_THRESHOLD_VALID = 10
#允許普通用戶登陸失敗的次數

DENY_THRESHOLD_ROOT = 5
#允許root登陸失敗的次數

HOSTNAME_LOOKUP=NO
#是否做域名反解

ADMIN_EMAIL =
#管理員郵件地址,它會給管理員發郵件

DAEMON_LOG = /var/log/denyhosts#自己的日志文件

然後就可以啟動了:
service denyhost start

可以看看/etc/hosts.deny內是否有禁止的IP,有的話說明已經成功了。

如果出現:./daemon-control start配置
starting DenyHosts: /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
Can't read: /private/var/log/system.log
[Errno 2] No such file or directory: '/private/var/log/system.log'
Error deleting DenyHosts lock file: /var/lock/subsys/denyhosts
[Errno 2] No such file or directory: '/var/lock/subsys/denyhosts'

修改一下denyhosts.cfg文件(24-25行),把25行用#注釋掉就可以了。
24、# Mac OS X (v10.3 or earlier):
25、SECURE_LOG=/private/var/log/system.log
Copyright © Linux教程網 All Rights Reserved