实验探索openssh(五):通过第三方防护应用denyhosts来防止ssh暴力破解

版权声明:欢迎转载与交流。https://blog.csdn.net/one2more/article/details/90368524

一、DenyHosts简介

    DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。
    DenyHosts官网http://denyhosts.sourceforge.net/(点击领取阿礼云2000元代金券,拥有一台属于你的云主机)

二、DenyHosts应用

    当你的linux服务器暴露在互联网之中,该服务器将会遭到互联网上的扫描软件进行扫描,并试图猜测SSH登录口令。
    你会发现,每天会有多条SSH登录失败纪录。那些扫描工具将对你的服务器构成威胁,你必须设置复杂登录口令,并将尝试多次登录失败的IP给阻止掉,让其在一段时间内不能访问该服务器。
    用DenyHosts可以阻止试图猜测SSH登录口令,它会分析/var/log/secure等日志文件,当发现同一IP在进行多次SSH密码尝试时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽该IP的目的。

三、安装DenyHosts

    1、下载官方最新版2.6版本
wget https://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz
    2、解压源码包
[root@testhost01 ~]# ll
总用量 48
-rw-------. 1 root root  1495 520 14:03 anaconda-ks.cfg
-rw-r--r--  1 root root 42667 128 2006 DenyHosts-2.6.tar.gz
[root@testhost01 ~]# tar xzf DenyHosts-2.6.tar.gz -C /usr/local/
[root@testhost01 ~]# cd /usr/local/DenyHosts-2.6/
[root@testhost01 DenyHosts-2.6]# ll
总用量 96
-rw-r--r-- 1 500 500 16549 128 2006 CHANGELOG.txt
-rwxr-xr-x 1 500 500  4076 422 2006 daemon-control-dist
drwxr-x--- 2 500 500  4096 128 2006 DenyHosts
-rw-r--r-- 1 500 500 20830 820 2006 denyhosts.cfg-dist
-rwxr-xr-x 1 500 500  6578 78 2006 denyhosts.py
-rw-r--r-- 1 500 500 18009 1217 2005 LICENSE.txt
-rw-r--r-- 1 500 500   353 45 2006 MANIFEST.in
-rw-r----- 1 500 500   533 128 2006 PKG-INFO
drwxr-x--- 2 500 500    99 128 2006 plugins
-rw-r--r-- 1 500 500  3575 23 2006 README.txt
drwxr-x--- 2 500 500    73 128 2006 scripts
-rw-r--r-- 1 500 500  1522 45 2006 setup.py
    3、查看安装说明(README)(点击领取阿礼云2000元代金券,拥有一台属于你的云主机)
[root@testhost01 DenyHosts-2.6]# cat README.txt 
DenyHosts is a utility developed by Phil Schwartz which aims to 
thwart sshd (ssh server) brute force attacks.      #Denyhosts是PhilSchwartz开发的一个实用程序,旨在阻止sshd(ssh服务器)暴力攻击。    
Please refer to http://www.denyhosts.net/faq.html      #详情请查阅http://www.denyhosts.net faq.html     

INSTALLATION:         #安装说明
=============

