防止SSH暴力攻击方法之一:Denyhosts工具使用(附:误封IP问题),


  最近在一次登录服务器时,系统提示多次登录失败:There were xxx failed login attemps since the last successful login.

  作为小白的我第一次遇到,虽说没什么损失,也借此也学习一下应对方法。为方便以后使用,在此做简单的整理记录。

SSH暴力破解攻击

  是一种通过遍历枚举的用户名(尤其是一些常用、惯用的用户名)、密码字典对远程登录设备(如:云服务器)进行尝试登录,来窃取设备权限,以获取非法利益的网络攻击手段(个人见解,参考:https://cloud.tencent.com/developer/article/1159622)。

  应对该问题有多种方法:更换ssh端口、使用安全组限制入流ip、使用fail2ban、denyhosts等工具。

  在这里只对本人使用Denyhosts工具的过程做描述。

Dneyhosts介绍

  Denyhosts是一个由Linux系统管理员运行,用来阻止SSH服务器攻击的python脚本(参考:Denyhosts官网http://denyhosts.sourceforge.net/)。

  在服务器安全日志(Centos:/var/log/secure;Ubutun:/var/log/auth.log)里,可以查看到访问服务器的记录。在受到不明IP多次访问时,我们可以通过将允许访问的IP添加至系统白名单(/etc/hosts.allow),或者将禁止的IP添加到黑名单(/etc/hosts.deny),来限制访问服务器的IP;但是攻击者一般都是通过不同IP对服务器进行访问,这样就对阻止攻击造成很大的麻烦。Dneyhosts就是一个自动查看分析安全日志,将符合设定禁止条件的IP添加到/etc/hosts.deny的脚本程序。

Dneyhosts手动安装(基于centos7系统)

一、下载denyhosts源码

wget http://github.com/denyhosts/denyhosts/archive/v2.10.tar.gz
tar -zxvf v2.10.tar.gz

二、安装denyhosts

cd denyhosts-2.10
python setup.py install    #注:由于denyhosts基于python2,若同时安装了python3,请注意python运行环境

三、修改配置

 安装完后,会在/usr/bin文件下生成daemon-control-dist denyhosts.py两个文件,默认生成/etc/denyhosts.conf配置文件。

vim /etc/denyhosts.conf  #修改配置文件

 修改内容:

########################################################################
#
# SECURE_LOG: the log file that contains sshd logging info
# if you are not sure, grep "sshd:" /var/log/*
#
# The file to process can be overridden with the --file command line
# argument
# 不同系统的日志文件名有差异
# Redhat or Fedora Core:  
SECURE_LOG = /var/log/secure   #修改日志文件路径指向以适合当前系统(centos7)
#
# Mandrake, FreeBSD or OpenBSD:
#SECURE_LOG = /var/log/auth.log
#
# SuSE or Gentoo:
#SECURE_LOG = /var/log/messages
#
# Mac OS X (v10.4 or greater -
#   also refer to:   http://www.denyhost.net/faq.html#macos
#SECURE_LOG = /private/var/log/asl.log
#
# Mac OS X (v10.3 or earlier):
#SECURE_LOG=/private/var/log/system.log
#
# Debian and Ubuntu
#SECURE_LOG = /var/log/auth.log
########################################################################
######################################################################
#
# LOCK_FILE
#
# LOCK_FILE=/path/denyhosts
# If this file exists when DenyHosts is run, then DenyHosts will exit
# immediately.  Otherwise, this file will be created upon invocation
# and deleted upon exit.  This ensures that only one instance is
# running at a time.
# 不同系统有差异
# Redhat/Fedora:
LOCK_FILE = /var/lock/subsys/denyhosts    #防止denyhosts多次运行,修改文件路径以适合当前系统(centos7)
#
# Debian or Gentoo
#LOCK_FILE = /run/denyhosts.pid
#
# Misc
#LOCK_FILE = /tmp/denyhosts.lock
#
######################################################################
# 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 =                   #过多久后清除已阻止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 = /var/lib/denyhosts #将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日志位置

根据自己的需求修改配置

四、设置denyhosts服务启动程序

 将运行主程序放在/etc/init.d/下并改名为denyhosts

cp /usr/bin/daemon-control-dist /etc/init.d/denyhosts
vim /etc/init.d/denyhosts    #修改文件匹配denyhosts配置文件

 修改内容如下:

###############################################
#### Edit these to suit your configuration ####
###############################################

DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"    #安装时denyhosts.py生成路径
DENYHOSTS_LOCK  = "/var/lock/subsys/denyhosts"    #/etc/denyhosts.conf配置文件LOCK_FILE指定路径
DENYHOSTS_CFG   = "/etc/denyhosts.conf"

PYTHON_BIN      = "/usr/bin/env python"

###############################################
####         Do not edit below             ####
###############################################

 至此denyhosts工具安装完成。

 建议:在启动denyhosts服务之前先了解启动后可能带来的问题(误封常用的IP,详见下文)。

 五、启动denyhosts服务

 启动命令

/etc/init.d/denyhosts start

 或者

service denyhosts start
service tdenyhosts status #可查看denyhosts服务是否运行(显示"DenyHosts is running with pid = xxxx"说明启动成功)

 加入开机启动

chkconfig --add denyhosts
chkconfig denyhosts on

(内容参考:https://www.iteait.com/archives/659)

误封常用IP问题

  在启动服务之前常常忽略一个问题,denyhosts启动后会遍历安全日志文件/var/log/secure(centos系统)中所有记录IP,只要符合配置条件的IP都会被记录,因此,自己常用的IP也有可能被加入到系统IP黑名单(/etc/hosts.deny),导致自己也无法登录服务器(心态崩了呀~~~~)。

解决办法:

启动前:

  从安全日志文件里将自己常用的IP所在行删除,删除文件指定行命令请自行查询(sed命令,sed -i -e '/string/d' filename,仅作参考)

启动后(已正常无法ssh登录服务器):

  首先你需要更换网络登录服务器,

  接下来就是查看被封IP所在的记录文件并删除:

vim /var/log/secure    #安全日志文件
vim /etc/hosts.deny    #系统禁止IP文件
cd /var/lib/denyhosts
vim hosts
vim hosts-root
vim hosts-restricted
vim hosts-valid
vim users-invalid
vim users-hosts
#配置文件里默认的工作目录(WORK_DIR = /var/lib/denyhosts)
#批量删除文件包含指定内容所在行请自行查询(sed命令)

  如果不在意其他记录,可清空这些文件(不推荐)

  按照常理删除这些记录,系统应该就解禁了指定IP,但此时该IP还是不能访问服务器(why? why? why? 然后各种翻配置文件,记录文件,查看有什么遗漏,在配置文件里发现denyhosts有自己的日志文件  DAEMON_LOG = /var/log/denyhosts)

     在denyhosts的日志里有这样的记录:

2020-07-27 11:20:38,664 - denyhosts   : INFO     Creating new firewall rule /sbin/iptables -I INPUT -s xxx.xxx.xxx.xxx -j DROP
2020-07-27 11:20:38,677 - denyhosts   : INFO     new denied hosts: ['xxx.xxx.xxx.xxx']

  抱着学习的态度了解了一下iptables(https://wangchujiang.com/linux-command/c/iptables.html),denyhosts不仅将IP添加到了黑名单,也新添加了防火墙规则,阻止该IP数据包输入。

  所以除了将上述文件中的IP记录删除之外,还得在防火墙中删除对该IP限制的规则:

iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP

  登录成功!

  (欢迎在评论区交流学习,纠错指正!)

相关内容

    暂无相关文章