denyhosts也是一个防止sshd暴力破解的开源软件,他可以有效的阻止对ssd服务器的攻击。它具有以下的特点:

1. 对/var/log/secure日志文件进行分析,查找所有的登录尝试,并且过滤出失败和成功的尝试。
2.记录下所有失败的登录尝试的用户名和主机,如果超过阀值,则记录主机。
3.保持对每一个登录失败的用户(存在系统中或不存在系统中的用户)的跟踪
4.对每一个可疑的登录进行跟踪。(虽然登录成功,但是有很多次登录失败的记录)
5.将可疑地址的主机加入到/etc/hosts.deny文件中。

具体的使用步骤

1下载
wget http://jaist.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz
2解压
tar -zxvf DenyHosts-2.6.tar.gz -C /usr/local/bin
3进入解压文件夹
cd DenyHosts-2.6
4安装python环境
yum install -y python
5开始编译安装setup.py
python setup.py install
6编辑配置文件
 cd /usr/share/denyhosts/    
cp denyhosts.cfg-dist denyhosts.cfg    
cp daemon-control-dist daemon-control
vim denyhosts.cfg
7启动denyhosts并且设置为开机自启动
cd /etc/rc.d/init.d/
ln -s /usr/share/denyhosts/daemon-control denyhosts
chkconfig --add denyhosts
chkconfig denyhosts on 
chkconfig --list denyhosts
8安装完成。

配置文件的重要说明

PURGE_DENY:当一个IP被阻止以后,过多长时间被自动解禁。可选如3m(三分钟)、5h(5小时)、2d(两天)、8w(8周)、1y(一年);
PURGE_THRESHOLD:定义了某一IP最多被解封多少次。即某一IP由于暴力破解SSH密码被阻止/解封达到了PURGE_THRESHOLD次,则会被永久禁止;
BLOCK_SERVICE:需要阻止的服务名;
DENY_THRESHOLD_INVALID:某一无效用户名(不存在的用户)尝试多少次登录后被阻止;
DENY_THRESHOLD_VALID:某一有效用户名尝试多少次登陆后被阻止(比如账号正确但密码错误),root除外;
DENY_THRESHOLD_ROOT:root用户尝试登录多少次后被阻止;
HOSTNAME_LOOKUP:是否尝试解析源IP的域名;

本机的详细配置,命令在上面

[root@break ~]# wget http://jaist.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz
--2019-03-22 19:57:51--  http://jaist.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz
正在解析主机 jaist.dl.sourceforge.net (jaist.dl.sourceforge.net)... 150.65.7.130, 2001:df0:2ed:feed::feed
正在连接 jaist.dl.sourceforge.net (jaist.dl.sourceforge.net)|150.65.7.130|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:42667 (42K) [application/x-gzip]
正在保存至: “DenyHosts-2.6.tar.gz.2”

100%[===========================================================>] 42,667      16.5KB/s 用时 2.5s   

2019-03-22 19:57:54 (16.5 KB/s) - 已保存 “DenyHosts-2.6.tar.gz.2” [42667/42667])