SOURCE DISTRIBUTION    #源码包安装说明
===================
If you downloaded the source distribution file (DenyHosts-#.#.#-tar.gz)      #如果你下载了源码包,版本文件如()括号内所示
then:
$ tar zxvf DenyHosts-#.#.#-tar.gz       (Where #.#.# is the version)           #解压
$ cd DenyHosts-#.#.#                    #进入解压目录
as root:               #使用root身份
# python setup.py install       #执行安装脚本
This will install the DenyHosts modules into python's site-packages 
directory.        #这将把denyhosts模块安装到python包中所在的目录。

BINARY DISTRIBUTION (rpm, deb, etc)     #二进制包安装说明
===================
It is assumed that you are familiar with installing a binary package   #假设您熟悉安装二进制软件包
on your particular operating system.  If you are unsure how to do      #在特定的操作系统上。如果你不确定怎么做
this, you may wish to install from souce instead.                     #您可能希望从源码安装。

ALL DISTRIBUTIONS       #适用于所有的软件包安装说明
=================
Once you have installed DenyHosts, by default the directory
/usr/share/denyhosts will be created and a sample configuration 
file will be copied into it. A sample daemon-control script will 
also be copied into the /usr/share/denyhosts directory.
#一旦安装了denyhosts,默认情况下目录
#将创建/usr/share/denyhosts并创建示例配置
#文件将被复制到其中。一个示例守护进程控制脚本将
#也可以复制到/usr/share/denyhosts目录中。

DenyHosts requires that a configuration file be created before
it can function.  The sample configuration file denyhosts.cfg-dist
contains most of the possible settings and should be copied and
then edited as such:
#Denyhosts要求在配置文件被创建之前
#它才可以工作。示例配置文件denyhosts.cfg-dist
#包含大多数可能的设置,你应当复制它
#然后编辑它,具体如下:
# cp denyhosts.cfg-dist denyhosts.cfg          #复制示例配置文件为指定配置文件
# <edit> denyhosts.cfg                         #编辑配置文件
(where <edit> is your preferred text editor such as emacs, vi, etc)   #其中<edit>是首选的文本编辑器,如emacs、vi等

The sample configuration file contains informational comments that
should help you quickly configure DenyHosts.  After you have
edited your configuration file, save it.
#示例配置文件包含的信息注释
#应该可以帮助您快速配置Denyhosts。之后如果你有
#编辑过配置文件,请保存。

Next, if you intend to run DenyHosts in daemon mode (recommended) 
copy the sample daemon-control.dist script as such:
#接下来,如果您打算在守护程序模式下运行denyhosts(推荐)
#请复制示例daemon-control.dist脚本,如下所示:

# cp daemon-control-dist daemon-control      #复制脚本为指定内容

Edit the daemon-control file.  You should only need to edit this section
near the top:
#编辑守护进程控制文件。您只需编辑此分区
#靠近顶部的部分:
###############################################
#### Edit these to suit your configuration ####      #编辑这些以适合您的配置
###############################################
DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"
DENYHOSTS_LOCK  = "/var/lock/subsys/denyhosts"
DENYHOSTS_CFG   = "/usr/share/denyhosts/denyhosts.cfg"

These defaults should be reasonable for many systems.  You
should customize these settings to match your particular
system.
#对于许多系统来说,这些默认值应该是合理的。你
#应该自定义这些设置以匹配您的特定
#系统。

Once you have edited the configuration and daemon control files
make sure that the daemon control script it executable (by root).
#一旦您编辑了配置和守护进程控制文件
#确保守护进程控制脚本是可被root执行的。
# chown root daemon-control       #更改所有者为root
# chmod 700 daemon-control        #授予执行权限

STARTING DENYHOSTS MANUALLY           #手动启动Denyhosts
===========================
Assuming you have configured DenyHosts to run as a daemon, you
can use the daemon-control script to control it:
#假设您已经将denyhosts配置为作为守护进程运行,那么
#可以使用守护进程控制脚本来控制它:
# daemon-control start

You should refer to the daemon log (typically /var/log/denyhosts)
to ensure that DenyHosts is running successfully.  If you
notice any problems you may wish to consult the FAQ at
http://www.denyhosts.net/faq.html
#您应该参考守护进程日志(通常是/var/log/denyhosts)
#确保Denyhosts成功运行。如果你
#仍存有任何问题,您可能希望在以下网址查阅常见问题解答:
#http://www.denyhosts.net/faq.html

If you wish to run DenyHosts from cron rather than as a 
daemon, please refer to the FAQ.
#如果你想通过计划任务而不是作为一个
#守护进程,请参阅常见问题解答。

STARTING DENYHOSTS AUTOMATICALLY        #自动启动Denyhosts
================================
METHOD 1 (preferred)         #方法1(首选)
====================
Create a symbolic link from /etc/init.d such as:      #从/etc/init.d创建符号链接,例如:
# cd /etc/init.d            #进入目录
# ln -s /usr/share/denyhosts/daemon-control denyhosts     #创建软链接

If you have chkconfig installed you can then use it to
ensure that DenyHosts runs at boot time:
#如果安装了chkconfig,则可以使用它
#确保Denyhosts在启动时运行:
# chkconfig --add denyhosts          #添加开机启动项

If you do not have chkconfig (or similar) installed you can either manually
create the symlinks in /etc/rc2.d, /etc/rc3.d, /etc/rc5.d but that is beyond
the scope of this document.  
#如果没有安装chkconfig(或类似配置),可以手动
#在/etc/rc2.d,/etc/rc3.d,/etc/rc5.d中创建符号链接,但这超出了
#当前文档的范围。

METHOD 2      #方法2
========
Add an entry into the /etc/rc.local file:      #添加以下内容,输入到/ etc / rc.local文件中:
/usr/share/denyhosts/daemon-control start
    4、安装DenyHosts(点击领取阿礼云2000元代金券,拥有一台属于你的云主机)
[root@testhost01 DenyHosts-2.6]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/DenyHosts
copying DenyHosts/prefs.py -> build/lib/DenyHosts
... 省略 ...
creating /usr/share/denyhosts/scripts
copying scripts/restricted_from_invalid.py -> /usr/share/denyhosts/scripts
copying scripts/restricted_from_passwd.py -> /usr/share/denyhosts/scripts
creating /usr/share/denyhosts/plugins
copying plugins/README.contrib -> /usr/share/denyhosts/plugins
copying plugins/shorewall_allow.sh -> /usr/share/denyhosts/plugins
copying plugins/shorewall_deny.sh -> /usr/share/denyhosts/plugins
copying plugins/test_deny.py -> /usr/share/denyhosts/plugins
copying LICENSE.txt -> /usr/share/denyhosts
running install_egg_info
Writing /usr/lib/python2.7/site-packages/DenyHosts-2.6-py2.7.egg-info

四、配置DenyHosts(点击领取阿礼云2000元代金券,拥有一台属于你的云主机)

    1、查看启动脚本位置
[root@testhost01 DenyHosts-2.6]# grep chkconfig /usr/local/DenyHosts-2.6/ * -R --color
/usr/local/DenyHosts-2.6/CHANGELOG.txt:- added chkconfig compatibility to the daemon-control-dist script.  Thanks to
/usr/local/DenyHosts-2.6/daemon-control-dist:# chkconfig: 2345 98 02
/usr/local/DenyHosts-2.6/README.txt:If you have chkconfig installed you can then use it to
/usr/local/DenyHosts-2.6/README.txt:# chkconfig --add denyhosts
/usr/local/DenyHosts-2.6/README.txt:If you do not have chkconfig (or similar) installed you can either manually
    2、进入安装目录
[root@testhost01 DenyHosts-2.6]# cp /usr/local/DenyHosts-2.6/daemon-control-dist /etc/rc.d/init.d/denyhosts
[root@testhost01 DenyHosts-2.6]# cd /usr/share/denyhosts/
[root@testhost01 denyhosts]# ll
总用量 76
-rw-r--r-- 1 root root 16549 128 2006 CHANGELOG.txt
-rwxr-xr-x 1 root root  4076 422 2006 daemon-control-dist
-rw-r--r-- 1 root root 20830 820 2006 denyhosts.cfg-dist
-rw-r--r-- 1 root root 18009 1217 2005 LICENSE.txt
drwxr-xr-x 2 root root    99 525 15:18 plugins
-rw-r--r-- 1 root root  3575 23 2006 README.txt
drwxr-xr-x 2 root root    73 525 15:18 scripts
-rw-r--r-- 1 root root  1522 45 2006 setup.py
    3、复制配置文件
[root@testhost01 denyhosts]# cp denyhosts.cfg-dist /usr/share/denyhosts/denyhosts.cfg

五、配置文件详解(点击领取阿礼云2000元代金券,拥有一台属于你的云主机)

[root@testhost01 denyhosts]# vim /usr/share/denyhosts/denyhosts.cfg
############ THESE SETTINGS ARE REQUIRED ############             #这些设置是必需的
########################################################################
SECURE_LOG = /var/log/secure            #ssh认证日志文件位置
HOSTS_DENY = /etc/hosts.deny            #拒绝列表文件
PURGE_DENY =                           #清理列表周期①'i' is an integer (eg. 7)每几天;② 'm' = minutes分钟(例如,10m);③'h' = hours小时(例如,1h); ④'d' = days天(例如,3d);⑤'w' = weeks周(例如,1w);⑥'y' = years每年(例如,1y);默认为永不清理。
PURGE_THRESHOLD =                    #用来定义清除的阈值,定义主机被清除的最大次数。一旦超过该值,则不会清除该主机。将此参数注释掉或设置为0(默认值)将禁用此功能。
BLOCK_SERVICE  = sshd               #block_service:主机中应阻止的服务名称。可以指定具体的服务也可以写ALL(所有)
DENY_THRESHOLD_INVALID = 5           #拒绝的阈值:在允许登录失败的次数后,阻止每个尝试次数已超过此值的主机。此值适用于无效用户登录尝试(例如不存在的用户帐户)
DENY_THRESHOLD_VALID = 10           #拒绝的阈值:在允许登录失败的次数后,阻止每个尝试次数已超过此值的主机。此值适用于有效用户登录尝试(例如/etc/passwd中存在的用户帐户),除了“root”用户(root用户除外)
DENY_THRESHOLD_ROOT = 1             #拒绝的阈值:在允许登录失败的次数后,阻止每个尝试次数已超过此值的主机。此值适用于“root”用户登录尝试
DENY_THRESHOLD_RESTRICTED = 1       #拒绝的阈值:在允许登录失败的次数后,阻止每个尝试次数已超过此值的主机。此值仅适用于显示在工作目录或受限用户名文件中的用户
WORK_DIR = /usr/share/denyhosts/data     #denyhosts将用于向其写入数据的路径(如果该路径不存在,则将创建该路径)。注意:建议对此值使用绝对路径名(例如/home/foo/denyhosts/data)
SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES     #如果设置为“是”,则如果可疑登录尝试是由允许的主机引起的,则将其视为可疑。如果为否,则不会报告来自允许主机的可疑登录。所有来自IP地址的不在允许主机中的可疑登录都将始终报告。
HOSTNAME_LOOKUP=YES         #如果设置为“是”,对于denyhosts报告的每个IP地址,也将查找并报告相应的主机名(如果可用)。
LOCK_FILE = /var/lock/subsys/denyhosts     #如果运行denyhosts时存在此文件,则denyhosts将立即退出。否则,此文件将在调用时创建,并在退出时删除。这样可以确保一次只运行一个实例。
######################################################################
       ############ THESE SETTINGS ARE OPTIONAL ############         #这些设置是可选的
#######################################################################
ADMIN_EMAIL =            #如果要接收有关新限制主机和可疑登录的电子邮件,请将此地址设置为与您的电子邮件地址匹配。如果不希望接收这些报告,请将此字段留空(或使用--noemail选项运行)。多个电子邮件地址可以用逗号分隔,例如:admin_email=foo@bar.com、bar@foo.com等@foobar.com
SMTP_HOST = localhost         #smtp主机:如果将denyhosts配置为电子邮件报告(请参阅admin电子邮件),则这些设置指定电子邮件服务器地址(smtp主机)
SMTP_PORT = 25               #smtp端口:如果将denyhosts配置为电子邮件报告(请参阅admin电子邮件),则这些设置指定电子邮件服务器端口(smtp端口)。
#SMTP_USERNAME=foo            #smtp用户名smtp密码:如果您的smtp电子邮件服务器需要身份验证,请设置这些参数
#SMTP_PASSWORD=bar            #smtp密码:如果您的smtp电子邮件服务器需要身份验证,请设置这些参数
SMTP_FROM = DenyHosts <nobody@localhost>     #用于指定发件人信息:您可以在从denyhosts发送的消息中指定“发件人:”地址,当它报告阻止了滥用尝试时。
SMTP_SUBJECT = DenyHosts Report        #用于指定邮件主题:您可以指定denyhosts在报告阻止的滥用尝试时发送的邮件的“主题”。
#SMTP_DATE_FORMAT = %a, %d %b %Y %H:%M:%S %z       #SMTP日期格式。使用明确的日期格式显示发送电子邮件时的标题(有关此参数的可能值,请参阅:man strftime)。默认值为:%a, %d %b %Y %H:%M:%S %z(周几,日 月 年 时:分:秒 时区)
#SYSLOG_REPORT= YES|NO                  #是否开启系统日志报告。如果设置为“是”,则在记录被拒绝的主机时,报告数据将发送到系统日志(系统上必须存在系统日志)。默认值为:否
#ALLOWED_HOSTS_HOSTNAME_LOOKUP=YES|NO      #用于设置是否允许的主机名查找。如果设置为“是”,则对于工作目录/allowed hosts文件中的每个条目,将查找主机名。如果您的tcp_wrappers和sshd版本有时会记录主机名以及IP地址,那么您可能希望指定此选项。
AGE_RESET_VALID=5d           #用于定义两次失败的尝试之间计算失败次数的时间间隔。超过此时间将导致此主机的失败计数重置为0。此值适用于所有有效用户(在/etc/passwd中的用户)的登录尝试,但root用户除外。如果未定义,则永远不会重置此计数。
AGE_RESET_ROOT=25d          #用于定义两次失败的尝试之间计算失败次数的时间间隔。超过此时间将导致此主机的失败计数重置为0。此值适用于root用户。如果未定义,则永远不会重置此计数。
AGE_RESET_RESTRICTED=25d     #用于定义两次失败的尝试之间计算失败次数的时间间隔。此值适用于在工作目录/受限用户名文件中找到的所有登录尝试。如果未定义,则永远不会重置计数。
AGE_RESET_INVALID=10d        #用于定义两次失败的尝试之间计算失败次数的时间间隔。此值适用于任何无效用户名(用户名不显示在/etc/passwd中的用户)。
#RESET_ON_SUCCESS = yes|no     #用于设置是否允许登录成功时重置失败计数。如果此参数设置为“是”,则如果登录成功,相应IP地址的失败计数将重置为0。默认为no。
#PLUGIN_DENY=/usr/bin/true     #用于设定拒绝插件的位置。如果设置了该值,则该值应指向一个可执行程序,该程序将在主机添加到主机拒绝文件时调用。此可执行文件将传递给将作为唯一参数添加的主机。
#PLUGIN_PURGE=/usr/bin/true    #用于设定清理插件的位置。如果设置,该值应该指向一个可执行程序,当主机从主机拒绝文件中删除时,将调用该程序。此可执行文件将作为唯一的参数传递给要清除的主机。
#USERDEF_FAILED_ENTRY_REGEX=    #使用正则表达式自定义失败尝试条目。如果设置了该值,则该值应包含一个正则表达式,该表达式可用于识别特定ssh配置的其他黑客。此功能扩展了denyhosts使用的内置正则表达式。此参数可以多次指定。
######################################################################
   ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE  ##########     #这些设置仅适用于守护程序模式
#######################################################################
DAEMON_LOG = /var/log/denyhosts       #用于守护进程下指定其状态信息的日志文件。当denyhosts以守护进程模式运行时(--daemon标志),这是denyhosts用来报告其状态的日志文件。要禁用日志记录,请保留空白。(默认为:/var/log/denyhosts)
#DAEMON_LOG_TIME_FORMAT = %b %d %H:%M:%S     #用于守护进程下指定日志消息的时间戳格式。当denyhosts在守护进程模式下运行时(--daemon标志),它指定守护进程日志消息的时间戳格式(默认为iso8061格式:ie.2005-07-22 10:38:01745)
#DAEMON_LOG_MESSAGE_FORMAT = %(asctime)s - %(name)-12s: %(levelname)-8s %(message)s     #用于守护进程下指定每个记录条目的消息格式。当denyhosts以守护进程模式(--daemon flag)运行时,它指定每个记录条目的消息格式。默认情况下,使用以下格式:%(asctime)s - %(name)-12s: %(levelname)-8s %(message)s。其中“%(asctime)s”部分扩展为守护进程日志时间格式定义的格式。此字符串将传递给python的logging.formatter contstutor。有关可能的格式类型的详细信息,可参阅:http://docs.python.org/lib/node357.html
DAEMON_SLEEP = 30s      #当denyhosts以守护进程模式运行时(--daemon flag),这是denyhosts在轮询安全日志之间休眠的时间量。请参阅清除拒绝部分(上面)中的注释。
DAEMON_PURGE = 1h       #deny hosts在以守护进程模式运行时,应多久运行一次清除机制以使主机中的旧条目过期。如果清除拒绝为空,则此操作无效。
#######################################################################
   #########   THESE SETTINGS ARE SPECIFIC TO     ##########    #这些设置特定于守护进程同步
   #########       DAEMON SYNCHRONIZATION         ##########
#######################################################################
同步模式允许denyhosts守护进程定期发送和接收被拒绝的主机数据,这样denyhosts守护进程可以在全球范围内自动通知对方有关被禁止的主机的信息。默认情况下禁用此模式,必须取消同步服务器的注释才能启用此模式。
#SYNC_SERVER = http://xmlrpc.denyhosts.net:9911    #用于设定同步服务器。与denyhost守护进程通信的中央服务器。目前,denyhosts.net是唯一可用的服务器,但是在未来,组织可能会安装自己的服务器进行内部网络同步。
#SYNC_INTERVAL = 1h    #用于设定同步的时间间隔。如果已取消同步服务器的注释,则执行同步的时间间隔。默认值为1小时。
#SYNC_UPLOAD = yes|no   #是否允许您的denyhosts守护进程上传被拒绝的主机?只有在取消对同步服务器的注释后,此选项才适用。默认为yes
#SYNC_DOWNLOAD = yes|no  #是否允许您的denyhosts守护进程下载接收被其他人拒绝的主机?只有在取消对同步服务器的注释后,此选项才适用。默认为yes
#SYNC_DOWNLOAD_THRESHOLD = 3   #用于设定筛选下载的数据被几台服务器设为拒绝后本机直接拒绝。如果启用了同步下载,此参数会将返回的主机筛选为已被其他人多次阻止的主机。也就是说,如果设置为1,那么如果单个denyhosts服务器拒绝了IP地址,那么您将收到拒绝的主机。默认为3。另请参见同步下载弹性(SYNC_DOWNLOAD_RESILIENCY)
#SYNC_DOWNLOAD_RESILIENCY = 5h   #同步下载弹性。如果启用了同步下载,则为此选项指定的值会将下载的数据限制在此恢复期或更高的恢复期内。弹性是指黑客第一次攻击和最近一次攻击之间的时间间隔。例如:如果集中式denyhosts.net服务器在下午2点记录攻击,然后在下午5点再次记录攻击,指定同步下载弹性=4h将不会下载此IP地址。但是,如果攻击者在下午6:15再次被记录,那么您的denyhosts实例将下载IP地址。此值与同步下载阈值一起使用,只有满足这两个值的主机才会被下载。如果同步下载阈值=1,此值无效。默认值为:SYNC_DOWNLOAD_RESILIENCY = 5h (5 小时)。即,仅获取在其上停留5小时或更长时间的黑客:

六、实战应用

    1、场景设定

    所有用户ssh远程登录3次密码验证失败,禁用用户IP访问主机一天,一天后失败计数清零后方可登陆。

    2、修改配置文件(点击领取阿礼云2000元代金券)
[root@testhost01 denyhosts]# vim /usr/share/denyhosts/denyhosts.cfg
PURGE_DENY =     在等号后写入1d( PURGE_DENY = 1d )
DENY_THRESHOLD_INVALID = 55改为3
DENY_THRESHOLD_VALID = 1010改为3
DENY_THRESHOLD_ROOT = 11改为3
DENY_THRESHOLD_RESTRICTED = 11改为3
AGE_RESET_VALID=5d       把5d改为1d
AGE_RESET_ROOT=25d       把25d改为1d
AGE_RESET_RESTRICTED=25d   把25d改为1d
AGE_RESET_INVALID=10d      把25d改为1d
    3、启动DenyHosts
[root@testhost01 denyhosts]# /etc/rc.d/init.d/denyhosts start
starting DenyHosts:    /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
    4、加入到自动重启(点击领取阿礼云2000元代金券)
[root@testhost01 denyhosts]# ll /etc/init.d/
总用量 44
-rwxr-xr-x  1 root root  4076 525 15:25 denyhosts      #查看到启动脚本在该目录下,没有的复制或者链接一下
-rw-r--r--. 1 root root 18281 824 2018 functions
-rwxr-xr-x. 1 root root  4569 824 2018 netconsole
-rwxr-xr-x. 1 root root  7923 824 2018 network
-rw-r--r--. 1 root root  1160 1031 2018 README
[root@testhost01 denyhosts]# chkconfig --add denyhosts     #加入开机启动项
[root@testhost01 denyhosts]# chkconfig denyhosts on
[root@testhost01 denyhosts]# chkconfig --list denyhosts
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 
      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。
denyhosts      	0:1:2:3:4:5:6:[root@testhost01 denyhosts]# reboot
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(192.168.0.201:22) at 19:00:17.
Type `help' to learn how to use Xshell prompt.
[c:\~]$ ssh root@192.168.0.201


Connecting to 192.168.0.201:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sat May 25 19:01:02 2019 from 192.168.0.135
[root@testhost01 ~]# /etc/rc.d/init.d/denyhosts status
DenyHosts is running with pid = 6817

七、测试

    1、故意输错三次密码
[root@testhost01 denyhosts]# > /var/log/secure
[root@localhost ~]# ssh root@192.168.0.201
root@192.168.0.201's password:       #第一次输错密码
Permission denied, please try again.
root@192.168.0.201's password:       #第二次输错密码
Permission denied, please try again.
root@192.168.0.201's password:       #第三次输错密码
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost ~]# ssh root@192.168.0.201
ssh_exchange_identification: read: Connection reset by peer     #ssh交换标识:读取:对等端重置连接,无法登陆
[root@localhost ~]# ssh root@192.168.0.201
ssh_exchange_identification: read: Connection reset by peer
[root@localhost ~]# ssh root@192.168.0.201
    2、查看服务端/etc/hosts.deny文件
[root@testhost01 denyhosts]# cat /etc/hosts.deny 
#
sshd: 192.168.0.111       #sshd服务禁止IP192.168.0.111已出现

八、直接编辑配置文件实现IP的允许和禁止

    1、如果不想让主机拒绝某一个ip,做法如下:
[root@localhost ~]# vim /etc/hosts.allow   
sshd: 192.168.0.101  #允许192.168.0.101访问该主机的ssh服务
    2、如果想拒绝某一个ip同样使用vi /etc/hosts.deny添加就Ok

    欢迎留言评论和交流


相关阅读:
    《实验探索openssh(一):服务安装搭建》
    《实验探索openssh(二):远程连接的使用方法及配置文件常用项解析》
    《实验探索openssh(三):如何提升服务安全性,开启秘钥认证防止ssh暴力破解》
    《实验探索openssh(四):通过第三方防护应用fail2ban来防止ssh暴力破解》

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值