简介
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。
当你的linux服务器暴露在互联网之中,该服务器将会遭到互联网上的扫描软件进行扫描,并试图猜测SSH登录口令。
你会发现,每天会有多条SSH登录失败纪录。那些扫描工具将对你的服务器构成威胁,你必须设置复杂登录口令,并将尝试多次登录失败的IP给阻止掉,让其在一段时间内不能访问该服务器。
用DenyHosts可以阻止试图猜测SSH登录口令,它会分析/var/log/secure等日志文件,当发现同一IP在进行多次SSH密码尝试时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽该IP的目的。
下载
#小显博客资源站直接下载 wget https://download.lixian.fun/Linux_LNMP_packers/DenyHosts-2.6.tar.gz #Denyhosts官网自己下载 http://denyhosts.sourceforge.net/ #最新版本2.6下载地址 https://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/
安装部署
#上传之后解压 [root@lixian ~]# tar xf DenyHosts-2.6.tar.gz #移动目录 [root@lixian ~]# mv DenyHosts-2.6 /usr/share/denyhosts #进入工作目录 [root@lixian ~]# cd /usr/share/denyhosts/ #安装denyhosts [root@lixian /usr/share/denyhosts]# python setup.py install #创建配置文件 [root@lixian /usr/share/denyhosts]# cp denyhosts.cfg-dist denyhosts.cfg #修改服务名称 [root@lixian /usr/share/denyhosts]# cp daemon-control-dist daemon-control #授权 [root@lixian /usr/share/denyhosts]# chown root:root daemon-control [root@lixian /usr/share/denyhosts]# chmod 700 daemon-control
配置
[root@lixian /usr/share/denyhosts]# vim 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日志位置
启动
#创建启动服务软连接 [root@lixian /usr/share/denyhosts]# ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts #添加启动项 [root@lixian /usr/share/denyhosts]# chkconfig denyhosts on #启动DenyHosts(如果是centos6.x 使用service denyhosts start命令启动) [root@lixian /usr/share/denyhosts]# systemctl start denyhosts #查看后台启动进程 [root@lixian /usr/share/denyhosts]# ps -ef |grep denyhosts |grep -v "grep" root 1728 1 0 15:59 ? 00:00:00 python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
测试
5次输入密码失败,直接拒绝连接
查看/etc/hosts.deny (拒绝主机IP列表)
[root@lixian /usr/share/denyhosts]# cat /etc/hosts.deny # # hosts.deny This file contains access rules which are used to # deny connections to network services that either use # the tcp_wrappers library or that have been # started through a tcp_wrappers-enabled xinetd. # # The rules in this file can also be set up in # /etc/hosts.allow with a 'deny' option instead. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers # sshd: 192.168.234.1