DenyHosts安装及配置详解

DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。

1. 到官网下载DenyHosts

DenyHosts官网:http://denyhosts.sourceforge.net/

2. 解压并安装

# 官网下载包安装

[root@www ~]

# tar zxvf DenyHosts-2.6.tar.gz

[root@www ~]

# cd DenyHosts-2.6

[root@www DenyHosts-2.6]

# yum install python -y

[root@www DenyHosts-2.6]

# python setup.py install

[root@www DenyHosts-2.6]

# cd /usr/share/denyhosts/

[root@www denyhosts]

# cp denyhosts.cfg-dist denyhosts.cfg //配置文件

[root@www denyhosts]

# cp daemon-control-dist daemon-control //启动文件

[root@www denyhosts]

# chown root daemon-control

[root@www denyhosts]

# chmod 700 daemon-control # yum安装

[root@www ~]

# yum install -y denyhosts

3. 配置文件内容说明

[root@www denyhosts]# vi denyhosts.cfg
SECURE_LOG = /var/log/secure   #ssh日志文件
# format is: i[dhwmy]
# Where i is an integer (eg. 7)
# m = minutes
# h = hours
# d = days
# w = weeks
# y = years
#
# never purge:
PURGE_DENY = 50m               #过多久后清除已阻止IP
HOSTS_DENY = /etc/hosts.deny   #将阻止IP写入到hosts.deny
BLOCK_SERVICE = sshd           #阻止服务名
PURGE_THRESHOLD =              #定义了某一IP最多被解封多少次。某IP暴力破解SSH密码被阻止/解封达到了PURGE_THRESHOLD次,则会被永久禁止;
DENY_THRESHOLD_INVALID = 1     #允许无效用户登录失败的次数
DENY_THRESHOLD_VALID = 10      #允许普通用户登录失败的次数
DENY_THRESHOLD_ROOT = 5        #允许root登录失败的次数
WORK_DIR = /usr/local/share/denyhosts/data #将deny的host或ip纪录到Work_dir中
DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹
LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
HOSTNAME_LOOKUP=NO            #是否做域名反解
ADMIN_EMAIL =                 #设置管理员邮件地址
DAEMON_LOG = /var/log/denyhosts #DenyHosts日志位置

ps:配置文件内容根据自己需要自行调整

4. 启动自启动服务

# ./daemon-control start #启动DenyHosts
# ln -s /usr/share/denyhosts/daemon-control /etc/init.d   //对daemon-control进行软连接,方便管理
# /etc/init.d/daemon-control start           //启动denyhosts
# chkconfig daemon-control on                //将denghosts设成开机启动
或者
# vi /etc/rc.local --------->加入下面这条命令:
/usr/share/denyhosts/daemon-control start

5. 以后可以直接查看hosts.deny文件就能找到攻击ip的记录

# vi /etc/hosts.deny

wget https://nchc.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz

tar zxvf DenyHosts-2.6.tar.gz

cd DenyHosts-2.6

python setup.py install

cd /usr/share/denyhosts

cp daemon-control-dist daemon-control

cp denyhosts.cfg-dist denyhosts.cfg

chown root daemon-control

chmod 700 daemon-control

echo “/usr/share/denyhosts/daemon-control start” >> /etc/rc.local
cat /etc/rc.local

echo “/usr/share/denyhosts/daemon-control start” >> /etc/bashrc
cat /etc/bashrc

echo “/usr/share/denyhosts/daemon-control start” >> /etc/profile
cat /etc/profile

echo “/usr/share/denyhosts/daemon-control start” >> /etc/rc.local
cat /etc/rc.local

nohup /usr/share/denyhosts/daemon-control start &

chmod +x /etc/rc.d/rc.local
/usr/share/denyhosts/daemon-control start

ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts

chkconfig –add denyhosts

chkconfig denyhosts on

chkconfig –list denyhosts

systemctl start denyhosts

systemctl status denyhosts

发表评论