SSH login failure recording and prevention (CentOS): A preliminary analysis of DenyHosts

1 ssh record

Environment: server, system CentOS 7.2

1.1 View normal login

Use command

last

1.2 View ssh login failure records

grep "Failed password for root" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more

2 Use denyhosts to prevent brute force cracking

  DenyHosts is a program written in Python language. It will analyze the sshd log file /var/log/secure and record the IP to the /etc/hosts.deny file when repeated attacks are found to achieve the function of automatically screen IP.

2.1 Installation script

   The installation server is required to have Internet access and create a /work directory

#!/bin/bash
wget http://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz    #下载软件 
tar -zxvf DenyHosts-2.6.tar.gz                                  #解压
mv DenyHosts-2.6 denyhost                                       #为了方便改个名
cd denyhost/                                                    #进入目录
yum install python -y                                           #安装python
python setup.py install                                         #安装denyhost,脚本
cd /usr/share/denyhosts/                                        #进入配置目录
cp daemon-control-dist daemon-control                           #为了方便改变配置文件名称
cp denyhosts.cfg-dist denyhosts.cfg                             #修改服务文件名称
chown root daemon-control                                       #修改服务文件名称
chmod 700 daemon-control                                        #提高安全级别修改权限
ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts #创建启动服务连接软连接
chkconfig denyhosts on                                          #添加启动项
cp denyhosts.cfg denyhosts.cfg.bak                          #备份配置文件为修改配置做准备
#cat /workspace/denyhost.txt > /usr/share/denyhosts/denyhosts.cfg  
#将配置文件内容导入配置文件我的配置文件安装之前已经配置好了
/etc/init.d/denyhosts start                                     #启动服务

 

echo install succeed!

2.2 Configuration file content

#cat /workspace/denyhost.txt > /usr/share/denyhosts/denyhosts.cfg

###########################2##################################

[root@107 workspace]# more denyhost.txt

SECURE_LOG = /var/log/secure

#ssh日志文件

HOSTS_DENY = /etc/hosts.deny

#将阻止IP写入到hosts.deny

PURGE_DENY = 5m

#过多久后清除已经禁止的其中w代表周d代表天h代表小时s代表秒m代表分钟

BLOCK_SERVICE = sshd

#阻止服务名

DENY_THRESHOLD_INVALID = 5

#允许无效用户在/etc/passwd未列出登录失败次数,允许无效用户登录失败的次数.

DENY_THRESHOLD_VALID = 5

#允许普通用户登录失败的次数

DENY_THRESHOLD_ROOT = 5

#允许root登录失败的次数

DENY_THRESHOLD_RESTRICTED = 1

#设定 deny host 写入到该资料夹

WORK_DIR = /usr/share/denyhosts/data

#将deny的host或ip纪录到Work_dir中

SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS = YES

HOSTNAME_LOOKUP=YES

#是否做域名反解

LOCK_FILE = /var/lock/subsys/denyhosts

#将DenyHOts启动的pid纪录到LOCK_FILE中已确保服务正确启动防止同时启动多个服务。

ADMIN_EMAIL = [email protected]

#设置管理员邮件地址

SMTP_HOST = localhost

SMTP_PORT = 25

SMTP_FROM = DenyHosts

SMTP_SUBJECT = DenyHosts Report

AGE_RESET_VALID=1d

#有效用户登录失败计数归零的时间

AGE_RESET_ROOT=1d

#root用户登录失败计数归零的时间

AGE_RESET_RESTRICTED=5d

#用户的失败登录计数重置为0的时间(/usr/share/denyhosts/data/restricted-usernames)

AGE_RESET_INVALID=10d

#无效用户登录失败计数归零的时间

DAEMON_LOG = /var/log/denyhosts

#自己的日志文件

DAEMON_SLEEP = 30s

DAEMON_PURGE = 5m

#该项与PURGE_DENY 设置成一样也是清除hosts.deniedssh 用户的时间

2.3 Other

    If you want to delete a banned host IP and add it to the list of allowed hosts, it is useless to delete it only in /etc/hosts.deny. Need to enter the /var/lib/denyhosts directory to enter the following operations

1. Stop DenyHosts service $ sudo service denyhosts stop

2. Delete the host IP you want to cancel in /etc/hosts.deny

3. Edit all files in the DenyHosts working directory to pass

$ sudo grep 192.168.1.191 /usr/share/denyhosts/data/*

Then delete the lines of the host IP that you want to cancel in the file one by one

* /usr/share/denyhosts/data/hosts

* /usr/share/denyhosts/data/hosts-restricted

* /usr/share/denyhosts/data/hosts-root

* /usr/share/denyhosts/data/hosts-valid

* /usr/share/denyhosts/data/users-hosts


4. Add the IP address of the host you want to allow to

/var/lib/denyhosts/allowed-hosts

vi /usr/share/denyhosts/data/allowed-hostsps

# We mustn’t block localhost

127.0.0.1

192.168.1.*

5. Start DenyHosts service service denyhosts start

View denyhosts service status:

/etc/init.d/denyhosts status

or

service denyhosts status

 

 

Reference materials:

1. A preliminary analysis of DenyHosts

2. SSh login failure log viewing and attack prevention

Guess you like

Origin blog.csdn.net/qq_27158179/article/details/90438932