[root@break ~]# tar -zvxf DenyHosts-2.6.tar.gz -C /usr/local/bin
DenyHosts-2.6/
DenyHosts-2.6/PKG-INFO
DenyHosts-2.6/denyhosts.py
DenyHosts-2.6/denyhosts.cfg-dist
DenyHosts-2.6/setup.py
DenyHosts-2.6/DenyHosts/
DenyHosts-2.6/DenyHosts/prefs.py
DenyHosts-2.6/DenyHosts/report.py
DenyHosts-2.6/DenyHosts/lockfile.py
DenyHosts-2.6/DenyHosts/__init__.py
DenyHosts-2.6/DenyHosts/plugin.py
DenyHosts-2.6/DenyHosts/denyfileutil.py
DenyHosts-2.6/DenyHosts/deny_hosts.py
DenyHosts-2.6/DenyHosts/regex.py
DenyHosts-2.6/DenyHosts/sync.py
DenyHosts-2.6/DenyHosts/counter.py
DenyHosts-2.6/DenyHosts/old-daemon.py
DenyHosts-2.6/DenyHosts/util.py
DenyHosts-2.6/DenyHosts/daemon.py
DenyHosts-2.6/DenyHosts/python_version.py
DenyHosts-2.6/DenyHosts/allowedhosts.py
DenyHosts-2.6/DenyHosts/filetracker.py
DenyHosts-2.6/DenyHosts/loginattempt.py
DenyHosts-2.6/DenyHosts/restricted.py
DenyHosts-2.6/DenyHosts/purgecounter.py
DenyHosts-2.6/DenyHosts/version.py
DenyHosts-2.6/DenyHosts/constants.py
DenyHosts-2.6/CHANGELOG.txt
DenyHosts-2.6/LICENSE.txt
DenyHosts-2.6/daemon-control-dist
DenyHosts-2.6/plugins/
DenyHosts-2.6/plugins/README.contrib
DenyHosts-2.6/plugins/shorewall_allow.sh
DenyHosts-2.6/plugins/shorewall_deny.sh
DenyHosts-2.6/plugins/test_deny.py
DenyHosts-2.6/scripts/
DenyHosts-2.6/scripts/restricted_from_invalid.py
DenyHosts-2.6/scripts/restricted_from_passwd.py
DenyHosts-2.6/README.txt
DenyHosts-2.6/MANIFEST.in
[root@break ~]# cd /etc/l
ld.so.conf.d/ libibverbs.d/ libreport/    logrotate.d/  lvm/          
libblockdev/  libnl/        libvirt/      lsm/          
[root@break ~]# cd /usr/local/
[root@break local]# cd DenyHosts-2.6/
[root@break DenyHosts-2.6]# ls -al
总用量 96
drwxr-x---.  6  500  500   248 3月  22 17:04 .
drwxr-xr-x. 14 root root   175 3月  22 16:49 ..
drwxr-xr-x.  4 root root    36 3月  22 16:52 build
-rw-r--r--.  1  500  500 16549 12月  8 2006 CHANGELOG.txt
-rwxr-xr-x.  1  500  500  4076 4月  22 2006 daemon-control-dist
drwxr-x---.  2  500  500  4096 3月  22 16:52 DenyHosts
-rw-r--r--.  1  500  500 20830 8月  20 2006 denyhosts.cfg-dist
-rwxr-xr-x.  1  500  500  6578 7月   8 2006 denyhosts.py
-rw-r--r--.  1  500  500 18009 12月 17 2005 LICENSE.txt
-rw-r--r--.  1  500  500   353 4月   5 2006 MANIFEST.in
-rw-r-----.  1  500  500   533 12月  8 2006 PKG-INFO
drwxr-x---.  2  500  500    99 12月  8 2006 plugins
-rw-r--r--.  1  500  500  3575 3月  22 16:52 README.txt
drwxr-x---.  2  500  500    73 12月  8 2006 scripts
-rw-r--r--.  1  500  500  1522 4月   5 2006 setup.py
[root@break DenyHosts-2.6]# python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/bin/denyhosts.py to 755
running install_data
running install_egg_info
Removing /usr/lib/python2.7/site-packages/DenyHosts-2.6-py2.7.egg-info
Writing /usr/lib/python2.7/site-packages/DenyHosts-2.6-py2.7.egg-info
[root@break DenyHosts-2.6]# ls -al
总用量 96
drwxr-x---.  6  500  500   248 3月  22 17:04 .
drwxr-xr-x. 14 root root   175 3月  22 16:49 ..
drwxr-xr-x.  4 root root    36 3月  22 16:52 build
-rw-r--r--.  1  500  500 16549 12月  8 2006 CHANGELOG.txt
-rwxr-xr-x.  1  500  500  4076 4月  22 2006 daemon-control-dist
drwxr-x---.  2  500  500  4096 3月  22 16:52 DenyHosts
-rw-r--r--.  1  500  500 20830 8月  20 2006 denyhosts.cfg-dist
-rwxr-xr-x.  1  500  500  6578 7月   8 2006 denyhosts.py
-rw-r--r--.  1  500  500 18009 12月 17 2005 LICENSE.txt
-rw-r--r--.  1  500  500   353 4月   5 2006 MANIFEST.in
-rw-r-----.  1  500  500   533 12月  8 2006 PKG-INFO
drwxr-x---.  2  500  500    99 12月  8 2006 plugins
-rw-r--r--.  1  500  500  3575 3月  22 16:52 README.txt
drwxr-x---.  2  500  500    73 12月  8 2006 scripts
-rw-r--r--.  1  500  500  1522 4月   5 2006 setup.py
[root@break DenyHosts-2.6]# cp daemon-control-dist daemon-control
[root@break DenyHosts-2.6]# cp denyhosts.cfg-dist denyhosts.cfg
[root@break DenyHosts-2.6]# vim denyhosts.cfg
[root@break DenyHosts-2.6]# cd /etc/rc.d/init.d/
[root@break init.d]# ln -s /usr/share/denyhosts/daemon-control denyhosts
[root@break init.d]# systemctl status denyhosts.service 
● denyhosts.service - SYSV: Activates/Deactivates the
   Loaded: loaded (/etc/rc.d/init.d/denyhosts; bad; vendor preset: disabled)
   Active: active (exited) since 五 2019-03-22 18:16:03 CST; 1h 51min ago
     Docs: man:systemd-sysv-generator(8)
3月 22 18:16:03 break.cn systemd[1]: Starting SYSV: Activates/Deactivates the...
3月 22 18:16:03 break.cn denyhosts[13191]: DenyHosts could not obtain lock (pid: 12428)
3月 22 18:16:03 break.cn denyhosts[13191]: [Errno 17] File exists: '/var/lock/subsys/denyhosts'
3月 22 18:16:03 break.cn denyhosts[13191]: starting DenyHosts:    /usr/bin/env python /usr/bin...cfg
3月 22 18:16:03 break.cn systemd[1]: Started SYSV: Activates/Deactivates the.
Hint: Some lines were ellipsized, use -l to show in full.
[root@break init.d]# chkconfig --add denyhosts 
[root@break init.d]# chkconfig denyhosts on
自己摸索出来的一些道理:
1在windows上使用xshell远程连接和使用本地的sshd服务连接我VMware中的服务器,使用的应该是同样的IP地址
2denyhosts的配置文件在/usr/share/denyhosts/denyhosts.cfg-dist
	服务程序为/usr/share/denyhosts/daemon-control-dist
3fail2ban的主要配置文件为/etc/fail2ban/jail.conf
服务程序为/usr/local/fail2ban-0.8.14/files/redhat-initd
4如何查找一些服务程序
grep chkconfig ./* -R --color
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