欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

[Linux] Centos DenyHosts 禁止针对linux sshd的暴力破解

发布时间:2025/4/16 linux 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 [Linux] Centos DenyHosts 禁止针对linux sshd的暴力破解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

DenyHosts介绍:

当linux服务器暴露在互联网之中,该Linux服务器将会遭到互联网上的扫描软件进行扫描,并试图猜测SSH登录口令的危险。而DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。

DenyHosts安装:

DenyHosts 需要 Python v2.3或者更高版本支持。

1.Yum 安装:

1 <span style="font-family: tahoma, arial, helvetica, sans-serif;">[root@DaoBiDao ~]# yum install -y denyhosts</span>

2.RPM安装:

1 2 <span style="font-family: tahoma, arial, helvetica, sans-serif;">[root@DaoBiDao ~]# wget cache.ali.dagaiba.com/DenyHosts/DenyHosts-2.6-1.src.rpm [root@DaoBiDao ~]# rpm -ivh DenyHosts-2.6-1.src.rpm</span>

3.压缩包安装

1 2 3 4 5 <span style="font-family: tahoma, arial, helvetica, sans-serif;">[root@DaoBiDao~]# yum install python [root@DaoBiDao~]# wget cache.ali.dagaiba.com/DenyHosts/DenyHosts-2.6.tar.gz [root@DaoBiDao~]# tar -zxvf DenyHosts-2.6.tar.gz [root@DaoBiDao~]# cd DenyHosts-2.6 [root@DaoBiDao~]# python setup.py install</span>

 DenyHosts配置:

1 2 [root@DaoBiDao~]# cd /usr/share/denyhosts/ [root@DaoBiDao~]# cp denyhosts.cfg-dist denyhosts.cfg

编辑denyhosts.cfg配置文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 SECURE_LOG = /var/log/secure #ssh 日志文件,它是根据这个文件来判断的。 HOSTS_DENY = /etc/hosts.deny #控制用户登陆的文件 PURGE_DENY = 5m #过多久后清除已经禁止的,这里代表是5分钟。 PURGE_THRESHOLD = 0 #默认0代表拒绝的主机可以被清除/重新添加无限期 BLOCK_SERVICE  = sshd #禁止的服务名 DENY_THRESHOLD_INVALID = 5 #允许无效用户失败的次数 DENY_THRESHOLD_VALID = 3 #允许普通用户登陆失败的次数 DENY_THRESHOLD_ROOT = 3 #允许root登陆失败的次数 DENY_THRESHOLD_RESTRICTED = 1 #允许在$WORK_DIR/hosts-restricted里面出现的用户失败的次数 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 = #设置管理员邮件地址 SMTP_HOST = localhost SMTP_PORT = 25 SMTP_USERNAME = foo SMTP_PASSWORD = bar SMTP_FROM = DenyHosts <nobody@localhost> SMTP_SUBJECT = DenyHosts Report SMTP_DATE_FORMAT = %a, %d %b %Y %H:%M:%S %z #设置发送邮件服务信息 SYSLOG_REPORT = YES #是否将denyhosts信息输如到系统日志 ALLOWED_HOSTS_HOSTNAME_LOOKUP=YES #是否做主机名泛解析 AGE_RESET_VALID=5d #有效用户登录失败计数归零的时间 AGE_RESET_ROOT=25d #root用户登录失败计数归零的时间 AGE_RESET_RESTRICTED=25d #用户的失败登录计数重置为0的时间 AGE_RESET_INVALID=10d #无效用户登录失败计数归零的时间 RESET_ON_SUCCESS = yes #如果一个ip登陆成功后,失败的登陆计数是否重置为0 DAEMON_LOG = /var/log/denyhosts #denyhosts的日志文件 DAEMON_LOG_TIME_FORMAT = %b %d %H:%M:%S #denyhosts的日志文件时间格式 DAEMON_LOG_MESSAGE_FORMAT = %(asctime)s - %(name)-12s: %(levelname)-8s %(message)s #denyhosts的日志文件内容格式 DAEMON_SLEEP = 30s DAEMON_PURGE = 1h #该项与PURGE_DENY设置成一样,设置是清除hosts.denied ssh用户的时间 设置denyhosts启动脚本
1 2 3 4 5 6 7 [root@DaoBiDao denyhosts]# cp daemon-control-dist daemon-control [root@DaoBiDao denyhosts]# chown root daemon-control [root@DaoBiDao denyhosts]# chmod 700 daemon-control [root@DaoBiDao denyhosts]# cp /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts [root@DaoBiDao denyhosts]# chkconfig --add denyhosts [root@DaoBiDao denyhosts]# chkconfig --level 2345 denyhosts on [root@DaoBiDao denyhosts]# service denyhosts start

使用其他主机登录,输入多次错误的密码就可以在/etc/hosts.deny查看到禁止的ip地址。

转载于:https://my.oschina.net/notbad/blog/338545

总结

以上是生活随笔为你收集整理的[Linux] Centos DenyHosts 禁止针对linux sshd的暴力破解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。